// * ================================================================================ * // @ main // * ================================================================================ * int main(int argc, char ** argv) { //open the log file pLogFile = &CCLogFile; CCLogFile.startlog(1); CCLogFile.SetLogSize(2*1000*1000); CCLogFile.SetLoglevel(1); CCLogFile.logopen(); if ( myconfig.readfile(CONFIG_FILE_PATH) == 0 ) { printf("appserver: read config file '%s' error\n", CONFIG_FILE_PATH); LOG(LOG1, "appserver: read config file '%s' error\n", CONFIG_FILE_PATH); sleep(2); exit(0); } CCLogFile.SetLoglevel(ToInt(myconfig["LogLevel"][1])); //TANG 2010-9-4 11:02 //处理命令行参数 if ( argc == 3 && strcmp(argv[1], "reload") == 0 && strcmp(argv[2], "config") == 0 ) { send_msg_reload_config("127.0.0.1", (( ToInt(myconfig["AppServerListenPort"][1]) > 0 ) ? ToInt(myconfig["AppServerListenPort"][1]) : 4016)); exit(0); } LOG(LOG1, "appserver: start\n"); child_process_init(SN3_SERVICE_NUMBER); //application_envinit(); //LOG(LOG1, "appserver: application_envinit() done\n"); _appserver_initial(); LOG(LOG1, "appserver: _appserver_initial() done\n"); nthread_create(thread_sockserver, NULL, 64*1024); nthread_create(thread_procdata_sendsmsg, NULL, 64*1024); tcp_server1.SetpLog(&CCLogFile); tcp_server1.SetCallBackFunc(deal_client_msg); if ( ToInt(myconfig["AppServerListenPort"][1]) > 0 ) { tcp_server1.Listen(ToInt(myconfig["AppServerListenPort"][1]), 180); } else { tcp_server1.Listen(4016, 180); } //NPOSITION pos; //rundata_realtest* p_runtest; string s_terminal, s_msg_type; time_t t_check_hb_timeout = time(NULL); time_t t_check_test_timeout = time(NULL); while(g_appserver.m_runing) { child_process_hb(SN3_SERVICE_NUMBER); /* pos = NULL; while(NULL != (p_runtest = realtest_next(pos))) { if ( time(NULL) >= p_runtest->tm_end ) { CClientMsg::ansy_getitem(p_runtest->s_testdata.c_str(), "terminal", s_terminal); CClientMsg::ansy_getitem(p_runtest->s_testdata.c_str(), "msg", s_msg_type); //PESQ的实时测试,ftp扫描单元可能直接修改数据库的测试结果为X CNString sql; sql.Format("select * from BUSSOP where BUSSOP_ID = '%s' and (STATUS != 'X' and STATUS != 'Y');", p_runtest->s_bussopid.c_str()); DATASET dbresult; if ( DB1.db_select(sql.c_str(), dbresult, LOG6) > 0 ) { LOG(LOG1, "测试超时 [%s] '%c', s_bussopid='%s', timeout=%ds\n\n", s_terminal.c_str(), p_runtest->u_buss, p_runtest->s_bussopid.c_str(), p_runtest->tm_end - p_runtest->tm_start ); int i_rescode = 1; if ( dbresult[0]["STATUS"] == "W" ) i_rescode = 5001; appserver_dbupdate_bussop_error(p_runtest->s_bussopid.c_str(), i_rescode); } t_uchar u_buss = p_runtest->u_buss; p_runtest = realtest_next(pos); realtest_delete(s_terminal.c_str(), u_buss); } } */ n_sleep(1000); if ( time(NULL) >= t_check_hb_timeout + 10*60 ) { CNString sql; sql.Format("select * from CONFIG_TERMINAL where TERMINAL_STATE = 0 and HEARTBEAT_STATE = 0 and %u >= T_RECV_HEARTBEAT + %d;", time(NULL), ToInt(myconfig["SMS_HEARTBEAT_TIMEOUT"][1])); DATASET dbresult; if ( DB1.db_select(sql.c_str(), dbresult, LOG6) > 0 ) { //某终端心跳短信超时 for ( int i=0; i<dbresult.size(); i++ ) { string s_terminal = dbresult[i]["TERMINAL_ID"]; string s_bussopid; string s_starttime = time_format_str(time(NULL), 1); _ori_bussopid(s_bussopid, BUSSOPTYPE_ROUTINE, s_terminal.c_str(), "IA", s_starttime.c_str()); appserver_dbinsert_bussop(s_bussopid.c_str(), BUSSOPTYPE_ROUTINE, "IA", s_terminal.c_str(), s_starttime.c_str(), "", 'X'); insert_bussop_exception_result(s_bussopid.c_str(), "HEARTBEAT_TMOUT"); update_terminal_heartbeat_timeout(s_terminal.c_str()); } } t_check_hb_timeout = time(NULL); } if ( time(NULL) >= t_check_test_timeout + 60 ) { DATASET dbresult; CNString sql; sql.Format("SELECT * from BUSSOP where (STATUS != 'X' and STATUS != 'Y') and INIVATE_TYPE = 'T' and '%s' >= TIMEOUT order by STARTTIME;", time_format_str(time(NULL), 1).c_str()); if ( DB1.db_select(sql.c_str(), dbresult, LOG6) > 0 ) { //测试超时 for ( int i=0; i<dbresult.size(); i++ ) { LOG(LOG1, "DB 测试超时 terminal='%s', KQI='%s', s_bussopid='%s', tid='%s', timeout=%ds\n\n", dbresult[i]["TERMINAL_ID"].c_str(), dbresult[i]["KQI_TYPE"].c_str(), dbresult[i]["BUSSOP_ID"].c_str(), dbresult[i]["TID"].c_str(), time_string_to_time_t(dbresult[i]["TIMEOUT"]) - time_string_to_time_t(dbresult[i]["STARTTIME"]) ); int i_rescode = 1; if ( dbresult[i]["STATUS"] == "W" ) i_rescode = 5001; appserver_dbupdate_bussop_error(dbresult[i]["BUSSOP_ID"].c_str(), i_rescode); } } t_check_test_timeout = time(NULL); } } _appserver_destroy(); tcp_server1.Close(); LOG(LOG1, "appserver: exit\n"); return 0; }
void deal_client_msg(int state, int sockfd, const string & sockname, const string & ip, int port, void * msg, int msglen) { char * buf = (char *) msg; int buflen = msglen; if ( state == TCP_CLOSED ) { LOG(LOG6, "server: 对方关闭连接, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state == TCP_MSG_TIMEOUT ) { LOG(LOG6, "server: 接收消息超时, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state == TCP_MSG_ERROR ) { LOG(LOG6, "server: 接收消息格式错误, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state == TCP_CONNECTED ) { LOG(LOG6, "server: 建立连接, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state == TCP_COMPLETE ) { LOG(LOG6, "server: 自己主动关闭连接, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state == TCP_SERVER_CLOSED ) { LOG(LOG6, "server: 服务器停止运行, sockfd=%d, sockname='%s', ip='%s', port=%d\n\n", sockfd, sockname.c_str(), ip.c_str(), port); return; } if ( state != TCP_RECV_MSG ) { LOG(LOG1, "server: unknown call back\n\n"); return; } if ( buflen <= 0 ) { LOG(LOG1, "server: msglen=%d, error\n", buflen); return; } if ( strncmp(buf, "<policy-file-request/>", strlen("<policy-file-request/>")) == 0 ) { LOG(LOG6, "service:send msg=[<policy-file-request/>] response\n"); string policy = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>"; tcp_server1.Send(sockfd, policy.c_str(), policy.length()+1, 6); return; } ConfigLine xml; xml.read_xml_msg(buf); //解析消息 if ( xml["msg"] == "hb" ) { tcp_server1.Send(sockfd, buf, strlen(buf), 100); return; } //<head>00059</head><msg>terminal_wifi_hb</msg><terminal>13701916487</terminal> if ( xml["msg"] == "terminal_wifi_hb" ) { tcp_server1.Send(sockfd, buf, strlen(buf), 100); return; } LOG(LOG6, "server: recv msg, sockfd=%d, sockname='%s', ip='%s', port=%d, msg='%s'\n", sockfd, sockname.c_str(), ip.c_str(), port, buf); if ( xml["msg"] == "login" ) { //答复 string outmsg; xml_put_para(outmsg, "head", "00000"); xml_put_para(outmsg, "msg", xml["msg"]); xml_put_para(outmsg, "result", 0); tcp_server1.Send(sockfd, outmsg.c_str(), outmsg.size(), 6); } else if ( xml["msg"] == "reload_config" ) { if ( myconfig.readfile(CONFIG_FILE_PATH) == 0 ) { LOG(LOG1, "server: read config file '%s' error\n", CONFIG_FILE_PATH); } CCLogFile.SetLoglevel(ToInt(myconfig["LogLevel"][1])); //答复 string outmsg; xml_put_para(outmsg, "head", "00000"); xml_put_para(outmsg, "msg", xml["msg"]); xml_put_para(outmsg, "result", 0); tcp_server1.Send(sockfd, outmsg.c_str(), outmsg.size(), 6); } else { LOG(LOG1, "server: unknown msg = '%s', ignore\n", xml["msg"].c_str()); return; } LOG(LOG6, "\n"); return; }