Examining CORBA Interoperability 
by Eric Ironside, Letha Etzkorn, David Zajac

Listing One
module echomodule
{
  interface echo
  {
    string echostring(in string message);
  };
};


Listing Two
module echomodule
{
  interface echo
  {
    typedef sequence<float> FloatSeq;
    string echoseq(inout FloatSeq smsg);
  };
};


Listing Three
import org.omg.CORBA.*;

public class Client {
  public static void main(String[] args) {
    // Initialize the ORB.
    org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
    org.omg.CORBA.Object obj = orb.string_to_object(args[0]);
    echomodule.echo echo = echomodule.echoHelper.narrow(obj);
    String mystring = "This is a test";
    System.out.println("The value to be sent is "+mystring);
    String echoed_string = echo.echostring(mystring);
    // Print out the balance.
    System.out.println
      ("The echoed value is " + echoed_string);
  }
}


Listing Four
import org.omg.PortableServer.*;
public class Server {
  public static void main(String[] args) {
    try {
     // Initialize the ORB.
     org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
     // get a reference to the root POA
     POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
     // Create policies for our persistent POA
     org.omg.CORBA.Policy[] policies = {
        rootPOA.create_lifespan_policy(LifespanPolicyValue.PERSISTENT),
        rootPOA.create_request_processing_policy(RequestProcessingPolicyValue.USE_DEFAULT_SERVANT)
      };
      // Create myPOA with the right policies
      POA myPOA = rootPOA.create_POA( "echo_poa", rootPOA.the_POAManager(),policies );
      // Create the servant
      echoImpl myServant = new echoImpl();
      myPOA.set_servant(myServant);
      // Decide on the ID for the servant
      byte[] myservantId = "echoModuleecho".getBytes();
      // Activate the servant with the ID on myPOA
      myPOA.activate_object_with_id(myservantId, myServant);
      // Activate the POA manager
      rootPOA.the_POAManager().activate();
      // Create the object
      org.omg.CORBA.Object ref;
      ref = myPOA.create_reference_with_id("echo".getBytes(),                                            "IDL:echomodule/echo:1.0");
      // Print out the IOR string of this object
      System.out.println(orb.object_to_string(ref));
      // Wait for incoming requests
      orb.run();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}


Listing Five
// File Client.cpp
#include "EchoC.h"
#include "TAO\orbsvcs\orbsvcs\CosNamingC.h"
#include <iostream>

int main (int argc, char* argv[])
{
  try {
    // First initialize the ORB
    CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "");
    // Convert the IOR to a reference
    CORBA::Object_var naming_context_object = orb->string_to_object(argv[1]);
    // Narrow the Naming Service
    CosNaming::NamingContext_var naming_context =
           CosNaming::NamingContext::_narrow (naming_context_object.in ());
    // Build a name
    CosNaming::Name name (1);
    name.length (1);
    name[0].id = CORBA::string_dup ("echo");
    // Find the server
    CORBA::Object_var obj = naming_context->resolve (name);
    // Narrow the server object
    echomodule::echo_var myecho;
    try {
    myecho=echomodule::echo::_narrow(obj);
    }
    catch (const CORBA::SystemException &se) {
    std::cerr << "Cannot narrow reference" << std::endl;
    throw 0;
    }
    // Call the echo function
    std::cout << "Initial message is " << "Harry Potter" << std::endl;
    CORBA::String_var echostring = myecho->echostring("Harry Potter");
    // Print out the results
    std::cout << "Echoed message is " << echostring.in() << std::endl;
    // NOTE: There's a bug in the TAO destroy() method orb->destroy ();
  }
  catch (CORBA::Exception &) {
    std::cerr << "CORBA exception raised!" << std::endl;
  }
  return 0;
}


Listing Six
// File Server.cpp
#include "Echo_i.h"
#include "TAO\orbsvcs\orbsvcs\CosNamingC.h"
#include <iostream>

int main (int argc, char* argv[])
{
  try {
   // First initialize the ORB
   CORBA::ORB_var orb =
      CORBA::ORB_init (argc, argv,"");
   CORBA::Object_var poa_object = orb->resolve_initial_references ("RootPOA");
   PortableServer::POA_var poa = PortableServer::POA::_narrow (poa_object.in ());
   PortableServer::POAManager_var poa_manager = poa->the_POAManager ();
   poa_manager->activate ();
   // Create the servant
    echo_i servant;
   // Activate it to obtain the object reference
    echomodule::echo_var myobject=servant._this();
   // Get the Naming Context reference
   CORBA::Object_var naming_context_object =
      orb->resolve_initial_references ("NameService");
   CosNaming::NamingContext_var naming_context =
      CosNaming::NamingContext::_narrow (naming_context_object.in ());
   // Create and initialize the name.
   CosNaming::Name name (1);
   name.length (1);
   name[0].id = CORBA::string_dup ("echo");
   // Bind the object
   naming_context->bind (name, myobject.in ());
   orb->run ();
   // Destroy the POA, waiting until the destruction terminates
   //poa->destroy (1, 1);
   //orb->destroy ();
 }
 catch (CORBA::Exception &) {
    std::cerr << "CORBA exception raised!" << std::endl;
 }
 return 0;
}
// File Echo_i.cpp
#include "Echo_i.h"
#include <iostream>

echo_i::echo_i ()
{
}
char *
echo_i::echostring(const char * message) throw (CORBA::SystemException)
{
    std::cout << "Message in " << message << std::endl;
    char * smsg = CORBA::string_dup(message);
    std::cout << "Returning " << smsg << std::endl;
    return smsg;
}
// File Echo_i.h:
#ifndef TAO_ECHO_SERVER_I_H

#include "EchoS.h"
#include <string>

class echo_i : public POA_echomodule::echo {
public:
  echo_i ();
  char *echostring (const char * message)
      throw (echomodule::Invalid_Message);
private:
};
#endif /* TAO_ECHO_SERVER_I_H */


Listing Seven
// File Server.cpp
#include <time.h>
#include <iostream.h>
#include <stdio.h>
#include "server.h"
#include <mico/CosNaming.h>
char *
echo_impl::
echostring(const char *message) throw(CORBA::SystemException)
{
    printf("message is %s\n", message);
    char *smsg= strcat(CORBA::string_dup(message)," Grainger");
    printf("Returning %s\n", smsg);
    return smsg;
}
int
main(int argc, char * argv[])
{
    try
    {
        // Initialize orb
        CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
        // Get reference to Root POA.
        CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
        PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
        // Activate POA manager
        PortableServer::POAManager_var mgr = poa->the_POAManager();
        mgr->activate();
        // Create an object
        echo_impl time_servant;
        // Write its stringified reference to stdout
        echomodule::echo_var tm = time_servant._this();
        CORBA::String_var str = orb->object_to_string(tm);
        cout << str << endl;
     // Register server with Naming Service (nsd)
     {
          CORBA::Object_var obj;
          obj = orb->resolve_initial_references ("NameService");
          CosNaming::NamingContext_var inc;
          inc = CosNaming::NamingContext::_narrow (obj);
          CosNaming::Name name;
          name.length (1);
          name[0].id = CORBA::string_dup("echo");
          inc->bind (name, tm);
         }
         // Accept requests
         orb->run();
    }
    catch (const CORBA::Exception & e)
    {
        cerr << "CORBA exception: " << e << endl;
        return 1;
    }
    return 0;
}
// File server.h:
#ifndef server_HH_
#define server_HH_
#include "echo.h"

class echo_impl : public virtual POA_echomodule::echo {
    public:
        virtual char *  echostring(const char * message)
            throw(CORBA::SystemException);
};
#endif


Listing Eight
// File Client.cpp
#include "EchoC.h"
#include "TAO\orbsvcs\orbsvcs\CosNamingC.h"
#include <iostream>

int main (int argc, char* argv[])
{
  try
  {
    // First initialize the ORB
    CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "");
    // Get the naming context reference
    CORBA::Object_var naming_context_object = orb->string_to_object(argv[1]);
    CosNaming::NamingContext_var naming_context =
        CosNaming::NamingContext::_narrow (naming_context_object.in ());
    // Create and initialize the name.
    CosNaming::Name name (1);
    name.length (1);
    name[0].id = CORBA::string_dup ("echo sequence");
    // Bind the object
    CORBA::Object_var obj = naming_context->resolve (name);
    // Narrow the object
    echomodule::echo_var myecho;
    try
    {
        myecho=echomodule::echo::_narrow(obj);
    }
    catch (const CORBA::SystemException &se) {
    std::cerr << "Cannot narrow reference" << std::endl;
    throw 0;
    }
    // Initialize the sequence
    echomodule::echo::FloatSeq seq;
    seq.length(5);
    seq[0] = 0.0;
    seq[1] = (float)0.1;
    seq[2] = (float)0.2;
    seq[3] = (float)0.3;
    seq[4] = (float)0.4;
    std::cout << "seq[0] starts as " << seq[0] << std::endl;
    std::cout << "seq[1] starts as " << seq[1] << std::endl;
    std::cout << "seq[2] starts as " << seq[2] << std::endl;
    std::cout << "seq[3] starts as " << seq[3] << std::endl;
    std::cout << "seq[4] starts as " << seq[4] << std::endl;

    // Test the sequence function
    CORBA::String_var echostring = myecho->echoseq(seq);

    // Output the sequence
    std::cout << "Message is " << echostring.in() << std::endl;
    std::cout << "seq[0] is now " << seq[0] << std::endl;
    std::cout << "seq[1] is now " << seq[1] << std::endl;
    std::cout << "seq[2] is now " << seq[2] << std::endl;
    std::cout << "seq[3] is now " << seq[3] << std::endl;
    std::cout << "seq[4] is now " << seq[4] << std::endl;

    // NOTE: There's a bug in this code if the ORB is destroyed.
    // orb->destroy ();
  }
  catch (CORBA::Exception &) {
    std::cerr << "CORBA exception raised!" << std::endl;
  }
  return 0;
}


