1 Star 2 Fork 1

夜半饿得慌 / KnifeSync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HttpInterface.cpp 2.77 KB
一键复制 编辑 原始数据 按行查看 历史
夜半饿得慌 提交于 2018-06-29 21:59 . init project
#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;
}
1
https://gitee.com/knifecms/KnifeSync.git
git@gitee.com:knifecms/KnifeSync.git
knifecms
KnifeSync
KnifeSync
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891