acl/app/wizard/tmpl/http/master_threads.cpp

152 lines
3.3 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "http_service.h"
#include "http_servlet.h"
#include "master_service.h"
2014-11-19 00:25:21 +08:00
2015-09-16 21:03:02 +08:00
//////////////////////////////////////////////////////////////////////////////
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2014-11-19 00:25:21 +08:00
char *var_cfg_redis_addrs;
2014-11-19 00:25:21 +08:00
acl::master_str_tbl var_conf_str_tab[] = {
{ "redis_addrs", "127.0.0.1:6379", &var_cfg_redis_addrs },
2014-11-19 00:25:21 +08:00
{ 0, 0, 0 }
};
int var_cfg_use_redis_session;
2014-11-19 00:25:21 +08:00
acl::master_bool_tbl var_conf_bool_tab[] = {
{ "use_redis_session", 1, &var_cfg_use_redis_session },
2014-11-19 00:25:21 +08:00
{ 0, 0, 0 }
};
int var_cfg_conn_timeout;
int var_cfg_rw_timeout;
int var_cfg_max_threads;
2014-11-19 00:25:21 +08:00
acl::master_int_tbl var_conf_int_tab[] = {
{ "rw_timeout", 120, &var_cfg_rw_timeout, 0, 0 },
{ "ioctl_max_threads", 128, &var_cfg_max_threads, 0, 0 },
2014-11-19 00:25:21 +08:00
{ 0, 0 , 0 , 0, 0 }
};
long long int var_cfg_int64;
2014-11-19 00:25:21 +08:00
acl::master_int64_tbl var_conf_int64_tab[] = {
{ "int64", 120, &var_cfg_int64, 0, 0 },
{ 0, 0 , 0 , 0, 0 }
};
2015-09-16 21:03:02 +08:00
//////////////////////////////////////////////////////////////////////////////
2014-11-19 00:25:21 +08:00
master_service::master_service(void)
2014-11-19 00:25:21 +08:00
{
redis_ = NULL;
service_ = new http_service;
2014-11-19 00:25:21 +08:00
}
master_service::~master_service(void)
2014-11-19 00:25:21 +08:00
{
delete service_;
2014-11-19 00:25:21 +08:00
}
http_service& master_service::get_service(void) const
{
return *service_;
}
bool master_service::thread_on_read(acl::socket_stream* conn)
2014-11-19 00:25:21 +08:00
{
http_servlet* servlet = (http_servlet*) conn->get_ctx();
if (servlet == NULL) {
2014-11-19 00:25:21 +08:00
logger_fatal("servlet null!");
}
2014-11-19 00:25:21 +08:00
return servlet->doRun();
2014-11-19 00:25:21 +08:00
}
bool master_service::thread_on_accept(acl::socket_stream* conn)
2014-11-19 00:25:21 +08:00
{
logger("connect from %s, fd: %d", conn->get_peer(true),
conn->sock_handle());
conn->set_rw_timeout(var_cfg_rw_timeout);
if (var_cfg_rw_timeout > 0) {
conn->set_tcp_non_blocking(true);
}
2014-11-19 00:25:21 +08:00
acl::session* session;
if (var_cfg_use_redis_session) {
session = new acl::redis_session(*redis_);
} else {
session = new acl::memcache_session("127.0.0.1:11211");
}
http_servlet* servlet = new http_servlet(service_->get_handlers(),
conn, session);
2014-11-19 00:25:21 +08:00
conn->set_ctx(servlet);
return true;
}
bool master_service::thread_on_timeout(acl::socket_stream* conn)
2014-11-19 00:25:21 +08:00
{
logger("read timeout from %s, fd: %d", conn->get_peer(),
conn->sock_handle());
return false;
}
void master_service::thread_on_close(acl::socket_stream* conn)
2014-11-19 00:25:21 +08:00
{
logger("disconnect from %s, fd: %d", conn->get_peer(),
conn->sock_handle());
http_servlet* servlet = (http_servlet*) conn->get_ctx();
acl::session* session = &servlet->getSession();
delete session;
delete servlet;
2014-11-19 00:25:21 +08:00
}
void master_service::thread_on_init(void)
2014-11-19 00:25:21 +08:00
{
}
void master_service::thread_on_exit(void)
2014-11-19 00:25:21 +08:00
{
}
void master_service::proc_on_listen(acl::server_socket& ss)
{
logger(">>>listen %s ok<<<", ss.get_addr());
}
void master_service::proc_on_init(void)
2014-11-19 00:25:21 +08:00
{
// create redis cluster for session cluster
2015-12-20 21:24:28 +08:00
redis_ = new acl::redis_client_cluster;
redis_->init(NULL, var_cfg_redis_addrs, var_cfg_max_threads,
var_cfg_conn_timeout, var_cfg_rw_timeout);
}
void master_service::proc_on_exit(void)
{
delete redis_;
2014-11-19 00:25:21 +08:00
}
bool master_service::proc_exit_timer(size_t nclients, size_t nthreads)
{
if (nclients == 0) {
logger("clients count: %d, threads count: %d",
(int) nclients, (int) nthreads);
return true;
}
return false;
}
bool master_service::proc_on_sighup(acl::string&)
{
logger(">>>proc_on_sighup<<<");
return true;
}