/*
 * Client to test the passing of arrays of complex types, showing the
 * use of in/inout/out parameters and Holders
 *
 * Note that IDL sequences map to Java arrays
 */

import java.io.*;

public class Client  {

    public static String s(seqOfComplex.BasicStruct[] s) {
        if (null == s) return "NULL";
        String r = "";            
        for (int i=0; i<s.length; i++)
            r = r+s[i].toString()+ "\n";
        return r;
    }
    

    public static void main(String[] args) {
        try {
            //Initialize the ORB
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

            String ior;
            if( args.length == 1 )
            {
                // get object reference from command-line argument
                ior = args[0] ;
            }
            else
            {
                // get object reference from file
                ior = new BufferedReader( new FileReader("ior")).readLine();
            }

            org.omg.CORBA.Object obj = orb.string_to_object( ior );

            // and narrow i
            seqOfComplex.SeqOfComplexTest seqOfComplexTest =
                seqOfComplex.SeqOfComplexTestHelper.narrow( obj );

            // check if stringified IOR is of the right type
            if( seqOfComplexTest == null ) 
            {
                 System.err.println("stringified IOR is not of expected type");
                 System.exit( 1 );
            }

			
            //Initialize the three parameters
            //	Create the array of BasicStrut's and initialize
            seqOfComplex.BasicStruct[] inSeqOfComplex = 
                new seqOfComplex.BasicStruct[2];
            inSeqOfComplex[0]= 
                new seqOfComplex.BasicStruct(5, "fromClient");
            inSeqOfComplex[1]= 
                new seqOfComplex.BasicStruct(5, "fromClient");
            
            //	Create holder for the array, using the same array
            //	created for the in parameter
            seqOfComplex.BasicStructSeqHolder inoutSeqOfComplex = 
                new seqOfComplex.BasicStructSeqHolder(inSeqOfComplex);
            
            //	Create the holder for the out param
            seqOfComplex.BasicStructSeqHolder outSeqOfComplex = 
                new seqOfComplex.BasicStructSeqHolder();
            

            //For observation, print out the parameters
            //before making the call
            System.out.println("Before calling testSeqOfComplex\n" + 
                       "in param:\n " + s(inSeqOfComplex) + "\n\n" + 
                       "inout param:\n " + s(inoutSeqOfComplex.value)+ "\n\n" + 
                       "out param:\n " + s(outSeqOfComplex.value));
            
            //Call the remote method with the in, inout, and out parameters
            int returnValue = seqOfComplexTest.testSeqOfComplex(
                                            inSeqOfComplex,
                                            inoutSeqOfComplex,
                                            outSeqOfComplex);

            //For observation, print out the parameters after making the call
            System.out.println("\n\nAfter calling testSeqOfComplex\n" + 
                      "in param:\n " + s(inSeqOfComplex) + "\n\n" + 
                      "inout param:\n " + s(inoutSeqOfComplex.value) + "\n\n" + 
                      "out param:\n " + s(outSeqOfComplex.value));
        }
        catch(org.omg.CORBA.SystemException ex) {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            System.err.println(ex);
            ex.printStackTrace();
        }

    }
}

