海鸥航迹

学习之笔记,好文之收集。

导航

ICE bidirectional connections 关键点

ICE是一套跨平台分布式通信框架。本文介绍ICE3.4.1的bidirectional connection特性。

客户端:

C++:

   1:  CallbackSenderPrx server = CallbackSenderPrx::checkedCast(communicator()->propertyToProxy("CallbackSender.Proxy"));
   2:      if(!server)
   3:      {
   4:          cerr << appName() << ": invalid proxy" << endl;
   5:          return EXIT_FAILURE;
   6:      }
   7:   
   8:      Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("");
   9:      Ice::Identity ident;
  10:      ident.name = IceUtil::generateUUID();
  11:      ident.category = "";
  12:      CallbackReceiverPtr cr = new CallbackReceiverI;
  13:      adapter->add(cr, ident);
  14:      adapter->activate();
  15:      server->ice_getConnection()->setAdapter(adapter);
  16:      server->addClient(ident);
  17:      communicator()->waitForShutdown();

C#:

   1:  CallbackSenderPrx server = CallbackSenderPrxHelper.checkedCast(
   2:                  communicator().propertyToProxy("CallbackSender.Proxy"));
   3:              if(server == null)
   4:              {
   5:                  System.Console.Error.WriteLine("invalid proxy");
   6:                  return 1;
   7:              }
   8:   
   9:              Ice.ObjectAdapter adapter = communicator().createObjectAdapter("");
  10:              Ice.Identity ident = new Ice.Identity();
  11:              ident.name = Guid.NewGuid().ToString();
  12:              ident.category = "";
  13:              adapter.add(new CallbackReceiverI(), ident);
  14:              adapter.activate();
  15:              server.ice_getConnection().setAdapter(adapter);
  16:              server.addClient(ident);
  17:              communicator().waitForShutdown();

 

 

要点:

1、创建服务器代理(注意:如果用checkCast,则此时已经和服务器建立了连接)
2、客户端创建一个ObjectAdapter,用于host回调Servant
3、创建一个回调Servant,添加到adapter中;
4、用服务器代理获取一个连接,并将此连接附加到adapter上,这一步,将导致adapter在此连接上侦听传入的请求;
5、将客户端servant的标识传递给服务器;

 

服务器端:

C++:

   1:   IceUtil::Monitor<IceUtil::Mutex>::Lock lock(*this);
   2:   
   3:      cout << "adding client `" << _communicator->identityToString(ident) << "'"<< endl;
   4:   
   5:      CallbackReceiverPrx client = CallbackReceiverPrx::uncheckedCast(current.con->createProxy(ident));
   6:      _clients.insert(client);

 

C#:

   1:   lock(this)
   2:          {
   3:              System.Console.Out.WriteLine("adding client `" + _communicator.identityToString(ident) + "'");
   4:   
   5:              Ice.ObjectPrx @base = current.con.createProxy(ident);
   6:              CallbackReceiverPrx client = CallbackReceiverPrxHelper.uncheckedCast(@base);
   7:              _clients.Add(client);
   8:          }

 

服务器端必须有一个客户端注册方法,即客户端必须将adapter代表的代理信息告知服务器,而又因为是bidirectional connections ,故调用服务器代理方法所在的连接就是adapter侦听的连接,这个连接的信息服务器和客户端都是知道的,因此服务器只需要简单的在这个连接上,用传入的客户端代理标识创建一个代理即可。

此外,如果客户端试图将其创建的代理信息传递给服务器,服务器发现代理具有连接信息(EndPoints),则会试图根据此连接信息创建一个新的连接(这显然不会成功),因此,客户端无需传递任何连接信息给服务器,服务器需要自己根据已知的连接信息(连接是在客户端请求server时创建的)来创建客户端代理。

服务器端根据当前连接创建的代理称为Fixed Proxy,即连接不可变的固定代理。(这显然可以理解,常规代理中,是先有代理,后有连接;而Fixed Proxy是先有连接,后有代理)。Fixed Proxy是不支持ACM的,同时也不允许显式的对连接进行操作,一旦连接被关闭,就意味着代理无效了。

 

BIDir方式在手机等限制传入的网络应用中经常用到,ICE对这种方式的网络应用支持非常不错。

参考:http://www.zeroc.com/doc/index.html

posted on 2011-04-24 10:41  海天一鸥  阅读(2710)  评论(0编辑  收藏  举报