C++、CORBA版HelloWorld程序

来源:互联网 发布:兄弟加工中心编程 编辑:程序博客网 时间:2024/06/07 09:00
环境:Windows XP, MICO2.3.12, Visual Studio 2003
开发步骤
A 编译MICO
B 设置VC开发环境
C 编写编译IDL接口
D 编写编译服务端程序
E 编写编译客户端程序
F 运行
 
实现详细

A 编译MICO
1 设置VC安装目录下的bin目录至环境变量Path
2 运行vcvars32.bat,该命令增加其他一些VC环境变量Path
3 MICO的解压目录,运行:
  C:/work/program/mico>nmake /f makefile.win32
编译结果生成win32-bin目录,将该目录加至环境变量Path
 
B 设置VC开发环境
1 “工具|选项|Projects|VC++目录|包含目录”中增加MICOInclude目录:
   C:/work/program/mico/include
   C:/work/program/mico/include/windows
2 “工具|选项|Projects|VC++目录|库文件”中增加MICOLib目录:
C:/work/program/mico/win32-bin/lib
3 Project的“属性|C/C++|预处理器”中增加DEFINE值:_WINDOWS,“属性|C/C++|代码生成|运行时库”改成“多线程调试(/MTd)”,“属性|链接器|输入”中增加“wsock32.lib,mico2312.lib,micocoss2312.lib
 
C 编写IDL接口:HelloWorld.idl
module sample{
       interface HelloWorld{
              string sayHello();
       };
};
D 编译IDL:
C:/xxf/aaron/study/corba>idl HelloWorld.idl
编译结果生成文件:HelloWorld.h, HelloWorld.cc
将该两个文件加至VC Project中,在HelloWorld.cc的“属性|C/C++|预编译头|创建/使用预编译头”属性中选择“不使用预编译头”
 
E 编写服务端程序:HelloWorldServer.cpp
 
#include "HelloWorld.h"
#include <iostream.h>
#include <coss/CosNaming.h>
using namespace std;
class HelloWorld_impl : virtual public POA_sample::HelloWorld {
    public:
        virtual char* sayHello() ;
};
char* HelloWorld_impl::sayHello()
{
    return "/nHello World!/n";
}
 
int main(int argc,char* argv[])
{
    try{
        //Init orb;
        CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
        //Get reference to Root POA
        CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
        PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
        PortableServer::POAManager_var mgr = poa->the_POAManager();
 
        HelloWorld_impl * hello = new HelloWorld_impl;
        /*
        * Activate the hello
        */
        PortableServer::ObjectId_var oid = poa->activate_object (hello);
        CORBA::Object_var ref = poa->id_to_reference (oid.in());
           
        /*
        * Acquire a reference to the Naming Service
        */
        CORBA::Object_var nsobj =
            orb->resolve_initial_references ("NameService");
        CosNaming::NamingContext_var nc =
            CosNaming::NamingContext::_narrow (nsobj);
        if (CORBA::is_nil (nc)) {
            cerr << "oops, I cannot access the Naming Service!" << endl;
            exit (1);
        }
        /*
        * Construct Naming Service name for HelloWorld
        */
        CosNaming::Name name;
        name.length (1);
        name[0].id = CORBA::string_dup ("HelloWorld");
        name[0].kind = CORBA::string_dup (""); 
        nc->rebind (name, ref);
        /*
        * Activate the POA and start serving requests
        */
        mgr->activate ();
        orb->run();
    }catch(const CORBA::Exception&){
        cerr<<"Uncaught CORBA exception "<<endl;
        return 1;
    }
    return 0;
}
 
F 编写客户端程序:HelloWorldClient.cpp
#include <iostream.h>
#include <iomanip.h>
#include <coss/CosNaming.h>
#include "HelloWorld.h"
using namespace std;
 
//Usage:HelloWorldClient -ORBInitRef NameService=IOR of Naming Server(IOR:0)
//   or:HelloWorldClient -ORBNoResolve -ORBInitRef NameService = //corbaloc::127.0.0.1:1050/NameService
 
int main(int argc,char* argv[])
{
    try{
        //Init orb
        CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
        CORBA::Object_var nsobj = orb->resolve_initial_references ("NameService");
        CosNaming::NamingContext_var nc =
             CosNaming::NamingContext::_narrow (nsobj);
        if (CORBA::is_nil (nc)) {
            cerr << "oops, I cannot access the Naming Service!" << endl;
            exit (1);
        }
        CosNaming::Name name;
        name.length (1);
        name[0].id = CORBA::string_dup ("HelloWorld");
        name[0].kind = CORBA::string_dup (""); 
        /*
        * try to find that node in the Naming Service tree
        */
        CORBA::Object_var obj;
        cout << "Looking up HelloWorld " << flush;
        obj = nc->resolve (name);
 
        sample::HelloWorld_var hello = sample::HelloWorld::_narrow(obj);
        if(CORBA::is_nil(hello)){
            cerr<<"Narrow err"<<endl;
            throw 0;           
        }
        //
        cout<<hello->sayHello()<<endl;
    }catch(const CORBA::Exception&){
        cerr<<"Uncaught CORBA exception "<<endl;
        return 1;
    }
    return 0;
}
 
G 运行:
启动名字服务器:
tnameserv -ORBInitialPort 1050
也可用MICO的名字服务器:
nsd -ORBNoResolve -ORBIIOPAddr inet:127.0.0.1:1050
启动服务端程序:
HelloWorldServer -ORBNoResolve -ORBInitRef NameService=corbaloc::127.0.0.1:1050/NameService
 
运行客户端程序:
HelloWorldClient -ORBNoResolve -ORBInitRef NameService=corbaloc::127.0.0.1:1050/NameService
输出:
Looking up HelloWorld
Hello World!