package com.wiley.compbooks.brose.chapter9.any;

import java.util.*;
import java.io.*;
import org.omg.CORBA.*;
import com.wiley.compbooks.brose.chapter9.HelloWorld.*;
import org.omg.CosNaming.*;

public class Client 
{

    public static void main(String args[]) 
    {

        AnyHolder any_time = new AnyHolder();
        Any any_locality;

        try 
        {
            // initialize the ORB.
            ORB orb = ORB.init( args, null );
	    
	    // get object reference ..
	    org.omg.CORBA.Object obj = null;

	    if( args.length > 0 )
	    {
                // get stringified IOR from command line
		String ior = new String( args[0] );
		obj = orb.string_to_object( ior );
	    }
	    else
	    {		
		NamingContextExt nc = NamingContextExtHelper.narrow( 
                       orb.resolve_initial_references("NameService"));		
		
		if( nc == null )
		    throw new RuntimeException("narrow failed");

		obj = nc.resolve( nc.to_name("any.example"));
	    }

            // and narrow it to GoodDay
            GoodDay goodDay = GoodDayHelper.narrow( obj );
	    
            if( goodDay == null )
            {
                System.err.println("narrow failed");
                System.exit( 1 );
            }

            // invoke the operation
            any_locality = goodDay.hello( any_time );

            // declare a type code object
            TypeCode tc;

            // get type of any_time.value and print type information
            tc = any_time.value.type();
            try 
            {
                System.out.println("IfRepId of any_time: " + tc.id() );
                System.out.println("Type code of any_time: " + tc.name() );

                for( int i = 0; i < tc.member_count(); i++ )
                    System.out.println("\tname: " + tc.member_name(i) ); 
            }
            catch(org.omg.CORBA.TypeCodePackage.BadKind ex_bk) 
            {
                System.err.println("any_time: " + ex_bk);
            }
            catch(org.omg.CORBA.TypeCodePackage.Bounds ex_b) 
            {
                System.err.println("any_time: " + ex_b);
            }
            
            // get length any_locality.value 
            tc = any_locality.type();

            try 
            {
		if( tc.kind() == TCKind.tk_string )
                    System.out.println( "length of any_locality: "
                                        + tc.length() );
                else
                    System.out.println("any_locality does NOT contain a string.");
            }
            catch(org.omg.CORBA.TypeCodePackage.BadKind ex_bt) 
            {
                System.err.println("any_locality: " + ex_bt);
            }

            // get String from any_locality
            String locality = any_locality.extract_string();

            // get struct from any_time
            Time time = TimeHelper.extract( any_time.value );

            // print results to stdout
            System.out.println("Hello World!");

            if( time.minute < 10 ) 
            {
                System.out.println("The local time in " +
                                   locality +
                                   " is " + time.hour + ":0" +
                                   time.minute + "." );
            }
            else
            {
                System.out.println("The local time in " +
                                   locality +
                                   " is " + time.hour + ":" +
                                   time.minute + "." );
            }
        }
        // catch CORBA system exceptions
        catch(UserException u) 
        {
            u.printStackTrace();
            System.err.println(u);
        }
        catch(SystemException ex) 
        {
            ex.printStackTrace();
            System.err.println(ex);
        }
    }
}

