中间件底层,websocket

CtiCore.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "StdAfx.h"
  2. #include "CtiCore.h"
  3. #include "MsgCenter.h"
  4. #include "CtiDataDef.h"
  5. #include "NetworkCti.h"
  6. #include "Config.h"
  7. #include "IvrFlowHolder.h"
  8. #include "LineHolder.h"
  9. #include "TaskMgr.h"
  10. #include "../Public/DaemonClient/DaemonClient.h"
  11. #include "LicenseMgr.h"
  12. SINGLETON_IMPLEMENT(CCtiCore)
  13. CCtiCore::CCtiCore(void)
  14. {
  15. }
  16. CCtiCore::~CCtiCore(void)
  17. {
  18. }
  19. /*****************************************************************
  20. **【函数名称】 __init
  21. **【函数功能】 内部初始化
  22. **【参数】
  23. **【返回值】
  24. ****************************************************************/
  25. void CCtiCore::__init(void)
  26. {
  27. // 注册底层设备消息事件
  28. CMsgCenter& MsgCenter = CMsgCenter::GetInstance();
  29. MsgCenter.regist(DEV_EVENT_LOG, this);
  30. MsgCenter.regist(DEV_EVENT_INIT_END, this);
  31. MsgCenter.regist(DEV_EVENT_DEV_CLOSED, this);
  32. }
  33. void CCtiCore::__uninit(void)
  34. {
  35. // 取消注册底层设备消息事件
  36. CMsgCenter& MsgCenter = CMsgCenter::GetInstance();
  37. MsgCenter.unregist(DEV_EVENT_LOG, this);
  38. MsgCenter.unregist(DEV_EVENT_INIT_END, this);
  39. MsgCenter.unregist(DEV_EVENT_DEV_CLOSED, this);
  40. }
  41. /*****************************************************************
  42. **【函数名称】 __onDevInitEnd
  43. **【函数功能】 处理设备初始化结束事处理函数
  44. **【参数】
  45. **【返回值】
  46. ****************************************************************/
  47. void CCtiCore::__onDevInitEnd(void)
  48. {
  49. AfxGetMainWnd()->PostMessage(WM_CORE_EVENT, CORE_EVENT_DEV_INITEND, 0);
  50. }
  51. /*****************************************************************
  52. **【函数名称】 __onDevClosed
  53. **【函数功能】 处理设备连接断开事件
  54. **【参数】
  55. **【返回值】
  56. ****************************************************************/
  57. void CCtiCore::__onDevClosed(void)
  58. {
  59. ILogger::getInstance().log(LOG_CLASS_DEV, LOG_LEVEL_ERROR, STR_ERR_DEVLINK_DROP);
  60. AfxGetMainWnd()->PostMessage(WM_CORE_EVENT, CORE_EVENT_DEV_CLOSE, 0);
  61. }
  62. /*****************************************************************
  63. **【函数名称】 stage1Start
  64. **【函数功能】 第一阶段启动
  65. **【参数】
  66. **【返回值】
  67. ****************************************************************/
  68. bool CCtiCore::stage1Start(void)
  69. {
  70. // 连接配置数据库
  71. if (!IOtlConnection::getInstance()->Connect())
  72. {
  73. AfxMessageBox(IOtlConnection::getInstance()->GetLastError());
  74. AfxMessageBox(STR_ERR_CORE_INIT_DB_CONN);
  75. return false;
  76. }
  77. // 加载配置
  78. if (!CConfig::GetInstance().loadConfig())
  79. {
  80. // 断开数据库连接
  81. IOtlConnection::getInstance()->Disconnect();
  82. AfxMessageBox(STR_ERR_CORE_INIT_CFG_LOAD);
  83. return false;
  84. }
  85. return true;
  86. }
  87. /*****************************************************************
  88. **【函数名称】 stage2Start
  89. **【函数功能】 第二阶段启动
  90. **【参数】
  91. **【返回值】
  92. ****************************************************************/
  93. bool CCtiCore::stage2Start(void)
  94. {
  95. __init();
  96. CLineHolder::GetInstance().init();
  97. CTaskMgr::GetInstance().init();
  98. // 初始化Ivrflow
  99. CIvrFlowHolder::GetInstance().createIvrFlow(CConfig::GetInstance().ivrFlowNum());
  100. // 建立网络通讯
  101. if (!CNetworkCti::GetInstance().init())
  102. {
  103. // 断开数据库连接
  104. IOtlConnection::getInstance()->Disconnect();
  105. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("%s"), STR_ERR_CORE_INIT_NET_SETUP);
  106. return false;
  107. }
  108. // 打开底层设备
  109. if (!m_DevLink.open())
  110. {
  111. // 断开数据库连接
  112. IOtlConnection::getInstance()->Disconnect();
  113. ILogger::getInstance().log(LOG_CLASS_BUSI, LOG_LEVEL_ERROR, _T("%s"), STR_ERR_CORE_INIT_DEV_OPEN);
  114. return false;
  115. }
  116. CDaemonClient::GetInstance().doWork();
  117. return true;
  118. }
  119. /*****************************************************************
  120. **【函数名称】 stop
  121. **【函数功能】 停止CTI
  122. **【参数】
  123. **【返回值】
  124. ****************************************************************/
  125. void CCtiCore::stop(void)
  126. {
  127. CNetworkCti::GetInstance().release();
  128. // 销毁IVRFlow
  129. CIvrFlowHolder::GetInstance().destroyIvrFlow();
  130. // 关闭设备
  131. m_DevLink.close();
  132. // 断开数据库连接
  133. IOtlConnection::getInstance()->Disconnect();
  134. // 2022-09-28 日志哪里开始关闭
  135. //ILogger::getInstance().close();
  136. }
  137. void CCtiCore::StopForContinue(void)
  138. {
  139. CNetworkCti::GetInstance().release();
  140. // 销毁IVRFlow
  141. CIvrFlowHolder::GetInstance().destroyIvrFlow();
  142. // 关闭设备
  143. m_DevLink.close();
  144. // 断开数据库连接
  145. IOtlConnection::getInstance()->Disconnect();
  146. CDaemonClient::GetInstance().Release();
  147. //ILogger::getInstance().close();
  148. }
  149. void CCtiCore::UnInit(void)
  150. {
  151. CTaskMgr::GetInstance().uninit();
  152. CLineHolder::GetInstance().uninit();
  153. __uninit();
  154. }
  155. /*****************************************************************
  156. **【函数名称】 onMessage
  157. **【函数功能】 消息中心分发的消息处理函数
  158. **【参数】 MsgType:消息事件类型
  159. lpContent:消息内容
  160. **【返回值】
  161. ****************************************************************/
  162. void CCtiCore::onMessage(UINT MsgType, const PARAM lpContent)
  163. {
  164. switch (MsgType)
  165. {
  166. case DEV_EVENT_LOG: // 设备日志
  167. {
  168. ASSERT(lpContent != NULL);
  169. if (lpContent == NULL) return;
  170. EventLog* pLogInfo = (EventLog*)lpContent;
  171. ILogger::getInstance().log(LOG_CLASS_DEV, (LOG_LEVEL)(pLogInfo->nLevel), _T("%s"), pLogInfo->szContent);
  172. }
  173. break;
  174. case DEV_EVENT_INIT_END: // 设备初始化结束
  175. {
  176. __onDevInitEnd();
  177. }
  178. break;
  179. case DEV_EVENT_DEV_CLOSED: // 设备已关闭
  180. {
  181. __onDevClosed();
  182. }
  183. break;
  184. } //end switch
  185. }
  186. /*****************************************************************
  187. **【函数名称】 KillProcess
  188. **【函数功能】 强制结束线程
  189. **【参数】 窗口名称
  190. **【返回值】
  191. ****************************************************************/
  192. int CCtiCore::KillProcess(LPCSTR pszClassName, LPCSTR pszWindowTitle)
  193. {
  194. HANDLE hProcessHandle;
  195. ULONG nProcessID;
  196. HWND TheWindow;
  197. TheWindow = ::FindWindow(NULL, pszWindowTitle);
  198. ::GetWindowThreadProcessId(TheWindow, &nProcessID);
  199. hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, nProcessID);
  200. return ::TerminateProcess(hProcessHandle, 4);
  201. }