📄 clientimpleventmanager.h
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#ifndef _CLIENTIMPLEVENTMANAGER_H#define _CLIENTIMPLEVENTMANAGER_H#include <map>#include <list>#include "base_Event.h"#include "inc/framework/win2linux.h"using namespace Intel::Mobile::BaseAPI;using namespace std;class EventClientImpl;class ObserverImpl;class ObserverImpl;/****************************************************************************************CEventBind to maintain the 1-1 relationship between class Intel::Mobile::BaseAPI::Event and event Handle For same event type, there can be more than one event properties. e.g. BatteryClass & NetworkAdapterClass all contain an event property "Add" whose event type is eAdd. ****************************************************************************************/class CEventBind {public: HANDLE GetHandle(); Intel::Mobile::BaseAPI::Event* GetTheEvent(); bool SetTheEvent(Intel::Mobile::BaseAPI::Event & event); CEventBind(EventClientImpl *pCltImpl, Intel::Mobile::BaseAPI::Event::EventType eType); virtual ~CEventBind();protected: Intel::Mobile::BaseAPI::Event *m_pTheEvent; HANDLE m_hEvent;};/****************************************************************************************Class CObserversOnSameEvent is to maintain all observers that registered on a same event. There are multiple instances of CObserversOnSameEvent, each for one event type.****************************************************************************************/class CObserversOnSameEvent {public: bool NotifyAllObservers(); int GetObservCount(); void RemoveObserver(ObserverImpl *pObserver, BOOL bOnce = TRUE); void AddObserver(ObserverImpl *pObserver); CObserversOnSameEvent(Intel::Mobile::BaseAPI::Event& theEvent); virtual ~CObserversOnSameEvent(); const ObserverImpl* GetObserver(unsigned int nIndex); void RemoveObservers(); bool SetTheEvent( Intel::Mobile::BaseAPI::Event & event);protected: typedef list<ObserverImpl *> ListObservers; ListObservers m_lstObservers; CRITICAL_SECTION m_csProtectList; HANDLE m_hEvent; Intel::Mobile::BaseAPI::Event m_theEvent;};class CMapEventType2EventBind{public: CMapEventType2EventBind(); virtual ~CMapEventType2EventBind(); //given [(ServerObject *) + Intel::Mobile::BaseAPI::Event::EventType], find respondent (CEventBind *), //if not exist and bCreateNoneExist, create it //EventClientImpl * is not persistent while the underlying ServerObject * is CEventBind *GetEventBind(EventClientImpl *pEvtCltImp, Intel::Mobile::BaseAPI::Event::EventType eType, BOOL bCreateNoneExist = TRUE); bool HasAddedObserver(Intel::Mobile::BaseAPI::Event::EventType eType); void RemoveEventBind(Intel::Mobile::BaseAPI::Event::EventType eType, BOOL &bDeleteMap);protected: typedef map<Intel::Mobile::BaseAPI::Event::EventType, CEventBind *> MapEventType2EventBind; MapEventType2EventBind m_MapEventType2EventBind;};//EventClientImpl * is not persistent while the underlying ServerObject * is.//relationship between [(ServerObject *) + Intel::Mobile::BaseAPI::Event::EventType] and (CEventBind *) is 1:1,//so use 2 layers of map:// 1st: (ServerObject *) map to Intel::Mobile::BaseAPI::Event::EventType, wrapped by CClientEventMapManager// 2nd: Intel::Mobile::BaseAPI::Event::EventType map to (CEventBind *), wrapped by CMapEventType2EventBind//class ServerObject;// TBDclass CClientEventMapManager{public: CClientEventMapManager(); virtual ~CClientEventMapManager(); //given [(ServerObject *) + Intel::Mobile::BaseAPI::Event::EventType], find respondent (CEventBind *), //if not exist && bCreateNoneExist, create it //EventClientImpl * is not persistent while the underlying ServerObject * is CEventBind *GetEventBind(EventClientImpl *pEvtCltImp, Intel::Mobile::BaseAPI::Event::EventType eType, BOOL bCreateNoneExist = TRUE); bool HasAddedObserver(void *pSvrObj, Intel::Mobile::BaseAPI::Event::EventType eType); void RemoveEventBind(EventClientImpl *pEvtCltImp, Intel::Mobile::BaseAPI::Event::EventType eType);protected: typedef map<void *, CMapEventType2EventBind *> MapSvrObj; MapSvrObj m_mapSvrObj;};/****************************************************************************************Class CClientImplEventManager is to wrap the monitoring thread.there is only one global instance of CClientImplEventManager since there is only one thread to monitor all events.****************************************************************************************/#define MAXIMUM_WAIT_OBJECTS 256class CClientImplEventManager {public: static CClientImplEventManager & GetClientImplEventManagerInst(); CEventBind *GetEventBind(EventClientImpl *pEvtCltImp, Intel::Mobile::BaseAPI::Event::EventType eType, BOOL bCreateNoneExist = TRUE); void RemoveEventBind(EventClientImpl *pEvtCltImp, Intel::Mobile::BaseAPI::Event::EventType eType); bool HasAddedObserver(void *pSvrObj, Intel::Mobile::BaseAPI::Event::EventType eType); bool RemoveObserver( HANDLE hEvent, ObserverImpl *pObserver, BOOL &bRemoveEvent); bool AddObserver(CEventBind &eb,ObserverImpl *pObserver); void EndMonitor(); BOOL StartMonitor(); CClientImplEventManager(); virtual ~CClientImplEventManager(); unsigned int GetObserverCount( HANDLE hEvent ); bool RemoveObservers(HANDLE hEvent) ; const ObserverImpl* GetObserver(HANDLE hEvent, unsigned int nIndex ) ; bool UpdateEventOfHandle(HANDLE hEvent, Intel::Mobile::BaseAPI::Event &pTheEvent);protected: //static CClientImplEventManager s_ClientImplEventManagerInst;//the only instance CClientEventMapManager m_CltEventMapMgr; void DeleteEventFromMonitor(HANDLE hEvent); bool OnUnderlayingEvent(int nEventIndex); DWORD WaitForEvents(); BOOL AddEvent2Monitor(HANDLE hEvent); static DWORD WINAPI MonitorThreadProc(LPVOID lpParam); typedef map<HANDLE, CObserversOnSameEvent * > MapObserversOnSameEvent; //Use only one event handle for all observers that on same event MapObserversOnSameEvent m_mapObserversOnSameEvent; /* replace m_nThreadCtrlFlag with m_bStopMonitorThread since event handle is added / removed only by main thread, no detail need monitor thread to know about added / removed */ BOOL m_bStopMonitorThread;//control monitor thread to STOP// ThreadCtrlFlag m_nThreadCtrlFlag;//control monitor thread to STOP or //ADD_EVENT_FOR_MONITOR or DELETE_EVENT_FROM_MONITOR //all Intel::Mobile::BaseAPI::Event Handles to monitor, the 1st should be m_hCtrlEvent //event handle is added / removed only by main thread HANDLE m_arrayEventHandle[MAXIMUM_WAIT_OBJECTS]; CRITICAL_SECTION m_cs;//use only one CRITICAL_SECTION to synchronize the access to //both m_arrayEventHandle and m_mapObserversOnSameEvent int m_nEventCount; //current event count in array HANDLE m_hCtrlEvent;//contain the event handle to be ADDed or //to be DELETEed FROM MONITOR DWORD m_dwThreadId; HANDLE m_hMonitorThread;};extern CClientImplEventManager s_ClientImplEventManagerInst;//the only instanceextern "C"/* __declspec(dllexport)*/ void MonitorTrigger(BOOL bStart);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -