ubuntu下minicorba例子

编程

一、开发环境安装

sudo apt install omniorb omniorb-idl omniidl libomniorb4-dev libomniorb4-2 omniorb-nameserver libomnithread4 libomnithread4-dev

二、源文件:

Hi.idl

module HiCorba

{

interface Hi

{

string sayHiTo(in string someone);

long add(in long numa, in long numb);

oneway void shutdown();

};

};

omniidl  -bcxx Hi.idl会生成: Hi.hh HiSK.cc

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;

}

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)

{

char *dest = new char[strlen("Hi,")+strlen(someone)+1];

strcpy(dest,"Hi,");

strcat(dest,someone);

return dest;

}

::CORBA::Long HiServerImpl::add(::CORBA::Long numa, ::CORBA::Long numb)

{

return numa+numb;

}

void HiServerImpl::shutdown()

{

}

HiClientImpl.hh

#include <iostream>

#include <fstream>

#include "Hi.hh"

using namespace std;

class HiClientImpl {

public:

HiClientImpl();

~HiClientImpl();

void TestAdd();

CosNaming::Name m_corbaCosName;

// CORBA ORB

CORBA::ORB_var m_orb;

// ORB Object

CORBA::Object_var m_obj;

// Resolved id to object reference

CORBA::Object_var m_obj1;

// Resolved and narrowed CORBA object for proxy calls

HiCorba::Hi_var m_Data;

};

class DS_ServerConnectionException{

public:

DS_ServerConnectionException() { cerr << "CORBA COMM_FAILURE" << endl; };

};

class DS_SystemException{

public:

DS_SystemException() { cerr << "CORBA Exception" << endl; };

};

class DS_FatalException{

public:

DS_FatalException() { cerr << "CORBA Fatal Exception" << endl; };

};

class DS_Exception{

public:

DS_Exception() { cerr << "Exception" << endl; };

};

HiClientImpl.cc

#include "assert.h"

#include "HiClientImpl.hh"

#include "Hi.hh"

HiClientImpl::HiClientImpl()

{

try {

//------------------------------------------------------------------------

// Initialize ORB object.

//------------------------------------------------------------------------

int argc=0; // Dummy variables to support following call.

char** argv=0;

CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

//------------------------------------------------------------------------

// Bind ORB object to name service object.

// (Reference to Name service root context.)

//------------------------------------------------------------------------

//CORBA::Object_var obj = orb->resolve_initial_references("OmniNameService");

CORBA::Object_var obj = orb->resolve_initial_references("NameService");

assert (!CORBA::is_nil(obj.in()));

cerr << "resolve_initial_reference OK" << endl;

//------------------------------------------------------------------------

// Narrow this to the naming context (Narrowed reference to root context.)

//------------------------------------------------------------------------

CosNaming::NamingContext_var nc = CosNaming::NamingContext::_narrow(obj.in());

assert (!CORBA::is_nil(nc.in()));

cerr << "narrow ok" << endl;

//------------------------------------------------------------------------

// The "name text" put forth by CORBA server in name service.

// This same name ("DataServiceName1") is used by the CORBA server when

// binding to the name server (CosNaming::Name).

//------------------------------------------------------------------------

CosNaming::Name _corbaCosName;

_corbaCosName.length(1);

_corbaCosName[0].id=CORBA::string_dup("Hi");

//------------------------------------------------------------------------

// Resolve "name text" identifier to an object reference.

//------------------------------------------------------------------------

CORBA::Object_var obj1 = nc->resolve(_corbaCosName);

assert(!CORBA::is_nil(obj1.in()));

cerr << "resolve OK" << endl;

m_Data = HiCorba::Hi::_narrow(obj1.in());

if (CORBA::is_nil(m_Data.in()))

{

cerr << "IOR is not an SA object reference." << endl;

}

else{

cerr << "narrow OK" << endl;

}

}

catch(CORBA::COMM_FAILURE& ex) {

cerr << "Caught system exception COMM_FAILURE -- unable to contact the "

<< "object." << endl;

throw DS_ServerConnectionException();

return;

}

catch(CORBA::SystemException& ) {

cerr << "Caught a CORBA::SystemException." << endl;

throw DS_SystemException();

return;

}

catch(CORBA::Exception& ) {

cerr << "Caught CORBA::Exception." << endl;

throw DS_Exception();

return;

} catch(omniORB::fatalException& fe) {

cerr << "Caught omniORB::fatalException:" << endl;

cerr << " file: " << fe.file() << endl;

cerr << " line: " << fe.line() << endl;

cerr << " mesg: " << fe.errmsg() << endl;

throw DS_FatalException();

return;

}

catch(...) {

cerr << "Caught unknown exception." << endl;

throw DS_Exception();

return;

}

return;

}

