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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.uqac.rthoni.java_rmi;
  2. import com.uqac.rthoni.java_rmi.client.ClientApplication;
  3. import com.uqac.rthoni.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.format("%s server port source_dir class_dir log_file\n", 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.format("%s client hostname port input_file output_file\n", 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.format("%s --help\n", name);
  26. printStream.format("%s -h\n", 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 app = new ServerApplication();
  62. app.run(port, sourceDir, classDir, logFile);
  63. }
  64. else if (mode.equals("client")) {
  65. String host = args[1];
  66. int port = getPort(name, args[2]);
  67. String inputFile = args[3];
  68. String outputFile = args[4];
  69. ClientApplication app = new ClientApplication();
  70. app.run(host, port, inputFile, outputFile);
  71. }
  72. else {
  73. usage(name, true);
  74. }
  75. System.exit(0);
  76. }
  77. }