omniORB各个例子中客户端连接服务端的方法基本都是采用IOR或者NameService,在使用NameService的例子中,NameService是通过配置文件配置的,不是程序中指定的。
IOR是很不方便的而且不具有可读性,而NameService在某些情况下是无法提供的,例如在NameService崩溃而需要直接连接很多服务器的情况下。
根据omniORB的说明 http://omniorb.sourceforge.net/omni40/omniORB/omniORB006.html ,在各个客户端程序调用string_to_object函数时,除了IOR,还可以使用如下的Uniform Resource Identifier (URI)地址形式:
1. corbaloc。形式如:corbaloc:iiop::/
2. corbaname。这种方式主要针对NameService服务,形式如:corbaname:/#。一般情况下,由于object key为NameService,可以直接写成corbaname:#的形式。例如omniORB的echo例子中,用eg2_clt连接eg3_impl时,可以在eg2_clt调用string_to_object函数时,使用corbaname:#test.my_context/Echo.Object作为地址。
我们再来看corbaloc形式,以echo例子中的eg2_clt连接eg2_impl为例,我们假定服务器地址为192.168.0.1,服务端口为1234,echo服务的名称为EchoService,那么URI地址为:
corbaloc:iiop:192.168.0.1:1234/EchoService
根据omniORB的说明 http://omniorb.sourceforge.net/omni40/omniORB/omniORB008.html#toc41 ,需要在调用ORB_init时指定服务端口,否则系统会自行选择一个。同时,参考 http://www.nabble.com/Re%3A-newbie%27s-question-about-register_initial_reference%28%29-p4467687.html 的介绍,需要注册特殊的POA类型: omniINSPOA 并以activate_object_with_id的方式激活该POA。
因此eg2_impl的主程序部分修改如下:
const char* options[][2] = { { "endPoint", "giop:tcp::1234" },{ 0, 0 } };
try {
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv,"omniORB4",options);
CORBA::Object_var obj = orb->resolve_initial_references("omniINSPOA");
PortableServer::POA_var ins_poa = PortableServer::POA::_narrow(obj);
Echo_i* myecho = new Echo_i();
PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId("EchoService");
ins_poa->activate_object_with_id(oid, myecho);
// Obtain a reference to the object, and print it out as a
// stringified IOR.
obj = myecho->_this();
CORBA::String_var sior(orb->object_to_string(obj));
cerr << "" << (char*)sior << "" << endl;
myecho->_remove_ref();
PortableServer::POAManager_var pman = ins_poa->the_POAManager();
pman->activate();
orb->run();
}
而eg2_clt只需要在调用string_to_object函数时,用corbaloc:iiop:192.168.0.1:1234/EchoService作为地址。