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"));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值