[omniORB] Date: Wed, 17 Jan 2001 17:20:58 +0800
GAO,YAN (HP-Singapore,ex3)
yan_gao@hp.com
Wed, 17 Jan 2001 09:21:22 +0000
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_000_01C08066.CD5EDE60
Content-Type: text/plain;
charset="iso-8859-1"
Hi, List
I need advise in communicating two servers through the use of the omni
naming service.
At the present moment, I have developed 2 seperate servers based on eg3_clt
and eg3_impl that attaches itself to the naming service.
The problem arises when an attempt to bind one of the servers with the other
via the naming service. OmniName cease to
respond when such an implementation was run. In fact it even cease to
respond to any other request even when my application was killed.
Following is the source code for my version of eg3_clt.cc ( eg3_impl.cc
remain unchanged)
Regards,
Gao Yan
<<eg3_clt2.cc>> <<eg3_impl.cc>> .
------_=_NextPart_000_01C08066.CD5EDE60
Content-Type: application/octet-stream;
name="eg3_clt2.cc"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="eg3_clt2.cc"
// eg3_clt2.cc - This is the source code of example 3 used in Chapter 2
// "The Basics" of the omniORB user guide.
//
// This is the client. It uses the COSS naming service
// to obtain the object reference.
//
// Usage: eg3_clt2
//
//
// On startup, the client lookup the object reference from the
// COS naming service.
//
// The name which the object is bound to is as follows:
// root [context]
// |
// text [context] kind [my_context]
// |
// Echo [object] kind [Object]
//
#include <iostream.h>
#include <stdlib.h>
#include <echo.hh>
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, =
CORBA::Object_ptr);
static CORBA::Object_ptr getObjectReference(CORBA::ORB_ptr orb);
/////////////////////////////////////////////////////////////
class Echo1 : public POA_Echo,public =
PortableServer::RefCountServantBase {
public:
inline Echo1() {cout << "echo1 here!!" << endl;}
virtual ~Echo1() {}
virtual char* echoString(const char* msg) {}
};
////////////////////////////////////////////////////////////////////////=
/
static void hello(Echo_ptr e)
{
if( CORBA::is_nil(e) ) {
cerr << "hello: The object reference is nil!\n" << endl;
return;
}
CORBA::String_var src =3D (const char*) "Hello!";
CORBA::String_var dest =3D e->echoString(src);
cerr << "I said, \"" << (char*)src << "\"." << endl
<< "The Echo object replied, \"" << (char*)dest <<"\"." << endl;
}
//////////////////////////////////////////////////////////////////////
int main (int argc, char **argv) {=20
try {
CORBA::ORB_var orb =3D CORBA::ORB_init(argc, argv, "omniORB3");
cout << " CORBA::ORB_init" << endl;
CORBA::Object_var obj =3D getObjectReference(orb);
cout << " getObjectReference" << endl;
Echo_var echoref =3D Echo::_narrow(obj);
for( int i =3D 0; i < atoi(argv[1]); i++)
{
hello(echoref);
}//till here1
//obj =3D orb->resolve_initial_references("RootPOA");
CORBA::Object_var obj1 =3D =
orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa =3D PortableServer::POA::_narrow(obj1);
Echo1* myecho1 =3D new Echo1();
PortableServer::ObjectId_var myechoid1 =3D =
poa->activate_object(myecho1);
obj1 =3D myecho1->_this();
if (!bindObjectToName(orb, obj1))
return 1;
=20
myecho1->_remove_ref();
PortableServer::POAManager_var pman =3D poa->the_POAManager();
pman->activate();
orb->run();
orb->destroy();// till here2
}
catch(CORBA::COMM_FAILURE& ex) {
cerr << "Caught system exception COMM_FAILURE -- unable to contact =
the "
<< "object." << endl;
}
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;
}
//////////////////////////////////////////////////////////////////////
static CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb)
{
CosNaming::NamingContext_var rootContext;
=20
try {
// Obtain a reference to the root context of the Name service:
CORBA::Object_var obj;
obj =3D orb->resolve_initial_references("NameService");
// Narrow the reference returned.
rootContext =3D CosNaming::NamingContext::_narrow(obj);
if( CORBA::is_nil(rootContext) ) {
cerr << "Failed to narrow the root naming context." << endl;
return CORBA::Object::_nil();
}
}
catch(CORBA::ORB::InvalidName& ex) {
// This should not happen!
cerr << "Service required is invalid [does not exist]." << endl;
return CORBA::Object::_nil();
}
// Create a name object, containing the name test/context:
CosNaming::Name name;
name.length(2);
name[0].id =3D (const char*) "test"; // string copied
name[0].kind =3D (const char*) "my_context"; // string copied
name[1].id =3D (const char*) "Echo";
name[1].kind =3D (const char*) "Object";
// Note on kind: The kind field is used to indicate the type
// of the object. This is to avoid conventions such as that used
// by files (name.type -- e.g. test.ps =3D postscript etc.)
try {
// Resolve the name to an object reference.
return rootContext->resolve(name);
}
catch(CosNaming::NamingContext::NotFound& ex) {
// This exception is thrown if any of the components of the
// path [contexts or the object] aren't found:
cerr << "Context not found." << endl;
}
catch(CORBA::COMM_FAILURE& ex) {
cerr << "Caught system exception COMM_FAILURE -- unable to contact =
the "
<< "naming service." << endl;
}
catch(CORBA::SystemException&) {
cerr << "Caught a CORBA::SystemException while using the naming =
service."
<< endl;
}
return CORBA::Object::_nil();
}
//////////////////////////////////////////////////////////////////////
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr orb, =
CORBA::Object_ptr objref) {
CosNaming::NamingContext_var rootContext;
try {
CORBA::Object_var obj;
obj =3D orb->resolve_initial_references("NamingService");
rootContext =3D CosNaming::NamingContext::_narrow(obj);
if (CORBA::is_nil(rootContext)) {
cerr <<"Failed to narow the root naming context." << endl;
return 0;
}
}
catch (CORBA::ORB::InvalidName& ex) {
cerr << "Service required is invalid [does not exits]." << endl;
return 0;
}
=09
try {
CosNaming::Name contextName;
contextName.length(1);
contextName[0].id =3D (const char*) "test";
contextName[0].kind =3D (const char*) "abc";
CosNaming::NamingContext_var test1;
try {
test1 =3D rootContext->bind_new_context(contextName);
}
catch (CosNaming::NamingContext::AlreadyBound& ex) {
CORBA::Object_var obj;
obj =3D rootContext->resolve(contextName);
test1 =3D CosNaming::NamingContext::_narrow(obj);
if (CORBA::is_nil(test1)) {
cerr << "Failed to narrow naming context." << endl;
return 0;
}
}
CosNaming::Name objectName;
objectName.length(1);
objectName[0].id =3D (const char*) "xyz";
objectName[0].kind =3D (const char*) "Object";
try {
test1->bind(objectName, objref);
}
=09
catch (CosNaming::NamingContext::AlreadyBound& ex) {
test1->rebind(objectName, objref);
}
}
catch (CORBA::COMM_FAILURE& ex) {
cerr << "Caught system exception COMM_FAILURE -- unable to "
<< "contact the naming service." << endl;
return 0;
}
=09
catch (CORBA::SystemException&) {
cerr << "Caught a CORBA::SystemException while using the "
<< "naming service." << endl;
return 0;
}
return 1;
}
------_=_NextPart_000_01C08066.CD5EDE60
Content-Type: application/octet-stream;
name="eg3_impl.cc"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="eg3_impl.cc"
// eg3_impl.cc - This is the source code of example 3 used in Chapter 2
// "The Basics" of the omniORB user guide.
//
// This is the object implementation.
//
// Usage: eg3_impl
//
// On startup, the object reference is registered with the
// COS naming service. The client uses the naming service to
// locate this object.
//
// The name which the object is bound to is as follows:
// root [context]
// |
// test [context] kind [my_context]
// |
// Echo [object] kind [Object]
//
#include <iostream.h>
#include <echo.hh>
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, =
CORBA::Object_ptr);
class Echo_i : public POA_Echo,
public PortableServer::RefCountServantBase
{
public:
inline Echo_i() {}
virtual ~Echo_i() {}
virtual char* echoString(const char* mesg);
};
char* Echo_i::echoString(const char* mesg)
{
//return CORBA::string_dup(mesg);
return CORBA::string_dup("this is eg3_impl");
}
//////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv)
{
try {
CORBA::ORB_var orb =3D CORBA::ORB_init(argc, argv, "omniORB3");
CORBA::Object_var obj =3D =
orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa =3D PortableServer::POA::_narrow(obj);
Echo_i* myecho =3D new Echo_i();
PortableServer::ObjectId_var myechoid =3D =
poa->activate_object(myecho);
// Obtain a reference to the object, and register it in
// the naming service.
obj =3D myecho->_this();
if( !bindObjectToName(orb, obj) )
return 1;
myecho->_remove_ref();
PortableServer::POAManager_var pman =3D poa->the_POAManager();
pman->activate();
orb->run();
orb->destroy();
}
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;
}
//////////////////////////////////////////////////////////////////////
static CORBA::Boolean
bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref)
{
CosNaming::NamingContext_var rootContext;
try {
// Obtain a reference to the root context of the Name service:
CORBA::Object_var obj;
obj =3D orb->resolve_initial_references("NameService");
// Narrow the reference returned.
rootContext =3D CosNaming::NamingContext::_narrow(obj);
if( CORBA::is_nil(rootContext) ) {
cerr << "Failed to narrow the root naming context." << endl;
return 0;
}
}
catch(CORBA::ORB::InvalidName& ex) {
// This should not happen!
cerr << "Service required is invalid [does not exist]." << endl;
return 0;
}
try {
// Bind a context called "test" to the root context:
CosNaming::Name contextName;
contextName.length(1);
contextName[0].id =3D (const char*) "test"; // string =
copied
contextName[0].kind =3D (const char*) "my_context"; // string =
copied
// Note on kind: The kind field is used to indicate the type
// of the object. This is to avoid conventions such as that used
// by files (name.type -- e.g. test.ps =3D postscript etc.)
CosNaming::NamingContext_var testContext;
try {
// Bind the context to root.
testContext =3D rootContext->bind_new_context(contextName);
}
catch(CosNaming::NamingContext::AlreadyBound& ex) {
// If the context already exists, this exception will be raised.
// In this case, just resolve the name and assign testContext
// to the object returned:
CORBA::Object_var obj;
obj =3D rootContext->resolve(contextName);
testContext =3D CosNaming::NamingContext::_narrow(obj);
if( CORBA::is_nil(testContext) ) {
cerr << "Failed to narrow naming context." << endl;
return 0;
}
}
// Bind objref with name Echo to the testContext:
CosNaming::Name objectName;
objectName.length(1);
objectName[0].id =3D (const char*) "Echo"; // string copied
objectName[0].kind =3D (const char*) "Object"; // string copied
try {
testContext->bind(objectName, objref);
}
catch(CosNaming::NamingContext::AlreadyBound& ex) {
testContext->rebind(objectName, objref);
}
// Note: Using rebind() will overwrite any Object previously bound
// to /test/Echo with obj.
// Alternatively, bind() can be used, which will raise a
// CosNaming::NamingContext::AlreadyBound exception if the =
name
// supplied is already bound to an object.
// Amendment: When using OrbixNames, it is necessary to first try =
bind
// and then rebind, as rebind on it's own will throw a =
NotFoundexception if
// the Name has not already been bound. [This is incorrect =
behaviour -
// it should just bind].
}
catch(CORBA::COMM_FAILURE& ex) {
cerr << "Caught system exception COMM_FAILURE -- unable to contact =
the "
<< "naming service." << endl;
return 0;
}
catch(CORBA::SystemException&) {
cerr << "Caught a CORBA::SystemException while using the naming =
service."
<< endl;
return 0;
}
return 1;
}
------_=_NextPart_000_01C08066.CD5EDE60--