This package is intended to ease construction of CORBA servers by templating actions that are common to all servers.
This package only contains one template class hpp::corba::Server. The parameter of this template class is the implementation of an idl interface.
Let us assume that you want to build a server implementing the following interface written in file interface.idl
.
// file interface.idl module hpp { interface MyInterface { // Compute the sum of two real numbers. double sum(in double a, in double b); }; };
Generate C++ code relative to this interface using omniidl.
omniidl -bcxx interface.idl
Two files are created: interface.hh
and interfaceSK.cc
.
You need to write the implementation of your server as a class deriving from POA_hpp::MyInterface
(defined in interface.hh
).
// file my-interface.impl.hh #ifndef HPP_CORBA_INTERFACE_IMPL_HH #define HPP_CORBA_INTERFACE_IMPL_HH #include <omniORB4/CORBA.h> #include "interface.hh" class MyImplementation : public virtual POA_hpp::MyInterface { virtual CORBA::Double sum(CORBA::Double a, CORBA::Double b) { return a+b; } }; // end of class MyImplementation #endif //HPP_CORBA_INTERFACE_IMPL_HH
You can now implement a server as follows:
//file server.cc #include <stdlib.h> #include <hpp/corba/template/server.hh> #include "my-interface.impl.hh" int main(int argc, char** argv) { hpp::corba::Server<MyImplementation> server (argc, argv, true); const std::string contextId("cId"); const std::string contextKind("cKind"); const std::string objectId("oId"); const std::string objectKind("oKind"); if (server.startCorbaServer(contextId, contextKind, objectId, objectKind) != 0) { exit (-1); } server.processRequest(true); }
To compile the above file, use omniORB
specific flags:
g++ -o server `pkg-config --cflags hpp-template-corba` `pkg-config --cflags omniORB4` `pkg-config --libs omniORB4` interfaceSK.cc server.cc
where includedir
is the header installation directory of this package. You get an executable implementing your CORBA. To run the server, you need to start a name server. Your server will be referenced in the name server by cId.cKind/oId.oKind
In this section, we implement a python client for the above server.
The first step consists in compiling interface.idl
to generate python stubs:
omniidl -bpython -Wbpackage=hpp_corba interface.idl
A new directory hpp_corba
is created. The following lines implement a python client,
# File client.py from omniORB import CORBA import CosNaming import sys orb = CORBA.ORB_init (sys.argv, CORBA.ORB_ID) obj = orb.resolve_initial_references("NameService") rootContext = obj._narrow(CosNaming.NamingContext) name = [CosNaming.NameComponent ("cId", "cKind"), CosNaming.NameComponent ("oId", "oKind")] obj = rootContext.resolve (name) from hpp_corba.hpp import * client = obj._narrow(MyInterface)
You need 3 terminals.
In the first terminal, run the name server
omniNames -start
in the second terminal, run the server:
./server
in the third terminal, open a python terminal
python Python 2.6.2 (r262:71600, Jan 25 2010, 18:46:45) [GCC 4.4.2 20091222 (Red Hat 4.4.2-20)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from client import client >>> client.sum(1.5, 2.5) 4.0 >>>