acl/lib_acl_cpp/samples/check_trigger/master_service.cpp

115 lines
2.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "http_job.h"
#include "db_store.h"
#include "master_service.h"
////////////////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
char* var_cfg_http_url_list;
char* var_cfg_dns_ip;
char* var_cfg_dbpath;
char* var_cfg_dbcharset;
acl::master_str_tbl var_conf_str_tab[] = {
{ "http_url_list", "http://www.baidu.com/", &var_cfg_http_url_list },
{ "dns_ip", "8.8.8.8", &var_cfg_dns_ip },
{ "dbpath", "./check.db", &var_cfg_dbpath },
{ "dbcharset", "utf-8", &var_cfg_dbcharset },
{ 0, 0, 0 }
};
acl::master_bool_tbl var_conf_bool_tab[] = {
{ 0, 0, 0 }
};
int var_cfg_http_cocurrent;
int var_cfg_dns_port;
int var_cfg_conn_timeout;
int var_cfg_rw_timeout;
acl::master_int_tbl var_conf_int_tab[] = {
{ "http_cocurrent", 10, &var_cfg_http_cocurrent, 0, 0 },
{ "dns_port", 53, &var_cfg_dns_port, 0, 0 },
{ "conn_timeout", 60, &var_cfg_conn_timeout, 0, 0 },
{ "rw_timeout", 60, &var_cfg_rw_timeout, 0, 0 },
{ 0, 0 , 0 , 0, 0 }
};
acl::master_int64_tbl var_conf_int64_tab[] = {
{ 0, 0 , 0 , 0, 0 }
};
////////////////////////////////////////////////////////////////////////////////
acl::thread_pool* var_thrpool;
acl::db_pool* var_dbpool;
master_service::master_service()
{
}
master_service::~master_service()
{
}
void master_service::on_trigger()
{
std::vector<acl::string>::const_iterator cit = url_list_.begin();
for (; cit != url_list_.end(); ++cit)
{
const char* url = (*cit).c_str();
for (int i = 0; i < var_cfg_http_cocurrent; i++)
{
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD><EFBFBD>񣬲<EFBFBD><F1A3ACB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>ȥִ<C8A5><D6B4>
2014-11-19 00:25:21 +08:00
acl::thread_job* job = new http_job(*var_thrpool, url,
var_cfg_dns_ip, var_cfg_dns_port);
var_thrpool->execute(job);
}
}
// <20>ȴ<EFBFBD><C8B4>̳߳ؽ<CCB3><D8BD><EFBFBD>
2014-11-19 00:25:21 +08:00
var_thrpool->wait();
}
void master_service::proc_on_init()
{
// ȡ<><C8A1><EFBFBD><EFBFBD>Ҫ̽<D2AA><CCBD><EFBFBD><EFBFBD> URL <20>б<EFBFBD>
2014-11-19 00:25:21 +08:00
acl::string buf(var_cfg_http_url_list);
const std::list<acl::string>& tokens = buf.split(",; \t");
std::list<acl::string>::const_iterator cit = tokens.begin();
for (; cit != tokens.end(); ++cit)
{
url_list_.push_back(*cit);
logger("add url: %s", (*cit).c_str());
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӳ<EFBFBD>
2014-11-19 00:25:21 +08:00
var_dbpool = new acl::sqlite_pool(var_cfg_dbpath,
var_cfg_http_cocurrent * url_list_.size());
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
2014-11-19 00:25:21 +08:00
db_store store;
if (store.db_create() == false)
logger_fatal("create db failed!");
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳ز<CCB3><D8B2><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>
2014-11-19 00:25:21 +08:00
var_thrpool = new acl::thread_pool;
var_thrpool->set_limit(var_cfg_http_cocurrent);
var_thrpool->set_idle(300);
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳ع<CCB3><D8B9><EFBFBD>
2014-11-19 00:25:21 +08:00
var_thrpool->start();
}
void master_service::proc_on_exit()
{
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
2014-11-19 00:25:21 +08:00
delete var_thrpool;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ӳ<EFBFBD>
2014-11-19 00:25:21 +08:00
delete var_dbpool;
}