//     $Id: Server.java,v 3.6 1998/12/14 19:06:48 smg9c Exp $    
 /////////////////////////////////////////////////////////////////////
 //
 // File: Server.java
 //
 // Purpose: The Server class is responsible for starting the GUI,
 //          and for passing user-triggered events to the GUI.
 //
 // Authors: Steve Geist smg9c@cs.virginia.edu
 // 
 // Modifications:
 //
 //   14-DEC-1998 smg          Added method comments for ShutDown and 
 //                            removed redundant print in Error method
 //
 //   11-DEC-1998 smg          Added shut down method
 //
 //   08-DEC-1998 smg          Modified to take port on command line
 //
 //   04-DEC-1998 smg          Modified Error method to print message
 //
 //   04-DEC-1998 smg          Changed comments to style guide format
 //
 //   19-NOV-1998 smg          Added Error method to print stack trace of
 //                            exceptions
 //
 //   18-NOV-1998 smg          Added user input of map file name if nothing is
 //                            passed on the command line
 //
 //   17-NOV-1998 smg          Added passing of command line parameter to 
 //                            ServerEngine constructor
 //
 //   09-NOV-1998 smg          Removed "\n" from print command
 //
 //   09-NOV-1998 smg          Added additional comments and corrected
 //                            date format
 //
 //   06-NOV-1998 rgb          Added version control header info
 //
 //   05-NOV-1998 smg          Deleted Execute() functions to work with new
 //                            program structure of ServerEngine
 //
 //   04-NOV-1998 smg          Added comments
 //
 //   02-NOV-1998 smg          Initial creation
 //
 /////////////////////////////////////////////////////////////////////
 import java.io.*;
 import java.lang.*;
 public class Server {
   private ServerEngine engine;
   ///////////////////////////////////////////////////////////////////
   //
   // Method:  main
   //
   // Purpose: main checks for a command line argument for the file name, 
   //          if not given it will prompt for one.  Then it instantiates 
   //          a copy of Server.
   //   
   ///////////////////////////////////////////////////////////////////
   public static void main(String[] args)
     throws java.io.IOException
   {
     // Check to see if map file name and port number were given on the 
     // command line
     if(args.length >= 2) {
       Integer port = new Integer(args[1]);
       Server s = new Server(args[0], port.intValue());
     }
     else {
       // Else check if the file name was given on the command line
       if(args.length == 1) {
     Server s = new Server(args[0], 0);
       }
       else {
     // Else read in the file name and port number
     String file_name;
     System.out.println("Please type the name of the map file");
     BufferedReader keyboard =
       new BufferedReader(new InputStreamReader(System.in));
     file_name = keyboard.readLine();
     Server s = new Server(file_name, 0);
       }
     }
   }
   ///////////////////////////////////////////////////////////////////
   //
   // Method:  Server [constructor]
   //
   // Purpose: Calls the constructor for ServerEngine passing it the file
   //          name and reference to itself.
   //   
   ///////////////////////////////////////////////////////////////////
   public Server(String file_name, int port) {
     engine = new ServerEngine(this, file_name, port);
   }
   ///////////////////////////////////////////////////////////////////
   //
   // Method:  PrintMessage
   //
   // Invoker: ServerEngine
   //
   // Purpose: PrintMessage prints the indicated string to the screen.
   //   
   ///////////////////////////////////////////////////////////////////
   public void PrintMessage(String s) {
     System.out.println(s);
   }
   ///////////////////////////////////////////////////////////////////
   //
   // Method:  Error
   //
   // Invoker: ServerEngine
   //
   // Purpose: Error method takes an exception, prints the message, and 
   //          prints a stack trace.
   //   
   ///////////////////////////////////////////////////////////////////
   public void Error (Exception e) {
     System.out.println ("\n");
     e.printStackTrace ();
   }
   ///////////////////////////////////////////////////////////////////
   //
   // Method:  ShutDown
   //
   // Invoker: ServerEngine
   //
   // Purpose: Stops the execution of the application.
   //
   ///////////////////////////////////////////////////////////////////
   public void ShutDown() {
     PrintMessage("Server shutting down");
     Runtime r = Runtime.getRuntime();
     r.exit(0);
   }
 }