-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpInterface.cpp
executable file
·103 lines (98 loc) · 2.77 KB
/
HttpInterface.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
#include<curl/curl.h>
#include<sstream>
#include "QueueFilter.h"
#include "Initialize.h"
#include "xmlParser.h"
#include "Inotify.h"
#include"HttpInterface.h"
HttpInterface::HttpInterface() : m_url("http://localhost")
{
}
void HttpInterface::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 == "http")
{
XMLNode xNode = xMainNode.getChildNode( "plugin", i );
m_watch = xNode.getChildNode( "localpath" ).getAttribute( "watch" );
m_url = xNode.getChildNode( "localpath" ).getChildNode( "deshost" ).getAttribute( "url" );
m_port = atoi( xNode.getChildNode( "localpath" ).getChildNode( "deshost" ).getAttribute( "port" ) );
cout << "监控路径: " << m_watch << " URL: " << m_url << " 端口号: " << m_port << endl;
break;
}
}
}
int HttpInterface::Execute(Event e)
{
CURL *curl;
CURLM *multi_handle;
int still_running;
int times = 0; //try times if select false
int TRY_TIMES = 50;
char strMask[10];
sprintf( strMask, "%d", e->mask );
string sm = strMask;
string sendBuffer = "watchroot=" + m_watch;
if (e->dir)
{
sendBuffer += "&isdir=1";
} else
{
sendBuffer += "&isdir=0";
}
sendBuffer += "&inotifyPath=" + e->path;
sendBuffer += "&mask=" + sm;
curl = curl_easy_init( );
multi_handle = curl_multi_init( );
if (curl && multi_handle)
{
/* what URL that receives this POST */
curl_easy_setopt( curl, CURLOPT_URL, m_url.c_str( ) );
curl_easy_setopt( curl, CURLOPT_HTTPPOST, 1 );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, sendBuffer.c_str( ) );
curl_multi_add_handle( multi_handle, curl );
while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform( multi_handle, &still_running ));
while (still_running && times < TRY_TIMES)
{
int rc; //select() return code
int maxfd;
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
FD_ZERO( &fdread );
FD_ZERO( &fdwrite );
FD_ZERO( &fdexcep );
//get file descriptors from the transfers
curl_multi_fdset( multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd );
rc = select( maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout );
switch (rc)
{
case -1://select error
break;
case 0:
default: // timeout
while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform( multi_handle, &still_running ));
break;
}
times++;
}//end while
curl_multi_remove_handle( multi_handle, curl );
curl_easy_cleanup( curl );
curl_multi_cleanup( multi_handle ); //always cleanup
if (times >= TRY_TIMES)
{
return 1;
}
return 0;
}//end if
return 1;
}