Listing Nine
// File Server.cpp
#include "Echo_i.h"
#include "TAO\orbsvcs\orbsvcs\CosNamingC.h"
#include <iostream>

int main (int argc, char* argv[])
{
  try {
    // First initialize the ORB
    CORBA::ORB_var orb =
      CORBA::ORB_init (argc, argv,"");
    // Create POA and activate it
    CORBA::Object_var poa_object=orb->resolve_initial_references ("RootPOA");
    PortableServer::POA_var poa =
                       PortableServer::POA::_narrow (poa_object.in ());
    PortableServer::POAManager_var poa_manager = poa->the_POAManager ();
    poa_manager->activate ();
    // Create the servant
    echo_i servant;
    // Activate it to obtain the object reference
    echomodule::echo_var myobject=servant._this();
    // Get the Naming Context reference
    CORBA::Object_var naming_context_object =
      orb->resolve_initial_references ("NameService");
    CosNaming::NamingContext_var naming_context =
      CosNaming::NamingContext::_narrow (naming_context_object.in ());
    // Create and initialize the name.
    CosNaming::Name name (1);
    name.length (1);
    name[0].id = CORBA::string_dup ("echo sequence");
    // Bind the object
    naming_context->bind (name, myobject.in ());
    orb->run ();
    // Destroy the POA, waiting until the destruction terminates
    poa->destroy (1, 1);
    orb->destroy ();
  }
  catch (CORBA::Exception &) {
    std::cerr << "CORBA exception raised!" << std::endl;
  }
  return 0;
}
// File Echo_i.cpp:
#include "Echo_i.h"
#include <iostream>

