Corba-omniORB编程了解

本文介绍如何使用 OmniORB 实现 CORBA 的简单编程案例,包括 IOR 和命名服务两种方式,详细展示了从环境搭建到客户端与服务器端程序的编写过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Corba-omniORB简单编程-IOR

omniORB4.1.4下载地址
VS2005下载omniORB-4.1.4-x86_win32-vs8.zip,VS2008下载omniORB-4.1.4-x86_win32-vs9.zip
http://sourceforge.net/projects/omniorb/files/omniORB/omniORB-4.1.4/ 

一、下载Corba-omniorb开源代码

首先到http://omniorb.sourceforge.net/下载一个win32的OmniORB。文件名为“omniORB-4.1.1-x86_win32_vc6.zip”。然后解压缩至文件夹“ D:\omniORB-4.1.1”。

二、设置环境变量

1)、将“D:\omniORB-4.1.1\bin\x86_win32;”加入到系统环境变量path中;

2)、设置用户环境变量“OMNI_ROOT= D:\omniORB-4.1.1”。

三、编辑IDL文件

使用记事本写一个time.idl文件,idl文件定义了的Time接口,其中包含一个get_gmt()方法。文件内容如下:

interface Time

{

short get_gmt();

};

四、编译IDL文件

在cmd中对idl进行编译:进入time.idl文件所在目录,使用omniidl -bcxx time.idl对time.idl进行编译。其中-bcxx告诉编译器将idl映射为C++,如果编译没有错误,则会在同一个目录下生成两个文件:分别是 time.hh,和timeSK.cc。time.hh是所有接口类型定义所在的文件,服务器和客户端的实现都需要这两个文件,作为相互之间交互的接口。

五、编辑由IDL工具生成的文件

修改time.hh和timeSK.cc两个生成文件扩展名称,将两个文件的后缀名分别改成.h和.cpp。并把timeSK.cpp中的#include "time.hh"改成#include "time.h"。

六、按VC向导新建控制台项目testOrbServer

    新建一个VC6 win32 Console的testOrbServer空项目。将time.h和timeSK.cpp拷贝到项目所在目录,并将文件插入项目中,新建一个testOrbServer.cpp的文件。

 

  1. // testOrbServer.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include <iostream.h>   
  5. #include "time.h"   
  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 1;  
  16. }  
  17.   
  18. int main(int argc, char* argv[])  
  19. {  
  20.    try  
  21.    {  
  22.        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);  
  23.        CORBA::Object_var obj= orb->resolve_initial_references("RootPOA");  
  24.        PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);  
  25.        PortableServer::POAManager_var mgr = poa->the_POAManager();  
  26.        mgr->activate();  
  27.        Time_impl time_servant;  
  28.        Time_var tm = time_servant._this();  
  29.        CORBA::String_var str = orb->object_to_string(tm);  
  30.        cout << str << endl;  
  31.        orb->run();  
  32.    }  
  33.    catch (const CORBA::Exception&)  
  34.    {  
  35.        cerr << "exception" << endl;  
  36.        return 1;  
  37.    }  
  38.    return 0;  
  39. }  
// testOrbServer.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include "time.h"

class Time_impl:public virtual POA_Time
{
public :
   virtual short get_gmt();
};

short Time_impl::get_gmt()
{
   return 1;
}

int main(int argc, char* argv[])
{
   try
   {
       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
       CORBA::Object_var obj= orb->resolve_initial_references("RootPOA");
       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
       PortableServer::POAManager_var mgr = poa->the_POAManager();
       mgr->activate();
       Time_impl time_servant;
       Time_var tm = time_servant._this();
       CORBA::String_var str = orb->object_to_string(tm);
       cout << str << endl;
       orb->run();
   }
   catch (const CORBA::Exception&)
   {
       cerr << "exception" << endl;
       return 1;
   }
   return 0;
}


七、按VC向导新建控制台项目testOrbClient

    新建一个VC6 win32 Console的testOrbClient空项目。将time.h和timeSK.cpp拷贝到项目所在目录,并将文件插入项目中,新建一个testOrbClient.cpp的文件。

  1. // testOrbClient.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include <iostream.h>   
  5. #include "time.h"   
  6.   
  7. int main(int argc, char* argv[])  
  8. {  
  9.    try  
  10.    {  
  11.        if (argc != 2)  
  12.        {  
  13.            throw 0;  
  14.        }  
  15.        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);  
  16.        CORBA::Object_var obj = orb->string_to_object(argv[1]);  
  17.        if (CORBA::is_nil(obj))  
  18.        {  
  19.            cerr << "Nil Time Reference" << endl;  
  20.            throw 0;  
  21.        }  
  22.        Time_var tm = Time::_narrow(obj);  
  23.        if (CORBA::is_nil(tm))  
  24.        {  
  25.            cerr << "Nil Time Reference" << endl;  
  26.            throw 0;  
  27.        }  
  28.        cout << "Time is " << tm->get_gmt() << endl;  
  29.   
  30.    }  
  31.    catch (const CORBA::Exception&)  
  32.    {  
  33.        cerr << "Exception" << endl;  
  34.        return 1;  
  35.    }  
  36.    return 0;  
  37. }  
