You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 2.8KB

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