ConnectionRegister.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package de.tudarmstadt.informatik.hostage;
  2. /**
  3. * Saves the amount of active connections and limits them to a specific number.
  4. * @author Wulf Pfeiffer
  5. */
  6. public class ConnectionRegister {
  7. /** Maximum connections that are allowed to be open. */
  8. private static int maxConnections = 2;
  9. /** Active connections . */
  10. private static int openConnections = 0;
  11. /**
  12. * Returns the maximum number of active connections.
  13. * @return maximum number of active connections.
  14. */
  15. public static int getMaxConnections() {
  16. return maxConnections;
  17. }
  18. /**
  19. * Sets the maximum number of active connections.
  20. * @param maxConnections the new maximum.
  21. */
  22. public static void setMaxConnections(int maxConnections) {
  23. ConnectionRegister.maxConnections = maxConnections;
  24. }
  25. /**
  26. * Returns the number of active connections.
  27. * @return number of active connections.
  28. */
  29. public static int getOpenConnections() {
  30. return openConnections;
  31. }
  32. /**
  33. * Returns if there are new connections allowed or not.
  34. * @return true if a new connection is allowed, else false.
  35. */
  36. public static boolean isConnectionFree() {
  37. return openConnections < maxConnections;
  38. }
  39. /**
  40. * Registers a new active connection if there are connections allowed.
  41. * @return true if a new connection has been successfully registered, else false.
  42. */
  43. public static boolean newOpenConnection() {
  44. if(isConnectionFree()) {
  45. openConnections++;
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51. /**
  52. * Deregisters a active connection if at least one active connection is registered.
  53. * @return true if the connection has been successfully unregistered, else false.
  54. */
  55. public static boolean closeConnection() {
  56. if(openConnections > 0) {
  57. openConnections--;
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. }
  63. }