From cdabaaebd6059077149a4856778ba51bcfd088fd Mon Sep 17 00:00:00 2001 From: Udo Walter Date: Thu, 14 May 2020 05:35:13 +0200 Subject: Keep track of the number of tunnels per user. --- .../de/bwlehrpool/bwlp_guac/TunnelListener.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/bwlehrpool/bwlp_guac/TunnelListener.java b/src/main/java/de/bwlehrpool/bwlp_guac/TunnelListener.java index 26aa93c..265bfc7 100644 --- a/src/main/java/de/bwlehrpool/bwlp_guac/TunnelListener.java +++ b/src/main/java/de/bwlehrpool/bwlp_guac/TunnelListener.java @@ -7,7 +7,7 @@ import org.apache.guacamole.net.event.listener.Listener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.HashSet; +import java.util.HashMap; /** * A listener to track whether a user currently is connected to a tunnel. @@ -16,10 +16,10 @@ public class TunnelListener implements Listener { private static final Logger LOGGER = LoggerFactory.getLogger(TunnelListener.class); - private static final HashSet usersWithTunnel = new HashSet(); + private static final HashMap userTunnelCount = new HashMap(); public static boolean hasTunnel(String username) { - return usersWithTunnel.contains(username); + return userTunnelCount.get(username) != null; } @Override @@ -27,12 +27,24 @@ public class TunnelListener implements Listener { if (event instanceof TunnelConnectEvent) { String username = ((TunnelConnectEvent)event).getCredentials().getUsername(); LOGGER.info("User " + username + " connected to a tunnel."); - usersWithTunnel.add(username); + + Integer count = userTunnelCount.get(username); + if (count == null) count = 1; + else count++; + userTunnelCount.put(username, count); + + LOGGER.info("Tunnel count: " + count); } else if (event instanceof TunnelCloseEvent) { String username = ((TunnelCloseEvent)event).getCredentials().getUsername(); LOGGER.info("User " + username + " closed a tunnel."); - usersWithTunnel.remove(username); + + Integer count = userTunnelCount.get(username); + if (count != null) { + if (count > 1) userTunnelCount.put(username, count - 1); + else userTunnelCount.remove(username); + LOGGER.info("Tunnel count: " + (count - 1)); + } } } -- cgit v1.2.3-55-g7522