zoukankan      html  css  js  c++  java
  • 【转】Cobra-omniORB简单编程-命名服务

     命名服务基本与上面的<<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代码:

    [cpp] view plain copy
     
    1. // testOrbServer.cpp : Defines the entry point for the console application.  
    2. //  
    3. #include <iostream>  
    4. #include "time.h"  
    5. using namespace std;  
    6.   
    7. class Time_impl:public virtual POA_Time  
    8. {  
    9. public :  
    10.     virtual short get_gmt();  
    11. };  
    12.   
    13. short Time_impl::get_gmt()  
    14. {  
    15.     return 1234;  
    16. }  
    17.   
    18. int main(int argc, char* argv[])  
    19. {  
    20.     CORBA::ORB_var orb;  
    21.     Time_impl* impl = NULL;  
    22.     try  
    23.     {  
    24.   
    25.         // Initialize the ORB  
    26.         orb = CORBA::ORB_init(argc,argv);  
    27.   
    28.         // Get a reference to the root POA  
    29.         CORBA::Object_var rootPOAObj = orb->resolve_initial_references("RootPOA");  
    30.   
    31.         // Narrow it to the correct type  
    32.         PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(rootPOAObj.in());  
    33.   
    34.         // Create POA policies  
    35.         CORBA::PolicyList policies;  
    36.         policies.length(1);  
    37.         policies[0] = rootPOA->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);  
    38.   
    39.         // Get the POA manager object  
    40.         PortableServer::POAManager_var manager = rootPOA->the_POAManager();  
    41.   
    42.         // Create a new POA with specified policies  
    43.         PortableServer::POA_var myPOA = rootPOA->create_POA("myPOA", manager, policies);  
    44.   
    45.         // Free policies  
    46.         CORBA::ULong len = policies.length();  
    47.         for (CORBA::ULong i = 0;i < len; i++)  
    48.             policies[i]->destroy();  
    49.   
    50.         //Get a reference to the Naming Service root_context  
    51.         CORBA::Object_var rootContextObj = orb->resolve_initial_references("NameService");  
    52.         // Narrow to the correct type  
    53.         CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(rootContextObj.in());  
    54.         //CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(rootContextObj);  
    55.   
    56.         // Create a reference to the servant  
    57.         impl = new Time_impl();  
    58.         // Activate object  
    59.         PortableServer::ObjectId_var myObjID = myPOA->activate_object(impl);  
    60.   
    61.         // Get a CORBA reference with the POA through the servant  
    62.         CORBA::Object_var o = myPOA->servant_to_reference(impl);  
    63.         // The reference is converted to a character string  
    64.         _CORBA_String_var s = orb->object_to_string(o);  
    65.         cout << "The IOR of the object is: " << s.in() << endl;  
    66.   
    67.         CosNaming::Name name;  
    68.         name.length(1);  
    69.         name[0].id = (const char *)"FirstTimeService";  
    70.         name[0].kind = (const char *)"";  
    71.         // Bind the Object into the name service  
    72.         nc->rebind(name, o);  
    73.   
    74.         //Activate the POA  
    75.         manager->activate();  
    76.         cout << "The server is ready. Awaiting for incoming requests..." << endl;  
    77.   
    78.         // Strat the ORB  
    79.         orb->run();  
    80.     }  
    81.     catch (const CORBA::Exception& e)  
    82.     {  
    83.         cerr << " exception " << e._name() << endl;  
    84.         return 1;  
    85.     }  
    86.     // Decrement reference count  
    87.     if (impl)  
    88.     {  
    89.         impl->_remove_ref();  
    90.     }  
    91.   
    92.     // End CORBA  
    93.     if (!CORBA::is_nil(orb))  
    94.     {  
    95.         try  
    96.         {  
    97.             orb->destroy();  
    98.             cout << "Ending CORBA..." << endl;  
    99.         }  
    100.         catch (const CORBA::Exception& e)  
    101.         {  
    102.             cout << "orb->destroy() failed:" << e._name() << endl;  
    103.             return 1;  
    104.         }  
    105.     }  
    106.     return 0;  
    107. }  

    四、使用上述testOrbClinet工程文件,修改testOrbClinet.cpp代码:

    [cpp] view plain copy
     
    1. // testOrbClient.cpp : Defines the entry point for the console application.  
    2. //  
    3. #include <iostream>  
    4.   
    5. #include "time.h"  
    6. using namespace std;  
    7.   
    8. int main(int argc,char* argv[])  
    9. {  
    10.     // Declare ORB  
    11.     CORBA::ORB_var orb;  
    12.     try  
    13.     {  
    14.         if (argc != 2)  
    15.         {  
    16.             throw 0;  
    17.         }  
    18.   
    19.         // Initialize the ORB  
    20.         orb = CORBA::ORB_init(argc, argv);  
    21.   
    22.         //Get a reference to the Naming Service  
    23.         CORBA::Object_var rootContextObj = orb->resolve_initial_references("NameService");  
    24.         if (CORBA::is_nil(rootContextObj))  
    25.         {  
    26.             cerr << "Nil Time Reference" << endl;  
    27.             throw 0;  
    28.         }  
    29.         CosNaming::NamingContext_var nc =   
    30.             CosNaming::NamingContext::_narrow(rootContextObj.in());  
    31.   
    32.   
    33.         CosNaming::Name name;  
    34.         name.length(1);  
    35.         name[0].id = (const char *)"FirstTimeService";  
    36.         name[0].kind = (const char *)"";  
    37.         //Invoke the root context to retrieve the object reference  
    38.         CORBA::Object_var managerObj = nc->resolve(name);  
    39.         //Narrow the previous object to obtain the correct type  
    40.         ::Time_var manager = ::Time::_narrow(managerObj.in());  
    41.   
    42.         if (CORBA::is_nil(manager))  
    43.         {  
    44.             cerr << "Nil Time Reference" << endl;  
    45.             throw 0;  
    46.         }  
    47.         cout << "OK, Let's have a look: " << manager->get_gmt() << endl;         
    48.     }  
    49.     catch (const CORBA::Exception& e)  
    50.     {  
    51.         cerr << "Client.main() Exception  " << e._name() << endl;  
    52.         return 1;  
    53.     }  
    54.     return 0;  
    55. }  

    五、编译上述两个工程

    六、启动命名服务

    打开一个命令窗口,输入omniNames –start (请不要关闭该窗口,如果不幸把这个窗口关闭了,那就重新输入omniNames就行了,不用带参数了。也就是说只有第一次用omniNames时才用-start,第二次以后就不用了,因为log文件已经存在了。)。

      

    六、运行testOrbServer.exe服务端程序

    七、运行testOrbClient.exe客户端程序

     

    转自:https://blog.csdn.net/liuxuezong/article/details/7232132

  • 相关阅读:
    软件开发过程须贯彻评估和测试
    【灌水】多维成功论
    改进c系列(目录)
    网站管理艺术
    .net 跨平台也是一句谎言
    用户界面和逻辑应该分离
    设计模式
    程序员找不到工作是因为管理差
    编码阶段
    保证软件开发质量的一种管理学
  • 原文地址:https://www.cnblogs.com/slz-coder150315/p/9138649.html
Copyright © 2011-2022 走看看