// testOrbClient.cpp : Defines the entry point for the console application.
//

#include <iostream.h>
#include "time.h"

int main(int argc, char* argv[])
{
   try
   {
       if (argc != 2)
	   {
           throw 0;
       }
       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
       CORBA::Object_var obj = orb->string_to_object(argv[1]);
       if (CORBA::is_nil(obj))
	   {
           cerr << "Nil Time Reference" << endl;
           throw 0;
       }
       Time_var tm = Time::_narrow(obj);
       if (CORBA::is_nil(tm))
	   {
           cerr << "Nil Time Reference" << endl;
           throw 0;
       }
       cout << "Time is " << tm->get_gmt() << endl;

   }
   catch (const CORBA::Exception&)
   {
       cerr << "Exception" << endl;
       return 1;
   }
   return 0;
}

八、VC向导工程设置

图8-1 Code Generation下选择“Run-time Library ”选择MultiThreaded DLL

 

图8-2 增加预定义宏

Preprocessor definitions:

1)、__WIN32__

2)、__x86__

3)、_WIN32_WINNT=0x0400

4)、__NT__,__OSVERSION__=4

图8-3 增加工程依赖的库文件

1、选择Link选项卡,Category选择Input,添加库模块:

1)、ws2_32.lib

2)、mswsock.lib

3)、advapi32.lib

4)、omniORB411_rt.lib

5)、omniDynamic411_rt.lib

6)、omnithread32_rt.lib

2、设置库模块的路径:$(OMNI_ROOT)/lib/x86_32;

3、testOrbClient工程设置与testOrbServer一致。

九、编译测试程序

编译testOrbServer工程,最后生成testOrbServer .exe文件;

编译testOrbClient工程,最后生成testOrbClient.exe文件;

十、测试程序

一、运行testOrbServer.exe

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

 

C:\Windows\system32>cd D:\omniORB-4.1.1\bin

 

C:\Windows\system32>d:

 

D:\omniORB-4.1.1\bin >testOrbServer

IOR:010000000d00000049444c3a54696d653a312e3000000000010000000000000064000000010102000e0000003139322e3130302e36342e353300a0e00e000000fe488c174f000006d0000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100

二、运行testOrbClient.exe

Microsoft Windows [版本 6.1.7601]

版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

 

C:\Windows\system32>cd D:\omniORB-4.1.1\bin

 

C:\Windows\system32>d:

 

D:\omniORB-4.1.1\bin>testOrbClient IOR:010000000d00000049444c3a54696d653a312e3

000000000010000000000000064000000010102000e0000003139322e3130302e36342e353300a0e00e000000fe488c174f000006d0000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100

Time is 1

D:\omniORB-4.11\bin>

运行testOrbServer.exe时获得IOR数据,将IOR作为运行testOrbClient.exe的参数。注意参数需要“IOR:××××××××”。调用CORBA对象的get_gmt之后,CORBA的伺服程序servant返回了数值1。

 

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代码:

  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. }  
// 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代码:

  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. }  
// 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客户端程序

 

 

VS2005 C++ OmniORB 使用NameService方式 实现CORBA

配置corba和vs2005结合的环境
一、下载OmniORB

http://sourceforge.net/projects/omniorb/files/

VS2005下载omniORB-4.1.4-x86_win32-vs8.zip,VS2008下载omniORB-4.1.4-x86_win32-vs9.zip

下载后解压到任意目录即可,本人解压后为E:/CORBA/omniORB-4.1.4

 

二、配置环境变量

计算机右键单击属性->高级环境系统设置->系统环境变量Path后面加上

omniORB路径/bin/x86_win32,本人机器上为E:/CORBA/omniORB-4.1.4/bin/x86_win32;

 

在任一目录新建目录Omninames,本人机器建在E:/CORBA/omniORB-4.1.4/,在环境变量中新增变量名:OMNINAMES_LOGDIR,变易值为:Omninames所在目录/Omninames,本人机器上为E:/CORBA/omniORB-4.1.4/Omninames(这一步是为以后使用NameService准备的)


三、配置命名服务


执行E:/CORBA/omniORB-4.1.4/sample.reg

在HKEY_LOCAL_MACHINE/SOFTWARE/omniORB/InitRef 加入类型为字符串键“1”,键值为"NameService=corbaname::my.host.name"(这里的my.host.name 是你的机器名)

由于我用本机来测试,所以键值为NameService=corbaname::127.0.0.1
四、配置VS目录

打开VS,选择Tools工具->Options选项,在弹出窗口中选择 Project and Solutions -> VC++ Directories,要设定三个文件夹目录

