CcosChannel.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // CcosChannel.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include <ctype.h>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <ftw.h>
  9. #include <string>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <filesystem>
  13. #include "CDI.h"
  14. #include "BusUnitClient.h"
  15. #include "LogicClient.h"
  16. #include "LocalConfig.h"
  17. #include "PacketAnalizer.h"
  18. #include "ConsoleThread.h"
  19. #include "AutoDmp.h"
  20. //#include "Vga_Demo.h"
  21. #include <chrono>
  22. #include "common_api.h"
  23. #include <set>
  24. #define CTRL_C_EVENT 0
  25. #define CTRL_BREAK_EVENT 1
  26. #define CTRL_CLOSE_EVENT 2
  27. // 3 is reserved!
  28. // 4 is reserved!
  29. #define CTRL_LOGOFF_EVENT 5
  30. #define CTRL_SHUTDOWN_EVENT 6
  31. inline std::string CurrentDateTime()
  32. {
  33. // 获取当前时间点
  34. auto now = std::chrono::system_clock::now();
  35. // 将时间长度转换为微秒数
  36. auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
  37. // 再转成tm格式
  38. auto now_time_t = std::chrono::system_clock::to_time_t(now);
  39. auto now_tm = std::localtime(&now_time_t);
  40. // 可以直接输出到标准输出
  41. // std::cout << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S.") << std::setfill('0') << std::setw(6) << now_us.count() % 1000000 << std::endl;
  42. // 格式化字符,年月日时分秒
  43. std::string now_time_str;
  44. char buf[64];
  45. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", now_tm);
  46. now_time_str += buf;
  47. // 格式化微秒
  48. snprintf(buf, sizeof(buf), ".%06ld ", now_us.count() % 1000000);
  49. now_time_str += buf;
  50. //printf("%s\n", now_time_str.c_str());
  51. return now_time_str;
  52. }
  53. AutoDmp atdmp;
  54. //CommonLogicClient WheelClient;
  55. #define MAX_THREAD_TEST (1)
  56. DWORD ConsoleThreadCount = MAX_THREAD_TEST;
  57. ConsoleThread g_ConsoleThread[MAX_THREAD_TEST];
  58. //UI_Thread g_UIThread;
  59. //Vga_Demo g_VgaDemoThread;
  60. bool StartConsoleThreads(bool start)
  61. {
  62. bool ret = true;
  63. //return ret;//
  64. for (DWORD i = 0; i < ConsoleThreadCount; i++)
  65. {
  66. if (start)
  67. {
  68. g_ConsoleThread[i].SetName("ConsoleThread");
  69. ret &= (g_ConsoleThread[i]).StartThread();
  70. printf("start console Thread:%lu!\n", i);
  71. }
  72. else
  73. {
  74. (g_ConsoleThread[i]).StopThread(100);
  75. }
  76. }
  77. return ret;
  78. }
  79. void ConsoleHandler(int signo)
  80. {
  81. // 处理常见的终止信号
  82. switch (signo) {
  83. case SIGINT: // Ctrl+C (类似 Windows 的 CTRL_CLOSE_EVENT)
  84. case SIGTERM: // 终止信号 (类似 Windows 的 CTRL_SHUTDOWN_EVENT)
  85. printf("Received termination signal! Shutting down...\n");
  86. // 停止控制台线程
  87. StartConsoleThreads(false);
  88. // 等待命令分发系统退出,最多等待 500ms
  89. // 注意: Linux 下需要确保 WaitExit 的实现支持超时和强制退出参数
  90. GetCommandDispathIF()->WaitExit(500, true);
  91. // 在 Linux 中,我们需要执行最终的清理并退出
  92. // Windows 会自动终止进程,但 Linux 需要显式退出
  93. exit(EXIT_SUCCESS);
  94. break;
  95. default:
  96. // 其他信号可以在这里处理
  97. // 例如: SIGQUIT (Ctrl+\), SIGHUP (终端断开) 等
  98. break;
  99. }
  100. }
  101. std::set<string> g_arrWhiteDirectory;
  102. string procdir;
  103. void KeepParent(string dir)
  104. {
  105. if (dir == procdir)
  106. return;
  107. g_arrWhiteDirectory.insert(dir);
  108. string parent = GetFileDirectory(dir);
  109. if (parent == procdir || parent.length() <= 0)
  110. return;
  111. KeepParent(parent);
  112. }
  113. void CheckWhiteDir(ResDataObject & driverConf, string& driverPath, string& sdkPath)
  114. {
  115. if (driverConf.GetFirstOf("oemdriver") > 0)
  116. {
  117. driverPath = (const char*)driverConf["oemdriver"];
  118. string temp("%ROOT%");
  119. driverPath = ReplaceSubString(driverPath, temp, procdir);
  120. if (driverConf.GetFirstOf("SDKPath") > 0)
  121. {
  122. sdkPath = (const char*)driverConf["SDKPath"];
  123. if (sdkPath.length() > 0)
  124. {
  125. if (sdkPath[0] != '\\')
  126. sdkPath = procdir + "\\" + sdkPath;
  127. else
  128. sdkPath = procdir + sdkPath;
  129. if (sdkPath[sdkPath.length() - 1] == '\\')
  130. sdkPath.erase(sdkPath.length() - 1, 1);
  131. }
  132. }
  133. }
  134. else if (driverConf.GetFirstOf("driver") > 0)
  135. {
  136. //普通驱动
  137. driverPath = (const char*)driverConf["driver"];
  138. string temp("%ROOT%");
  139. driverPath = ReplaceSubString(driverPath, temp, procdir);
  140. }
  141. driverPath = GetFileDirectory(driverPath);
  142. g_arrWhiteDirectory.insert(driverPath);
  143. KeepParent(driverPath);
  144. cout<< endl << "Keep Driver White Path " << driverPath << endl;
  145. if (sdkPath.length() > 0)
  146. {
  147. bool isSub = false;
  148. string sdkPpath;
  149. sdkPpath = GetFileDirectory(sdkPath);
  150. if (sdkPpath.length() >= driverPath.length())
  151. {
  152. //if (sdkPath.substr(0, driverPath.length()) == driverPath)
  153. if(sdkPpath == driverPath)
  154. {
  155. //sdk目录在driver子目录
  156. isSub = true;
  157. }
  158. }
  159. if (!isSub)
  160. {
  161. g_arrWhiteDirectory.insert(sdkPath);
  162. KeepParent(sdkPath);
  163. cout << "Keep Driver S D K Path " << sdkPath << endl;
  164. }
  165. else
  166. {
  167. cout << "With Driver S D K Path " << sdkPath << endl;
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// 清理已安装Driver驱动目录,保留白名单驱动
  173. /// </summary>
  174. /// <param name="whitelist"></param>
  175. void CleanDriver(ResDataObject* whitelist)
  176. {
  177. procdir = GetProcessDirectory();
  178. ResDataObject fileList;
  179. GetSpecificDriverConfigFiles(fileList, false);
  180. for (int x = 0; x < fileList.size(); x++)
  181. {
  182. string filename = fileList.GetKey(x);
  183. bool bFind = false;
  184. //std::cout << "CleanDriver " << fileList.GetKey(x) << " white list " << whitelist->encode() << endl;
  185. for (int y = 0; y < whitelist->size(); y++)
  186. {
  187. string key = (*whitelist)[y].encode();
  188. if (filename.find(key) != string::npos)
  189. {
  190. bFind = true;
  191. //
  192. }
  193. }
  194. ResDataObject driverConfFile;
  195. ResDataObject driverConf;
  196. string driverPath,sdkPath;
  197. if (driverConfFile.loadFile(filename.c_str()))
  198. {
  199. if (driverConfFile.GetFirstOf("CONFIGURATION") < 0)
  200. continue;
  201. driverConf = driverConfFile["CONFIGURATION"];
  202. }
  203. if (!bFind)
  204. {
  205. remove(filename.c_str());
  206. }
  207. else
  208. {
  209. CheckWhiteDir(driverConf, driverPath, sdkPath);
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 校验驱动,不在列表的需要移除配置
  215. /// </summary>
  216. /// <param name="fileList">驱动配置文件列表in/out paht:bool </param>
  217. /// <param name="driverList">要保留的驱动目录out</param>
  218. /// <param name="oneDriver">当前要检查的驱动信息 in</param>
  219. /// <param name="driverType">驱动类型如Detector/SynBox等</param>
  220. /// <returns></returns>
  221. bool CheckInDriver(ResDataObject& fileList, ResDataObject& driverList, ResDataObject& oneDriver, const char* driverType)
  222. {
  223. string procdir = GetProcessDirectory();
  224. for (int x = 0; x < fileList.size(); x++)
  225. {
  226. ResDataObject driverConfFile;
  227. if (driverConfFile.loadFile(fileList.GetKey(x)))
  228. {
  229. ResDataObject driverConf;
  230. if (driverConfFile.GetFirstOf("CONFIGURATION") < 0)
  231. continue;
  232. driverConf = driverConfFile["CONFIGURATION"];
  233. if (driverConf.GetFirstOf("MajorID") < 0)
  234. continue;
  235. string MajorID = (const char*)driverConf["MajorID"];
  236. if (strcmp(driverType, MajorID.c_str()) != 0)
  237. {
  238. continue;
  239. }
  240. if (driverConf.GetFirstOf("VendorID") >= 0 && oneDriver.GetFirstOf("VendorID") >= 0)
  241. {
  242. if (driverConf.GetFirstOf("ProductID") >= 0 && oneDriver.GetFirstOf("ProductID") >= 0)
  243. {
  244. if (strcmp(driverConf["ProductID"], oneDriver["ProductID"]) == 0 &&
  245. strcmp(driverConf["VendorID"], oneDriver["VendorID"]) == 0)
  246. {
  247. //匹配了
  248. if (driverConf.GetFirstOf("oemdriver") > 0)
  249. {
  250. string sdkPath, driverPath = driverConf["oemdriver"].encode();
  251. string temp("%ROOT%");
  252. driverPath = ReplaceSubString(driverPath, temp, procdir);
  253. FILE* f = fopen(driverPath.c_str(), "r");
  254. if (f != NULL)
  255. {
  256. fileList[x] = true;
  257. fclose(f);
  258. if (oneDriver.GetFirstOf("Install") >= 0)
  259. {
  260. const char* install = oneDriver["Install"];
  261. if (strcmp(install, "true") == 0)
  262. {
  263. string FullConfig("%FullConfig%");
  264. string DriverConfig("%DriverConfig%");
  265. string dstPath = ReplaceSubString(fileList.GetKey(x), FullConfig, DriverConfig);
  266. //拷贝安装驱动到DriverConfig,暂时不预装
  267. //CopyFile(fileList.GetKey(x), dstPath.c_str(), FALSE);
  268. //std::cout << "Install Driver: " << dstPath << endl;
  269. }
  270. }
  271. //记录驱动保留目录
  272. //存在多个设备共享驱动情况
  273. //只要有一个需要安装,驱动不能删除
  274. driverPath = GetFileDirectory(driverPath);
  275. driverList.update(driverPath.c_str(), true);
  276. CheckWhiteDir(driverConf, driverPath, sdkPath);
  277. return true;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. return false;
  286. }
  287. // 递归删除目录的回调函数
  288. static int remove_directory_entry(const char* fpath, const struct stat* /*sb*/,
  289. int typeflag, struct FTW* /*ftwbuf*/) {
  290. int rv = 0;
  291. switch (typeflag) {
  292. case FTW_F: // 普通文件
  293. rv = remove(fpath);
  294. break;
  295. case FTW_DP: // 空目录
  296. rv = rmdir(fpath);
  297. break;
  298. default:
  299. std::cerr << "无法删除特殊文件: " << fpath << std::endl;
  300. rv = -1;
  301. }
  302. return rv;
  303. }
  304. // 删除文件或目录
  305. // 参数:
  306. // strPath - 要删除的文件/目录路径
  307. // bDelete - Linux环境忽略此参数(保留用于接口兼容)
  308. bool RecycleFileOrFolder(string strPath, bool bDelete = true)
  309. {
  310. if (!strPath.empty() && strPath.back() == '\0') {
  311. strPath.pop_back();
  312. }
  313. struct stat path_stat;
  314. // 检查路径是否存在
  315. if (lstat(strPath.c_str(), &path_stat) != 0) {
  316. perror(("检查路径失败: " + strPath).c_str());
  317. return false;
  318. }
  319. int result = 0;
  320. if (S_ISDIR(path_stat.st_mode)) {
  321. // 递归删除目录及其内容
  322. // FTW_DEPTH: 深度优先遍历(先处理子目录)
  323. // FTW_PHYS: 不解析符号链接
  324. result = nftw(strPath.c_str(), remove_directory_entry,
  325. 64, FTW_DEPTH | FTW_PHYS);
  326. }
  327. else {
  328. // 删除单个文件
  329. result = remove(strPath.c_str());
  330. }
  331. if (result != 0) {
  332. perror(("删除失败: " + strPath).c_str());
  333. return false;
  334. }
  335. std::cout << "已删除: " << strPath << std::endl;
  336. return true;
  337. }
  338. /// <summary>
  339. /// 移除驱动,配置文件和驱动目录
  340. /// </summary>
  341. /// <param name="pszDriverConf">要移除的驱动文件路径</param>
  342. /// <param name="resRetainDriverList">要保留的驱动目录列表</param>
  343. void DeleteDriver(const char* pszDriverConf, ResDataObject& resRetainDriverList)
  344. {
  345. string procdir = GetProcessDirectory();
  346. ResDataObject driverConfFile;
  347. if (driverConfFile.loadFile(pszDriverConf))
  348. {
  349. ResDataObject driverConf;
  350. if (driverConfFile.GetFirstOf("CONFIGURATION") >= 0)
  351. {
  352. driverConf = driverConfFile["CONFIGURATION"];
  353. if (driverConf.GetFirstOf("oemdriver") > 0)
  354. {
  355. string driverPath = driverConf["oemdriver"].encode();
  356. string temp("%ROOT%");
  357. driverPath = ReplaceSubString(driverPath, temp, procdir);
  358. driverPath = GetFileDirectory(driverPath);
  359. if (resRetainDriverList.GetFirstOf(driverPath.c_str()) < 0)
  360. {
  361. RecycleFileOrFolder(driverPath.c_str());
  362. std::cout << "Remove Driver Module: " << driverPath << endl;
  363. }
  364. }
  365. }
  366. }
  367. remove(pszDriverConf);
  368. std::cout << "Delete Driver Config: " << pszDriverConf << endl;
  369. }
  370. bool CopyFile(const std::string& source, const std::string& destination, bool overwrite = true) {
  371. // 检查源文件是否存在
  372. if (!std::filesystem::exists(source)) {
  373. std::cerr << "错误: 源文件不存在 - " << source << std::endl;
  374. return false;
  375. }
  376. // 检查目标文件是否已存在
  377. if (std::filesystem::exists(destination)) {
  378. if (overwrite) {
  379. // 删除已存在的目标文件
  380. if (!std::filesystem::remove(destination)) {
  381. std::cerr << "警告: 无法覆盖已存在的文件 - " << destination << std::endl;
  382. return false;
  383. }
  384. }
  385. else {
  386. // 不覆盖已存在文件
  387. std::cerr << "警告: 目标文件已存在且未设置覆盖 - " << destination << std::endl;
  388. return false;
  389. }
  390. }
  391. try {
  392. // 确保目标目录存在
  393. std::filesystem::path destPath(destination);
  394. std::filesystem::create_directories(destPath.parent_path());
  395. // 执行文件复制
  396. std::filesystem::copy(source, destination, std::filesystem::copy_options::overwrite_existing);
  397. std::cout << "成功复制文件: " << source << " -> " << destination << std::endl;
  398. return true;
  399. }
  400. catch (const std::filesystem::filesystem_error& e) {
  401. std::cerr << "文件复制错误: " << e.what() << std::endl;
  402. return false;
  403. }
  404. catch (...) {
  405. std::cerr << "未知错误发生在文件复制过程中" << std::endl;
  406. return false;
  407. }
  408. }
  409. /// <summary>
  410. /// 校验驱动配置
  411. /// </summary>
  412. /// <param name="pszFileName">驱动配置文件名,不含路径,文件必须在设备目录里</param>
  413. void DefineDriver(const char* pszFileName)
  414. {
  415. string procdir = GetProcessDirectory();
  416. ResDataObject resDriver;
  417. string driverFile = procdir + "\\" + pszFileName;
  418. g_arrWhiteDirectory.clear();
  419. if (resDriver.loadFile(driverFile.c_str()))
  420. {
  421. string canInsstall = procdir + "\\DriverDefine\\DriverDefine.json";
  422. CopyFile(driverFile.c_str(), canInsstall.c_str(), FALSE);
  423. //先清理已安装驱动
  424. ResDataObject whitelist;
  425. if (resDriver.GetFirstOf("WhiteList") >= 0)
  426. {
  427. whitelist = resDriver["WhiteList"];
  428. CleanDriver(&whitelist);
  429. }
  430. if (resDriver.GetFirstOf("Drivers") >= 0)
  431. {
  432. //获取所有配置
  433. ResDataObject fileList, driverList;
  434. GetSpecificDriverConfigFiles(fileList, true);
  435. for (int fn = 0; fn < fileList.size(); fn++)
  436. {
  437. fileList[fn] = false;
  438. }
  439. //校验每个驱动
  440. ResDataObject ResDrivers = resDriver["Drivers"];
  441. for (int x = 0; x < ResDrivers.size(); x++)
  442. {
  443. //Detector/Generator/...
  444. ResDataObject resDrConf = ResDrivers[x];
  445. for (int y = 0; y < resDrConf.size(); y++)
  446. {
  447. ResDataObject oneDriver = resDrConf[y];
  448. CheckInDriver(fileList, driverList, oneDriver, ResDrivers.GetKey(x));
  449. }
  450. }
  451. //移除没有驱动程序的驱动配置
  452. //移除未在驱动配置列表的驱动
  453. //白名单驱动要保留
  454. for (int afn = 0; afn < fileList.size(); afn++)
  455. {
  456. if ((bool)fileList[afn] == false)
  457. {
  458. bool isWhite = false;
  459. string driverFile = fileList.GetKey(afn);
  460. for (int wn = 0; wn < whitelist.size(); wn++)
  461. {
  462. if (driverFile.find((const char*)whitelist[wn]) != string::npos)
  463. {
  464. isWhite = true;
  465. break;
  466. }
  467. }
  468. if (!isWhite)
  469. DeleteDriver(driverFile.c_str(), driverList);
  470. }
  471. }
  472. }
  473. }
  474. std::vector<string> dirList;
  475. FindSubFiles(procdir + "\\OEMDrivers", dirList, true, "*");//, true, 3, 2
  476. FindSubFiles(procdir + "\\ServiceDrivers", dirList, true, "*");//, true, 2, 2
  477. FindSubFiles(procdir + "\\SystemDrivers", dirList, true, "*");//, true, 2, 2
  478. cout << endl << "After Define Left Driver Dirs." << endl;
  479. for (string td : dirList)
  480. {
  481. cout << td << endl;
  482. string tmp = td.substr(td.length() - 4);
  483. //size_t pos = td.find_last_of("conf");
  484. if (tmp == "conf" )
  485. {
  486. g_arrWhiteDirectory.insert(td);
  487. cout << "Keeped." << endl;
  488. }
  489. else
  490. {
  491. if (tmp == "Conf")
  492. {
  493. g_arrWhiteDirectory.insert(td);
  494. cout << "Keeped." << endl;
  495. }
  496. else
  497. {
  498. if (g_arrWhiteDirectory.find(td) != g_arrWhiteDirectory.end())
  499. {
  500. //
  501. cout << "Keeped." << endl;
  502. }
  503. else
  504. {
  505. cout << "Removed. Useless Dir" << endl;
  506. RecycleFileOrFolder(td);
  507. }
  508. }
  509. }
  510. }
  511. cout << endl <<endl << "After Define Left Driver Diretory " << endl;
  512. for (string dir : g_arrWhiteDirectory)
  513. {
  514. cout << dir << endl;
  515. }
  516. }
  517. int main(int argc, char* argv[])
  518. {
  519. bool status = true;
  520. printf("Main Thread:%lu\n", GetCurrentThreadId());
  521. int nTotal = 0;
  522. int nMsgCount = 0;
  523. char szName[20][256];
  524. int count[10] = {0,0,0,0,0,0,0,0,0,0};
  525. if (argc > 1)
  526. {
  527. if (strcmp(argv[1], "test") == 0)
  528. {
  529. int nConnNum = 1;
  530. if (argc > 2)
  531. nConnNum = atoi(argv[2]);
  532. if (nConnNum > 20)
  533. {
  534. nConnNum = 20;
  535. }
  536. ccos_mqtt_connection** pConns = new ccos_mqtt_connection * [nConnNum];
  537. for (int x = 0; x < nConnNum; x++)
  538. {
  539. sprintf(szName[x], "TEST_MQTT_Server_%d", x);
  540. // Lambda回调函数 - 处理收到的MQTT消息
  541. auto callback = [szName, x, &count, &nTotal, &nMsgCount](ResDataObject* rsp, const char* topic, void* conn_void) {
  542. ccos_mqtt_connection* conn = (ccos_mqtt_connection*)conn_void;
  543. cout << "Server [" << szName[x] << "] " << CurrentDateTime()
  544. << " Get Msg from " << topic << " Msg Body " << rsp->encode() << endl;
  545. // 计算消息延迟
  546. string tickSend = PacketAnalizer::GetPacketKey(rsp);
  547. DWORD dwTick = GetTickCount() - atoi(tickSend.c_str());
  548. nTotal += dwTick;
  549. nMsgCount++;
  550. // 统计延迟分布
  551. if (dwTick < 3) count[0]++;
  552. else if (dwTick < 5) count[1]++;
  553. else if (dwTick < 10) count[2]++;
  554. else if (dwTick < 15) count[3]++;
  555. else if (dwTick < 20) count[4]++;
  556. else {
  557. count[5]++;
  558. // 可选:打印长延迟消息
  559. // cout << "Receive Packet Use Time [" << dwTick << "]ms" << endl;
  560. }
  561. // 处理响应包
  562. if (PacketAnalizer::GetPacketType(rsp) == PACKET_TYPE_RES) {
  563. if (PacketAnalizer::GetPacketCmd(rsp) == PACKET_CMD_OPEN) {
  564. // 连接成功后订阅通知主题
  565. string notifyTopic = "CCOS/Tetst//Notify/#";
  566. SubscribeTopic(conn, notifyTopic.c_str());
  567. }
  568. else {
  569. cout << szName[x] << CurrentDateTime()
  570. << " Try Conn callback get unknown packet" << endl;
  571. cout << "Unknown Packet: cmd ["
  572. << PacketAnalizer::GetPacketCmd(rsp)
  573. << "] packet key: " << PacketAnalizer::GetPacketKey(rsp) << endl;
  574. }
  575. }
  576. };
  577. pConns[x] = NewConnection(szName[x], callback);
  578. // 订阅主题
  579. char szTopic[256];
  580. sprintf(szTopic, "CCOS/TEST/Notify/+/%d/#", x);
  581. SubscribeTopic(pConns[x], szTopic);
  582. SubscribeTopic(pConns[x], "CCOS/TEST/Action/#", true);
  583. }
  584. std::cout << "Waiting quit..." << endl;
  585. char cmd;
  586. while (1)
  587. {
  588. cmd = getchar();
  589. if (cmd == 'q')
  590. break;
  591. else if (cmd == 'p')
  592. {
  593. std::cout << "Time < 3 : " << count[0] << endl;
  594. std::cout << "Time < 5 : " << count[1] << endl;
  595. std::cout << "Time < 10 : " << count[2] << endl;
  596. std::cout << "Time < 15 : " << count[3] << endl;
  597. std::cout << "Time < 20 : " << count[4] << endl;
  598. std::cout << "Time > 20 : " << count[5] << endl;
  599. // 计算平均延迟
  600. if (nMsgCount > 0) {
  601. cout << "TotalTime " << nTotal << "ms, Msg count: " << nMsgCount
  602. << ", Average: " << (nTotal * 1.0 / nMsgCount) << "ms" << endl;
  603. }
  604. else {
  605. cout << "No messages received" << endl;
  606. }
  607. memset(count, 0, sizeof(count));
  608. nTotal = nMsgCount = 0;
  609. }
  610. else if (cmd == 'x')
  611. {
  612. for (int x = 0; x < nConnNum; x++)
  613. {
  614. ResetConnection(pConns[x]);
  615. }
  616. }
  617. usleep(10000);
  618. }
  619. for (int x = 0; x < nConnNum; x++)
  620. {
  621. CloseConnection(pConns[x]);
  622. }
  623. delete[] pConns;
  624. std::cout << "Hello World!\n";
  625. return 0;
  626. }
  627. else
  628. {
  629. std::cout << "Starting Process Driver Define....." << endl;
  630. DefineDriver(argv[1]);
  631. std::cout << "Driver Define Done." << endl;
  632. return 0;
  633. }
  634. }
  635. setLogRootpath(((string)getChannelRootpath()).c_str());
  636. if (GetCommandDispathIF()->InitAs(CCOS_PROC_CHANNEL, (UINT64)getpid()))
  637. {
  638. // 设置信号处理
  639. signal(SIGINT, ConsoleHandler);
  640. signal(SIGTERM, ConsoleHandler);
  641. LogicClient buClient("channel_temp", "", "", false);
  642. if (buClient.Open((const char*)getRootpath(), ALL_ACCESS) == RET_SUCCEED)
  643. {
  644. //buClient.ExitDriverProc();
  645. buClient.Close();
  646. /*DWORD count = buClient.GetDeviceCount();
  647. for (DWORD i = 0; i < count; i++)
  648. {
  649. UINT64 ProcId, Addr;
  650. ResDataObject DevType,devpath, MachineId;
  651. buClient.GetDeviceDescript(i, devpath, DevType, MachineId, ProcId, Addr);
  652. printf("path:%s\nProc:%I64u\nAddr:%I64u\n", (const char*)devpath, ProcId, Addr);
  653. LogicClient DevClient("channel_exe_temp","channel");
  654. if (DevClient.Open((const char*)devpath, ALL_ACCESS) >= RET_SUCCEED)
  655. {
  656. DevClient.Close();
  657. printf("Open Succeed\n");
  658. }
  659. else
  660. {
  661. printf("Open Failed\n");
  662. }
  663. }*/
  664. status = true;
  665. cout << "Channel initialization successful" << endl;
  666. }
  667. else
  668. {
  669. status = false;
  670. cerr << "Channel initialization failed" << endl;
  671. }
  672. //start work
  673. if (status)
  674. {
  675. if (StartConsoleThreads(true))
  676. {
  677. cout << "Channel initialized. Entering main loop..." << endl;
  678. while (1) {
  679. if (GetCommandDispathIF()->WaitExit(500))
  680. {
  681. StartConsoleThreads(false);
  682. cout << "Service shutdown complete" << endl;
  683. //ShareMemory_ServerStop();
  684. return 0;
  685. }
  686. }
  687. }
  688. }
  689. cerr << "Service initialization failed. Exiting..." << endl;
  690. sleep(3);
  691. }
  692. //ShareMemory_ServerStop();
  693. return 1;
  694. }