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 6.3KB

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