分类:
2008-10-13 16:12:05
class MyServer : public SingleTon
{
public:
/**主服务器初始化工作
*
* \param *listenaddr 监听地址:"192.168.0.188:80"-在192.168.0.188的80端口进行监听
"80"-在所有IP地址的80端口进行监听
* \param numOfThreads 服务器网络消息处理的线程个数
* \return 1:成功,0或者其他值:失败
*/
int init(const char *listenaddr,unsigned int numOfThreads);
/**最后清理工作,资源释放工作
*
*/
void fini();
/**主服务器开始运行
*
* \return 1-成功,-1失败
*/
int start();
/**主服务器停止运行
*
*/
void stop();
private:
//任务管理器(线程池)
ServerTask _task;
//监听地址
ACE_INET_Addr _listen_addr;
//网络接收器
ClientAcceptor _acceptor;
//网络消息处理线程数量
unsigned int _num_of_threads;
private:
Observer _observer;
//检查是否有客户端超时
CheckClientTimeoutHandler _check_time_out_handler;
};
int MyServer::init(const char *listenaddr,unsigned int numOfThreads)
{
//ACE_WIN32_Proactor *pImpl = new ACE_WIN32_Proactor;
//static ACE_Proactor proactor(pImpl,1);
//ACE_Proactor::instance( & proactor);
_listen_addr=ACE_INET_Addr(listenaddr);
//_num_of_threads=numOfThreads;
_num_of_threads=1;
_observer.init();
_syn_cms_handler.init();
_check_time_out_handler.init();
return 1;
}
void MyServer::fini()
{
ItemManager::get_instance().purge_all_items();
_observer.fini();
_syn_cms_handler.fini();
_check_time_out_handler.fini();
}
/**主服务器开始运行
*
* \return 1-成功,-1失败
*/
int MyServer::start()
{
int Rc = _acceptor.open (_listen_addr,0,1);
if(Rc==-1)
{
ACE_ERROR_RETURN ((LM_ERROR, "acceptor error.\n"), -1);
}
//每20秒检查一次,检查是否有客户端超时
ACE_Time_Value check_client_timeout_interval(120);
Rc=ACE_Proactor::instance ()->schedule_timer (
_check_time_out_handler,
(void *) "timeout",
ACE_Time_Value::zero,
check_client_timeout_interval);
if(Rc==-1)
{
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "check_client_timeout schedule_timer"), -1);
}
ACE_Time_Value observertime(20);
Rc=ACE_Proactor::instance ()->schedule_timer (
_observer,
(void *) "observer",
ACE_Time_Value::zero,
observertime);
if(Rc==-1)
{
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "observer schedule_timer"), -1);
}
if (_task.activate (THR_NEW_LWP, _num_of_threads ) == -1)
{
ACE_ERROR_RETURN ((LM_ERROR, "task start error.\n", "main"), -1);
}
return 1;
}
/**主服务器停止运行
*
*/
void MyServer::stop()
{
ACE_Proactor::end_event_loop () ;
ACE_Thread_Manager * pTM = ACE_Thread_Manager::instance();
pTM->wait_task ( & _task) ;
//ACE_Proactor::instance( ( ACE_Proactor* )NULL );
}
int ACE_TMAIN(int argc,char *argv[]) //设置日志 //读入配置文件 //初始化MSGBLOCK池 //初始化连接池 //开始服务器 ACE_DEBUG((LM_DEBUG,"starting init MyServer...\n")); ACE_DEBUG((LM_DEBUG,"starting MyServer...\n")); while(true) //char c ; 没啥好说的。
{
FUNC_ENTER;
std::cout<<"size of item is "<
ACE_LOG_MSG->open (argv[0],ACE_Log_Msg::OSTREAM);
//std::ofstream myostream (SERVER_LOG_FILE_NAME, ios::out | ios::trunc);
//ACE_LOG_MSG->msg_ostream (&myostream);
ACE_DEBUG((LM_DEBUG,"read config file from %s\n",SERVER_CONFIG_FILE));
XMLConfig::get_instance().read_config_from_xml_file(SERVER_CONFIG_FILE);
ACE_DEBUG((LM_DEBUG,"starting init MsgBlockManager...\n"));
MsgBlockManager::get_instance().init(XMLConfig::get_instance().get_num_of_msg_blocks(),XMLConfig::get_instance().get_size_of_write_msg_blocks());
ACE_DEBUG((LM_DEBUG,"starting init ClientManager...\n"));
ClientManager::get_instance().init(XMLConfig::get_instance().get_num_of_clients(),XMLConfig::get_instance().get_size_of_client_read_buf());
MyServer::get_instance().init(XMLConfig::get_instance().get_listen_addr(),XMLConfig::get_instance().get_num_of_proactor_threads());
MyServer::get_instance().start();
ACE_DEBUG((LM_DEBUG,"Enter looping...\n"));
{
std::cout << "Input Command=>\n" << std::flush ;
std::string inputcmd;
std::getline(std::cin,inputcmd);
if(!handle_console_cmd(inputcmd))
break;
}
//std::cout << "Press any key to stop and exit=>\n" << std::flush ;
//std::cin.clear ();
//std::cin >> c ;
MyServer::get_instance().stop();
MsgBlockManager::get_instance().fini();
ClientManager::get_instance().fini();
return 1;
}
--------------------next---------------------