1.在右上角下拉菜单中选择Executable files,在下面新加一个目录 OmniORB路径/bin/x86_win32(注意不要光写到bin把后面的x86_win32掉了)

 

2.在右上角下拉菜单中选择Include files,在下面新加一个目录OmniORB路径/include


3.在下拉菜单中选择Library files,添加目录OmniORB路径/lib/x86_win32


 

实例程序演示:
一、编写time.idl

新建一个txt文本文件,编辑内容如下

interface Time{

    short get_gmt();

};
 


保存并修改扩展名为time.idl

二、编译time.idl

打开命令行并到time.idl所在目录下,运行

omniidl -bcxx time.idl

如果运行找不到omniidl命令,则是配置时未将安装路径/bin/x86_win32添加到Path环境变量中。

运行编译成功后将生成time.hh和timeSK.cc两个文件, time.hh是所有的接口,类型的定义所在的文件,服务器和客户端的实现都需要这两个文件。

三、新建服务器端项目并添加文件

新建一个Win32 Console Application的空工程,名为FirstOmniORB_Server,作为服务器端程序,在头文件中新建time.h,并将上一步编译的出time.hh中的内容复制到time.h中,源文件中新建timeSK.cpp,并将上一步编译出的timeSK.cc中的内容复制到timeSK.cpp中,修改内容的#include "time.hh"为time.h

源文件中再新建一个myserver.cpp,添加以下内容

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

}
  


再新建一个Win32 Console Application空工程,名为FirstOmniORB_Client,作为客户端应用程序,同样添加time.h和timeSK.cpp,并在头文件中添加myclient.cpp,内容如下

[cpp] view plaincopyprint?
01.#include <iostream>  
02. 
03.#include "time.h"  
04. 
05.using namespace std; 
06. 
07.int main(int argc,char* argv[]) 
08.{ 
09.    //Declare ORB  
10.    CORBA::ORB_var orb; 
11. 
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.            cerr<<"Nil Time Reference"<<endl; 
26.            throw 0; 
27.        } 
28.        CosNaming::NamingContext_var nc =  
29.            CosNaming::NamingContext::_narrow(rootContextObj.in()); 
30. 
31. 
32.        CosNaming::Name name; 
33.        name.length(1); 
34.        name[0].id = (const char *)"FirstTimeService"; 
35.        name[0].kind = (const char *)""; 
36.        //Invoke the root context to retrieve the object reference  
37.        CORBA::Object_var managerObj = nc->resolve(name); 
38.        //Narrow the previous object to obtain the correct type  
39.        ::Time_var manager = 
40.            ::Time::_narrow(managerObj.in()); 
41. 
42.        if(CORBA::is_nil(manager)){ 
43.            cerr<<"Nil Time Reference"<<endl; 
44.            throw 0; 
45.        } 
46.        cout<<"OK, Let's have a look: "<<manager->get_gmt()<<endl; 
47.         
48. 
49. 
50.    }catch(const CORBA::Exception& e){ 
51.        cerr<<"Client.main() Exception  "<<e._name()<<endl; 
52.        return 1; 
53.    } 
54. 
55.    return 0; 
56. 
57.} 
#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;

}
最后的目录结构应该像这样:

 

   
我是把Server和Client分别建了一个解决方案,当然也可以放在同一个解决方案中。

四、修改工程参数

需要修改三个工程参数

在工程名FirstOmniORB_Server上右击-> Properties打开属性页(如果分别建立了两个解决方案则在FirstOmniORB_Client上也要进行同样的操作)

1.选择Configuration Properties -> C/C++ -> Preprocessor,修改右边的PreprocessorDefinitions,添加如下项:

__WIN32__

__x86__

_WIN32_WINNT=0x0400

__NT__

__OSVERSION__=4
 


__OSVERSION__的值,Win7,应该为6

win xp,应该为4

 

2. C/C++ -> Code Generation 右边的Runtime Library 选择Multi-threaded Dll(/MD)

3.

Linker-> Input -> Additional Dependencies中添加如下链接库

ws2_32.lib

mswsock.lib

advapi32.lib

omniORB414_rt.lib

omniDynamic414_rt.lib

omnithread34_rt.lib
 


后三个lib文件的文件名有可能因为omniORB的版本号不同而不同,可以在OmniORB安装路径/lib/x86_Win32中查看对应得文件

另一个工程也是相同的配置,注意如果将debug模式修改为release时所有的参数仍要重新配置。


五、编译运行

编译两个工程将产生两个exe文件,可以在两工程所在的Solution路径下的debug或release文件夹中找到两个编译完成的exe文件。

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


 
 
再打开一个CMD窗口, server端,输入生成的服务端exe名,本机为FirstOmniORB_Server.exe,回车:
 
 
再打开一个CMD窗口,Client端,  输入客服端exe文件名,以FirstTimeService(这是在Server端的代码实现在定义的服务名称)为参数,回车:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值