summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/bwlehrpool/bwlp_guac/TunnelListener.java
blob: 5ed8927970795647b11ec12dc5a2f6cef0588a6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package de.bwlehrpool.bwlp_guac;

import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.event.TunnelCloseEvent;
import org.apache.guacamole.net.event.TunnelConnectEvent;
import org.apache.guacamole.net.event.listener.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 * A listener to track whether a user currently is connected to a tunnel.
 */
public class TunnelListener implements Listener {

	private static final Logger LOGGER = LoggerFactory.getLogger(TunnelListener.class);

	private static final HashMap<String, Integer> userTunnelCount = new HashMap<String, Integer>();

	public static boolean hasTunnel(String username) {
		synchronized (userTunnelCount) {
			return userTunnelCount.get(username) != null;
		}
	}
	
	@Override
	public void handleEvent(Object event) throws GuacamoleException {
		Integer count;
		int total;
		if (event instanceof TunnelConnectEvent) {
			String username = Util.getUsername((TunnelConnectEvent)event);
			synchronized (userTunnelCount) {
				count = userTunnelCount.get(username);
				if (count == null) {
					count = 1;
				} else {
					count++;
				}
				userTunnelCount.put(username, count);
				total = userTunnelCount.size();
			}
			LOGGER.info("User " + username + " connected to a tunnel, count: " + count + ", total: " + total);
		} else if (event instanceof TunnelCloseEvent) {
			String username = Util.getUsername((TunnelCloseEvent)event);
			synchronized (userTunnelCount) {
				count = userTunnelCount.get(username);
				if (count != null) {
					count -= 1;
					if (count > 0) {
						userTunnelCount.put(username, count);
					} else {
						userTunnelCount.remove(username);
					}
				} else {
					count = 0;
				}
				total = userTunnelCount.size();
			}
			LOGGER.info("User " + username + " closed a tunnel, count: " + count + ", total: " + total);
		}

	}
	
}