CGenericObserverNotifier.cpp
资源名称:rnso.zip [点击查看]
上传用户:lianquan
上传日期:2007-01-02
资源大小:197k
文件大小:4k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- //
- // CGenericObserverNotifier.cpp
- //
- #include "StdAfx.h"
- #include "..ServerRNSO.H"
- #include "CGenericObserverNotifier.h"
- STDMETHODIMP CGenericObserverNotifier::OnNotify(DWORD dwSenderSubject,LONG nSize,byte * pBytes)
- {
- m_apNotifications.Lock();
- m_apNotifications.Add( new CNotificationFromManagerNOTIFY(dwSenderSubject,pBytes,nSize) );
- m_apNotifications.Unlock();
- m_evtNotification.SetEvent();
- return S_OK;
- }
- STDMETHODIMP CGenericObserverNotifier::OnNotifySubjectBroken(DWORD dwSubjectID)
- {
- m_apNotifications.Lock();
- m_apNotifications.Add( new CNotificationFromManagerNOTIFY_SUBJECTBROKEN(dwSubjectID) );
- m_apNotifications.Unlock();
- m_evtNotification.SetEvent();
- return S_OK;
- }
- void CGenericObserverNotifier::NotificationMessage(CNotificationFromManager * pNotification)
- {
- switch( pNotification->m_nType )
- {
- case CNotificationFromManager::NOTIFY :
- OnNotifyNOTIFY( ((CNotificationFromManagerNOTIFY*)pNotification)->m_pNotificationData );
- break;
- case CNotificationFromManager::NOTIFY_SUBJECTBROKEN :
- OnNotifyNOTIFY_SUBJECTBROKEN( ((CNotificationFromManagerNOTIFY_SUBJECTBROKEN*)pNotification)->m_dwSubjectID );
- break;
- default:
- ASSERT(FALSE); // You didnt update that method to be coherent with
- // other outgoing methods..
- break;
- }
- }
- void CGenericObserverNotifier::OnNotifyNOTIFY(ObserverNotification* pNotification)
- {
- if( m_pRouter )
- m_pRouter->OnNotifyNOTIFY(pNotification);
- #ifdef _DEBUG
- else
- {
- CString s;
- s.Format("GenericNotification from %dn",pNotification->m_dwSenderObject);
- TRACE(s);
- }
- #endif
- }
- void CGenericObserverNotifier::OnNotifyNOTIFY_SUBJECTBROKEN(DWORD dwSubjectID)
- {
- if( m_pRouter )
- m_pRouter->OnNotifyNOTIFY_SUBJECTBROKEN(dwSubjectID);
- #ifdef _DEBUG
- else
- {
- CString s;
- s.Format("Subject %d brokenn",dwSubjectID);
- TRACE(s);
- }
- #endif
- }
- void CGenericObserverNotifier::FinalRelease( )
- {
- m_bContinueToRun=FALSE;
- if( m_hThread != NULL )
- {
- m_evtStopThread.SetEvent();
- WaitForSingleObject(m_hThread,INFINITE);
- }
- }
- HRESULT CGenericObserverNotifier::FinalConstruct( )
- {
- DWORD dwThreadId;
- m_bContinueToRun=TRUE;
- HRESULT hRes=S_OK;
- m_hThread = ::CreateThread(NULL,0,ThreadStarter,this,
- 0, &dwThreadId );
- if( m_hThread == NULL )
- {
- hRes = E_FAIL;
- }
- else
- {
- if( WaitForSingleObject((HANDLE) m_evtStartStopThread,INFINITE) == WAIT_OBJECT_0 )
- {
- hRes = S_OK;
- }
- else
- {
- m_bContinueToRun=FALSE;
- hRes = E_FAIL;
- }
- }
- return hRes;
- }
- DWORD _stdcall CGenericObserverNotifier::ThreadStarter( LPVOID lpParam )
- {
- return ((CGenericObserverNotifier*)lpParam)->ThreadProc();
- }
- DWORD CGenericObserverNotifier::ThreadProc()
- {
- #define NBHANDLES 2
- m_evtStartStopThread.SetEvent();
- HANDLE handles[NBHANDLES] = { (HANDLE) m_evtNotification,
- (HANDLE) m_evtStopThread
- };
- while( m_bContinueToRun )
- {
- // get an event from mailbox and from :
- // m_evtStartStopThread
- DWORD dwWait;
- if( m_apNotifications.GetSize() == 0 )
- {
- dwWait = WaitForMultipleObjects(NBHANDLES,handles,FALSE,INFINITE);
- if( dwWait==WAIT_OBJECT_0+1 )
- {
- break;
- }
- }
- if( m_apNotifications.GetSize() )
- {
- CNotificationFromManager * pNotification;
- m_apNotifications.Lock();
- pNotification = m_apNotifications.GetAt(0);
- m_apNotifications.RemoveAt(0);
- m_apNotifications.Unlock();
- NotificationMessage(pNotification);
- delete pNotification;
- }
- }
- return TRUE;
- }
- RnsoNotificationRout * CGenericObserverNotifier::SetNotificationRout(RnsoNotificationRout * pRout)
- {
- RnsoNotificationRout * pLastRout = m_pRouter;
- m_pRouter = pRout;
- return pLastRout;
- }