acl/lib_acl_cpp/samples/connect_manager/main.cpp

217 lines
4.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-11-19 00:25:21 +08:00
#include "connect_manager.h"
2015-04-04 15:25:29 +08:00
#include "mymonitor.h"
2014-11-19 00:25:21 +08:00
#include "connect_pool.h"
#include "connect_client.h"
static int __loop_count = 10;
static acl::connect_manager* __conn_manager = NULL;
static acl_pthread_pool_t* __thr_pool = NULL;
static void sleep_while(int n)
{
for (int i = 0; i < n; i++)
{
putchar('.');
fflush(stdout);
sleep(1);
}
printf("\r\n");
}
static void check_all_connections(void)
{
std::vector<acl::connect_pool*>& pools = __conn_manager->get_pools();
std::vector<acl::connect_pool*>::const_iterator cit = pools.begin();
for (; cit != pools.end(); ++cit)
printf(">>>addr: %s %s\r\n", (*cit)->get_addr(),
(*cit)->aliving() ? "alive" : "dead");
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-04 15:25:29 +08:00
static void init(const char* addrs, int count,
bool sync_check, const acl::string& proto)
2014-11-19 00:25:21 +08:00
{
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳؼ<D3B3>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
__conn_manager = new connect_manager();
// <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA>ַ
2014-11-19 00:25:21 +08:00
__conn_manager->init(addrs, addrs, 100);
printf(">>>start monitor thread\r\n");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
2014-11-19 00:25:21 +08:00
int check_inter = 1, conn_timeout = 5;
2015-04-04 15:25:29 +08:00
acl::connect_monitor* monitor = new mymonitor(*__conn_manager, proto);
monitor->set_check_inter(check_inter);
monitor->set_conn_timeout(conn_timeout);
if (sync_check)
monitor->open_rpc_service(10, NULL);
(void) __conn_manager->start_monitor(monitor);
2014-11-19 00:25:21 +08:00
int n = 10;
printf(">>>sleep %d seconds for monitor check\r\n", n);
sleep_while(n);
printf(">>>create thread pool\r\n");
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
2014-11-19 00:25:21 +08:00
__thr_pool = acl_thread_pool_create(count, 60);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
2014-11-19 00:25:21 +08:00
static void end(void)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>̳߳<DFB3>
2014-11-19 00:25:21 +08:00
acl_pthread_pool_destroy(__thr_pool);
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳؼ<D3B3>Ⱥ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>״̬
2014-11-19 00:25:21 +08:00
printf("\r\n");
check_all_connections();
printf("\r\n>>> STOPPING check thread now\r\n");
2015-04-04 15:25:29 +08:00
#if 0
int i = 0;
while (i++ < 10)
{
sleep(1);
printf("----------- sleep %d seconds -----------\r\n", i);
}
#endif
// ֹͣ<CDA3><D6B9>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
2015-04-04 15:25:29 +08:00
acl::connect_monitor* monitor = __conn_manager->stop_monitor(true);
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-04 15:25:29 +08:00
delete monitor;
2014-11-19 00:25:21 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>
2014-11-19 00:25:21 +08:00
delete __conn_manager;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static bool get(connect_client* conn, int n)
{
printf(">>>check addr: %s, n: %d\r\n", conn->get_addr(), n);
return true;
}
// <20><><EFBFBD>̴߳<DFB3><CCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
static void thread_main(void*)
{
for (int i = 0; i < __loop_count; i++)
{
connect_pool* pool = (connect_pool*) __conn_manager->peek();
if (pool == NULL)
{
printf("\r\n>>>%lu(%d): peek pool failed<<<\r\n",
(unsigned long) acl_pthread_self(), __LINE__);
check_all_connections();
2015-04-04 15:25:29 +08:00
break;
2014-11-19 00:25:21 +08:00
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵij<D3B5>ʱʱ<CAB1><EFBFBD><E4BCB0><EFBFBD><EFBFBD>ʱʱ<CAB1><CAB1>
2014-11-19 00:25:21 +08:00
pool->set_timeout(2, 2);
// <20><><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>л<EFBFBD>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
connect_client* conn = (connect_client*) pool->peek();
if (conn == NULL)
{
printf("\r\n>>>%lu: peek connect failed from %s<<<\r\n",
(unsigned long) acl_pthread_self(),
pool->get_addr());
check_all_connections();
2015-04-04 15:25:29 +08:00
break;
2014-11-19 00:25:21 +08:00
}
// <20><>Ҫ<EFBFBD>Ի<EFBFBD><D4BB>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵<EFBFBD><CCB5><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
else
conn->reset();
// <20><>ʼ<EFBFBD>µ<EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
if (get(conn, i) == false)
{
printf("one request failed, close connection\r\n");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ر<EFBFBD>
2014-11-19 00:25:21 +08:00
pool->put(conn, false);
}
else
pool->put(conn, true);
}
printf(">>>>thread: %lu OVER<<<<\r\n", (unsigned long) acl_pthread_self());
}
static void run(int cocurrent)
{
// <20><><EFBFBD>̳߳<DFB3><CCB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
for (int i = 0; i < cocurrent; i++)
acl_pthread_pool_add(__thr_pool, thread_main, NULL);
}
static void usage(const char* procname)
{
printf("usage: %s -h [help]\r\n"
" -s server_addrs [www.sina.com.cn:80;www.263.net:80;www.qq.com:80]\r\n"
" -c cocurrent [default: 10]\r\n"
2015-04-04 15:25:29 +08:00
" -S [sync check io]\r\n"
" -P protocol [http|pop3]\r\n"
2014-11-19 00:25:21 +08:00
" -n loop_count[default: 10]\r\n", procname);
}
int main(int argc, char* argv[])
{
int ch, cocurrent = 10;
2015-04-04 15:25:29 +08:00
bool sync_check = false;
2014-11-19 00:25:21 +08:00
acl::string addrs("www.sina.com.cn:80;www.263.net:80;www.qq.com:81");
2015-04-04 15:25:29 +08:00
acl::string proto("pop3");
2014-11-19 00:25:21 +08:00
// <20><>ʼ<EFBFBD><CABC> acl <20><>
2014-11-19 00:25:21 +08:00
acl::acl_cpp_init();
// <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
acl::log::stdout_open(true);
2015-04-04 15:25:29 +08:00
while ((ch = getopt(argc, argv, "hs:n:c:SP:")) > 0)
2014-11-19 00:25:21 +08:00
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addrs = optarg;
break;
case 'c':
cocurrent = atoi(optarg);
break;
case 'n':
__loop_count = atoi(optarg);
break;
2015-04-04 15:25:29 +08:00
case 'S':
sync_check = true;
break;
case 'P':
proto = optarg;
break;
2014-11-19 00:25:21 +08:00
default:
usage(argv[0]);
return 0;
}
}
2015-04-04 15:25:29 +08:00
init(addrs, cocurrent, sync_check, proto);
2014-11-19 00:25:21 +08:00
run(cocurrent);
end();
#ifdef WIN32
printf("enter any key to exit...\r\n");
getchar();
#endif
return 0;
}