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

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // GenericObserver.cpp : Implementation of CGenericObserver
  2. #include "stdafx.h"
  3. #include "RNSO.h"
  4. #include "GenericObserver.h"
  5. #include "Manager.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CGenericObserver
  8. STDMETHODIMP_(HRESULT) CGenericObserver::Subscribe(DWORD dwSubjectID)
  9. {
  10. // TODO: Add your implementation code here
  11. if( GenericManager::AddSubscription(m_dwObserverID,dwSubjectID) )
  12. return S_OK;
  13. else
  14. return E_FAIL;
  15. }
  16. STDMETHODIMP_(HRESULT) CGenericObserver::Unsubscribe(DWORD dwSubjectID,BOOL bAllSubjects)
  17. {
  18. // TODO: Add your implementation code here
  19. GenericManager::RemoveSubscription(m_dwObserverID,dwSubjectID,bAllSubjects);
  20. return S_OK;
  21. }
  22. void CGenericObserver::FinalRelease( )
  23. {
  24. GenericManager::RemoveObject(this);
  25. }
  26. void CGenericObserver::OnNotify(DWORD dwSubjectSender,LONG nSize,byte * pBytes)
  27. {
  28. IUnknown ** ppUnk = m_vec.begin();
  29. if( ppUnk != NULL )
  30. {
  31. //while( *ppUnk )
  32. {
  33. IGenericObserverNotification * pINotif = (IGenericObserverNotification *)*ppUnk;
  34. HRESULT hr = pINotif->OnNotify(dwSubjectSender,nSize,pBytes);
  35. if( hr == 0x800706BA )
  36. {
  37. // error 
  38. }
  39. //ppUnk++;
  40. }
  41. }
  42. }
  43. void CGenericObserver::OnNotifySubjectBroken(DWORD dwSubject)
  44. {
  45. IUnknown ** ppUnk = m_vec.begin();
  46. if( ppUnk != NULL )
  47. {
  48. IGenericObserverNotification * pINotif = (IGenericObserverNotification *)*ppUnk;
  49. pINotif->OnNotifySubjectBroken(dwSubject);
  50. }
  51. }
  52. HRESULT CGenericObserver::FinalConstruct( )
  53. {
  54. BOOL bOk = FALSE;
  55. m_dwObserverID = GenericManager::GenerateObjectID(bOk,this);
  56. char sTmp[64];
  57. sprintf(sTmp,"%d",m_dwObserverID);
  58. m_sname=CComBSTR(sTmp);
  59. if( bOk )
  60. return S_OK;
  61. else
  62. return E_FAIL;
  63. }
  64. STDMETHODIMP_(HRESULT) CGenericObserver::IsSubscribed(DWORD dwSubjectID)
  65. {
  66. // TODO: Add your implementation code here
  67. if( GenericManager::IsObserverSubscribedToSubject(m_dwObserverID,dwSubjectID) )
  68. return S_OK;
  69. else
  70. return S_FALSE ;
  71. }
  72. STDMETHODIMP_(HRESULT) CGenericObserver::GetCountSubscriptions(long * lCount)
  73. {
  74. // TODO: Add your implementation code here
  75. *lCount = m_lCountSubjects;
  76. return S_OK;
  77. }
  78. STDMETHODIMP_(HRESULT) CGenericObserver::GetCountSubscribers(long * lCount)
  79. {
  80. // TODO: Add your implementation code here
  81. *lCount = m_lCountObservers;
  82. return S_OK;
  83. }
  84. STDMETHODIMP_(HRESULT) CGenericObserver::Notify(long nSizeNotification, byte * pNotification)
  85. {
  86. // TODO: Add your implementation code here
  87. if( GenericManager::AddNotification( new ObserverNotification(m_dwObserverID,pNotification,nSizeNotification)) )
  88. return S_OK;
  89. else
  90. return E_FAIL;
  91. }
  92. STDMETHODIMP_(HRESULT) CGenericObserver::FindObjectID(BSTR strname,DWORD * dwObjectID)
  93. {
  94. if( GenericManager::FindObjectID(strname,dwObjectID) )
  95. return S_OK;
  96. return E_FAIL;
  97. }
  98. void CGenericObserver::IncObs()
  99. {
  100. InterlockedIncrement(&m_lCountObservers);
  101. }
  102. void CGenericObserver::SubObs()
  103. {
  104. InterlockedDecrement(&m_lCountObservers);
  105. }
  106. void CGenericObserver::IncSub()
  107. {
  108. InterlockedIncrement(&m_lCountSubjects);
  109. }
  110. void CGenericObserver::SubSub()
  111. {
  112. InterlockedDecrement(&m_lCountSubjects);
  113. }
  114. STDMETHODIMP CGenericObserver::GetObjectID(DWORD * dwID)
  115. {
  116. // TODO: Add your implementation code here
  117. *dwID = m_dwObserverID;
  118. return S_OK;
  119. }
  120. STDMETHODIMP_(HRESULT) CGenericObserver::GetName(BSTR *strName)
  121. {
  122. *strName=m_sname.Copy();
  123. return S_OK;
  124. }
  125. STDMETHODIMP_(HRESULT) CGenericObserver::SetName(BSTR strName)
  126. {
  127. CComBSTR strLastname = m_sname;
  128. CComBSTR str(strName);
  129. m_sname = str;
  130. if( GenericManager::FindName(m_sname) == 1 )
  131. return S_OK;
  132. return E_FAIL;
  133. }