/*
  client.java

  Client for simple client/server CORBA application with file-based IORs.

  To run this example, prepare your environment as follows:

c:\ set ORB=c:\JOB-4.1.0\lib
c:\ set PATH=c:\jdk1.3.1\bin;c:\orbacus\bin;%PATH%
c:\ set CLASSPATH=.;%ORB%\OB.jar
c:\ jidl --package Hallo Hallo.idl
c:\ javac Hallo\*.java
c:\ java Hallo.client

  Author: Ingo Kloeckl
 */

package Hallo;

public class client {
    public static void main(String args[]){
	java.util.Properties props = System.getProperties();
	props.put("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB");
	props.put("org.omg.CORBA.ORBSingletonClass", "com.ooc.CORBA.ORBSingleton");
	org.omg.CORBA.ORB oORB = null;
	
	try {
	    oORB = org.omg.CORBA.ORB.init(args, props);
	} catch (org.omg.CORBA.SystemException e){
	    System.err.println("Error initializing ORB");
	    System.exit(1);
	}
	
	Hallo server = null;
	try {
	    org.omg.CORBA.Object obj = null;
	    try {
		String strRefFile = "HAL.ref";
		java.io.BufferedReader in = new java.io.BufferedReader(
								       new java.io.FileReader(strRefFile));
		String strRef = in.readLine();
		obj = oORB.string_to_object(strRef);
	    } catch (java.io.IOException e){
		System.err.println("Error reading IOR: "+e);
		System.exit(1);
	    }
	    if (obj==null){
		System.err.println("Invalid reference to server");
		System.exit(1);
	    }
	    server = HalloHelper.narrow(obj);
	    if (server==null){
		System.err.println("Reference is not a Hallo server");
		System.exit(1);
	    }
	} catch (org.omg.CORBA.TRANSIENT e){
	    System.err.println("server is not reachable: "+e);
	    System.exit(1);
	} catch (org.omg.CORBA.BAD_PARAM e){
	    System.err.println("incorrect IOR format: "+e);
	    System.exit(1);
	} catch (org.omg.CORBA.SystemException e){
	    System.err.println("System exception: "+e);
	    System.exit(1);
	}
	
	try {
	    // now use the server object
	    server.sayHi();
	    String strName = server.name();
	    System.out.println("The server's name is " + strName);
	    server.sayHi();
	    server.shutdown();
	} catch (org.omg.CORBA.SystemException e){
	    System.err.println("Error using server: "+e);
	    System.exit(1);
	}
	
	if (oORB != null){
	    try {
		oORB.destroy();
	    } catch (org.omg.CORBA.SystemException e){
		System.err.println("System exception: " + e);
	    }
	}
	
    }

}