echo_i::echo_i (){}
char *
echo_i::echoseq(echomodule::echo::FloatSeq &seq)
                                    throw (CORBA::SystemException)
{
    CORBA::ULong seqlen = seq.length();
    seq[0] = (float)99.0;
    seq[1] = (float)99.1;
    seq[2] = (float)99.2;
    seq[3] = (float)99.3;
    seq[4] = (float)99.4;

    std::cout << "seq[0] is " << seq[0] << std::endl;
    std::cout << "seq[1] is " << seq[1] << std::endl;
    std::cout << "seq[2] is " << seq[2] << std::endl;
    std::cout << "seq[3] is " << seq[3] << std::endl;
    std::cout << "seq[4] is " << seq[4] << std::endl;

    char * message=CORBA::string_dup("Server Says Hi!");
    return message;
}
// File Echo_i.h:
#ifndef TAO_SEQUENCE_SERVER_ECHO_I_H
#define TAO_SEQUENCE_SERVER_ECHO_I_H

#include "EchoS.h"
#include <string>

class echo_i : public POA_echomodule::echo {
    public:
        echo_i ();
        char * echoseq (echomodule::echo::FloatSeq &seq)
            throw (echomodule::Invalid_Message);
    private:
};
#endif /* TAO_SEQUENCE_SERVER_ECHO_I_H */




