//#
//# Corba Time Client
//# $Revision: 1.1 $
//# Copyright 2004 by Eric Y. Theriault
//# All Rights Reserved.
//# http://www.eyt.ca/CORBA
//#
package ca.eyt.corba;

/**
 * Implements a class to acquire the Time from a CORBA server.
 *
 * @author Eric Y. Theriault <eric@eyt.ca>
 */
public class TimeClient3 {
   /**
    * Port to multicast on.  This number is random. 
    */
   public static final int MulticastPort = 7007; 

   /**
    * Address to multicast to.  The default is link-local, which is
    * generally OK, but in the presence of a segmented network with
    * routers, this address and the TTL will have to be changed.
    */
   public static final String MulticastAddress = "224.0.0.1";

   /**
    * This is the message that is multicasted to discover the new
    * server.
    */
   public static final String MulticastDiscoverServer = "DiscoverIOR";

   /**
    * main
    *
    * @param args Currently forwarded directly to ORB.init.
    */
   public static void main( String args[] ) {
      try {
         // Create and Initialize the ORB
         org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( args, null );

         // Multicast for the Nameservice IOR
         String ior = null;
         {
            // Please note that a real implementation would provide more
            // options, and possibly share this with the server.  This
            // is only meant to show the functionality.
            java.net.MulticastSocket mcast =
                              new java.net.MulticastSocket();
            mcast.setSoTimeout( 1000 );
            mcast.send(
                        new java.net.DatagramPacket(
                                     MulticastDiscoverServer.getBytes(),
                                     MulticastDiscoverServer.length(),
                                     java.net.InetAddress.getByName(
                                                        MulticastAddress
                                                                   ),
                                     MulticastPort
                                                  )
                      );
            try {
               while ( true ) {
                  // Acquire a packet. 
                  byte[] buffer = new byte[1000];
                  java.net.DatagramPacket receive =
                              new java.net.DatagramPacket(
                                                          buffer, 
                                                          buffer.length
                                                         );      
                  mcast.receive( receive );

                  // Create a received String
                  ior = new String( buffer, 0, receive.getLength() );
                  if ( ior.startsWith( MulticastDiscoverServer ) == false ) {
                     break;
                  }
               }
            } catch ( java.net.SocketTimeoutException ste ) {
               System.err.println( "Unable to discover the IOR via " +
                                   "Multicast; Is the Multicast server " +
                                   "running?" );
               System.exit( 1 );
            }
         }

         // Get the root naming context.
         org.omg.CORBA.Object objectReference = orb.string_to_object( ior );

         org.omg.CosNaming.NamingContextExt ncRef =
                       org.omg.CosNaming.NamingContextExtHelper.narrow(
                                                         objectReference
                                                                      );

         // Resolve a reference into an object.
         String name = "Time";
         org.omg.CORBA.Object object = ncRef.resolve_str( name );
         ca.eyt.corba.corba.Time timeImpl =
                         ca.eyt.corba.corba.TimeHelper.narrow( object );


         // OK.  Let's call the Time interface's methods.
         ca.eyt.corba.corba.TimeOfDay time = timeImpl.getTime();
         System.out.println( "Time server's time is: " +
                             time.hour + ":" + time.minute + ":" +
                             time.second );
      } catch ( Throwable t ) {
         System.err.println( "Exception raised: " );
         t.printStackTrace();
      }
   }
};

