Listener.java 5.9 KB

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