代码改变世界

Observer pattern and IStatusCallBack in VSIP

2004-06-18 15:57  atempcode  阅读(603)  评论(0编辑  收藏  举报

In the VSIP, it is common that some part needs to be notified when status of some other part changed. To do this, we need to define 2 interfaces, one for the observer, and the other for the subject, the event server.

This is a simple example for the VCM event handle. IVcmStatusCallback is the interface that the observer needs to implement. Fill each interface function with the the thing you want to do when event comes. IVcmStatusAdvisor is the interface the subject needs to implement. All the observer call these 2 functions to register or un-register themselves for the event alert.

//for VCM multi-thread
//// {B52A3189-56F6-4a02-BBD6-3E9069C18944}
[
 uuid(B52A3189-56F6-4a02-BBD6-3E9069C18944),
 version(1.0),
 helpstring("IVcmStatusCallback Interface")
]
interface IVcmStatusCallback : IUnknown
{
 HRESULT VcmBegin(
  [in, out] BOOL* pfContinue);
 HRESULT VcmEnd(
  [in] BOOL fSuccess,
  [in] BSTR OutPutFile,
  [in] BSTR ErrFile);
 HRESULT Tick(
  [in, out] BOOL* pfContinue);
 
}

//{A17F59FA-098E-4e48-B301-8EF47208538B}
[
 uuid(A17F59FA-098E-4e48-B301-8EF47208538B),
 version(1.0),
 helpstring("IVcmStatusAdvisor Interface")
]
interface IVcmStatusAdvisor : IUnknown
{
 HRESULT AdviseVcmStatusCallback(
  /* [in] */ IVcmStatusCallback *pIVcmStatusCallback,
  /* [out] */ VSCOOKIE *pdwCookie);

 HRESULT UnadviseVcmStatusCallback(
  /* [in] */ VSCOOKIE dwCookie);

}

The general routine is: first, the observer gets the interface of the subject, then call Advise...StatusCallback with the observer interface pointer and get a VSCOOKIE as the identifier of itself. The VSCOOKIE can be used to un-register itself. The subject added the observer interface into its event alert array if possible. When the subject finished something and need to notify the observer, it go through the observers array and call their functions.