[omniORB] OmniOrb as Server, JacOrb as Client
Olivier Raoul
or@axlog.fr
Tue, 04 Aug 1998 19:41:42 +0100
Thien Nguyen wrote:
> Hello,
>
> I remember somebody mentioned they were successful with using OmniOrb as
> a C++ Server, and JacOrb as a Java Client server. Would you please share
> the sample code on how to initiate both server and client apps and any
> quirks that I should be aware of.
you must use jdk1.2 beta4 and the java nameservice
this is my test code
the idl file is testcorba.idl
module TestcorbaApp
{
interface Testinterface
{
string sayHello();
};
};
the main testinterface_client.cc is
#include "testcorba.h"
int
main (int argc, char **argv)
{
CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");
try {
// Obtain a reference to the root context of the Name service:
CORBA::Object_var initServ =
orb->resolve_initial_references("NameService");
CosNaming::NamingContext_var rootContext =
CosNaming::NamingContext::_narrow(initServ);
if (CORBA::is_nil(rootContext))
{
cerr << "Failed to narrow naming context." << endl;
return 1;
}
CosNaming::Name name;
name.length(2);
name[0].id = (const char*) "Testcorba"; // string copied
name[0].kind = (const char*) "framework"; // string copied
name[1].id = (const char*) "Testinterface";
name[1].kind = (const char*) "Object";
CORBA::Object_var obj = rootContext->resolve(name);
TestcorbaApp::Testinterface_var testinterface =
TestcorbaApp::Testinterface::_narrow(obj);
if (!CORBA::is_nil(testinterface ) )
{
for ( int i = 0;i<50;++ i )
{
cout<<"test "<<testinterface ->sayHello()<<endl;
}
}
}
catch(CORBA::ORB::InvalidName& ex)
{
cerr << "Service required is invalid [does not exist]." << endl;
}
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.";
CosNaming::Name name = ex.rest_of_name;
for ( int i =0; i<name.length();++ i )
{
cerr<< ex.rest_of_name[i].id<<" ";
}
cout<<endl;
}
catch (CORBA::COMM_FAILURE& ex)
{
cerr << "Caught system exception COMM_FAILURE, unable to contact the "
<< "naming service." << endl;
}
catch (...) {
cerr << "Caught a system exception while using the naming service."<<
endl;
}
return 1;
}
the java client TestcorbaClient.java
the import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import java.util.Properties;
public class TestcorbaClient
{
public static void main(String args[])
{
try{
// create and initialize the ORB
Properties props = new Properties();
props.put("org.omg.CORBA.ORBInitialPort", "900");
ORB orb = ORB.init(args, props);
// get the root naming context
System.out.println( "Get name service" );
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
// get Testcorba Context
System.out.println( "Get testcorba context" );
NameComponent nc = new NameComponent("Testcorba", "framework");
NameComponent docnc = new NameComponent("Testinterface",
"Object");
NameComponent docpath[] = {nc, docnc};
Testinterface testinterfaceRef =
TestinterfaceHelper.narrow(ncRef.resolve(docpath));
// call the Testinterface server object and print results
String testinterface = testinterfaceRef.sayHello();
System.out.println(testinterface);
} catch (Exception e) {
System.out.println("ERROR : " + e) ;
e.printStackTrace(System.out);
}
}
}
the server implementations testinterface_i.h
#include <string.h>
#include "testcorba.h"
class Testinterface_i : public virtual TestcorbaApp::_sk_Testinterface {
public:
Testinterface_i() {}
virtual ~Testinterface_i() {}
virtual char * sayHello();
};
the testinterface_i.cc
#include "testinterface_i.h"
#include <iostream.h>
char *
Testinterface_i::sayHello() {
char *p = "hello";
cerr<<"say Hello called"<<endl;
return p;
}
the main source for server
int
main(int argc, char **argv)
{
CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");
Testinterface_i *myobj = new Testinterface_i();
myobj->_obj_is_ready(boa);
CosNaming::NamingContext_var rootContext;
try {
CORBA::Object_var initServ =
orb->resolve_initial_references("NameService");
CosNaming::NamingContext_var rootContext =
CosNaming::NamingContext::_narrow(initServ);
if (CORBA::is_nil(rootContext))
{
cerr << "Failed to narrow naming context." << endl;
return 0;
}
CosNaming::Name contextName;
contextName.length(1);
contextName[0].id = (const char*) "Testcorba"; // string copied
contextName[0].kind = (const char*) "framework"; // string copied
CosNaming::NamingContext_var testcorbaContext;
try
{
testcorbaContext= 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 testcorbaContext
// to the object returned:
CORBA::Object_var tmpobj;
tmpobj = rootContext->resolve(contextName);
testcorbaContext = CosNaming::NamingContext::_narrow(tmpobj);
if (CORBA::is_nil(testcorbaContext)) {
cerr << "Failed to narrow naming context." << endl;
return 0;
}
}
CosNaming::Name objectName;
objectName.length(1);
objectName[0].id = (const char*) "Testinterface"; // string copied
objectName[0].kind = (const char*) "Object"; // string copied
try {
testcorbaContext->bind(objectName,myobj ->_this());
}
catch(CosNaming::NamingContext::AlreadyBound& ex) {
testcorbaContext->rebind(objectName,myobj ->_this());
}
}
catch(CORBA::ORB::InvalidName& ex) {
cerr << "Service required is invalid [does not exist]." << endl;
return 1;
}
{
TestcorbaApp::Testinterface_var myobjRef = myobj->_this();
CORBA::String_var p = orb->object_to_string(myobjRef);
cerr << "'" << (char*)p << "'" << endl;
}
boa->impl_is_ready(); // block here indefinitely
// See the explanation in example 1
return 0;
}
you MUST launch tnameserv.exe and update and registry with the good IOR