#include <echo.hh>
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr,
CORBA::Object_ptr);
class Echo_i : public POA_Echo
{
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);
}
//////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv)
{
try {
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
Echo_i* myecho = new Echo_i();
PortableServer::ObjectId_var myechoid =
poa->activate_object(myecho);
// Obtain a reference to the object, and register it in
// the naming service.
obj = myecho->_this();
CORBA::String_var x;
x = orb->object_to_string(obj);
cout << x << endl;
if( !bindObjectToName(orb, obj) )
return 1;
myecho->_remove_ref();
PortableServer::POAManager_var pman = poa->the_POAManager();
pman->activate();
orb->run();
}
catch(CORBA::SystemException& ex) {
cerr << "Caught CORBA::" << ex._name() << endl;
}
catch(CORBA::Exception& ex) {
cerr << "Caught CORBA::Exception: " << ex._name() << 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;
}
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 = orb->resolve_initial_references("NameService");
// Narrow the reference returned.
rootContext = CosNaming::NamingContext::_narrow(obj);
if( CORBA::is_nil(rootContext) ) {
cerr << "Failed to narrow the root naming context." << endl;
return 0;
}
}
catch (CORBA::NO_RESOURCES&) {
cerr << "Caught NO_RESOURCES exception. You must configure omniORB
"
<< "with the location" << endl
<< "of the naming service." << endl;
return 0;
}
catch (CORBA::ORB::InvalidName&) {
// 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 = (const char*) "test"; // string copied
contextName[0].kind = (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 = postscript etc.)
CosNaming::NamingContext_var testContext;
try {
// Bind the context to root.
testContext = 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 = rootContext->resolve(contextName);
testContext = 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 = (const char*) "Echo"; // string copied
objectName[0].kind = (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::TRANSIENT& ex) {
cerr << "Caught system exception TRANSIENT -- unable to contact the
"
<< "naming service." << endl
<< "Make sure the naming server is running and that omniORB is "
<< "configured correctly." << endl;
return 0;
}
catch(CORBA::SystemException& ex) {
cerr << "Caught a CORBA::" << ex._name()
<< " while using the naming service." << endl;
return 0;
}
return 1;
}
//-----------------------------------------------------------------------------------------------------------
I start this server and if I try to test with c++ client, works very
well!
Now I'd want to test with a java client :
//------------------------------------------------------------------------------------------------------------
import java.util.*;
import EchoApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
//import org.omg.CORBA.*;
public class client {
public static void main(String [] args) {
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
if (orb == null) System.exit(-1);
try {
// obtain service from naming server
org.omg.CORBA.Object ns_obj =
orb.resolve_initial_references("NameService");
org.omg.CosNaming.NamingContext nc =
org.omg.CosNaming.NamingContextHelper.narrow(ns_obj);
org.omg.CosNaming.NameComponent [] path = { new
org.omg.CosNaming.NameComponent("test","my_context")};
org.omg.CORBA.Object obj = nc.resolve(path);
Echo echo = EchoHelper.narrow(obj);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//------------------------------------------------------------------------------------------------------------
1) start omniOrb
2) start server eg3_impl
3) start client :
java -classpath . client -ORBInitRef
NameService=corbaloc::<my_ip>:2809/NameService
I receive the following exception:
org.omg.CosNaming.NamingContextPackage.NotFound:
IDL:omg.org/CosNaming/NamingContext/NotFound:1.0
at
org.omg.CosNaming.NamingContextPackage.NotFoundHelper.read(Unknown
Source)
at org.omg.CosNaming._NamingContextStub.resolve(Unknown Source)
at client.main(client.java:34)
What's is wrong?
thanks teo
line : org.omg.CosNaming.NameComponent("test","my_context")}; //KO
is : org.omg.CosNaming.NameComponent("Echo","")}; //OK
rs232 ha scritto:
However, your Java client is trying to resolve an object called "test"
in the root context, and then narrow it to an Echo object. Since "test"
is not an Echo object, but rather a NameContext, this would never work.
I'd suggest simply binding the Echo object in the root context as a
starting point, rather than creating a child context. If you want to
stick with the existing server code, then you need to modify the Java
client code to reference the "Echo" object in the "test" context.
Mark
I ask you for another problem:
Now when I start client I receive the following error. I have found in
newsgroup but I don't understand
org.omg.CORBA.BAD_PARAM: vmcid: 0x0 minor code: 0 completed: No
at EchoApp.EchoHelper.narrow(EchoHelper.java:60)
at client.main(client.java:25)
I checked if : org.omg.CORBA.Object obj = nc.resolve(path); was null
but It's not null
but this statement : Echo hello = EchoHelper.narrow(obj);
return:
org.omg.CORBA.BAD_PARAM: vmcid: 0x0 minor code: 0 completed: No
at EchoApp.EchoHelper.narrow(EchoHelper.java:60)
at client.main(client.java:25)
Thanks
Teo.
Mark Woyna ha scritto:
thanks teo
rs232 ha scritto: