在面向對象的系統中,當一個對象接收到一條消息時,可能會發生一系列的事件。通常,這些事件是以 同步(synchronous) 模式處理的:調用進程或向這個對象發送消息的線程在發送消息調用完成之前都會接收并處理一系列事件。然而,如果產生這些事件的對象是由多個進程進行共享并且保存在共享內存中時,情況就稍微有些不同了。
本文將使用兩種 C++ 設計模式來詳細介紹這種情況,并使用一些樣例代碼來展示這種解決方案(這些樣例代碼可以從本文 下載 一節中獲得):
我們將簡要介紹不使用共享內存的樣例代碼。
使用第一種設計模式來修改這些代碼,讓其使用共享內存。
然后闡述如何使用第二種設計模式來實現進程間通信(IPC)。 您可以在任何機器體系架構、操作系統和編譯器上應用這兩種設計模式中的概念。對于本文來說,我們使用的是 RedHat Linux 7.1 for 32-bit x86 Intel? 體系架構的發行版;使用 GNU C++ compiler version 3.2.3 編譯器及其相關工具來編譯并測試樣例程序。
不使用共享內存
下面讓我們開始介紹這個樣例程序,首先是不使用共享內存的程序:
清單 1. common.h<
#ifndef __COMMON_H__
#define __COMMON_H__
class IObjectWithEvents
{
public:class IEventSink{public:virtual void OnEvent(pid_t pid, const char * msg) = 0;};
static IObjectWithEvents * getInstance();
virtual bool AddEventHandler(IEventSink * pEI) = 0;virtual void SendMessage() = 0;
};
#endif //__COMMON_H__
|
接口類 IObjectWithEvents 包含了一個內嵌的接口類 IEventSink,它定義了 OnEvent() 方法。這個事件處理程序接收一個發送者的 id 和一個字符串消息。getInstance() 方法返回對共享內存中對象的引用,AddEventHandler() 注冊一個事件處理程序,SendMessage() 向這個對象發送一條消息。由于不需要引用共享內存,所以可以像清單 2 中那樣來使用 IObjectWithEvents:
清單 2. shm-client1.cpp<
#include
#include
#include
#include "common.h"
#define HERE __FILE__ << ":" << __LINE__ << " "
using namespace std;
class EventSink : public IObjectWithEvents::IEventSink
{
public:void OnEvent(pid_t pid, const char * msg){cout << HERE << "Message from pid(" << pid << ")\t : " << msg << endl;}
};
int main()
{IObjectWithEvents * powe = IObjectWithEvents::getInstance();
EventSink sink;powe->AddEventHandler(&sink);
powe->SendMessage();return 0;
}
|
類 EventSink 提供了這個事件處理程序的實現。主函數中給出了用于發送消息和處理事件的標準序列。
ObjectWithEvents 的典型實現如清單 3、4 所示:
清單 3. ObjectWithEvents.h
#include "common.h"
class ObjectWithEvents : public IObjectWithEvents
{
public:// We assume singleton design pattern for illustrationstatic ObjectWithEvents * ms_pObjectWithEvents;
ObjectWithEvents();
//the implementation for IObjectWithEventsvoid FireEvent();virtual bool AddEventHandler(IEventSink * pEI);virtual void SendMessage();
//Collection for maintaining eventsenum { MAX_EVENT_HANDLERS = 16, };long m_npEI;IEventSink * m_apEI[MAX_EVENT_HANDLERS];pid_t m_alPID[MAX_EVENT_HANDLERS];
};
|
清單 4. ObjectWithEvents.cpp
#include
#include
#include
#include
#include
#include "ObjectWithEvents.h"
using namespace std;
ObjectWithEvents * ObjectWithEvents::ms_pObjectWithEvents = NULL;
IObjectWithEvents * IObjectWithEvents::getInstance()
{// the following commented code is for illustration only./*if (NULL == ObjectWithEvents::ms_pObjectWithEvents){ObjectWithEvents::ms_pObjectWithEvents = new ObjectWithEvents();}*/
return ObjectWithEvents::ms_pObjectWithEvents;
}
ObjectWithEvents::ObjectWithEvents() : m_npEI(0)
{
}
void ObjectWithEvents::FireEvent()
{// iterate through the collectionfor (long i = 0; i < m_npEI; i++){//Recheck for NULLif (0 != m_apEI[i]){// Fire the eventm_apEI[i]->OnEvent(m_alPID[i], "");}}
return;
}
bool ObjectWithEvents::AddEventHandler(IEventSink * pEI)
{// NULL checkif (NULL == pEI){return false;}
// check if there is space for this event handlerif (MAX_EVENT_HANDLERS == m_npEI){return false;}
// Add this event handler to the collectionm_alPID[m_npEI] = getpid();m_apEI[m_npEI++] = pEI;
return true;
}
void ObjectWithEvents::SendMessage()
{//Some processing//And then fire the event
FireEvent();
return;
}
|
清單 4 中的代碼可以使用下面的腳本來編譯:
g++ -g -o shm_client shm_client1.cpp ObjectWithEvents.cpp
|
在運行 shm_client 時,應該可以看到下面的輸出:
$ ./shm_client shm_client1.cpp:16 Message from pid(3920) :
|
使用共享內存:沒有事件緩存
現在,為了在共享內存中對 ObjectWithEvents 進行實例化,我們需要修改 ObjectWithEvents 的實現。