Listener.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import javax.net.ssl.SSLContext;
  7. import javax.net.ssl.SSLSocket;
  8. import javax.net.ssl.SSLSocketFactory;
  9. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  10. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  11. import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
  12. /**
  13. * Protocol listener class:<br>
  14. * Creates a Socket on the port of a given protocol and listens for incoming
  15. * connections.<br>
  16. * For each connection creates a Socket and instantiate an {@link Handler}.
  17. *
  18. * @author Mihai Plasoianu
  19. *
  20. */
  21. public class Listener implements Runnable {
  22. private ArrayList<Handler> handlers = new ArrayList<Handler>();
  23. private Protocol protocol;
  24. private ServerSocket server;
  25. private Thread thread;
  26. private int port;
  27. private Hostage service;
  28. private ConnectionRegister conReg;
  29. private boolean running = false;
  30. /**
  31. * Constructor for the class. Instantiate class variables.
  32. *
  33. * @param service
  34. * The Background service that started the listener.
  35. * @param protocol
  36. * The Protocol on which the listener is running.
  37. */
  38. public Listener(Hostage service, Protocol protocol) {
  39. this.service = service;
  40. this.protocol = protocol;
  41. port = protocol.getPort();
  42. conReg = new ConnectionRegister(service);
  43. }
  44. public Listener(Hostage service, Protocol protocol, int port) {
  45. this.service = service;
  46. this.protocol = protocol;
  47. this.port = port;
  48. conReg = new ConnectionRegister(service);
  49. }
  50. /**
  51. * Determines the amount of active handlers.
  52. *
  53. * @return The number of active handlers.
  54. */
  55. public int getHandlerCount() {
  56. return handlers.size();
  57. }
  58. /**
  59. * Return the port number on which the listener listening.
  60. *
  61. * @return Used port number.
  62. */
  63. public int getPort() {
  64. return port;
  65. }
  66. /**
  67. * Determine the name of the protocol the listener is running on.
  68. *
  69. * @return Name of the protocol
  70. */
  71. public String getProtocolName() {
  72. return protocol.toString();
  73. }
  74. /**
  75. * Determines if the service is running.
  76. *
  77. * @return True if the service is running, else false.
  78. */
  79. public boolean isRunning() {
  80. return running;
  81. }
  82. /**
  83. * Remove all terminated handlers from its internal ArrayList.
  84. */
  85. public void refreshHandlers() {
  86. for (Iterator<Handler> iterator = handlers.iterator(); iterator.hasNext();) {
  87. Handler handler = iterator.next();
  88. if (handler.isTerminated()) {
  89. conReg.closeConnection();
  90. iterator.remove();
  91. }
  92. }
  93. }
  94. @Override
  95. public void run() {
  96. while (!thread.isInterrupted()) {
  97. addHandler();
  98. }
  99. for (Handler handler : handlers) {
  100. handler.kill();
  101. }
  102. }
  103. /**
  104. * Starts the listener. Creates a server socket runs itself in a new Thread
  105. * and notifies the background service.
  106. */
  107. public boolean start() {
  108. try {
  109. server = new MyServerSocketFactory().createServerSocket(port);
  110. if (server == null)
  111. return false;
  112. (this.thread = new Thread(this)).start();
  113. running = true;
  114. service.notifyUI(this.getClass().getName(),
  115. new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(port) });
  116. return true;
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. return false;
  120. }
  121. }
  122. /**
  123. * Stops the listener. Closes the server socket, interrupts the Thread its
  124. * running in and notifies the background service.
  125. */
  126. public void stop() {
  127. try {
  128. server.close();
  129. thread.interrupt();
  130. running = false;
  131. service.notifyUI(this.getClass().getName(),
  132. new String[] { service.getString(R.string.broadcast_stopped), protocol.toString(), Integer.toString(port) });
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. /**
  138. * Waits for an incoming connection, accepts it and starts a
  139. * {@link Handler}
  140. */
  141. private void addHandler() {
  142. if (conReg.isConnectionFree()) {
  143. try {
  144. Socket client = server.accept();
  145. conReg.newOpenConnection();
  146. if (protocol.isSecure()) {
  147. startSecureHandler(client);
  148. } else {
  149. startHandler(client);
  150. }
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. }
  156. /**
  157. * Creates a new instance of an {@link Handler}.
  158. *
  159. * @param service
  160. * The background service
  161. * @param listener
  162. * The listener that created the handler
  163. * @param protocol
  164. * The Protocol the handler will run on
  165. * @param client
  166. * The Socket the handler uses
  167. * @return A Instance of a {@link Handler} with the specified
  168. * parameter.
  169. */
  170. private Handler newInstance(Hostage service, Listener listener, Protocol protocol, Socket client) {
  171. return new Handler(service, listener, protocol, client);
  172. }
  173. /**
  174. * Starts a {@link Handler} with the given socket.
  175. *
  176. * @param client
  177. * The socket with the accepted connection.
  178. * @throws Exception
  179. */
  180. private void startHandler(Socket client) throws Exception {
  181. handlers.add(newInstance(service, this, protocol.getClass().newInstance(), client));
  182. }
  183. /**
  184. * Creates a SSLSocket out of the given socket and starts a
  185. * {@link Handler}.
  186. *
  187. * @param client
  188. * The socket with the accepted connection.
  189. * @throws Exception
  190. */
  191. private void startSecureHandler(Socket client) throws Exception {
  192. SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext();
  193. SSLSocketFactory factory = sslContext.getSocketFactory();
  194. SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null, client.getPort(), false);
  195. sslClient.setUseClientMode(false);
  196. handlers.add(newInstance(service, this, protocol.getClass().newInstance(), sslClient));
  197. }
  198. }