Listener.java 6.0 KB

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