
import tauzaman.*;

import java.net.URL;
import java.rmi.*;

import java.io.IOException;

/**
* This is an example program, which uses TauZaman System to
* declare two remote TauZaman services. And it uses this remote
* service to load an american Calendric System and an islamic 
* Calendric System. And then program creates one Instant by using
* american Calendric System and prints out this Instant as string 
* again. Then it gets this string and forms an Instant by using 
* islamic Calendric System.
*/

public class RemoteTauZaman{

  /* note that main method throws only IOexception */
  public static void main(String args[]) throws IOException{

      /***************BEGINNING OF INITIALIZATION AND CONFIGURATION PHASE***************/

      /* get TauZamanSystem via a static method */
      TauZamanSystem tzs = TauZamanSystem.getTauZamanSystem();
      
      String urlOfAmericanServer, urlOfIslamicServer;
      urlOfAmericanServer = "dyreson2.eecs.wsu.edu";
      urlOfIslamicServer = "nif-c10.eecs.wsu.edu";

      URL urlOfDefaultProperties, urlOfAmericanCalendricSystem, urlOfIslamicCalendricSystem;

      /* form URLs that we are interested */
      try{
          urlOfDefaultProperties = new URL("http://www.eecs.wsu.edu/~burgun/files/defaultProperties.xml");
          urlOfAmericanCalendricSystem = new URL("http://www.eecs.wsu.edu/~burgun/files/americanv1.xml");
          urlOfIslamicCalendricSystem = new URL("http://www.eecs.wsu.edu/~burgun/files/islamicv1.xml");
      }
      catch(MalformedURLException mue){
          // take corresponding action
          System.exit(1);
      }

      /* declare two remote TauZamanServices */
      TauZamanRemoteService remoteServiceAmerican, remoteServiceIslamic = null, ;


      try{
          /* initialize services */          
          remoteServiceIslamic = tzs.getRemoteService(urlOfIslamicServer, urlOfIslamicCalendricSystem, 
                                                                              urlOfDefaultProperties));
          /* this service will be active service */
          remoteServiceAmerican = tzs.getRemoteService(urlOfAmericanServer, urlOfAmericanCalendricSystem, 
                                                                              urlOfDefaultProperties));
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains causes.
          System.exit(1);
      }
      catch(RemoteException re){
          //analyze the exception, re contains causes.
          System.exit(1);
      }

      /***************END OF INITIALIZATION AND CONFIGURATION PHASE***************/


      /* declare temporal data types, we'll use */
      Instant i = null, k = null;

      String temporalConstant;

      try{ 

         /* form an Instant */
         i = new Instant("September, 17, 2003");

         /* output it */
         temporalConstant = i.output();
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
       
      try{
          tzs.setActiveService(remoteServiceIslamic);
      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
  
      try{

         /* form an Instant */
         k = new Instant(temporalConstant);
                  
         System.out.println(k.output()); // or System.out.println(Output.formatInstant(k));

      }
      catch(TauZamanException tze){
          //analyze the exception, tze contains message and possible causes.
          System.exit(1);
      }
      
  }

}

