$>find ./
./
./echoServer.cpp
./echoClient.cpp
./Makefile
./idl
./idl/echo.idl
./idl/Makefile
1. set build environment
-. set compiler
-. set TAO environment
2. build idl
cd idl
make
3. build application
cd ..
make
4. execute program
before execute, you must set your TAO environment firstly.
Usage: echoClient [-o filename|NamingService]
IOR can be written into either a text file or NamingService.
if String 'NamingServcie' is provided, ior will be written into NamingService. (NamingService must be passed over -ORBInitRef NameService=XXX.
if String other than 'NamingServcie' is provided, ior will be written into a text file with that name.
examples 1: write to NamingService
./echoServer -o NamingService -ORBInitRef NameService=iioploc://192.9.10.1:30000/NameService
./echoClient -o NamingService -ORBInitRef NameService=iioploc://192.9.10.1:30000/NameService
example2 2: write to text file
./echoServer -o ior.txt -ORBInitRef NameService=iioploc://192.9.10.1:30000/NameService
./echoClient -o ior.txt -ORBInitRef NameService=iioploc://192.9.10.1:30000/NameService
-------------------------------------------------------------------------------------------------------------------
$>cat idl/echo.idl
#ifndef __ECHO_IDL__
#define __ECHO_IDL__
interface Echo {
string echoString(in string mesg);
};
#endif // __ECHO_IDL__
-------------------------------------------------------------------------------------------------------------------
$>cat idl/Makefile
all: echoC.o echoS.o
echoC.o : echoC.cpp
CC -mt -I$(TAO_ROOT)/include -I$(TAO_ROOT)/include/tao -DACE_HAS_EXCEPTIONS -c $^
echoS.o : echoS.cpp
CC -mt -I$(TAO_ROOT)/include -I$(TAO_ROOT)/include/tao -DACE_HAS_EXCEPTIONS -c $^
echoC.cpp echoS.cpp : echo.idl
$(TAO_ROOT)/bin/NR/tao_idl -H dynamic_hash -GT $^
clean:
rm -rf *.o *.cpp *.h *.inl
.PHONY: clean
-------------------------------------------------------------------------------------------------------------------
$>cat echoClient.cpp
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <orbsvcs/CosNamingC.h>
#include <orbsvcs/Naming/Naming_Client.h>
#include "idl/echoC.h"
using namespace std;
static void hello(Echo_ptr e)
{
CORBA::String_var src = (const char*) "Hello!";
CORBA::String_var dest = e->echoString(src);
cerr << "Request: [" << (char*)src << "], Response: [" << (char *)dest << "]" << endl;
}
//
int main(int argc, char* argv[]) {
char * fromFile=NULL;
bool fromNamingService = false;
printf("Usage: echoClient [-o filename|NamingService]/n");
char ch;
opterr = 0;
while ((ch = getopt(argc, argv, "o:")) != -1) {
switch(ch) {
case 'o':
if (!strcmp("NamingService",optarg))
fromNamingService = true;
else
fromFile = optarg;
break;
default:
break;
}
}
try {
// initialize orb
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
/*********************NamingService**********************/
CORBA::Object_var obj = 0;
if (fromNamingService)
{
printf("read out ior from [%s]/n","NamingService");
// Find the CORBA Services Naming Service
CORBA::Object_var naming_obj = orb->resolve_initial_references("NameService");
CosNaming::NamingContext_var namingcontext = CosNaming::NamingContext::_narrow(naming_obj.in());
if(CORBA::is_nil(namingcontext.in()))
{
cerr << "Could not narrow NameService to NamingContext!" << endl;
throw 0;
}
CosNaming::Name name;
name.length(2);
name[0].id = CORBA::string_dup( "hui" );
name[1].id = CORBA::string_dup( "echoService" );
obj = namingcontext->resolve(name);
}
if (fromFile != NULL)
{
printf("read out ior from file[%s]/n",fromFile);
ifstream ifile;
string iorStr;
ifile.open(fromFile);
ifile >> iorStr;
ifile.close();
obj = orb->string_to_object(iorStr.c_str());
}
/*********************************************************/
// Obtain reference from servant IOR
// CORBA::Object_var obj = orb->string_to_object(argv[1]);
Echo_var echoref = Echo::_narrow(obj);
if( CORBA::is_nil(echoref) ) {
cerr << "Can't narrow reference to type Echo (or it was nil)." << endl;
return 1;
}
for (CORBA::ULong count=0; count<10; count++)
// communicate with servant
hello(echoref);
orb->destroy();
}
catch(CORBA::COMM_FAILURE&) {
cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
<< "object." << endl;
}
catch(CORBA::SystemException&) {
cerr << "Caught a CORBA::SystemException." << endl;
}
catch(CORBA::Exception&) {
cerr << "Caught CORBA::Exception." << endl;
}
catch(...) {
cerr << "Caught unknown exception." << endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------
$>cat echoServer.cpp
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <orbsvcs/CosNamingC.h>
#include <orbsvcs/Naming/Naming_Client.h>
#include "idl/echoS.h"
using namespace std;
class Echo_i : public POA_Echo,
public PortableServer::RefCountServantBase
{
public:
inline Echo_i() {}
virtual ~Echo_i() {}
virtual char* echoString(const char* mesg) ACE_THROW_SPEC ((::CORBA::SystemException));
};
char* Echo_i::echoString(const char* mesg) ACE_THROW_SPEC (( ::CORBA::SystemException ))
{
cerr << "Get: [" << mesg << "]" << endl;
return CORBA::string_dup(mesg);
}
//
int main(int argc, char** argv)
{
char * toFile=NULL;
bool toNamingService = false;
printf("Usage: echoServer [-o filename|NamingService]/n");
char ch;
opterr = 0;
while ((ch = getopt(argc, argv, "o:")) != -1) {
switch(ch) {
case 'o':
if (!strcmp("NamingService",optarg))
toNamingService = true;
else
toFile = optarg;
break;
default:
break;
}
}
try {
// initialize ORB
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
// find RootPOA
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
// create servant object and activate it
Echo_i* myecho = new Echo_i();
PortableServer::ObjectId_var myechoid = poa->activate_object(myecho);
// Obtain a reference to the object, and print it out as a
// stringified IOR.
obj = myecho->_this();
CORBA::String_var sior(orb->object_to_string(obj));
cerr << (char*)sior << endl;
myecho->_remove_ref();
// find and activate POAManager
PortableServer::POAManager_var pman = poa->the_POAManager();
pman->activate();
if (toNamingService)
{
printf("written ior to [%s]/n","NamingService");
CORBA::Object_var naming_obj = orb->resolve_initial_references("NameService");
CosNaming::NamingContext_var namingcontext = CosNaming::NamingContext::_narrow(naming_obj.in());
if(CORBA::is_nil(namingcontext.in()))
{
cerr << "Could not narrow NameService to NamingContext!" << endl;
throw 0;
}
CosNaming::Name name;
name.length(1);
name[0].id = CORBA::string_dup("hui");
try
{
CORBA::Object_var dummy = namingcontext->resolve(name);
}
catch (CosNaming::NamingContext::NotFound& )
{
CosNaming::NamingContext_var dummy =
namingcontext->bind_new_context(name);
}
name.length(2);
name[1].id = CORBA::string_dup("echoService");
CORBA::Object_var echoServant_obj = poa->id_to_reference(myechoid.in());
namingcontext->rebind(name,echoServant_obj.in());
}
if (toFile != NULL)
{
printf("written ior into file[%s]/n",toFile);
ofstream ofile;
ofile.open(toFile);
ofile << (char*)sior << endl;
ofile.close();
}
orb->run();
}
catch(CORBA::SystemException&) {
cerr << "Caught CORBA::SystemException." << endl;
}
catch(CORBA::Exception&) {
cerr << "Caught CORBA::Exception." << endl;
}
catch(...) {
cerr << "Caught unknown exception." << endl;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------
$>cat Makefile
TAO_LIB = -lACE -lTAO -lTAO_CosNaming -lTAO_Svc_Utils -lTAO_IORTable -lTAO_AnyTypeCode -lTAO_DynamicAny -lTAO_PortableServer -lTAO_Messaging -lTAO_IFR_Client -lTAO_TypeCodeFactory
all: echoClient echoServer
echoClient : echoClient.o
CC -mt -L$(TAO_ROOT)/lib/NR idl/echoC.o $^ $(TAO_LIB) -o $@
echoServer: echoServer.o
CC -mt -L$(TAO_ROOT)/lib/NR idl/echoS.o idl/echoC.o $^ $(TAO_LIB) -o $@
echoClient.o : echoClient.cpp
CC -mt -I$(TAO_ROOT)/include -I$(TAO_ROOT)/include/tao -DACE_HAS_EXCEPTIONS -c $^
echoServer.o : echoServer.cpp
CC -mt -I$(TAO_ROOT)/include -I$(TAO_ROOT)/include/tao -DACE_HAS_EXCEPTIONS -c $^
clean:
rm -rf *.o echoClient echoServer
.PHONY: clean
-------------------------------------------------------------------------------------------------------------------