|  | @@ -1,17 +1,81 @@
 | 
		
	
		
			
			| 1 | 1 |  package com.uqac.rthoni.java_rmi;
 | 
		
	
		
			
			| 2 | 2 |  
 | 
		
	
		
			
			| 3 |  | -import com.uqac.rthoni.com.java_rmi.client.ClientTestClass;
 | 
		
	
		
			
			| 4 |  | -import com.uqac.rthoni.com.java_rmi.server.ServerTestClass;
 | 
		
	
		
			
			|  | 3 | +import java.io.PrintStream;
 | 
		
	
		
			
			| 5 | 4 |  
 | 
		
	
		
			
			| 6 | 5 |  /**
 | 
		
	
		
			
			| 7 | 6 |   * Created by robin on 9/15/16.
 | 
		
	
		
			
			| 8 | 7 |   */
 | 
		
	
		
			
			| 9 | 8 |  public class Main {
 | 
		
	
		
			
			|  | 9 | +    public static void usage(String name, boolean error)
 | 
		
	
		
			
			|  | 10 | +    {
 | 
		
	
		
			
			|  | 11 | +        PrintStream printStream = (error ? System.err : System.out);
 | 
		
	
		
			
			|  | 12 | +
 | 
		
	
		
			
			|  | 13 | +        printStream.println("Usage:");
 | 
		
	
		
			
			|  | 14 | +        printStream.println(String.format("%1s server port source_dir class_dir log_file", name));
 | 
		
	
		
			
			|  | 15 | +        printStream.println("\tPut the program in server mode");
 | 
		
	
		
			
			|  | 16 | +        printStream.println("\tport: the port to listen on [1-65535]");
 | 
		
	
		
			
			|  | 17 | +        printStream.println("\tsource_dir: the folder to search for java files");
 | 
		
	
		
			
			|  | 18 | +        printStream.println("\tclass_dir: the folder to build java files");
 | 
		
	
		
			
			|  | 19 | +        printStream.println("\tlog_file: the file to log events");
 | 
		
	
		
			
			|  | 20 | +        printStream.println(String.format("%1s client hostname port input_file output_file", name));
 | 
		
	
		
			
			|  | 21 | +        printStream.println("\tPut the program in client mode");
 | 
		
	
		
			
			|  | 22 | +        printStream.println("\thost: the remote server ip or hostname");
 | 
		
	
		
			
			|  | 23 | +        printStream.println("\tport: the port to connect on [1-65535]");
 | 
		
	
		
			
			|  | 24 | +        printStream.println("\tinput_file: the command file to use");
 | 
		
	
		
			
			|  | 25 | +        printStream.println("\toutput_file: the file to output command results");
 | 
		
	
		
			
			|  | 26 | +        printStream.println(String.format("%1s --help", name));
 | 
		
	
		
			
			|  | 27 | +        printStream.println(String.format("%1s -h", name));
 | 
		
	
		
			
			|  | 28 | +        printStream.println("\tPrint this help and exit");
 | 
		
	
		
			
			|  | 29 | +
 | 
		
	
		
			
			|  | 30 | +        if (error) {
 | 
		
	
		
			
			|  | 31 | +            System.exit(64);
 | 
		
	
		
			
			|  | 32 | +        }
 | 
		
	
		
			
			|  | 33 | +    }
 | 
		
	
		
			
			|  | 34 | +
 | 
		
	
		
			
			|  | 35 | +    private static int getPort(String name, String str) {
 | 
		
	
		
			
			|  | 36 | +        int port = 0;
 | 
		
	
		
			
			|  | 37 | +        try {
 | 
		
	
		
			
			|  | 38 | +            port = Integer.parseInt(str);
 | 
		
	
		
			
			|  | 39 | +        } catch (Exception e) {
 | 
		
	
		
			
			|  | 40 | +            usage(name, true);
 | 
		
	
		
			
			|  | 41 | +        }
 | 
		
	
		
			
			|  | 42 | +        if (port < 1 || port > 65535) {
 | 
		
	
		
			
			|  | 43 | +            usage(name, true);
 | 
		
	
		
			
			|  | 44 | +        }
 | 
		
	
		
			
			|  | 45 | +        return port;
 | 
		
	
		
			
			|  | 46 | +    }
 | 
		
	
		
			
			|  | 47 | +
 | 
		
	
		
			
			| 10 | 48 |      public static void main(String[] args)
 | 
		
	
		
			
			| 11 | 49 |      {
 | 
		
	
		
			
			| 12 |  | -        ClientTestClass t2;
 | 
		
	
		
			
			| 13 |  | -        ServerTestClass t3;
 | 
		
	
		
			
			| 14 |  | -        System.out.println("Hello World!");
 | 
		
	
		
			
			|  | 50 | +        String name = "java-rmi";
 | 
		
	
		
			
			|  | 51 | +        for (String arg : args) {
 | 
		
	
		
			
			|  | 52 | +            if (arg.equals("--help") || arg.equals("-h")) {
 | 
		
	
		
			
			|  | 53 | +                usage(name, false);
 | 
		
	
		
			
			|  | 54 | +            }
 | 
		
	
		
			
			|  | 55 | +        }
 | 
		
	
		
			
			|  | 56 | +        if (args.length != 5) {
 | 
		
	
		
			
			|  | 57 | +            usage(name, true);
 | 
		
	
		
			
			|  | 58 | +        }
 | 
		
	
		
			
			|  | 59 | +        String mode = args[0];
 | 
		
	
		
			
			|  | 60 | +        if (mode.equals("server")) {
 | 
		
	
		
			
			|  | 61 | +            int port = getPort(name, args[1]);
 | 
		
	
		
			
			|  | 62 | +            String sourceDir = args[2];
 | 
		
	
		
			
			|  | 63 | +            String classDir = args[3];
 | 
		
	
		
			
			|  | 64 | +            String logFile = args[4];
 | 
		
	
		
			
			|  | 65 | +
 | 
		
	
		
			
			|  | 66 | +            System.out.println("Hello World! server");
 | 
		
	
		
			
			|  | 67 | +        }
 | 
		
	
		
			
			|  | 68 | +        else if (mode.equals("client")) {
 | 
		
	
		
			
			|  | 69 | +            String host = args[1];
 | 
		
	
		
			
			|  | 70 | +            int port = getPort(name, args[2]);
 | 
		
	
		
			
			|  | 71 | +            String inputFile = args[3];
 | 
		
	
		
			
			|  | 72 | +            String outputFile = args[4];
 | 
		
	
		
			
			|  | 73 | +
 | 
		
	
		
			
			|  | 74 | +            System.out.println("Hello World! client");
 | 
		
	
		
			
			|  | 75 | +        }
 | 
		
	
		
			
			|  | 76 | +        else {
 | 
		
	
		
			
			|  | 77 | +            usage(name, true);
 | 
		
	
		
			
			|  | 78 | +        }
 | 
		
	
		
			
			| 15 | 79 |          System.exit(0);
 | 
		
	
		
			
			| 16 | 80 |      }
 | 
		
	
		
			
			| 17 | 81 |  }
 |