| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #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();
- }
- /*****************************************************************
- **【函数名称】 __releaseMsg
- **【函数功能】 释放消息
- **【参数】
- **【返回值】
- ****************************************************************/
- void CMsgCenter::__releaseMsg( UINT MsgType, PARAM lpContent )
- {
- // 填充事件内容
- switch(MsgType)
- {
- case DEV_EVENT_LOG: // 设备日志
- {
- delete (EventLog*)lpContent;
- }
- break;
- case DEV_EVENT_RES_TYPE: // 设备资源类型
- {
- delete (EventResType*)lpContent;
- }
- break;
- case DEV_EVENT_RES_DETAIL: // 设备资源明细
- {
- delete (EventResDetail*)lpContent;
- }
- break;
- case DEV_EVENT_RES_STATUS: // 设备资源状态变化
- {
- delete (EventResStatus*)lpContent;
- }
- break;
- case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展
- {
- delete (EventOpProcess*)lpContent;
- }
- break;
- case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果
- {
- delete (EventOpResult*)lpContent;
- }
- break;
- case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件
- {
- delete (EventDevOperation*)lpContent;
- }
- break;
- } // end switch
- }
- /*****************************************************************
- **【函数名称】 __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);
- __releaseMsg(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 DEV_EVENT_LOG: // 设备日志
- {
- pMsg->Content = new EventLog;
- memcpy(pMsg->Content, lpContent, sizeof(EventLog));
- }
- break;
- case DEV_EVENT_RES_TYPE: // 设备资源类型
- {
- pMsg->Content = new EventResType;
- memcpy(pMsg->Content, lpContent, sizeof(EventResType));
- }
- break;
- case DEV_EVENT_RES_DETAIL: // 设备资源明细
- {
- pMsg->Content = new EventResDetail;
- memcpy(pMsg->Content, lpContent, sizeof(EventResDetail));
- }
- break;
- case DEV_EVENT_INIT_END: // 设备初始化结束
- case DEV_EVENT_DEV_CLOSED: // 设备已关闭
- {
- pMsg->Content = NULL;
- }
- break;
- case DEV_EVENT_RES_STATUS: // 设备资源状态变化
- {
- pMsg->Content = new EventResStatus;
- memcpy(pMsg->Content, lpContent, sizeof(EventResStatus));
- }
- break;
- case DEV_EVENT_LINE_OP_PROCESS: // 线路操作进展
- {
- pMsg->Content = new EventOpProcess;
- memcpy(pMsg->Content, lpContent, sizeof(EventOpProcess));
- }
- break;
- case DEV_EVENT_LINE_OP_RESULT: // 线路操作执行结果
- {
- pMsg->Content = new EventOpResult;
- memcpy(pMsg->Content, lpContent, sizeof(EventOpResult));
- }
- break;
- case DEV_EVENT_DEV_OPERATOR: // 设备主动操作事件
- {
- pMsg->Content = new EventDevOperation;
- memcpy(pMsg->Content, lpContent, sizeof(EventDevOperation));
- }
- break;
- } // end switch
- m_RWLock.Lock(); // 互斥加锁
- // 将消息插入缓冲队列
- m_MsgList.AddTail(pMsg);
- m_RWLock.Unlock();
- // 需要唤醒分发线程
- m_ThreadSleepFlag.SetEvent();
- }
|