CGenericObserverNotifier.cpp
上传用户:lianquan
上传日期:2007-01-02
资源大小:197k
文件大小:4k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. //
  2. // CGenericObserverNotifier.cpp
  3. //
  4. #include "StdAfx.h"
  5. #include "..ServerRNSO.H"
  6. #include "CGenericObserverNotifier.h"
  7. STDMETHODIMP CGenericObserverNotifier::OnNotify(DWORD dwSenderSubject,LONG nSize,byte * pBytes)
  8. {
  9. m_apNotifications.Lock();
  10. m_apNotifications.Add( new CNotificationFromManagerNOTIFY(dwSenderSubject,pBytes,nSize) );
  11. m_apNotifications.Unlock();
  12. m_evtNotification.SetEvent();
  13. return S_OK;
  14. }
  15. STDMETHODIMP CGenericObserverNotifier::OnNotifySubjectBroken(DWORD dwSubjectID)
  16. {
  17. m_apNotifications.Lock();
  18. m_apNotifications.Add( new CNotificationFromManagerNOTIFY_SUBJECTBROKEN(dwSubjectID) );
  19. m_apNotifications.Unlock();
  20. m_evtNotification.SetEvent();
  21. return S_OK;
  22. }
  23. void CGenericObserverNotifier::NotificationMessage(CNotificationFromManager * pNotification)
  24. {
  25. switch( pNotification->m_nType )
  26. {
  27. case CNotificationFromManager::NOTIFY :
  28. OnNotifyNOTIFY( ((CNotificationFromManagerNOTIFY*)pNotification)->m_pNotificationData );
  29. break;
  30. case CNotificationFromManager::NOTIFY_SUBJECTBROKEN :
  31. OnNotifyNOTIFY_SUBJECTBROKEN( ((CNotificationFromManagerNOTIFY_SUBJECTBROKEN*)pNotification)->m_dwSubjectID );
  32. break;
  33. default:
  34. ASSERT(FALSE); // You didnt update that method to be coherent with
  35. // other outgoing methods..
  36. break;
  37. }
  38. }
  39. void CGenericObserverNotifier::OnNotifyNOTIFY(ObserverNotification* pNotification)
  40. {
  41. if( m_pRouter )
  42. m_pRouter->OnNotifyNOTIFY(pNotification);
  43. #ifdef _DEBUG
  44. else
  45. {
  46. CString s;
  47. s.Format("GenericNotification from %dn",pNotification->m_dwSenderObject);
  48. TRACE(s);
  49. }
  50. #endif
  51. }
  52. void CGenericObserverNotifier::OnNotifyNOTIFY_SUBJECTBROKEN(DWORD dwSubjectID)
  53. {
  54. if( m_pRouter )
  55. m_pRouter->OnNotifyNOTIFY_SUBJECTBROKEN(dwSubjectID);
  56. #ifdef _DEBUG
  57. else
  58. {
  59. CString s;
  60. s.Format("Subject %d brokenn",dwSubjectID);
  61. TRACE(s);
  62. }
  63. #endif
  64. }
  65. void CGenericObserverNotifier::FinalRelease( )
  66. {
  67. m_bContinueToRun=FALSE;
  68. if( m_hThread != NULL )
  69. {
  70. m_evtStopThread.SetEvent();
  71. WaitForSingleObject(m_hThread,INFINITE);
  72. }
  73. }
  74. HRESULT CGenericObserverNotifier::FinalConstruct( )
  75. {
  76. DWORD dwThreadId;
  77. m_bContinueToRun=TRUE;
  78. HRESULT hRes=S_OK;
  79. m_hThread = ::CreateThread(NULL,0,ThreadStarter,this,
  80. 0, &dwThreadId );
  81. if( m_hThread == NULL )
  82. {
  83. hRes = E_FAIL;
  84. }
  85. else
  86. {
  87. if( WaitForSingleObject((HANDLE) m_evtStartStopThread,INFINITE) == WAIT_OBJECT_0 )
  88. {
  89. hRes = S_OK;
  90. }
  91. else
  92. {
  93. m_bContinueToRun=FALSE;
  94. hRes = E_FAIL;
  95. }
  96. }
  97. return hRes;
  98. }
  99. DWORD   _stdcall CGenericObserverNotifier::ThreadStarter( LPVOID lpParam ) 
  100. {
  101. return ((CGenericObserverNotifier*)lpParam)->ThreadProc();
  102. }
  103. DWORD CGenericObserverNotifier::ThreadProc()
  104. {
  105. #define NBHANDLES 2
  106. m_evtStartStopThread.SetEvent();
  107. HANDLE handles[NBHANDLES] = { (HANDLE) m_evtNotification,
  108. (HANDLE) m_evtStopThread
  109. };
  110. while( m_bContinueToRun )
  111. {
  112. // get an event from mailbox and from :
  113. // m_evtStartStopThread
  114. DWORD dwWait;
  115. if( m_apNotifications.GetSize() == 0 )
  116. {
  117. dwWait = WaitForMultipleObjects(NBHANDLES,handles,FALSE,INFINITE);
  118. if( dwWait==WAIT_OBJECT_0+1 )
  119. {
  120. break;
  121. }
  122. }
  123. if( m_apNotifications.GetSize() )
  124. {
  125. CNotificationFromManager * pNotification;
  126. m_apNotifications.Lock();
  127. pNotification = m_apNotifications.GetAt(0);
  128. m_apNotifications.RemoveAt(0);
  129. m_apNotifications.Unlock();
  130. NotificationMessage(pNotification);
  131. delete pNotification;
  132. }
  133. }
  134. return TRUE;
  135. }
  136. RnsoNotificationRout * CGenericObserverNotifier::SetNotificationRout(RnsoNotificationRout * pRout)
  137. {
  138. RnsoNotificationRout * pLastRout = m_pRouter;
  139. m_pRouter = pRout;
  140. return pLastRout;
  141. }