命名服务基本与上面的<<Cobra-omniORB简单编程-IOR>>相似,下面具体对需要的操作步骤进行描述。
一、设置环境变量
1)、在“D:/omniORB-4.1.1/”目录下新建目录Omninames;
2)、设置用户环境变量“OMNINAMES_LOGDIR = D:/omniORB-4.1.1/Omninames”。
二、配置命名服务
执行D:/omniORB-4.1.1/sample.reg注册文件,
在HKEY_LOCAL_MACHINE/SOFTWARE/omniORB/InitRef 加入类型为字符串键“1”,键值为"NameService=corbaname::my.host.name"(这里的my.host.name 是你的机器名)
如果使用本机来测试,键值可以为NameService=corbaname::127.0.0.1。
三、使用上述testOrbServer工程文件,修改testOrbServer.cpp代码:
- // testOrbServer.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include "time.h"
- using namespace std;
- class Time_impl:public virtual POA_Time
- {
- public :
- virtual short get_gmt();
- };
- short Time_impl::get_gmt()
- {
- return 1234;
- }
- int main(int argc, char* argv[])
- {
- CORBA::ORB_var orb;
- Time_impl* impl = NULL;
- try
- {
- // Initialize the ORB
- orb = CORBA::ORB_init(argc,argv);
- // Get a reference to the root POA
- CORBA::Object_var rootPOAObj = orb->resolve_initial_references("RootPOA");
- // Narrow it to the correct type
- PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(rootPOAObj.in());
- // Create POA policies
- CORBA::PolicyList policies;
- policies.length(1);
- policies[0] = rootPOA->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
- // Get the POA manager object
- PortableServer::POAManager_var manager = rootPOA->the_POAManager();
- // Create a new POA with specified policies
- PortableServer::POA_var myPOA = rootPOA->create_POA("myPOA", manager, policies);
- // Free policies
- CORBA::ULong len = policies.length();
- for (CORBA::ULong i = 0;i < len; i++)
- policies[i]->destroy();
- //Get a reference to the Naming Service root_context
- CORBA::Object_var rootContextObj = orb->resolve_initial_references("NameService");
- // Narrow to the correct type
- CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(rootContextObj.in());
- //CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(rootContextObj);
- // Create a reference to the servant
- impl = new Time_impl();
- // Activate object
- PortableServer::ObjectId_var myObjID = myPOA->activate_object(impl);
- // Get a CORBA reference with the POA through the servant
- CORBA::Object_var o = myPOA->servant_to_reference(impl);
- // The reference is converted to a character string
- _CORBA_String_var s = orb->object_to_string(o);
- cout << "The IOR of the object is: " << s.in() << endl;
- CosNaming::Name name;
- name.length(1);
- name[0].id = (const char *)"FirstTimeService";
- name[0].kind = (const char *)"";
- // Bind the Object into the name service
- nc->rebind(name, o);
- //Activate the POA
- manager->activate();
- cout << "The server is ready. Awaiting for incoming requests..." << endl;
- // Strat the ORB
- orb->run();
- }
- catch (const CORBA::Exception& e)
- {
- cerr << " exception " << e._name() << endl;
- return 1;
- }
- // Decrement reference count
- if (impl)
- {
- impl->_remove_ref();
- }
- // End CORBA
- if (!CORBA::is_nil(orb))
- {
- try
- {
- orb->destroy();
- cout << "Ending CORBA..." << endl;
- }
- catch (const CORBA::Exception& e)
- {
- cout << "orb->destroy() failed:" << e._name() << endl;
- return 1;
- }
- }
- return 0;
- }
四、使用上述testOrbClinet工程文件,修改testOrbClinet.cpp代码:
- // testOrbClient.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include "time.h"
- using namespace std;
- int main(int argc,char* argv[])
- {
- // Declare ORB
- CORBA::ORB_var orb;
- try
- {
- if (argc != 2)
- {
- throw 0;
- }
- // Initialize the ORB
- orb = CORBA::ORB_init(argc, argv);
- //Get a reference to the Naming Service
- CORBA::Object_var rootContextObj = orb->resolve_initial_references("NameService");
- if (CORBA::is_nil(rootContextObj))
- {
- cerr << "Nil Time Reference" << endl;
- throw 0;
- }
- CosNaming::NamingContext_var nc =
- CosNaming::NamingContext::_narrow(rootContextObj.in());
- CosNaming::Name name;
- name.length(1);
- name[0].id = (const char *)"FirstTimeService";
- name[0].kind = (const char *)"";
- //Invoke the root context to retrieve the object reference
- CORBA::Object_var managerObj = nc->resolve(name);
- //Narrow the previous object to obtain the correct type
- ::Time_var manager = ::Time::_narrow(managerObj.in());
- if (CORBA::is_nil(manager))
- {
- cerr << "Nil Time Reference" << endl;
- throw 0;
- }
- cout << "OK, Let's have a look: " << manager->get_gmt() << endl;
- }
- catch (const CORBA::Exception& e)
- {
- cerr << "Client.main() Exception " << e._name() << endl;
- return 1;
- }
- return 0;
- }
五、编译上述两个工程
六、启动命名服务
打开一个命令窗口,输入omniNames –start (请不要关闭该窗口,如果不幸把这个窗口关闭了,那就重新输入omniNames就行了,不用带参数了。也就是说只有第一次用omniNames时才用-start,第二次以后就不用了,因为log文件已经存在了。)。
六、运行testOrbServer.exe服务端程序
七、运行testOrbClient.exe客户端程序
转自:https://blog.csdn.net/liuxuezong/article/details/7232132