Browse Source

Merge remote-tracking branch 'origin/master'

lp-tu 10 years ago
parent
commit
cfcee44626

+ 72 - 0
src/de/tudarmstadt/informatik/hostage/ConnectionRegister.java

@@ -0,0 +1,72 @@
+package de.tudarmstadt.informatik.hostage;
+
+/**
+ *  Saves the amount of active connections and limits them to a specific number.  
+ * @author Wulf Pfeiffer
+ */
+public class ConnectionRegister {
+	
+	/** Maximum connections that are allowed to be open. */
+	private static int maxConnections = 2;
+	/** Active connections . */
+	private static int openConnections = 0;
+	
+	/** 
+	 * Returns the maximum number of active connections.
+	 * @return maximum number of active connections.
+	 */
+	public static int getMaxConnections() {
+		return maxConnections;
+	}
+	
+	/**
+	 * Sets the maximum number of active connections.
+	 * @param maxConnections the new maximum.
+	 */
+	public static void setMaxConnections(int maxConnections) {
+		ConnectionRegister.maxConnections = maxConnections;
+	}
+	
+	/**
+	 * Returns the number of active connections.
+	 * @return number of active connections.
+	 */
+	public static int getOpenConnections() {
+		return openConnections;
+	}
+	
+	/**
+	 * Returns if there are new connections allowed or not.
+	 * @return true if a new connection is allowed, else false.
+	 */
+	public static boolean isConnectionFree() {
+		return openConnections < maxConnections;
+	}
+	
+	/**
+	 * Registers a new active connection if there are connections allowed.
+	 * @return true if a new connection has been successfully registered, else false.
+	 */
+	public static boolean newOpenConnection() {
+		if(isConnectionFree()) {
+			openConnections++;
+			return true;
+		} else {
+			return false;
+		}
+	}
+	
+	/**
+	 * Deregisters a active connection if at least one active connection is registered.
+	 * @return true if the connection has been successfully unregistered, else false.
+	 */
+	public static boolean closeConnection() {
+		if(openConnections > 0) {
+			openConnections--;
+			return true;
+		} else {
+			return false;
+		}
+	}
+	
+}

+ 17 - 14
src/de/tudarmstadt/informatik/hostage/HoneyListener.java

@@ -141,21 +141,24 @@ public class HoneyListener implements Runnable {
 	 * Waits for an incoming connection, accepts it and starts a {@link AbstractHandler}
 	 */
 	private void addHandler() {
-		try {
-			Socket client = server.accept();
-			if (protocol.isSecure()) {
-				startSecureHandler(client);
-			} else {
-				startHandler(client);
+		if(ConnectionRegister.isConnectionFree()) {
+			try {
+				Socket client = server.accept();
+				ConnectionRegister.newOpenConnection();
+				if (protocol.isSecure()) {
+					startSecureHandler(client);
+				} else {
+					startHandler(client);
+				}
+				int handlerCount = pref.getInt(protocol
+						+ MainActivity.HANDLER_COUNT, 0);
+				editor.putInt(protocol + MainActivity.HANDLER_COUNT,
+						handlerCount + 1);
+				editor.commit();
+				service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
+			} catch (Exception e) {
+				e.printStackTrace();
 			}
-			int handlerCount = pref.getInt(protocol
-					+ MainActivity.HANDLER_COUNT, 0);
-			editor.putInt(protocol + MainActivity.HANDLER_COUNT,
-					handlerCount + 1);
-			editor.commit();
-			service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
-		} catch (Exception e) {
-			e.printStackTrace();
 		}
 	}
 

+ 3 - 0
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -8,6 +8,7 @@ import java.net.Socket;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.preference.PreferenceManager;
+import de.tudarmstadt.informatik.hostage.ConnectionRegister;
 import de.tudarmstadt.informatik.hostage.HoneyListener;
 import de.tudarmstadt.informatik.hostage.HoneyService;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
@@ -90,6 +91,7 @@ public abstract class AbstractHandler implements Runnable {
 			talkToClient(in, out);
 		} catch (Exception e) {
 			e.printStackTrace();
+			ConnectionRegister.closeConnection();
 		}
 		kill();
 	}
@@ -101,6 +103,7 @@ public abstract class AbstractHandler implements Runnable {
 		thread.interrupt();
 		try {
 			client.close();
+			ConnectionRegister.closeConnection();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}