ICE的服務器對象實現

1、需要增加一個類繼承至生成的接口類,並實現接口類的虛方法。
2、創建實現類的對象
3、調用adpater的add方法將創建的對象綁定到adapter中,並傳入一個全局唯一標示符,該唯一標示可以通過如下方法生成:
   adapter->add(hello, communicator()->stringToIdentity("hello"));
   adapter->addWithUUID(hello);
   Ice::Ideentity id; id.name="hello"; adapter->add(hello, id);
4、adapter的add和addWithUUID方法返回一個代理對象,可以將該代理對象返回給客戶端讓其調用代理的方法
5、在實現接口的操作方法時,在每個操作的最後一個參數都會被ice映射成const Current& current,可以通過該成員獲取操作調用上下文信息
   Current的定義如下:
   module Ice 
   {
        local dictionary<string, string> Context;
        enum OperationMode { Normal, \Idempotent };
        local struct Current 
   {
            ObjectAdapter adapter;    // 服務器的對象適配器,可以通過它再調用getCommunicator得到通信器
           Connection con;           // 連接對象
            Identity id;              // 服務對象標示
            string facet;             // 
            string operation;         // 操作的名稱
            OperationMode mode;       // 操作模式
            Context ctx;              // 操作上下文屬性
            int requestId;            // 請求ID
        };

    };

6、服務器端接收到請求後會自動的分派請求給正確的服務對象,因爲在請求中攜帶了對象標示。在分派給服務對象前,可以設置攔截器,具體操作如下:
   adapter->add(hello, communicator()->stringToIdentity("hello"));
   該處不添加hello這個servant,取而代之的是設置一個DispatchInterceptor
   class MyDipatchInterceptor : public Ice::DispatchInterceptor
   {
   public:
       MyDipatchInterceptor(const MyServantIPtr& servant) : _servant(servant)
  {
  
  }
  
       virtual DispatchStatus dispatch(Request& request)
       {
            return _servant->dispatch(request);
       }
private:
  const MyServantIPtr& _servant;
   };
   adapter->add(new MyDipatchInterceptor(hello), communicator()->stringToIdentity("hello"));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章