Coding client and server with the Name Service - CosNaming 
 

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);
      }

      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
// Jan 1998

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;
      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);
      }

      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 = aircraft.codeNumber();

      System.out.println("Aircraft " + designation + " is airborne");

      org.omg.CORBA.IntHolder alt = new org.omg.CORBA.IntHolder(10000);

      try {
         aircraft.attitude(alt, "headup");
         System.out.println(designation + " going up to " + alt.value + " Ft.");
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Carrier launch failure " + se);
         System.exit(1);
      }

      try {
         aircraft.attitude(alt, "headdown");
         System.out.println(designation + " going down to " + alt.value + " Ft.");
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Carrier launch failure " + se);
         System.exit(1);
      }

   }
}
 


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 VisiBroker:

Place Client.java into the same directory as above or another directory together with Ship.idl
 


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

Running the program:

Three windows will be required
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> vbj -DORBservices=CosNaming -DSVCnameroot=objects Client "SunDowner 102"
 


Note:

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