Listener.java 6.0 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()
  115. .getSharedPreferences(Hostage.getContext().getString(R.string.connection_info), Hostage.MODE_PRIVATE)
  116. .getString(Hostage.getContext().getString(R.string.connection_info_internal_ip), ""));
  117. }
  118. (this.thread = new Thread(this)).start();
  119. running = true;
  120. service.notifyUI(this.getClass().getName(),
  121. new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(port) });
  122. return true;
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. return false;
  126. }
  127. }
  128. /**
  129. * Stops the listener. Closes the server socket, interrupts the Thread its
  130. * running in and notifies the background service.
  131. */
  132. public void stop() {
  133. try {
  134. server.close();
  135. thread.interrupt();
  136. running = false;
  137. service.notifyUI(this.getClass().getName(),
  138. new String[] { service.getString(R.string.broadcast_stopped), protocol.toString(), Integer.toString(port) });
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. /**
  144. * Waits for an incoming connection, accepts it and starts a {@link Handler}
  145. */
  146. private void addHandler() {
  147. if (conReg.isConnectionFree()) {
  148. try {
  149. Socket client = server.accept();
  150. ConnectionGuard.registerConnection();
  151. conReg.newOpenConnection();
  152. if (protocol.isSecure()) {
  153. startSecureHandler(client);
  154. } else {
  155. startHandler(client);
  156. }
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. }
  162. /**
  163. * Creates a new instance of an {@link Handler}.
  164. *
  165. * @param service
  166. * The background service
  167. * @param listener
  168. * The listener that created the handler
  169. * @param protocol
  170. * The Protocol the handler will run on
  171. * @param client
  172. * The Socket the handler uses
  173. * @return A Instance of a {@link Handler} with the specified parameter.
  174. */
  175. private Handler newInstance(Hostage service, Listener listener, Protocol protocol, Socket client) {
  176. return new Handler(service, listener, protocol, client);
  177. }
  178. /**
  179. * Starts a {@link Handler} with the given socket.
  180. *
  181. * @param client
  182. * The socket with the accepted connection.
  183. * @throws Exception
  184. */
  185. private void startHandler(Socket client) throws Exception {
  186. handlers.add(newInstance(service, this, protocol.getClass().newInstance(), client));
  187. }
  188. /**
  189. * Creates a SSLSocket out of the given socket and starts a {@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. }