From 8a5087f1b75f0d9c5cf2042c317edbe8e0f71c3c Mon Sep 17 00:00:00 2001 From: lnk Date: Thu, 6 Mar 2025 18:41:48 +0800 Subject: [PATCH] fix recallhis in pingtest --- cfg_parse/cfg_parser.cpp | 147 ++++++++++++++++++++++----------------- json/save2json.h | 39 ++++++++++- 2 files changed, 120 insertions(+), 66 deletions(-) diff --git a/cfg_parse/cfg_parser.cpp b/cfg_parse/cfg_parser.cpp index 902c4d2..7deb64c 100644 --- a/cfg_parse/cfg_parser.cpp +++ b/cfg_parse/cfg_parser.cpp @@ -217,6 +217,8 @@ std::string g_strOTLConnect = "postgres/bmdev@123@pgsql"; //OTL //lnk2024-8-14添加角型接线标志,0不存在角形接线,1存在角形接线 int isdelta_flag = 0; +//测试终端连接 +bool g_stopTelnetTest = false; //////CZY 2023-09-06 config //多前置flag:1为开启,0为关闭 @@ -352,6 +354,7 @@ std::string WEB_FILEDOWNLOAD = ""; extern pthread_mutex_t mtx; /*lnk 2024-10-21 */ +std::string intToString(int number); int OTL_Select_DecideRecall_web(char* time, char* id);//判断是否需要补招 void OTL_Select_recall_web(char* time, char* id); //数据完整性补招判断 bool CheckPG_To_Recall_web(long long start, long long end, char* Monitorid); @@ -1166,70 +1169,76 @@ bool ping_ip(const std::string& ip) { return false; } } +void pingPrint(QTcpSocket* clientSocket, const std::string &msg) { + // 输出到标准输出 + std::cout << msg << std::endl; + // 记录到日志文件 + add_comm_log(const_cast(msg.c_str())); + // 如果 clientSocket 不为空,则发送到 shell + if (clientSocket != nullptr) { + clientSocket->write((msg + "\r\n").c_str()); + clientSocket->flush(); + } +} +int init_ping_telnet(QTcpSocket* clientSocket, int& ip_count, int& telnet_count) { + pingPrint(clientSocket, "start test ping telnet"); + ied_t* ied = NULL; + int iedno; + // 遍历所有设备 + for (iedno = 0; iedno < g_node->n_clients; iedno++) { + // 检查是否被中止 + if (g_stopTelnetTest) { + pingPrint(clientSocket, "Telnet test stopped by user."); + break; + } + ied = g_node->clients[iedno]; + if (ied) { + if (g_onlyIP[0] != 0 && (strcmp(g_onlyIP, ied->channel[0].addr_str) != 0)) { + continue; + } + bool pingResult = ping_ip(ied->channel[0].addr_str); + bool telnetResult = telnet_port_socket(ied->channel[0].addr_str, ied->channel[0].port); -int init_ping_telnet(int& ip_count, int& telnet_count) { - cout << "start test ping telnet" << endl; - ied_t* ied = NULL; - int iedno; - LD_info_t* LD_info = NULL; - int count = 0; - for (iedno = 0; iedno < g_node->n_clients; iedno++) { - ied = g_node->clients[iedno]; - if (ied) { - //ied->channel[0].addr_str[LONGNAME - 1] = 0;//DEV_IP - if (g_onlyIP[0] != 0 && (strcmp(g_onlyIP, ied->channel[0].addr_str) != 0)) { - continue; - } - ied->channel[0].addr_str;//DEV_IP - ied->channel[0].port;//DEV_PortID - bool pingResult = ping_ip(ied->channel[0].addr_str); - //bool pingResult = test_ping(ied->channel[0].addr_str); + std::string logMsg; + logMsg.append("Ping to IP "); + logMsg.append(ied->channel[0].addr_str); + if (pingResult) { + ip_count++; + logMsg.append(" is successful."); + } else { + logMsg.append(" is unsuccessful."); + } + pingPrint(clientSocket, logMsg); + add_comm_log(const_cast(logMsg.c_str())); - //bool telnetResult = telnetIPPort(ied->channel[0].addr_str, ied->channel[0].port); - bool telnetResult = telnet_port_socket(ied->channel[0].addr_str, ied->channel[0].port); - std::string strlog = ""; - strlog.append("Ping to IP "); - strlog.append(ied->channel[0].addr_str); + logMsg = ""; + logMsg.append("Telnet port "); + logMsg.append(QString::number(ied->channel[0].port).toStdString()); + if (telnetResult) { + telnet_count++; + logMsg.append(" is open on IP "); + } else { + logMsg.append(" is closed on IP "); + } + logMsg.append(ied->channel[0].addr_str); + add_comm_log(const_cast(logMsg.c_str())); + pingPrint(clientSocket, logMsg); - if (pingResult) { - ip_count++; - std::cout << "Ping to IP " << ied->channel[0].addr_str << " is successful." << std::endl; - strlog.append(" is successful."); - } - else { - std::cout << "Ping to IP " << ied->channel[0].addr_str << " is unsuccessful." << std::endl; - strlog.append(" is unsuccessful."); + std::string countMsg = "iedno:" + intToString(iedno) + + " ip_count:" + intToString(ip_count) + + " telnet_count:" + intToString(telnet_count); + pingPrint(clientSocket, countMsg); - } - add_comm_log(const_cast(strlog.c_str())); - strlog = ""; - std::string str = QString::number(ied->channel[0].port).toStdString(); - strlog.append("Telnet port "); - strlog.append(str); - if (telnetResult) { - telnet_count++; - std::cout << "Telnet port " << ied->channel[0].port << " is open on IP " << ied->channel[0].addr_str << std::endl; - strlog.append(" is open on IP "); - } - else { - std::cout << "Telnet port " << ied->channel[0].port << " is closed on IP " << ied->channel[0].addr_str << std::endl; - strlog.append(" is closed on IP "); - - } - strlog.append(ied->channel[0].addr_str); - add_comm_log(const_cast(strlog.c_str())); - - cout << "iedno:" << iedno << " ip_count:" << ip_count << " telnet_count:" << telnet_count << endl; - QString MyKafkaIniFilename = QString("../etc/") + QString("testping.ini"); //+QString::fromAscii(subdir) - QSettings settings(MyKafkaIniFilename, QSettings::IniFormat); - settings.setValue("test/IpCount", ip_count); - settings.setValue("test/TelnetCount", telnet_count); - settings.setValue("test/IedCount", iedno); - } - } - cout << "end test ping telnet" << endl; - - return 1; + // 更新配置文件 + QString MyKafkaIniFilename = QString("../etc/") + QString("testping.ini"); + QSettings settings(MyKafkaIniFilename, QSettings::IniFormat); + settings.setValue("test/IpCount", ip_count); + settings.setValue("test/TelnetCount", telnet_count); + settings.setValue("test/IedCount", iedno); + } + } + pingPrint(clientSocket, "end test ping telnet"); + return 1; } //CZY 2023-08-30 read device account from web api @@ -11892,10 +11901,10 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock clientSocket->flush(); while (!stopViewLog) { - // **1. 监听输入,用户输入 `q` 退出** + // **1. 监听输入,用户输入 ``` 退出** if (clientSocket->waitForReadyRead(500)) { // ? 监听输入 QByteArray input = clientSocket->readAll().trimmed(); - if (input == "`") { // ? 用户输入 `q`,退出日志模式 + if (input == "`") { // ? 用户输入 ```,退出日志模式 std::cout << "Received '`' from shell socket! Exiting viewlog...\n"; stopViewLog = true; showinshellflag = false; @@ -13134,7 +13143,7 @@ void OnTimerThread::run() << localTime.tm_hour << ":" << localTime.tm_min << ":" << localTime.tm_sec << endl; - //web手动补招的进程,测试前置所有连接的终端连接情况 +/* //web手动补招的进程,测试前置所有连接的终端连接情况 int ip_count = 0; int telnet_count = 0; if (g_node_id == RECALL_HIS_DATA_BASE_NODE_ID) { @@ -13144,7 +13153,7 @@ void OnTimerThread::run() Cout_account_information(); pthread_mutex_unlock(&mtx); std::cout << "testping free lock !!!!!!!!!!!" << std::endl; } - +*///放入testsheell //单联模式 if (g_onlyIP[0] != 0) { @@ -15636,5 +15645,15 @@ void echo_msg_debugexy(const char *file_name, int line_no, const char *fmt, ...) } } /////////////////////////////////////////////////////////////////////////////// +void telnetetst(QTcpSocket* clientSocket) { + int ip_count = 0; + int telnet_count = 0; + g_stopTelnetTest = false; + //在测试连接时给台账加锁lnk20250114 + pthread_mutex_lock(&mtx); std::cout << "testping hold lock !!!!!!!!!!!" << std::endl; + init_ping_telnet(clientSocket,ip_count, telnet_count); + Cout_account_information(); + pthread_mutex_unlock(&mtx); std::cout << "testping free lock !!!!!!!!!!!" << std::endl; +} diff --git a/json/save2json.h b/json/save2json.h index 9eba7f4..b57ac1c 100644 --- a/json/save2json.h +++ b/json/save2json.h @@ -52,7 +52,12 @@ extern int G_TEST_NUM; extern void ledger(const char* terminal_id = NULL,QIODevice* outputDevice = NULL); extern void value_print(const char *variableName, QTcpSocket *clientSocket); extern int TEST_PORT; +extern void telnetetst(QTcpSocket* clientSocket); +extern void redirectErrorOutput(bool enable); +extern void redirectWarnOutput(bool enable); +extern void redirectNormalOutput(bool enable); +extern void redirectDebugOutput(bool enable); ////////////////////////////////////////////////////////////////////////////// @@ -150,6 +155,7 @@ public: timer(NULL), historyIndex(-1), stopViewLog(false), + g_stopTelnetTest(false), activeClient(NULL) { } @@ -231,6 +237,13 @@ public slots: TEST_NUM = num; } + void setTestlog(bool flag) { + redirectErrorOutput(flag); + redirectWarnOutput(flag); + redirectNormalOutput(flag); + redirectDebugOutput(flag); + } + private slots: /** * @brief 定时任务 @@ -309,11 +322,12 @@ private slots: continue; } - // 1) 处理 '`' 退出 viewlog + // 1) 处理 '`' 退出 viewlog 和ping if (c == '`') { std::cout << "Received '`' from shell socket! Exiting viewlog...\n"; if (activeClient == clientSocket) { stopViewLog = true; + g_stopTelnetTest = true; clientSocket->write("\r\x1B[K"); clientSocket->write("\r\nLog view stopped. Returning to shell.\r\n"); printPrompt(clientSocket); @@ -417,6 +431,8 @@ private: if (cmd == "help") { QString helpText = "Available commands:\r\n"; helpText += "TEST_NUM= - Set the TEST_NUM\r\n"; + helpText += "LOG= - Set the LOG\r\n"; + helpText += "telnettest - Set the telnettest\r\n"; helpText += "rc - Execute rocketmq_test_rc\r\n"; helpText += "rt - Execute rocketmq_test_rt\r\n"; helpText += "ud - Execute rocketmq_test_ud\r\n"; @@ -450,6 +466,23 @@ private: clientSocket->write("Invalid number\r\n"); } } + else if (cmd.startsWith("LOG=")) { + bool ok; + bool flag = cmd.mid(4).toInt(&ok); + if (ok) { + setTestlog(flag); + clientSocket->write("\r\x1B[K"); + clientSocket->write("TEST_NUM updated\r\n"); + } else { + clientSocket->write("\r\x1B[K"); + clientSocket->write("Invalid number\r\n"); + } + } + else if (cmd.startsWith("telnettest")) { + telnetetst(clientSocket); + clientSocket->write("\r\x1B[K"); + clientSocket->write("Executed telnettest warning!!! it woont stop until finish!!!\r\n"); + } else if (cmd.startsWith("rc")) { rocketmq_test_rc(); clientSocket->write("\r\x1B[K"); @@ -586,7 +619,9 @@ private: QString currentCommand; // 当前正在输入的命令 // viewlog 相关 - bool stopViewLog; + bool stopViewLog; + //ping相关 + bool g_stopTelnetTest; QTcpSocket* activeClient; };