[omniORB] Naming Server

Hai Wang wangh@Teltec.DCU.IE
Sun, 27 Jun 1999 18:33:36 +0100


Hi  all,
	I have got a serious problem working with naming server. In 
server side, I tried to produce an object of my application and 
register it with a name in naming server, for the purpose of 
comparison I print out the "ior" of the object.
	In client, I tried to get the object reference and print out its "ior", 
an exception of COMM_Failure is thrown when the client tried to run 
the function in the object reference. I compared the two "ior"s, they 
are different.

Please give a help if you can.

Hai

My IDL is as follows:
Module Metering
{
	.....
	interface Consumer
	{
           ..........
		void pushStr(in string inStr)
						raises(e_error);
	}
}

My appliccation of the server and client is as follows.
Client:

//------------------------------------------------------------------------//
	//                          Client Implementation						  
//
	//------------------------------------------------------------------------//

	CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv, 
"omniORB2");
	CORBA::BOA_ptr boa = orb->BOA_init(argc, argv, 
"omniORB2_BOA");

	try
	{
		CORBA::Object_ptr  obj  = getObjectReference(orb);

		Hello(orb ,obj);
	}
	catch(CORBA::COMM_FAILURE& ex)
	{
		cerr << "Caught system exception COMM_FAILURE, unable 
to contact the"
			 <<"object." <<endl;
	}
	catch(omniORB::fatalException& ex) 
	{
    cerr << "Caught omniORB2 fatalException. This indicates a bug 
is caught "
         << "within omniORB2.\nPlease send a bug report.\n"
         << "The exception was thrown in file: " << ex.file() << "\n"
         << "                            line: " << ex.line() << "\n"
         << "The error message is: " << ex.errmsg() << endl;
	}
    catch(...) 
	{
    cerr << "Caught a system exception." << endl;
	}


	//----------------------------Client End----------------------------------//

	}

	return nRetCode;
}


static 
CORBA::Object_ptr
getObjectReference(CORBA::ORB_ptr orb)
{
  CosNaming::NamingContext_var rootContext;
  
  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");
	CORBA::String_var tmp = orb->object_to_string(initServ);
	cerr << "IOR of Init Server ==\n" << (char *)tmp <<endl;

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:
    rootContext = CosNaming::NamingContext::_narrow(initServ);
    if (CORBA::is_nil(rootContext)) 
      {
        cerr << "Failed to narrow naming context." << endl;
        return CORBA::Object::_nil();
      }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    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   = (const char*) "test";       // string copied
  name[0].kind = (const char*) "my_context"; // string copied
  name[1].id   = (const char*) "MConsumer";
  name[1].kind = (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 = postscript etc.)

  
  CORBA::Object_ptr obj;
  try {
    // Resolve the name to an object reference, and assign the 
reference 
    // returned to a CORBA::Object:
    obj = 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;
      return CORBA::Object::_nil();
    }
  catch (CORBA::COMM_FAILURE& ex) {
    cerr << "Caught system exception COMM_FAILURE, unable to 
contact the "
         << "naming service." << endl;
    return CORBA::Object::_nil();
  }
  catch(omniORB::fatalException& ex) {
    throw;
  }
  catch (...) {
    cerr << "Caught a system exception while using the naming 
service."<< endl;
    return CORBA::Object::_nil();
  }
  return obj;
}


void Hello(CORBA::ORB_ptr orb, CORBA::Object_ptr  obj)
{
	Metering::Consumer_var consumerRef = 
Metering::Consumer::_narrow(obj);

	if(CORBA::is_nil(consumerRef))
	{
		cerr << "Hello: cannot invoke on a nill object reference.\n" 
<< endl;
		return;
	}

	CORBA::String_var tmp = orb->object_to_string(consumerRef);
		
	cerr << "IOR of consumerRef is: \n" << (char*)tmp <<endl;

	CORBA::String_var src = (const char*)"Hello!";

	consumerRef->pushStr(src);
}


Server:
	//---------------------------------------------------------------//
	//				           Server Implementation				 //
	//---------------------------------------------------------------//
	
	CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv, 
"omniORB2");
	CORBA::BOA_ptr boa  = orb->BOA_init(argc, argv, 
"omniORB2_BOA");


	MConsumer_i* consumerObj = new MConsumer_i();

 	consumerObj->_obj_is_ready(boa);
	{
		Metering::Consumer_ptr consumerRef = consumerObj-
>_this();

		CORBA::String_var tmp = orb-
>object_to_string(consumerRef);
 	    cerr << "IOR of consumerRef is : " << endl << (char*)tmp 
<<endl;

		if(!bindObjectToName(orb, consumerRef))
		{
			return 1;
		}
	}
	
	boa->impl_is_ready(0,1);

	// Tell the BOA we are ready. 
	//--------------------Server End----------------------------//
	}

	return nRetCode;
}



static
CORBA::Boolean
bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr obj)
{

// NEW CODES

	Metering::Consumer_ptr testRef = 
Metering::Consumer::_narrow(obj);
	CORBA::String_var tmp = orb->object_to_string(obj);
	cout << endl;
	cout << "BEFORE Resolve refernce :" << endl;
	cout << (char *) tmp << endl;  

  CosNaming::NamingContext_var rootContext;
  
  try {
    // Obtain a reference to the root context of the Name service:
    CORBA::Object_var initServ;
    initServ = orb->resolve_initial_references("NameService");

    // Narrow the object returned by resolve_initial_references()
    // to a CosNaming::NamingContext object:

    rootContext = CosNaming::NamingContext::_narrow(initServ);

    if (CORBA::is_nil(rootContext))
      {
        cerr << "Failed to narrow naming context." << endl;
        return 0;
      }
  }
  catch(CORBA::ORB::InvalidName& ex) {
    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   = CORBA::string_dup ("test");       // string 
copied
    contextName[0].kind = CORBA::string_dup ("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, and assign testContext to it:
      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 tmpobj;
      tmpobj = rootContext->resolve(contextName);
      testContext = CosNaming::NamingContext::_narrow(tmpobj);
      if (CORBA::is_nil(testContext)) {
        cerr << "Failed to narrow naming context." << endl;
        return 0;
      }
    } 

    // Bind the object (obj) to testContext, naming it Echo:
    CosNaming::Name objectName;
    objectName.length(1);
    objectName[0].id   = CORBA::string_dup ("MConsumer");   // 
string copied
    objectName[0].kind = CORBA::string_dup ("Object"); // string 
copied


    // Bind obj with name Echo to the testContext:
    try 
	{
      testContext->bind(objectName,obj);
    }
    catch(CosNaming::NamingContext::AlreadyBound& ex) 
	{
      testContext->rebind(objectName, obj);
	  cerr << "Rebind is susessful" << endl;
    }


//NEW CODES

    try
	{
		CORBA::Object_var objVar = testContext-
>resolve(objectName);
		Metering::Consumer_var consumerRef = 
Metering::Consumer::_narrow(objVar);
		CORBA::String_var tmp = orb-
>object_to_string(consumerRef);
		cout << endl;
		cout << "Resolve reference :" << endl;
		cout << (char *) tmp << endl;
	}
	catch (...)
	{
		cout << "error with resolve reference" << endl;
	}

    // 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 (omniORB::fatalException& ex) {
    throw;
  }
  catch (...) 
  {
    cerr << "Caught a system exception while using the naming 
service."<< endl;
    return 0;
  }
  return 1;
}