| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include "StdAfx.h"
- #include "MsgCenter.h"
- #include "IMsgObserver.h"
- SINGLETON_IMPLEMENT(CMsgCenter)
- CMsgCenter::CMsgCenter(void) : m_Stop(false)
- {
- m_pThreadObj = AfxBeginThread(__dipatchThreadFunc, this);
- }
- CMsgCenter::~CMsgCenter(void)
- {
- m_Stop = true;
- if(m_pThreadObj != NULL)
- {
- DWORD ExitCode;
- GetExitCodeThread(m_pThreadObj->m_hThread, &ExitCode);
- if(ExitCode == STILL_ACTIVE)
- {
- m_ThreadSleepFlag.SetEvent();
- WaitForSingleObject(m_pThreadObj->m_hThread, INFINITE);
- }
- m_pThreadObj = NULL;
- }
- }
- /*****************************************************************
- **【函数名称】 __wait
- **【函数功能】 置线程于等待信号状态
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::__wait( void )
- {
- m_ThreadSleepFlag.Lock();
- }
- /*****************************************************************
- **【函数名称】 __dispatchMsg
- **【函数功能】 分派消息
- **【参数】
- **【返回值】
- ****************************************************************/
- int CMsgCenter::__dispatchMsg( void )
- {
- CSingleLock Locker(&m_RWLock, TRUE); // 互斥加锁
- // 获取事件队列信息
- if(m_MsgList.IsEmpty())
- return 0;
- MSG_INNER * pMsg = m_MsgList.RemoveHead();
- Locker.Unlock();
- // 根据实际类型分发消息
- __sendMsgToObserver(pMsg->MsgType, pMsg->Content);
-
- // 删除节点,释放内存
- delete pMsg;
- // 返回
- return m_MsgList.GetCount();
- }
- /*****************************************************************
- **【函数名称】 __sendMsgToObserver
- **【函数功能】 消息发送给订阅者
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::__sendMsgToObserver( UINT MsgType, const PARAM lpContent )
- {
- ObsvrMap::iterator itr;
- ObsvrMap::iterator itrEnd;
- itr = m_ObsvrMap.find(MsgType);
- if(itr == m_ObsvrMap.end())
- return;
- itrEnd = m_ObsvrMap.upper_bound(MsgType);
- for(; itr != itrEnd; ++itr)
- {
- itr->second->onMessage(MsgType, lpContent);
- }
- }
- /*****************************************************************
- **【函数名称】 __dipatchThreadFunc
- **【函数功能】 消息分发线程函数
- **【参数】
- **【返回值】
- ****************************************************************/
- UINT CMsgCenter::__dipatchThreadFunc( LPVOID pParam )
- {
- CMsgCenter* pSelf = (CMsgCenter*)pParam;
- while(!pSelf->m_Stop)
- {
- if(pSelf->__dispatchMsg() == 0)
- pSelf->__wait();
- } // end while
- return 0;
- }
- /*****************************************************************
- **【函数名称】 regist
- **【函数功能】 订阅消息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::regist( UINT MsgType, IMsgObserver* pObserver )
- {
- m_ObsvrMap.insert(pair<UINT, IMsgObserver*>(MsgType, pObserver));
- }
- /*****************************************************************
- **【函数名称】 unregist
- **【函数功能】 取消订阅
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::unregist( UINT MsgType, IMsgObserver* pObserver )
- {
- ObsvrMap::iterator itr;
- ObsvrMap::iterator itrEnd;
- itr = m_ObsvrMap.find(MsgType);
- if(itr == m_ObsvrMap.end())
- return;
- itrEnd = m_ObsvrMap.upper_bound(MsgType);
- while(itr != itrEnd)
- {
- if(itr->second == pObserver)
- {
- m_ObsvrMap.erase(itr);
- break;
- }
- itr++;
- }
- }
- /*****************************************************************
- **【函数名称】 pushMsg
- **【函数功能】 压入消息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::pushMsg( UINT MsgType, const PARAM lpContent )
- {
- // 生成消息实体
- MSG_INNER* pMsg = new MSG_INNER;
- memset(pMsg, 0, sizeof(pMsg));
- pMsg->MsgType = MsgType; // 消息类型
- // 填充事件内容
- switch(MsgType)
- {
- case IVR_MSG_FLOW_STATE_UPDAET: // 流程状态变更
- case IVR_MSG_FLOW_CELL_UPDATE: // 流程节点更新
- {
- pMsg->Content = lpContent;
- }
- break;
- default:
- ASSERT(FALSE);
- } // end switch
- m_RWLock.Lock(); // 互斥加锁
- // 将消息插入缓冲队列
- m_MsgList.AddTail(pMsg);
- m_RWLock.Unlock();
- // 需要唤醒分发线程
- m_ThreadSleepFlag.SetEvent();
- }
|