MiddleWares_YiHe 郑州颐和医院随访系统中间件

MsgCenter.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. **【函数名称】 __dispatchMsg
  36. **【函数功能】 分派消息
  37. **【参数】
  38. **【返回值】
  39. ****************************************************************/
  40. int CMsgCenter::__dispatchMsg( void )
  41. {
  42. CSingleLock Locker(&m_RWLock, TRUE); // 互斥加锁
  43. // 获取事件队列信息
  44. if(m_MsgList.IsEmpty())
  45. return 0;
  46. MSG_INNER * pMsg = m_MsgList.RemoveHead();
  47. Locker.Unlock();
  48. // 根据实际类型分发消息
  49. __sendMsgToObserver(pMsg->MsgType, pMsg->Content);
  50. // 删除节点,释放内存
  51. delete pMsg;
  52. // 返回
  53. return m_MsgList.GetCount();
  54. }
  55. /*****************************************************************
  56. **【函数名称】 __sendMsgToObserver
  57. **【函数功能】 消息发送给订阅者
  58. **【参数】
  59. **【返回值】
  60. ****************************************************************/
  61. void CMsgCenter::__sendMsgToObserver( UINT MsgType, const PARAM lpContent )
  62. {
  63. ObsvrMap::iterator itr;
  64. ObsvrMap::iterator itrEnd;
  65. itr = m_ObsvrMap.find(MsgType);
  66. if(itr == m_ObsvrMap.end())
  67. return;
  68. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  69. for(; itr != itrEnd; ++itr)
  70. {
  71. itr->second->onMessage(MsgType, lpContent);
  72. }
  73. }
  74. /*****************************************************************
  75. **【函数名称】 __dipatchThreadFunc
  76. **【函数功能】 消息分发线程函数
  77. **【参数】
  78. **【返回值】
  79. ****************************************************************/
  80. UINT CMsgCenter::__dipatchThreadFunc( LPVOID pParam )
  81. {
  82. CMsgCenter* pSelf = (CMsgCenter*)pParam;
  83. while(!pSelf->m_Stop)
  84. {
  85. if(pSelf->__dispatchMsg() == 0)
  86. pSelf->__wait();
  87. } // end while
  88. return 0;
  89. }
  90. /*****************************************************************
  91. **【函数名称】 regist
  92. **【函数功能】 订阅消息
  93. **【参数】
  94. **【返回值】
  95. ****************************************************************/
  96. void CMsgCenter::regist( UINT MsgType, IMsgObserver* pObserver )
  97. {
  98. m_ObsvrMap.insert(pair<UINT, IMsgObserver*>(MsgType, pObserver));
  99. }
  100. /*****************************************************************
  101. **【函数名称】 unregist
  102. **【函数功能】 取消订阅
  103. **【参数】
  104. **【返回值】
  105. ****************************************************************/
  106. void CMsgCenter::unregist( UINT MsgType, IMsgObserver* pObserver )
  107. {
  108. ObsvrMap::iterator itr;
  109. ObsvrMap::iterator itrEnd;
  110. itr = m_ObsvrMap.find(MsgType);
  111. if(itr == m_ObsvrMap.end())
  112. return;
  113. itrEnd = m_ObsvrMap.upper_bound(MsgType);
  114. while(itr != itrEnd)
  115. {
  116. if(itr->second == pObserver)
  117. {
  118. m_ObsvrMap.erase(itr);
  119. break;
  120. }
  121. itr++;
  122. }
  123. }
  124. /*****************************************************************
  125. **【函数名称】 pushMsg
  126. **【函数功能】 压入消息
  127. **【参数】
  128. **【返回值】
  129. ****************************************************************/
  130. void CMsgCenter::pushMsg( UINT MsgType, const PARAM lpContent )
  131. {
  132. // 生成消息实体
  133. MSG_INNER* pMsg = new MSG_INNER;
  134. memset(pMsg, 0, sizeof(pMsg));
  135. pMsg->MsgType = MsgType; // 消息类型
  136. // 填充事件内容
  137. switch(MsgType)
  138. {
  139. case IVR_MSG_FLOW_STATE_UPDAET: // 流程状态变更
  140. case IVR_MSG_FLOW_CELL_UPDATE: // 流程节点更新
  141. {
  142. pMsg->Content = lpContent;
  143. }
  144. break;
  145. default:
  146. ASSERT(FALSE);
  147. } // end switch
  148. m_RWLock.Lock(); // 互斥加锁
  149. // 将消息插入缓冲队列
  150. m_MsgList.AddTail(pMsg);
  151. m_RWLock.Unlock();
  152. // 需要唤醒分发线程
  153. m_ThreadSleepFlag.SetEvent();
  154. }