sclass ConnectedInstances { ExpiringMap2 recentlyConnected_internal = new ExpiringMap2(5000, r updateValue); new MultiSet activeConnections; Map recentlyConnected = synchroMap(recentlyConnected_internal); SimpleLiveValue connected2 = new(Int, 0); // filtered count *() { // TODO (maybe): add countdown to ExpiringMap instead doEvery(1000, r { synchronized(recentlyConnected) { recentlyConnected_internal.clean(); } }); } void updateValue { Set set1 = keys(activeConnections); Set set2 = keys(recentlyConnected); int n = l(mergeSets(set1, set2)); connected2.set(n); print("Updated value to " + n + " (" + l(set1) + "/" + l(set2) + ")"); } void gotConnection(S computerID) { if (computerID != null) { activeConnections.add(computerID); print("Have active connections from " + computerID + ": " + activeConnections.get(computerID)); updateValue(); } } void lostConnection(S computerID) { if (computerID != null) { activeConnections.remove(computerID); print("Disconnected. Have active connections from " + computerID + ": " + activeConnections.get(computerID)); recentlyConnected.put(computerID, true); updateValue(); } } int value() { ret connected2!; } /*void waitForChange(int value, int timeout) { waitForVarToChange_withTimeout(connected2, value, timeout); }*/ }