hpp-template-corba  4.9.0
Template corba server
hpp-template-corba Documentation

Introduction

This package is intended to ease construction of CORBA servers by templating actions that are common to all servers.

Principle

This package only contains one template class hpp::corba::Server. The parameter of this template class is the implementation of an idl interface.

How to

Implementing the server

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 in two ways.

Using a name server.

//file server.cc
#include <stdlib.h>
#include "my-interface.impl.hh"
int main(int argc, char** argv)
{
server.initRootPOA(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);
}

Using a fixed address.

//file server.cc
#include <stdlib.h>
#include "my-interface.impl.hh"
int main(int argc, char** argv)
{
// The adress of the server is specified by the option endpoint
const char* options[][2] = { { "endPoint", ":::13331" }, { 0, 0 } };
hpp::corba::Server<MyImplementation> server (argc, argv, "", options);
server.initOmniINSPOA("server_name");
// This line is mandatory only if you want to enable multithreading.
// It must be called after
// server.initRootPOA(true);
if (server.startCorbaServer() != 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

Implementing the client

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,

Using a name server.

# 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)

Using a fixed address.

# File client.py
from omniORB import CORBA
orb = CORBA.ORB_init (sys.argv, CORBA.ORB_ID)
obj = orb.string_to_object(":::13331/server_name")
from hpp_corba.hpp import *
client = obj._narrow(MyInterface)

Running the server and client

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
>>>