[omniORB] Communication between a OmniServer and JavaClient using NamingService

Rajesh Khan rajeshkhan808 at gmail.com
Sun Dec 25 03:19:19 GMT 2011


Skipped content of type multipart/alternative-------------- next part --------------
import java.io.Console;

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class HelloClient
{
  static Hello helloImpl;

  public static void main(String args[])
    {
      try{
        // create and initialize the ORB

	    ORB orb = ORB.init(args, null);

        // get the root naming context
        org.omg.CORBA.Object objRef = 
	    orb.resolve_initial_references("NameService");
        // Use NamingContextExt instead of NamingContext. This is 
        // part of the Interoperable naming Service.  
        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
 
        // resolve the Object Reference in Naming
        String name = "Hello";
        helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));

        
        System.out.println("Obtained a handle on server object: " + helloImpl);
        System.out.println(helloImpl.sayHello());
        
       // System.console().readLine();
       // helloImpl.shutdown();

	} catch (Exception e) {
          System.out.println("ERROR : " + e) ;
	  e.printStackTrace(System.out);
	  }
    }

}
-------------- next part --------------
//Creates the object and passes it to the client developed in C++

#include "hello.hh"

#ifdef HAVE_STD
#  include <iostream>
using namespace std;
#else
#  include <iostream.h>
#endif

static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr);


class CustObj_implimentation : public POA_HelloApp::Hello
{
public:
	virtual char* sayHello();
	virtual void shutdown();
};


//Provide a body
char* CustObj_implimentation::sayHello() throw (CORBA::SystemException)
{
	//Initialize the object as usual
	return "Hello Java Server";
}

void CustObj_implimentation::shutdown() throw (CORBA::SystemException)
{
	std::cout << "Function called";
}

//////////////////////////////////////////////////////////////////////

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); //returns reference

		CustObj_implimentation *obj_ptr = new CustObj_implimentation();
		PortableServer::ObjectId_var myechoid = poa->activate_object(obj_ptr);

		// Obtain a reference to the object, and register it in
		obj = obj_ptr->_this();


		//------------------------------------------------------------------------------------------------
		CORBA::String_var x;
		x = orb->object_to_string(obj);
		cout << x << endl;

		if( !bindObjectToName(orb, obj) )
			return 1;
		//------------------------------------------------------------------------------------------------

		obj_ptr->_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;
	}
	std::cin.get();
	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;
			std::cin.get();
			return 0;
		}
	
		// Bind a context called "test" to the root context:
		CosNaming::Name contextName;

		contextName.length(1);
		contextName[0].id   = (const char*) "Hello";       // string copied
		//contextName[0].kind = (const char*) "my_context"; // string copied
		

		CosNaming::NamingContext_var testContext;
			// Bind the context to root.
		//testContext = rootContext->bind_new_context(contextName);
		rootContext->bind(contextName,objref);

	}
	catch(CORBA::SystemException& ex) 
	{
		cerr << "Caught a CORBA::" << ex._name()
			<< " while using the naming service." << endl;
		std::cin.get();
		return 0;
	}

	return 1;
}


More information about the omniORB-list mailing list