中航光电的中间件仓库

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. ILogger::getInstance().close();
  135. }
  136. void CCtiCore::StopForContinue(void)
  137. {
  138. CNetworkCti::GetInstance().release();
  139. // 销毁IVRFlow
  140. CIvrFlowHolder::GetInstance().destroyIvrFlow();
  141. // 关闭设备
  142. m_DevLink.close();
  143. // 断开数据库连接
  144. IOtlConnection::getInstance()->Disconnect();
  145. CDaemonClient::GetInstance().Release();
  146. //ILogger::getInstance().close();
  147. }
  148. void CCtiCore::UnInit(void)
  149. {
  150. CTaskMgr::GetInstance().uninit();
  151. CLineHolder::GetInstance().uninit();
  152. __uninit();
  153. }
  154. /*****************************************************************
  155. **【函数名称】 onMessage
  156. **【函数功能】 消息中心分发的消息处理函数
  157. **【参数】 MsgType:消息事件类型
  158. lpContent:消息内容
  159. **【返回值】
  160. ****************************************************************/
  161. void CCtiCore::onMessage(UINT MsgType, const PARAM lpContent)
  162. {
  163. switch (MsgType)
  164. {
  165. case DEV_EVENT_LOG: // 设备日志
  166. {
  167. ASSERT(lpContent != NULL);
  168. if (lpContent == NULL) return;
  169. EventLog* pLogInfo = (EventLog*)lpContent;
  170. ILogger::getInstance().log(LOG_CLASS_DEV, (LOG_LEVEL)(pLogInfo->nLevel), _T("%s"), pLogInfo->szContent);
  171. }
  172. break;
  173. case DEV_EVENT_INIT_END: // 设备初始化结束
  174. {
  175. __onDevInitEnd();
  176. }
  177. break;
  178. case DEV_EVENT_DEV_CLOSED: // 设备已关闭
  179. {
  180. __onDevClosed();
  181. }
  182. break;
  183. } //end switch
  184. }
  185. /*****************************************************************
  186. **【函数名称】 KillProcess
  187. **【函数功能】 强制结束线程
  188. **【参数】 窗口名称
  189. **【返回值】
  190. ****************************************************************/
  191. int CCtiCore::KillProcess(LPCSTR pszClassName, LPCSTR pszWindowTitle)
  192. {
  193. HANDLE hProcessHandle;
  194. ULONG nProcessID;
  195. HWND TheWindow;
  196. TheWindow = ::FindWindow(NULL, pszWindowTitle);
  197. ::GetWindowThreadProcessId(TheWindow, &nProcessID);
  198. hProcessHandle = ::OpenProcess(PROCESS_TERMINATE, FALSE, nProcessID);
  199. return ::TerminateProcess(hProcessHandle, 4);
  200. }