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.

ServerApplication.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.uqac.rthoni.java_rmi.server;
  2. import com.uqac.rthoni.java_rmi.server.executors.AbstractCommandExecutor;
  3. import com.uqac.rthoni.java_rmi.common.Command;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.InetAddress;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10. import java.util.Vector;
  11. /**
  12. * Created by robin on 9/15/16.
  13. */
  14. public class ServerApplication {
  15. private Vector<AbstractCommandExecutor> _executors = null;
  16. public static String ipToString(InetAddress ip) {
  17. String ipString = ip.toString();
  18. if (!ipString.isEmpty() && ipString.startsWith("/")) {
  19. ipString = ipString.substring(1);
  20. }
  21. return ipString;
  22. }
  23. public static String getFullIp(InetAddress ip) {
  24. try {
  25. InetAddress address = InetAddress.getByName(ip.toString().split("/")[1]);
  26. return String.format("%s/%s", address.getCanonicalHostName(), ipToString(ip));
  27. }
  28. catch (Exception e) {
  29. return ipToString(ip);
  30. }
  31. }
  32. public void run(int port, String sourceDir, String classDir, String logFile)
  33. {
  34. try {
  35. loadExecutors();
  36. } catch (ClassNotFoundException e) {
  37. System.err.format("Failed to load executors: %s\n", e.getMessage());
  38. System.exit(1);
  39. }
  40. String host = "0.0.0.0";
  41. InetAddress ip = null;
  42. try {
  43. ip = InetAddress.getByName(host);
  44. }
  45. catch (Exception e) {
  46. }
  47. if (ip == null) {
  48. System.err.format("Failed to resolve '%s'\n", host);
  49. System.exit(2);
  50. }
  51. String ipString = ipToString(ip);
  52. try {
  53. ServerSocket serverSocket = new ServerSocket(port, 1, ip);
  54. System.out.format("Listening for clients on %s:%d...\n", ipString, port);
  55. runServer(serverSocket);
  56. } catch (IOException e) {
  57. System.err.format("Failed to listen on %s:%d: %s\n", ipString, port, e.getMessage());
  58. System.exit(3);
  59. }
  60. }
  61. private void loadExecutors() throws ClassNotFoundException
  62. {
  63. _executors = new Vector<>();
  64. Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
  65. for (Class c : classes) {
  66. try {
  67. AbstractCommandExecutor executor = (AbstractCommandExecutor)c.newInstance();
  68. _executors.add(executor);
  69. } catch (Exception e) {
  70. System.err.format("Failed to load executor '%s': %s\n", c.getName(), e.getMessage());
  71. }
  72. }
  73. }
  74. private void runServer(ServerSocket serverSocket)
  75. {
  76. while (true) {
  77. Socket client = null;
  78. try {
  79. client = serverSocket.accept();
  80. }
  81. catch (Exception e) {
  82. System.err.format("Failed to accept client: %s\n", e.getMessage());
  83. }
  84. if (client != null) {
  85. String ipString = getFullIp(client.getInetAddress());
  86. System.out.format("New client: %s:%d\n", ipString, client.getPort());
  87. try {
  88. handleClient(client);
  89. } catch (Exception e) {
  90. System.out.format("Error when handling client: %s\n", e.getMessage());
  91. }
  92. try {
  93. client.close();
  94. } catch (Exception e) {
  95. }
  96. System.out.format("Client disconnected: %s:%d\n", ipString, client.getPort());
  97. }
  98. }
  99. }
  100. private void handleClient(Socket client)
  101. {
  102. String str;
  103. try {
  104. BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  105. str = in.readLine();
  106. } catch (IOException e) {
  107. System.err.format("Failed to read from client: %s", e.getMessage());
  108. return;
  109. }
  110. Command command = Command.fromString(str);
  111. if (command == null) {
  112. System.err.format("Failed to deserialize command: %s\n", str);
  113. }
  114. else {
  115. System.out.format("Received command: %s\n", command.getCommandName());
  116. handleCommand(command, client);
  117. }
  118. }
  119. private AbstractCommandExecutor getExecutor(Command command)
  120. {
  121. for (AbstractCommandExecutor executor : _executors) {
  122. if (executor.getCommandName().equals(command.getCommandName())) {
  123. return executor;
  124. }
  125. }
  126. return null;
  127. }
  128. private void handleCommand(Command command, Socket client)
  129. {
  130. String data;
  131. AbstractCommandExecutor executor = getExecutor(command);
  132. if (executor == null) {
  133. data = String.format("Unknown command '%s'", command.getCommandName());
  134. System.err.println(data);
  135. }
  136. else {
  137. try {
  138. data = executor.run(command);
  139. } catch (Exception e) {
  140. data = String.format("Error when handling command: %s", e.getMessage());
  141. System.err.println(data);
  142. }
  143. }
  144. try {
  145. client.getOutputStream().write(String.format("%s\n", data).getBytes());
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. }