- CORBA基本架构
- IDL文件编写
- CPP示例实现(上)
- CPP示例实现(下)
- C示例实现(IOR+NS上)
- C示例实现(IOR+NS下)
- C示例实现(IOR上)
- C示例实现(IOR下)
在linux系统下,有很多开源的CORBA通讯框架,在CPP的示例中,使用了OmniORB的框架。
如我用的是Debian系统,可以直接安装:
apt-get install omniorb apt-get install omniorb-nameserver apt-get install omniidl apt-get install libomniorb4-dev export OMNIORB_CONFIG=/etc/omniORB.cfg
然后修改配置文件/etc/omniORB.cfg,修改日志输出级别。
编译Hi.idl文件,将生成文件Hi.hh及HiSK.cc
omniidl -bcxx Hi.idl
首先完成服务端部分,分了三个文件:
HiServerImpl.hh
#include <CORBA.h> #include "Hi.hh" class HiServerImpl : public virtual POA_HiCorba::Hi, public virtual PortableServer::RefCountServantBase { public: HiServerImpl(); ~HiServerImpl(); char* sayHiTo(const char* someone); ::CORBA::Long add(::CORBA::Long numa, ::CORBA::Long numb); void shutdown(); };
HiServerImpl.cc
#include <vector> #include <string> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <fstream> #include <stdio.h> #include <sys/wait.h> #include "HiServerImpl.hh" using namespace HiCorba; HiServerImpl::HiServerImpl() { } HiServerImpl::~HiServerImpl() { } char* HiServerImpl::sayHiTo(const char* someone) { } ::CORBA::Long HiServerImpl::add(::CORBA::Long numa, ::CORBA::Long numb) { return numa+numb; } void HiServerImpl::shutdown() { }
HiServer.cc
#include <stdlib.h> #include <iostream> #include <string> #include <assert.h> #include <signal.h> #include <unistd.h> #include "HiServerImpl.hh" #include "Hi.hh" using namespace std; int main(int argc, char** argv) { try { //------------------------------------------------------------------------ // Initialize CORBA ORB - "orb" //------------------------------------------------------------------------ CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); //------------------------------------------------------------------------ // Servant must register with POA in order to be made available for client // Get reference to the RootPOA. //------------------------------------------------------------------------ CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var _poa = PortableServer::POA::_narrow(obj.in()); //------------------------------------------------------------------------ // Operations defined in object interface invoked via an object reference. // Instance of CRequestSocketStream_i servant is initialized. //------------------------------------------------------------------------ HiServerImpl* impl = new HiServerImpl(); //------------------------------------------------------------------------ // Servant object activated in RootPOA. // (Object id used for various POA operations.) //------------------------------------------------------------------------ PortableServer::ObjectId_var myOid = _poa->activate_object(impl); //------------------------------------------------------------------------ // Obtain object reference from servant and register in naming service(??) //------------------------------------------------------------------------ CORBA::Object_var SA_obj = impl->_this(); //------------------------------------------------------------------------ // Obtain a reference to the object, and print it out as string IOR. //------------------------------------------------------------------------ CORBA::String_var sior(orb->object_to_string(SA_obj.in())); cerr << "'" << (char*)sior << "'" << endl; //======================================================================== // Bind (rebind) object (orb) to name (SA_obj) //======================================================================== //------------------------------------------------------------------------ // Bind object to name service as defined by directive InitRef // and identifier "OmniNameService" in config file omniORB.cfg. //------------------------------------------------------------------------ //CORBA::Object_var obj1=orb->resolve_initial_references("OmniNameService"); CORBA::Object_var obj1=orb->resolve_initial_references("NameService"); assert(!CORBA::is_nil(obj1.in())); cerr << "resolve_initial_references successed" << endl; //------------------------------------------------------------------------ // narrow this to the naming context //------------------------------------------------------------------------ CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(obj1.in()); assert(!CORBA::is_nil(nc.in())); cerr << "narrow successed" << endl; //------------------------------------------------------------------------ // Bind to CORBA name service. Same name to be requested by client. //------------------------------------------------------------------------ CosNaming::Name name; name.length(1); name[0].id=CORBA::string_dup("Hi"); nc->rebind (name,SA_obj.in()); cerr << "rebind successed" << endl; //======================================================================== impl->_remove_ref(); //------------------------------------------------------------------------ // Activate the POA manager //------------------------------------------------------------------------ PortableServer::POAManager_var pmgr = _poa->the_POAManager(); pmgr->activate(); cerr << "poamanager activate successed" << endl; //------------------------------------------------------------------------ // Accept requests from clients //------------------------------------------------------------------------ orb->run(); //------------------------------------------------------------------------ // If orb leaves event handling loop. // - currently configured never to time out (??) //------------------------------------------------------------------------ orb->destroy(); free(name[0].id); // str_dup does a malloc internally } catch(CORBA::SystemException&) { cerr << "Caught CORBA::SystemException." << endl; } catch(CORBA::Exception&) { cerr << "Caught CORBA::Exception." << endl; } catch(omniORB::fatalException& fe) { cerr << "Caught omniORB::fatalException:" << endl; cerr << " file: " << fe.file() << endl; cerr << " line: " << fe.line() << endl; cerr << " mesg: " << fe.errmsg() << endl; } catch(...) { cerr << "Caught unknown exception." << endl; } return 0; }
MakeServer
CC = /usr/bin/g++ CPPFLAGS = -g -c -I/usr/include -I/usr/include/omniORB4 LDFLAGS = -g -L/usr/lib -lomniORB4 -lomnithread -lomniDynamic4 OMNIIDL = /usr/bin/omniidl all: HiServer.bin HiServer.bin: HiSK.o HiServerImpl.o HiServer.o $(CC) $(LDFLAGS) -o HiServer.bin HiSK.o HiServerImpl.o HiServer.o HiSK.o: HiSK.cc Hi.hh $(CC) $(CPPFLAGS) $(INCLUDES) HiSK.cc HiServer.o: HiServer.cc Hi.hh $(CC) $(CPPFLAGS) $(INCLUDES) HiServer.cc HiServerImpl_i.o: HiServerImpl.cc HiServerImpl.hh Hi.hh $(CC) $(CPPFLAGS) $(INCLUDES) HiServerImpl.cc nidl: Hi.idl $(OMNIIDL) -bcxx Hi.idl clean: rm -f *.o rm -f *.bin
编译
make -f MakeServer