HiClientImpl::~HiClientImpl()

{

}

void HiClientImpl::TestAdd()

{

CORBA::Long num1=70;

CORBA::Long num2=80;

CORBA::Long retNum;

cout << "Values input to Server: "

<< num1 << " "

<< num2 << " " << endl;

if( retNum = m_Data->add( num1, num2)) // This is the CORBA call which is to be executed remotely

{ // Ok

cout << "Values returned by Server: "

<< num1 << "+"

<< num2 << "="

<< retNum << endl;

}

}

$ cat HiClient.cc

#include "HiClientImpl.hh"

int main(int argc, char** argv)

{

HiClientImpl impl;

impl.TestAdd();

return 0;

}

三、Makefile

CC = /usr/bin/g++

CPPFLAGS = -g -c -I/usr/include/omniORB4

LDFLAGS = `pkg-config --libs omniDynamic4 omniORB4`

OMNIIDL = /usr/bin/omniidl

all: HiServer.bin HiClient.bin

HiServer.bin: HiSK.o HiServerImpl.o HiServer.o

$(CC) -g -o HiServer.bin HiSK.o HiServerImpl.o HiServer.o $(LDFLAGS)

HiClient.bin: HiClient.o HiClientImpl.o HiSK.o

$(CC) -g -o HiClient.bin HiClient.o HiClientImpl.o HiSK.o $(LDFLAGS)

HiSK.o: HiSK.cc Hi.hh

$(CC) $(CPPFLAGS) $(INCLUDES) HiSK.cc

HiClient.o: HiClient.cc HiClientImpl.hh

$(CC) $(CPPFLAGS) HiClient.cc

HiClientImpl.o: HiClientImpl.cc HiClientImpl.hh Hi.hh

$(CC) $(CPPFLAGS) HiClientImpl.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

HiSK.cc Hi.hh: Hi.idl

$(OMNIIDL) -bcxx Hi.idl

clean:

rm -f *.o

rm -f HiSK.cc Hi.hh

rm -f *.bin

四、编译运行

$ make

/usr/bin/omniidl -bcxx Hi.idl

/usr/bin/g++ -g -c -I/usr/include/omniORB4 HiSK.cc

g++ -g -c -I/usr/include/omniORB4 -c -o HiServerImpl.o HiServerImpl.cc

/usr/bin/g++ -g -c -I/usr/include/omniORB4 HiServer.cc

/usr/bin/g++ -g -o HiServer.bin HiSK.o HiServerImpl.o HiServer.o `pkg-config --libs omniDynamic4 omniORB4`

/usr/bin/g++ -g -c -I/usr/include/omniORB4 HiClient.cc

/usr/bin/g++ -g -c -I/usr/include/omniORB4 HiClientImpl.cc

/usr/bin/g++ -g -o HiClient.bin HiClient.o HiClientImpl.o HiSK.o `pkg-config --libs omniDynamic4 omniORB4`

$ ./HiServer.bin &

[1] 16154

mymotif@mymotif-S40-51:~/prg/miniORBtest/hello1$ "IOR:010000001300000049444c3a4869436f7262612f48693a312e300000010000000000000064000000010102000d0000003139322e3136382e302e3938000015d70e000000fed38a085f00003f1a000000000000000200000000000000080000000100000000545441010000001c00000001000000010001000100000001000105090101000100000009010100"

resolve_initial_references successed

narrow successed

rebind successed

poamanager activate successed

$ ./HiClient.bin

resolve_initial_reference OK

narrow ok

resolve OK

narrow OK

Values input to Server: 70 80

Values returned by Server: 70+80=150

mymotif@mymotif-S40-51:~/prg/miniORBtest/hello1$ ps aux|grep Hi

mymotif 16154 0.0 0.0 180156 5612 pts/2 Sl 23:35 0:00 ./HiServer.bin

mymotif 16169 0.0 0.0 16884 1124 pts/2 S+ 23:36 0:00 grep --color=auto Hi

mymotif@mymotif-S40-51:~/prg/miniORBtest/hello1$ kill 16154

mymotif@mymotif-S40-51:~/prg/miniORBtest/hello1$ ./HiClient.bin

resolve_initial_reference OK

narrow ok

resolve OK

narrow OK

Values input to Server: 70 80

terminate called after throwing an instance of "CORBA::TRANSIENT"

[1]+ 已终止 ./HiServer.bin

已放弃 (核心已转储)

可以看出,如server不运行client便会出错。

代码下载

以上是 ubuntu下minicorba例子 的全部内容, 来源链接: utcz.com/z/518221.html

回到顶部