mbox客户端的作用是作为消息中心的客户端框架插件,是在原有消息盒子的基础上基于hotfox框架,重新定义后的初步实现,暂时屏蔽了许多有用的特性.
mbox作用:
.处理消息通知消息(680-Indication)
.提供本地消息引擎接口(INMEngine)
.提供Tip窗口(TfrmTip2),负责消息通知的提示
.接收通知后调用登记的处理者处理
.自动向服务器确认
.通知保存在客户端本地数据库
mbox代码
相关定义:
namespace mbox {
const char NM_ENGINE[] = "BCQ_NMENGINE";
typedef int (*NotificationOpenFunc)(HWND parent,CMessage *nm); ///< 打开通知消息函数
//---------------------------------------------------------------------------
///< 通知消息处理器
class CNotificationHandler {
public:
///< done表示是否已经有处理器处理过,其它处理器可以根据此值决定是否继续处理
virtual int Handle(boost::shared_ptr<CMessage>& nm,bool &done) = 0; ///< 消息处理
virtual void Release() = 0;
};
//---------------------------------------------------------------------------
struct CNotifycationOpener {
CNotificationType nt_; ///< 通知类型
NotificationOpenFunc open_func_; ///< 消息打开函数
CNotifycationOpener(NOTIFICATION_DOMAIN app_id,NOTIFICATION_TYPE nm_type,NotificationOpenFunc func):open_func_(func) {
nt_.domain_ = app_id;
nt_.type_id_ = nm_type;
}
};
//---------------------------------------------------------------------------
///< 通知消息引擎接口
class INMEngine {
public:
virtual int RegisterHandler(CNotificationHandler *handler) = 0; ///< 注册通知处理器(所有通知)
virtual int RegisterHandler(NOTIFICATION_DOMAIN app_id,NOTIFICATION_TYPE nm_type,CNotificationHandler *handler) = 0; ///< 注册指定类型的通知处理函数
virtual int RegisterHandler(NOTIFICATION_DOMAIN app_id,CNotificationHandler *handler) = 0; ///< 注册指定应用的通知处理函数
virtual void RemoveHandler(CNotificationHandler *handler) = 0; ///< 移除通知处理器
virtual void RemoveHandler(NOTIFICATION_DOMAIN app_id,NOTIFICATION_TYPE nm_type,CNotificationHandler *handler) = 0;
virtual void RemoveHandler(NOTIFICATION_DOMAIN app_id,CNotificationHandler *handler) = 0;
virtual void SetDefaultNotificationOpenFunc(NotificationOpenFunc func) = 0; ///< 设置默认的消息打开函数
virtual int RegisterOpener(NOTIFICATION_DOMAIN app_id,NOTIFICATION_TYPE nm_type,NotificationOpenFunc func) = 0; ///< 设置指定通知的打开函数
virtual int Notify(boost::shared_ptr<CMessage>& nm) = 0;
virtual int Delete(CQQ_OBJECT_ID object_id) = 0;
virtual int Open(HWND parent,CMessage *nm) = 0;
virtual int Get(vector<CMessage*> &v) = 0;
};
};
namespace mbox {
//---------------------------------------------------------------------------
typedef int (TForm::*FormNotificationHandleFunc)(boost::shared_ptr<CMessage>& nm,void *arg); ///< 通知消息处理函数(TForm成员函数)
typedef int (CBaseClientModule::*PluginNotificationHandleFunc)(boost::shared_ptr<CMessage>& nm,void *arg); ///< 通知消息处理函数(CBaseClientModule)
//---------------------------------------------------------------------------
class CNotificationHandlerBase : public CNotificationHandler {
public:
virtual ~CNotificationHandlerBase() {
}
void Release() {
delete this;
}
};
//---------------------------------------------------------------------------
///< 窗体通知消息处理器
class CFormNotificationHandler : public CNotificationHandlerBase {
TForm *form_;
FormNotificationHandleFunc func_; ///< 消息处理函数
void *arg_;
CFormNotificationHandler(TForm *form,FormNotificationHandleFunc fp,void *arg=0):form_(form),func_(fp),arg_(arg) {
}
int Handle(boost::shared_ptr<CMessage>&nm) {
return (form_->*func_)(nm,arg_);
}
};
//---------------------------------------------------------------------------
class CPluginNotificationHandler : public CNotificationHandlerBase {
CBaseClientModule *plugin_;
PluginNotificationHandleFunc func_;
void *arg_;
public:
CPluginNotificationHandler(CBaseClientModule *plugin,PluginNotificationHandleFunc fp,void *arg=0):plugin_(plugin),func_(fp),arg_(arg) {
}
int Handle(boost::shared_ptr<CMessage>&nm) {
return (plugin_->*func_)(nm,arg_);
}
};
};
使用示例代码:
//---------------------------------------------------------------------------
class TFrmMainGDSNSup : public TfrmBase1,
public IEventHandler,
public HTX_Event_Handler
,public CNotificationHandler {
private:
int Handle(boost::shared_ptr<CMessage>&nm,bool &done);
};
//---------------------------------------------------------------------------
void __fastcall TFrmMainGDSNSup::FormCreate(TObject *Sender)
{
///< 登记通知处理者
CBaseClientModule::i_nme_->RegisterHandler(this);
///< 指定默认的通知打开函数
CBaseClientModule::i_nme_->SetDefaultNotificationOpenFunc(&ShowNotification);
///< 从本地通知引擎获取通知,在当前窗口中显示
vector<CMessage*> v_nm;
CBaseClientModule::i_nme_->Get(v_nm);
vector<CMessage*>::iterator iter = v_nm.begin();
while(iter!=v_nm.end()) {
CMessage *nm = *iter;
lbMsg->Items->AddObject(nm->text_.c_str(),(TObject*)nm);
iter++;
}
return;
}
//---------------------------------------------------------------------------
void __fastcall TFrmMainGDSNSup::FormClose(TObject *Sender,
TCloseAction &Action)
{
CBaseClientModule::i_nme_->RemoveHandler(this);
}
//---------------------------------------------------------------------------
///< 默认的通知打开函数
int ShowNotification(HWND parent,CMessage *nm) {
TfrmShowNM *frm = new TfrmShowNM(NULL);
frm->SetMessage(nm);
::SetWindowLong(frm->TForm::Handle,GWL_HWNDPARENT,(LONG)parent);
frm->ShowModal();
delete frm;
return 0;
}
//---------------------------------------------------------------------------
///< 通知处理函数
int TFrmMainGDSNSup::Handle(boost::shared_ptr<CMessage>& nm,bool &done) {
CMessage *nnm = new CMessage;
*nnm = *nm; ///<
lbMsg->Items->AddObject(nnm->text_.c_str(),(TObject*)nnm);
SetMsgCount(nm_count_+1);
done = true;
return 0;
}