map/das-dn/cmg/main.cpp

306 lines
9.3 KiB
C++
Raw Permalink Normal View History

2024-07-08 10:27:17 +08:00
/*
* File: main.cpp
* Author: zhouhuang
*
* Created on October 10, 2015, 3:17 PM
*/
#include <cstdlib>
#include <signal.h>
2024-07-19 17:28:02 +08:00
#include <getopt.h>
2024-07-08 10:27:17 +08:00
#include "process.h"
2024-10-14 20:26:21 +08:00
#include "changemaster.h"
#include "ry.h"
2024-07-08 10:27:17 +08:00
2024-10-14 20:26:21 +08:00
pthread_mutex_t mutex;
2024-07-08 10:27:17 +08:00
2024-10-14 20:26:21 +08:00
void stop(int signo)
2024-07-08 10:27:17 +08:00
{
2024-10-14 20:26:21 +08:00
pthread_mutex_lock(&mutex);
int i;
2024-07-08 10:27:17 +08:00
2024-10-14 20:26:21 +08:00
vLog(LOG_ERROR, "cmg received exit signel(%d)\n", signo);
2024-12-03 10:36:06 +08:00
for (i = 0; i < PROCESSES_NUM; i++)
2024-07-08 10:27:17 +08:00
{
2024-12-03 10:36:06 +08:00
if (procs[i] != NULL)
2024-10-14 20:26:21 +08:00
{
2024-12-03 10:36:06 +08:00
procs[i]->Destroy();
delete procs[i];
procs[i] = NULL;
2024-10-14 20:26:21 +08:00
}
}
2024-12-03 10:36:06 +08:00
destroy_thread();
2024-10-14 20:26:21 +08:00
if (zlog_inited) zlog_fini();
2024-07-08 10:27:17 +08:00
2024-10-14 20:26:21 +08:00
signo = 0;
pthread_mutex_unlock(&mutex);
exit(signo);
2024-07-08 10:27:17 +08:00
}
2024-10-14 20:26:21 +08:00
2024-07-19 17:28:02 +08:00
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;
}
}
2024-07-08 10:27:17 +08:00
int main(int argc, char** argv)
{
int c;
BOOLEAN enable_auto_platform = TRUE;
2024-07-19 17:28:02 +08:00
char host[256] = {"127.0.0.1"};
2025-01-16 13:23:42 +08:00
int port = 8080;
char nodeId[128] = {"1"};
2024-10-21 10:53:15 +08:00
char version[128] = {"0"};
2024-07-08 10:27:17 +08:00
//获取可执行文件所在目录
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");
2024-07-19 17:28:02 +08:00
if (rc < 0) {
2024-07-08 10:27:17 +08:00
rc = dzlog_init(default_config, "my_cat");
2024-07-19 17:28:02 +08:00
if (rc < 0) {
zlog_inited = FALSE;
} else {
2024-07-08 10:27:17 +08:00
fprintf(stderr, "dzlog_init(\"./application.conf\", \"my_cat\") failed, load default config.\n");
zlog_inited = TRUE;
}
2024-07-19 17:28:02 +08:00
} else {
2024-07-08 10:27:17 +08:00
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'},
2024-07-19 17:28:02 +08:00
{ "nodeId", required_argument, NULL, 'n' },
{ "version", required_argument, NULL, 'v'},
2024-07-08 10:27:17 +08:00
{ "help", no_argument, NULL, 'H' },
{ NULL, 0, NULL, 0 }
};
while (1)
{
int opt_index = 0;
2024-07-19 17:28:02 +08:00
c = getopt_long(argc, argv, "d:h:p:n:v:H", long_options, &opt_index);
2024-07-08 10:27:17 +08:00
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;
2024-07-19 17:28:02 +08:00
case 'n':
snprintf(nodeId, sizeof(nodeId), "%s", optarg);
2024-07-08 10:27:17 +08:00
break;
2024-07-19 17:28:02 +08:00
case 'v':
snprintf(version, sizeof(version), "%s", optarg);
2024-07-08 10:27:17 +08:00
break;
case '?':
case 'H':
vLog(LOG_DEBUG, "Usage: %s [OPTION]... \n"
" -d, --directory : set configuration file directory. Default is /data/config/rtufiles\n"
2024-07-19 17:28:02 +08:00
" -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"
2024-07-08 10:27:17 +08:00
" -H, --help : print this usage\n", argv[0]);
return (EXIT_SUCCESS);
default:
vLog(LOG_DEBUG, "?? getopt returned character code 0%c ??\n", c);
}
}
2024-07-19 17:28:02 +08:00
2024-07-08 10:27:17 +08:00
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);
2024-10-14 20:26:21 +08:00
CRYDevice ryDevice;
2024-11-27 11:56:30 +08:00
g_conn = &ryDevice;
2024-10-14 20:26:21 +08:00
2024-07-19 17:28:02 +08:00
do {
2024-07-08 10:27:17 +08:00
vLog(LOG_DEBUG, "system initialize...\n");
char szHostCode[128];
memset(szHostCode, '\0', sizeof(szHostCode));
2024-07-19 17:28:02 +08:00
if (!initialize_system(FALSE, FALSE, NULL, szHostCode)) {
2024-07-08 10:27:17 +08:00
vLog(LOG_ERROR, "system initialize error.\n");
2024-07-19 17:28:02 +08:00
if (enable_auto_platform) { //主动和平台链接
2024-07-08 10:27:17 +08:00
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);
2024-07-19 17:28:02 +08:00
nodes.m_node[0].irn = 0; //没有配置
2024-07-08 10:27:17 +08:00
vLog(LOG_DEBUG, "nodes.m_node[0].m_machine_name is: %s\n", nodes.m_node[0].m_machine_name);
}
2024-07-19 17:28:02 +08:00
} else {
if (enable_auto_platform) { //增加协议和单元配置的数量统计
2024-10-14 20:26:21 +08:00
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");
2024-08-09 08:50:19 +08:00
}
2024-07-08 10:27:17 +08:00
}
}
2024-07-19 17:28:02 +08:00
if (enable_auto_platform) {
2024-10-21 10:53:15 +08:00
ryDevice.ry_init(host, port, nodeId, version);
2024-07-08 10:27:17 +08:00
}
2024-11-15 16:12:14 +08:00
//此处增加一条协议配置
2024-11-21 09:06:36 +08:00
#if 1
2024-12-09 09:41:04 +08:00
int i, uid;
2024-11-15 16:12:14 +08:00
for (i = 0; i < PROCESSES_NUM; i++)
{
2024-12-30 17:20:28 +08:00
if ((config.processes[i].state & 0x01) == FALSE) break;
2024-11-15 16:12:14 +08:00
}
2024-12-03 10:36:06 +08:00
for (uid = 0; uid < UNIT_NUM; uid++)
{
2024-12-30 17:20:28 +08:00
if ((config.units[uid].state & 0x01) == FALSE) break;
2024-12-03 10:36:06 +08:00
}
2024-11-21 09:06:36 +08:00
snprintf(config.processes[i].name, sizeof(config.processes[i].name), "%s", "本地调试");
2024-12-05 14:03:07 +08:00
config.processes[i].irn = 0;
2024-11-21 09:06:36 +08:00
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));
2024-12-03 10:36:06 +08:00
config.processes[i].units[0] = uid;
2024-11-21 09:06:36 +08:00
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;
2024-12-03 10:36:06 +08:00
config.units[uid].state = TRUE;
2024-11-15 16:12:14 +08:00
#endif
2024-07-08 10:27:17 +08:00
unsigned int m_runCount = 0;
unsigned int count = 0;
unsigned int critical = 0;
CChangeMaster masterTci;
2024-10-21 10:53:15 +08:00
if (!masterTci.Init()) {
2024-07-08 10:27:17 +08:00
break;
}
masterTci.MasterTciFirstRun();
2024-07-19 17:28:02 +08:00
while (TRUE) {
2024-07-08 10:27:17 +08:00
m_runCount++;
masterTci.MasterSend();
usleep(MASTER_TCI_SEND_INTERVAL);
2024-07-19 17:28:02 +08:00
if (MASTER_TCI == CChangeMaster::m_tcitype) {
if (m_runCount > count) {
2024-07-08 10:27:17 +08:00
count = m_runCount;
critical = 0;
2024-07-19 17:28:02 +08:00
} else {
2024-07-08 10:27:17 +08:00
critical++;
2024-07-19 17:28:02 +08:00
if (critical > 15) {
2024-07-08 10:27:17 +08:00
break;
}
}
2024-07-19 17:28:02 +08:00
if (enable_auto_platform) {
2024-10-14 20:26:21 +08:00
if (ryDevice.ry_run()) {
break;
}
2024-07-08 10:27:17 +08:00
}
}
}
2024-07-19 17:28:02 +08:00
if (critical > 15) {
2024-07-08 10:27:17 +08:00
vLog(LOG_ERROR, "unknow error.\n");
}
masterTci.ChangeDelete();
2024-07-19 17:28:02 +08:00
destroy_thread();
freeMem();
vLog(LOG_DEBUG, "App: dataAcquisition start reload.\n");
2025-01-16 13:23:42 +08:00
} while(0);
2024-07-08 10:27:17 +08:00
pthread_mutex_destroy(&mutex);
2024-07-19 17:28:02 +08:00
if (zlog_inited) zlog_fini();
2024-10-14 20:26:21 +08:00
ryDevice.ry_destroy();
2024-07-08 10:27:17 +08:00
vLog(LOG_DEBUG, "system stop okay.\n");
return EXIT_SUCCESS;
}