GenericObserver.cpp
资源名称:rnso.zip [点击查看]
上传用户:lianquan
上传日期:2007-01-02
资源大小:197k
文件大小:4k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- // GenericObserver.cpp : Implementation of CGenericObserver
- #include "stdafx.h"
- #include "RNSO.h"
- #include "GenericObserver.h"
- #include "Manager.h"
- /////////////////////////////////////////////////////////////////////////////
- // CGenericObserver
- STDMETHODIMP_(HRESULT) CGenericObserver::Subscribe(DWORD dwSubjectID)
- {
- // TODO: Add your implementation code here
- if( GenericManager::AddSubscription(m_dwObserverID,dwSubjectID) )
- return S_OK;
- else
- return E_FAIL;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::Unsubscribe(DWORD dwSubjectID,BOOL bAllSubjects)
- {
- // TODO: Add your implementation code here
- GenericManager::RemoveSubscription(m_dwObserverID,dwSubjectID,bAllSubjects);
- return S_OK;
- }
- void CGenericObserver::FinalRelease( )
- {
- GenericManager::RemoveObject(this);
- }
- void CGenericObserver::OnNotify(DWORD dwSubjectSender,LONG nSize,byte * pBytes)
- {
- IUnknown ** ppUnk = m_vec.begin();
- if( ppUnk != NULL )
- {
- //while( *ppUnk )
- {
- IGenericObserverNotification * pINotif = (IGenericObserverNotification *)*ppUnk;
- HRESULT hr = pINotif->OnNotify(dwSubjectSender,nSize,pBytes);
- if( hr == 0x800706BA )
- {
- // error
- }
- //ppUnk++;
- }
- }
- }
- void CGenericObserver::OnNotifySubjectBroken(DWORD dwSubject)
- {
- IUnknown ** ppUnk = m_vec.begin();
- if( ppUnk != NULL )
- {
- IGenericObserverNotification * pINotif = (IGenericObserverNotification *)*ppUnk;
- pINotif->OnNotifySubjectBroken(dwSubject);
- }
- }
- HRESULT CGenericObserver::FinalConstruct( )
- {
- BOOL bOk = FALSE;
- m_dwObserverID = GenericManager::GenerateObjectID(bOk,this);
- char sTmp[64];
- sprintf(sTmp,"%d",m_dwObserverID);
- m_sname=CComBSTR(sTmp);
- if( bOk )
- return S_OK;
- else
- return E_FAIL;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::IsSubscribed(DWORD dwSubjectID)
- {
- // TODO: Add your implementation code here
- if( GenericManager::IsObserverSubscribedToSubject(m_dwObserverID,dwSubjectID) )
- return S_OK;
- else
- return S_FALSE ;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::GetCountSubscriptions(long * lCount)
- {
- // TODO: Add your implementation code here
- *lCount = m_lCountSubjects;
- return S_OK;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::GetCountSubscribers(long * lCount)
- {
- // TODO: Add your implementation code here
- *lCount = m_lCountObservers;
- return S_OK;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::Notify(long nSizeNotification, byte * pNotification)
- {
- // TODO: Add your implementation code here
- if( GenericManager::AddNotification( new ObserverNotification(m_dwObserverID,pNotification,nSizeNotification)) )
- return S_OK;
- else
- return E_FAIL;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::FindObjectID(BSTR strname,DWORD * dwObjectID)
- {
- if( GenericManager::FindObjectID(strname,dwObjectID) )
- return S_OK;
- return E_FAIL;
- }
- void CGenericObserver::IncObs()
- {
- InterlockedIncrement(&m_lCountObservers);
- }
- void CGenericObserver::SubObs()
- {
- InterlockedDecrement(&m_lCountObservers);
- }
- void CGenericObserver::IncSub()
- {
- InterlockedIncrement(&m_lCountSubjects);
- }
- void CGenericObserver::SubSub()
- {
- InterlockedDecrement(&m_lCountSubjects);
- }
- STDMETHODIMP CGenericObserver::GetObjectID(DWORD * dwID)
- {
- // TODO: Add your implementation code here
- *dwID = m_dwObserverID;
- return S_OK;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::GetName(BSTR *strName)
- {
- *strName=m_sname.Copy();
- return S_OK;
- }
- STDMETHODIMP_(HRESULT) CGenericObserver::SetName(BSTR strName)
- {
- CComBSTR strLastname = m_sname;
- CComBSTR str(strName);
- m_sname = str;
- if( GenericManager::FindName(m_sname) == 1 )
- return S_OK;
- return E_FAIL;
- }