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

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