中间件标准版5.1git,去除基础模块

MsgCenter.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "StdAfx.h"
  2. #include "MsgCenter.h"
  3. #include "IMsgObserver.h"
  4. SINGLETON_IMPLEMENT(CMsgCenter)
  5. CMsgCenter::CMsgCenter(void) : m_Stop(false)
  6. {
  7. m_pThreadObj = AfxBeginThread(__dipatchThreadFunc, this);
  8. }
  9. CMsgCenter::~CMsgCenter(void)
  10. {
  11. m_Stop = true;
  12. if(m_pThreadObj != NULL)
  13. {
  14. DWORD ExitCode;
  15. GetExitCodeThread(m_pThreadObj->m_hThread, &ExitCode);
  16. if(ExitCode == STILL_ACTIVE)
  17. {
  18. m_ThreadSleepFlag.SetEvent();
  19. WaitForSingleObject(m_pThreadObj->m_hThread, INFINITE);
  20. }
  21. m_pThreadObj = NULL;
  22. }
  23. }
  24. /*****************************************************************
  25. **【函数名称】 __wait
  26. **【函数功能】 置线程于等待信号状态
  27. **【参数】
  28. **【返回值】
  29. ****************************************************************/
  30. void CMsgCenter::__wait( void )
  31. {
  32. m_ThreadSleepFlag.Lock();
  33. }
  34. /*****************************************************************
  35. **【函数名称】 __releaseMsg
  36. **【函数功能】 释放消息
  37. **【参数】
  38. **【返回值】
  39. ****************************************************************/
  40. void CMsgCenter::__releaseMsg( UINT MsgType, PARAM lpContent )
  41. {
  42. // 填充事件内容
  43. switch(MsgType)
  44. {
  45. case DEV_EVENT_LOG: // 设备日志
  46. {
  47. delete (EventLog*)lpContent;
  48. }
  49. break;
  50. case DEV_EVENT_RES_TYPE: // 设备资源类型
  51. {
  52. delete (EventResType*)lpContent;
  53. }
  54. break;
  55. case DEV_EVENT_RES_DETAIL: // 设备资源明细
  56. {
  57. delete (EventResDetail*)lpContent;
  58. }
  59. break;
  60. case DEV_EVENT_RES_STATUS: // 设备资源状态变化
  61. {
  62. delete (EventResStatus*)lpContent;
  63. }
  64. break;
  65. case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展
  66. {
  67. delete (EventOpProcess*)lpContent;
  68. }
  69. break;
  70. case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果
  71. {
  72. delete (EventOpResult*)lpContent;
  73. }
  74. break;
  75. case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件
  76. {
  77. delete (EventDevOperation*)lpContent;
  78. }
  79. break;
  80. } // end switch
  81. }
  82. /*****************************************************************
  83. **【函数名称】 __dispatchMsg
  84. **【函数功能】 分派消息
  85. **【参数】
  86. **【返回值】
  87. ****************************************************************/
  88. int CMsgCenter::__dispatchMsg( void )
  89. {
  90. CSingleLock Locker(&m_RWLock, TRUE);
  91. // 获取事件队列信息
  92. if(m_MsgList.IsEmpty())
  93. return 0;
  94. MSG_INNER* pMsg = m_MsgList.RemoveHead();
  95. Locker.Unlock();
  96. // 根据实际类型分发消息
  97. __sendMsgToObserver(pMsg->MsgType, pMsg->Content);
  98. __releaseMsg(pMsg->MsgType, pMsg->Content);
  99. // 删除节点,释放内存
  100. delete pMsg;
  101. // 返回
  102. return m_MsgList.GetCount();
  103. }
  104. /*****************************************************************
  105. **【函数名称】 __sendMsgToObserver
  106. **【函数功能】 消息发送给订阅者
  107. **【参数】
  108. **【返回值】
  109. ****************************************************************/
  110. void CMsgCenter::__sendMsgToObserver( UINT MsgType, const PARAM lpContent )
  111. {
  112. ObsvrMap::iterator itr;
  113. ObsvrMap::iterator itrEnd;
  114. itr = m_ObsvrMap.find(MsgType);
  115. if(itr == m_ObsvrMap.end())
  116. return;
  117. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  118. for(; itr != itrEnd; ++itr)
  119. {
  120. itr->second->onMessage(MsgType, lpContent);
  121. }
  122. }
  123. /*****************************************************************
  124. **【函数名称】 __dipatchThreadFunc
  125. **【函数功能】 消息分发线程函数
  126. **【参数】
  127. **【返回值】
  128. ****************************************************************/
  129. UINT CMsgCenter::__dipatchThreadFunc( LPVOID pParam )
  130. {
  131. CMsgCenter* pSelf = (CMsgCenter*)pParam;
  132. while(!pSelf->m_Stop)
  133. {
  134. if(pSelf->__dispatchMsg() == 0)
  135. pSelf->__wait();
  136. } // end while
  137. return 0;
  138. }
  139. /*****************************************************************
  140. **【函数名称】 regist
  141. **【函数功能】 订阅消息
  142. **【参数】
  143. **【返回值】
  144. ****************************************************************/
  145. void CMsgCenter::regist( UINT MsgType, IMsgObserver* pObserver )
  146. {
  147. m_ObsvrMap.insert(pair<UINT, IMsgObserver*>(MsgType, pObserver));
  148. }
  149. /*****************************************************************
  150. **【函数名称】 unregist
  151. **【函数功能】 取消订阅
  152. **【参数】
  153. **【返回值】
  154. ****************************************************************/
  155. void CMsgCenter::unregist( UINT MsgType, IMsgObserver* pObserver )
  156. {
  157. ObsvrMap::iterator itr;
  158. ObsvrMap::iterator itrEnd;
  159. itr = m_ObsvrMap.find(MsgType);
  160. if(itr == m_ObsvrMap.end())
  161. return;
  162. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  163. while(itr != itrEnd)
  164. {
  165. if(itr->second == pObserver)
  166. {
  167. m_ObsvrMap.erase(itr);
  168. break;
  169. }
  170. itr++;
  171. }
  172. }
  173. /*****************************************************************
  174. **【函数名称】 pushMsg
  175. **【函数功能】 压入消息
  176. **【参数】
  177. **【返回值】
  178. ****************************************************************/
  179. void CMsgCenter::pushMsg( UINT MsgType, const PARAM lpContent )
  180. {
  181. // 生成消息实体
  182. MSG_INNER* pMsg = new MSG_INNER;
  183. memset(pMsg, 0, sizeof(pMsg));
  184. pMsg->MsgType = MsgType; // 消息类型
  185. // 填充事件内容
  186. switch(MsgType)
  187. {
  188. case DEV_EVENT_LOG: // 设备日志
  189. {
  190. pMsg->Content = new EventLog;
  191. memcpy(pMsg->Content, lpContent, sizeof(EventLog));
  192. }
  193. break;
  194. case DEV_EVENT_RES_TYPE: // 设备资源类型
  195. {
  196. pMsg->Content = new EventResType;
  197. memcpy(pMsg->Content, lpContent, sizeof(EventResType));
  198. }
  199. break;
  200. case DEV_EVENT_RES_DETAIL: // 设备资源明细
  201. {
  202. pMsg->Content = new EventResDetail;
  203. memcpy(pMsg->Content, lpContent, sizeof(EventResDetail));
  204. }
  205. break;
  206. case DEV_EVENT_INIT_END: // 设备初始化结束
  207. case DEV_EVENT_DEV_CLOSED: // 设备已关闭
  208. {
  209. pMsg->Content = NULL;
  210. }
  211. break;
  212. case DEV_EVENT_RES_STATUS: // 设备资源状态变化
  213. {
  214. pMsg->Content = new EventResStatus;
  215. memcpy(pMsg->Content, lpContent, sizeof(EventResStatus));
  216. }
  217. break;
  218. case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展
  219. {
  220. pMsg->Content = new EventOpProcess;
  221. memcpy(pMsg->Content, lpContent, sizeof(EventOpProcess));
  222. }
  223. break;
  224. case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果
  225. {
  226. pMsg->Content = new EventOpResult;
  227. memcpy(pMsg->Content, lpContent, sizeof(EventOpResult));
  228. }
  229. break;
  230. case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件
  231. {
  232. pMsg->Content = new EventDevOperation;
  233. memcpy(pMsg->Content, lpContent, sizeof(EventDevOperation));
  234. }
  235. break;
  236. } // end switch
  237. m_RWLock.Lock(); // 互斥加锁
  238. // 将消息插入缓冲队列
  239. m_MsgList.AddTail(pMsg);
  240. m_RWLock.Unlock();
  241. // 需要唤醒分发线程
  242. m_ThreadSleepFlag.SetEvent();
  243. }