-
Notifications
You must be signed in to change notification settings - Fork 0
/
CdnRefreshInterface.cpp
246 lines (217 loc) · 5.7 KB
/
CdnRefreshInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <errno.h>
#include<arpa/inet.h>
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <cstdlib>
#include <errno.h>
#include <string.h>
#include<string>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <boost/regex.hpp>
#include<fcntl.h>
#include<sys/time.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fstream>
#include <boost/regex.hpp>
#include "xmlParser.h"
#include "CdnRefreshInterface.h"
#include "Initialize.h"
#include"main.h"
#define MAXDATASIZE 4096 /*the maximum number of every transfer*/
CdnRefreshInterface::CdnRefreshInterface()
{
//设定超时重传时间
timeout.tv_sec = 0;
timeout.tv_usec = 300000;
}
void CdnRefreshInterface::XmlParse(std::string config_file_name)
{
XMLNode xMainNode = XMLNode::openFileHelper( config_file_name.c_str( ), "head" );
int num = xMainNode.nChildNode( "plugin" );
if (num == 0)
{
perror( "错误,配置文件中没有插件标签\n" );
exit( 1 );
}
for (int i = 0; i < num; i++)
{
string name = xMainNode.getChildNode( "plugin", i ).getAttribute( "name" );
if (name != "refreshCDN")
{
continue;
}
XMLNode xNode = xMainNode.getChildNode( "plugin", i );
XMLNode lNode = xNode.getChildNode( "localpath", 0 );
int wtchnum = xNode.nChildNode( "localpath" ); //get the numbers of the localpath tag
if (wtchnum == 0)
{
perror( "错误,配置文件中没有指明需要监控的路径\n" );
exit( 1 );
}
m_watch = lNode.getAttribute( "watch" );
m_watch = Initialize::SplitLastSlash( m_watch );
XMLNode Node = lNode.getChildNode( "cdninfo" );
m_domainName = Node.getAttribute( "domainname" );
m_port = atoi( Node.getAttribute( "port" ) );
m_username = Node.getAttribute( "username" );
m_passwd = Node.getAttribute( "passwd" );
Node = lNode.getChildNode( "sendurl" );
m_urlBase = Node.getAttribute( "base" );
Node = lNode.getChildNode( "regexurl" );
m_urlRegex = Node.getAttribute( "match" );
m_regexFlag = Node.getAttribute( "regex" ); //if =='false'will ignore the regex match;
if (m_domainName.empty( ) || m_username.empty( ) || m_passwd.empty( ) || \
m_urlBase.empty( ) || m_regexFlag.empty( ) || m_urlRegex.empty( ) \
)
{
cout << "配置文件错误,一些xml属性不应为空,请检查" << endl;
exit( 1 );
}
break;
}
}
int CdnRefreshInterface::Execute(Event e)
{
string fullPath = PackagePath( e );
if(fullPath.empty())
{
return 1;
}
if (debug_level & SUB_CLASS)
{
cout << fullPath << endl;
}
char buf[MAXDATASIZE] = "\0";//receive date from cdn
int sockfd, recvbytes, flags, n;
struct sockaddr_in serv_addr;
struct hostent *host;
if ((host = gethostbyname( m_domainName.c_str() )) == NULL)
{
printf( "get host by name error ccms.chinacache.com down" );
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons( m_port );
serv_addr.sin_addr = *((struct in_addr *) host->h_addr);
bzero( &(serv_addr.sin_zero), 8 );
if ((sockfd = socket( AF_INET, SOCK_STREAM, 0 )) == -1)
{
return 1;
}
flags = fcntl( sockfd, F_GETFL, 0 );
fcntl( sockfd, F_SETFL, flags | O_NONBLOCK );
connect( sockfd, (struct sockaddr *) & serv_addr, sizeof (struct sockaddr) );
fd_set rset, wset;
FD_ZERO( &wset );
FD_SET( sockfd, &wset );
if ((n = select( sockfd + 1, NULL, &wset, NULL, &timeout )) <= 0)
{
if (debug_level & SUB_CLASS)
{
printf( "time out connect error" );
}
close( sockfd );
return 1;
}
if (send( sockfd, fullPath.c_str( ), strlen( fullPath.c_str( ) ), 0 ) == -1)
{
if (debug_level & SUB_CLASS)
{
cout << "send error!";
}
close( sockfd );
return 1;
}
//需要重置,因为之前select之后,会对相应的rset置为非0
FD_ZERO( &rset );
FD_SET( sockfd, &rset );
if ((n = select( sockfd + 1, &rset, NULL, NULL, &timeout )) <= 0)
{
if (debug_level & SUB_CLASS)
{
printf( "time out connect error" );
}
close( sockfd );
return 1;
}
recvbytes = recv( sockfd, buf, MAXDATASIZE, 0 );
if (0 == recvbytes || -1 == recvbytes)
{
if (debug_level & SUB_CLASS)
{
cout << "this recvbytes==0" << endl;
}
close( sockfd );
return 1;
}
ErrorLog( buf ,fullPath);
close( sockfd );
if (debug_level & SUB_CLASS)
{
printf( "Received: %s\n", buf ); /*从返回结果读取 whatsup信息,如果为succeed说明提交成功,另超量提交类似获取 */
}
return 0;
}
string CdnRefreshInterface::PackagePath(Event e)
{
string temp;
char buf[1024] = "\0";
string fullPath;
fullPath += m_urlBase;
if (m_regexFlag == "true")
{
boost::smatch what;
boost::regex expression( m_urlRegex );
int res = boost::regex_search( e->path, what, expression );
if (false == res || what.size( ) <= 0)
{
return temp;
}
fullPath += what[1];
fullPath += what.suffix( );
} else
{
string tp = (e->path).substr( m_watch.size( ), (e->path).size( ) - m_watch.size( ) );
fullPath += tp;
}
if (e->dir)
{
fullPath = "&dirs=" + fullPath + "/";
} else
{
fullPath = "&urls=" + fullPath;
}
snprintf( buf, sizeof (buf), \
"HEAD /index.jsp?user=%s&pswd=%s&ok=ok%s HTTP/1.0\r\nHost: %s\r\n\r\n", \
m_username.c_str( ), m_passwd.c_str( ), fullPath.c_str( ), m_domainName.c_str( ) );
temp = buf;
return temp;
}
void CdnRefreshInterface::ErrorLog(string temp,string fullPath)
{
static int iWriteTime = 0;
if (iWriteTime < 1000)
{
m_fout.open( "errorlog.txt", ofstream::app );
iWriteTime++;
} else
{
m_fout.open( "errorlog.txt", ofstream::out );
iWriteTime = 0;
}
int pos = 0;
if ((pos = (temp.find( "\n", temp.find( "whatsup" ) ))) != string::npos)
{
int i = pos - 1 - temp.find( "whatsup" );
temp = temp.substr( temp.find( "whatsup" ), i );
}
m_fout << temp << " " << fullPath << "\n";
m_fout.close( );
}