Listener.java 6.3 KB

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