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

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