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; } } }