Interoperable ORB Example using IOR with CosNaming 
 

Downloads:

You will need JavaIDL to test the client

Getting Started:

Win95 system environment settings for JavaIDL:
 


REM
SET PATH=C:\jdk1.2beta4\bin;%path%
REM
 

Introduction:

This example extends the IDL from the previous pages, and also demonstrates Threading.

Ship.idl
 


// Ship.idl

#pragma prefix "Waveman"
module Ship {

  interface Aircraft {
    string codeNumber();
    void run();
    readonly attribute long fuelCapacity;
  };

  interface AircraftCarrier {
    Aircraft launch(in string name);
  };

};
 


Server.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Jan 1998

import java.io.*;
import org.omg.CosNaming.*;

public class Server 
{
   public static void main(String[] args)
   {
      org.omg.CORBA.ORB orb = null;
      try {
         orb = org.omg.CORBA.ORB.init(args, null);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("ORB init failure " + se); 
         System.exit(1); 
      }

      org.omg.CORBA.BOA boa = null;
      try {
         boa = orb.BOA_init();
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("BOA init failure " + se); 
         System.exit(1); 
      }

      // TIE uses these two new lines

      AircraftCarrierImpl new_carrier = new AircraftCarrierImpl(boa);
      Ship.AircraftCarrier carrier = new Ship._tie_AircraftCarrier(new_carrier, "Nimitz");

      try {
         boa.obj_is_ready(carrier);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Object Ready failure " + se); 
         System.exit(1); 
      }

      org.omg.CORBA.Object initRef = null;
      try {
         initRef = orb.resolve_initial_references("NameService");
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Resolve init failure " + se); 
         System.exit(1); 
      }
      catch (org.omg.CORBA.UserException ue) 
      { 
         System.err.println("Resolve init failure " + ue); 
         System.exit(1); 
      }

      NamingContext initContext = null;
      try {
         initContext = NamingContextHelper.narrow(initRef);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Context narrow failure " + se); 
         System.exit(1); 
      }

//    Write object reference to an IOR file so Orbix Client can access

      try 
      {
         FileWriter output = new FileWriter("ns.ior");
         output.write(orb.object_to_string(initRef));
         output.close();
         System.out.println("Wrote IOR to file: ns.ior");
      }
      catch(java.io.IOException e) 
      {
         System.out.println("Exception: " + e);
         System.exit(1);
      }

      NameComponent[] name = new NameComponent[1];
      NamingContext objContext = null;
      try {
         name[0] = new NameComponent("objects" , "");
         objContext = initContext.bind_new_context(name);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Bind init failure " + se); 
         System.exit(1); 
      }
      catch (org.omg.CORBA.UserException ue) 
      { 
         System.err.println("Bind init failure " + ue); 
         System.exit(1); 
      }

      NamingContext milContext = null;
      try {
         name[0] = new NameComponent("military" , "");
         milContext = objContext.bind_new_context(name);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Bind obj failure " + se); 
         System.exit(1); 
      }
      catch (org.omg.CORBA.UserException ue) 
      { 
         System.err.println("Bind obj failure " + ue); 
         System.exit(1); 
      }

      try {
         name[0] = new NameComponent("navy" , "");
         milContext.rebind(name, carrier);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Bind mil failure " + se); 
         System.exit(1); 
      }
      catch (org.omg.CORBA.UserException ue) 
      { 
         System.err.println("Bind mil failure " + ue); 
         System.exit(1); 
      }

      System.out.println(carrier + " ready for launch !!!");

      try {
         boa.impl_is_ready();
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Impl Ready failure " + se); 
         System.exit(1); 
      }
   }
}
 


Client.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Aug 1998

import java.io.*;
import org.omg.CosNaming.*;

