/* * File: main.cpp * Author: zhouhuang * * Created on October 10, 2015, 3:17 PM */ #include #include #include #include "process.h" #include "changemaster.h" #include "ry.h" pthread_mutex_t mutex; void stop(int signo) { pthread_mutex_lock(&mutex); int i; vLog(LOG_ERROR, "cmg received exit signel(%d)\n", signo); { for (i = 0; i < PROCESSES_NUM; i++) { if (procs[i] != NULL) { procs[i]->Destroy(); delete procs[i]; procs[i] = NULL; } } destroy_thread(); } if (zlog_inited) zlog_fini(); signo = 0; pthread_mutex_unlock(&mutex); exit(signo); } void freeMem(void) { if (database.yxs) { delete [] database.yxs; database.yxs = NULL; } if (database.ycs) { delete [] database.ycs; database.ycs = NULL; } if (database.yms) { delete [] database.yms; database.yms = NULL; } if (database.yks) { delete [] database.yks; database.yks = NULL; } if (database.yts) { delete [] database.yts; database.yts = NULL; } for (int i = 0; i < UNIT_NUM; i++) { if (config.units[i].yxs) { delete [] config.units[i].yxs; config.units[i].yxs = NULL; } if (config.units[i].ycs) { delete [] config.units[i].ycs; config.units[i].ycs = NULL; } if (config.units[i].yms) { delete [] config.units[i].yms; config.units[i].yms = NULL; } if (config.units[i].yks) { delete [] config.units[i].yks; config.units[i].yks = NULL; } if (config.units[i].yts) { delete [] config.units[i].yts; config.units[i].yts = NULL; } } if (msgBuffer.buf) { delete [] msgBuffer.buf; msgBuffer.buf = NULL; } if (channelBuffer.buf) { delete [] channelBuffer.buf; channelBuffer.buf = NULL; } } int main(int argc, char** argv) { int c; BOOLEAN enable_auto_platform = TRUE; char host[256] = {"127.0.0.1"}; int port = 7790; char nodeId[128] = {"runyang_dn"}; char version[128] = {"0"}; //获取可执行文件所在目录 const char default_config[] = "[global]\n" "default format = \"%d.%ms [%-6V] - %m%n\"\n" "[rules]\n" "my_cat.* >stderr\n"; int rc = dzlog_init("./application.conf", "my_cat"); if (rc < 0) { rc = dzlog_init(default_config, "my_cat"); if (rc < 0) { zlog_inited = FALSE; } else { fprintf(stderr, "dzlog_init(\"./application.conf\", \"my_cat\") failed, load default config.\n"); zlog_inited = TRUE; } } else { zlog_inited = TRUE; } pthread_mutex_init(&mutex, NULL); static struct option long_options[] = { { "directory", required_argument, NULL, 'd' }, { "host", required_argument, NULL, 'h'}, { "port", required_argument, NULL, 'p'}, { "nodeId", required_argument, NULL, 'n' }, { "version", required_argument, NULL, 'v'}, { "help", no_argument, NULL, 'H' }, { NULL, 0, NULL, 0 } }; while (1) { int opt_index = 0; c = getopt_long(argc, argv, "d:h:p:n:v:H", long_options, &opt_index); if (c == -1) break; switch (c) { case 'd': snprintf(configpath, sizeof(configpath), "%s", optarg); break; case 'h': snprintf(host, sizeof(host), "%s", optarg); break; case 'p': port = strtol(optarg, NULL, 10); break; case 'n': snprintf(nodeId, sizeof(nodeId), "%s", optarg); break; case 'v': snprintf(version, sizeof(version), "%s", optarg); break; case '?': case 'H': vLog(LOG_DEBUG, "Usage: %s [OPTION]... \n" " -d, --directory : set configuration file directory. Default is /data/config/rtufiles\n" " -h, --host : ws ip. Default is localhost\n" " -p, --port : ws port, default is 1883\n" " -n, --nodeid : ws nodeId, default is runyang_dn\n" " -v, --version : sw version, default is v1.0\n" " -H, --help : print this usage\n", argv[0]); return (EXIT_SUCCESS); default: vLog(LOG_DEBUG, "?? getopt returned character code 0%c ??\n", c); } } signal(SIGHUP, stop); signal(SIGQUIT, stop); signal(SIGKILL, stop); signal(SIGPIPE, stop); signal(SIGSTOP, stop); signal(SIGINT, stop); signal(SIGILL, stop); signal(SIGSEGV, stop); signal(SIGTERM, stop); signal(SIGABRT, stop); signal(SIGFPE, stop); CRYDevice ryDevice; do { vLog(LOG_DEBUG, "system initialize...\n"); char szHostCode[128]; memset(szHostCode, '\0', sizeof(szHostCode)); if (!initialize_system(FALSE, FALSE, NULL, szHostCode)) { vLog(LOG_ERROR, "system initialize error.\n"); if (enable_auto_platform) { //主动和平台链接 char szHostName[32] = ""; gethostname(szHostName, sizeof(szHostName)); nodes.m_node[0].m_netnode_no = 0; nodes.m_node[0].m_tcitype = MASTER_TCI; nodes.m_node[0].m_target_addr = INADDR_LOOPBACK; nodes.m_node[0].m_target_port = 15000; snprintf(nodes.m_node[0].m_machine_name, sizeof(nodes.m_node[0].m_machine_name), "%s", szHostName); nodes.m_node[0].irn = 0; //没有配置 vLog(LOG_DEBUG, "nodes.m_node[0].m_machine_name is: %s\n", nodes.m_node[0].m_machine_name); } } else { if (enable_auto_platform) { //增加协议和单元配置的数量统计 vLog(LOG_DEBUG, "configed node id is: %s.\n", nodes.m_node[0].m_machine_name); snprintf(nodeId, sizeof(nodeId), "%s", nodes.m_node[0].m_machine_name); if (system32.version[0] != '\0') { snprintf(version, sizeof(version), "%s", system32.version); } else { snprintf(version, sizeof(version), "%s", "1"); } } } if (enable_auto_platform) { ryDevice.ry_init(host, port, nodeId, version); } //此处增加一条协议配置 #if 1 int i; for (i = 0; i < PROCESSES_NUM; i++) { if (config.processes[i].state == FALSE) break; } snprintf(config.processes[i].name, sizeof(config.processes[i].name), "%s", "本地调试"); config.processes[i].state = TRUE; config.processes[i].time_accept = FALSE; config.processes[i].proto = PROTOCOL_LOCAL_DEBUG; config.processes[i].mode = PROCESS_MODE_MASTER; config.processes[i].time_gap = 300; config.processes[i].poll_gap = 5; config.processes[i].order = 0; memset(config.processes[i].units, -1, sizeof(config.processes[i].units)); config.processes[i].units[0] = 255; config.processes[i].option.network.ignored_source = TRUE; config.processes[i].option.network.socket_type = SOCK_STREAM; config.processes[i].option.network.bind_port = 9009; config.processes[i].option.network.bind_addr = INADDR_ANY; config.processes[i].option.network.target_port = 0; config.processes[i].option.network.target_addr = INADDR_ANY; config.units[255].state = TRUE; #endif unsigned int m_runCount = 0; unsigned int count = 0; unsigned int critical = 0; CChangeMaster masterTci; if (!masterTci.Init()) { break; } masterTci.MasterTciFirstRun(); while (TRUE) { m_runCount++; masterTci.MasterSend(); usleep(MASTER_TCI_SEND_INTERVAL); if (MASTER_TCI == CChangeMaster::m_tcitype) { if (m_runCount > count) { count = m_runCount; critical = 0; } else { critical++; if (critical > 15) { break; } } if (enable_auto_platform) { if (ryDevice.ry_run()) { break; } } } } if (critical > 15) { vLog(LOG_ERROR, "unknow error.\n"); } masterTci.ChangeDelete(); destroy_thread(); freeMem(); vLog(LOG_DEBUG, "App: dataAcquisition start reload.\n"); } while(1); pthread_mutex_destroy(&mutex); if (zlog_inited) zlog_fini(); ryDevice.ry_destroy(); vLog(LOG_DEBUG, "system stop okay.\n"); return EXIT_SUCCESS; }