public class Client
{
   public static void main(String[] args)
   {
      org.omg.CORBA.ORB orb = null;
      try {
          orb = org.omg.CORBA.ORB.init(args, null);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("ORB init failure " + se); 
         System.exit(1); 
      }
 

      org.omg.CORBA.Object initRef = null;
 

// Read IOR from file

      try 
      {
         LineNumberReader input = new LineNumberReader(new FileReader("ns.ior"));
         initRef = orb.string_to_object(input.readLine());
      }
      catch(java.io.IOException e) 
      {
         System.out.println("Exception: " + e);
         System.exit(1);
      }

// End IOR

      NamingContext initContext = null;
      try {
         initContext = NamingContextHelper.narrow(initRef);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Context narrow failure " + se); 
         System.exit(1); 
      }
 

      NameComponent[] name = new NameComponent[3];
      name[0] = new NameComponent("objects", "");
      name[1] = new NameComponent("military", "");
      name[2] = new NameComponent("navy", "");

      org.omg.CORBA.Object objRef = null;
      try {
         objRef = initContext.resolve(name);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Resolve name failure " + se); 
         System.exit(1); 
      }
      catch (org.omg.CORBA.UserException ue) 
      { 
         System.err.println("Resolve name failure " + ue); 
         System.exit(1); 
      }

      Ship.AircraftCarrier carrier = null;
      try {
      carrier = Ship.AircraftCarrierHelper.narrow(objRef);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("AircraftCarrier narrow failure " + se); 
         System.exit(1); 
      }

      // Standard program continues 

      String flight = args.length > 0 ? args[0]: "Ghost Rider 101";

      Ship.Aircraft aircraft = null;
      try {
      aircraft = carrier.launch(flight);
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Carrier launch failure " + se); 
         System.exit(1); 
      }

      String designation = null;
      try {
         designation = aircraft.codeNumber();
         System.out.println("Aircraft " + designation + " is airborne");
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Get name failure " + se); 
         System.exit(1); 
      }

      try {
         int fuel = aircraft.fuelCapacity();
         System.out.println ("Aircraft " + designation + " has " + fuel + " Pounds");
      }
      catch (org.omg.CORBA.SystemException se) 
      { 
         System.err.println("Get fuel failure " + se); 
         System.exit(1); 
      }
   }
}
 


AircraftCarrierImpl.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Jan 1998

import java.util.*;

public class AircraftCarrierImpl implements Ship.AircraftCarrierOperations
{

   // data member and constructor for TIE

   private org.omg.CORBA.BOA _boa;
   private Dictionary _aircrafts = new Hashtable();

   public AircraftCarrierImpl(org.omg.CORBA.BOA boa)
   {
      _boa = boa;
   }

   public synchronized Ship.Aircraft launch(String name)
   {
      Ship.Aircraft aircraft = (Ship.Aircraft) _aircrafts.get(name);

      if (aircraft == null)
      {
         AircraftImpl new_aircraft = new AircraftImpl(name);
         aircraft = new Ship._tie_Aircraft(new_aircraft);

         // TIE uses _boa. instead of _boa().

         _boa.obj_is_ready(aircraft);

         System.out.println(aircraft + " on Catapult 2");
         _aircrafts.put(name, aircraft);
      }
      return aircraft;
   }
}
 


AircraftImpl.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Jan 1998

public class AircraftImpl implements Ship.AircraftOperations, Runnable
{
   private String _codeNumber;
   private int _fuelCapacity;
   private static Thread engineThread;

   public void run()
   {
      System.out.println(_codeNumber + " fuel status = " + _fuelCapacity + " pounds");
      while (_fuelCapacity > 0)
      {
         try 
         {
            Thread.sleep(10000);
         }
         catch (InterruptedException e)
         {
            return;
         }
         _fuelCapacity = _fuelCapacity - 500;
         System.out.println(_codeNumber + " fuel status = " + _fuelCapacity + " pounds");
      }
   }
   public AircraftImpl(String codeNumber)
   {
      _codeNumber = codeNumber;
      _fuelCapacity = 10000;

      this.engineThread = new Thread(this);
      this.engineThread.start(); 
   }

   public String codeNumber()
   {
      return _codeNumber;
   }

   public int fuelCapacity()
   {
      return _fuelCapacity;
   }
}
 


Building the server with VisiBroker:

Create a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and AircraftImpl.java into it.
 


prompt> idl2java Ship.idl
prompt> vbjc Server.java
 

Building the client with OrbixWeb:

Place Client.java into the same directory as above
 


prompt> idl Ship.idl
prompt> owjavac Client.java java_output\Ship\*.java
 

Building the client with JavaIDL:

Place Client.java into the same directory as above
 


prompt> idltojava Ship.idl
prompt> javac Client.java
 

Running the program:

Three windows will be required

The flight deck of the USS Nimitz

Smart Agent and NameService in one window, server in one window, client in another
 


prompt> osagent
prompt> vbj -DJDKrenameBug -DORBservices=CosNaming com.visigenic.vbroker.services.CosNaming.ExtFactory objects navy.log

prompt> vbj -DORBservices=CosNaming -DSVCnameroot=objects Server

prompt> owjava Client

 or

prompt> java Client
 


Note:

It is usually easier to create two BAT files to contain the commands above.