Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

55
LINES

< > BotCompany Repo | #1016567 // ConnectedInstances - robustly counts computers currently connected through network

JavaX fragment (include)

1  
sclass ConnectedInstances {
2  
  ExpiringMap2<S, Bool> recentlyConnected_internal = new ExpiringMap2(5000/*, r updateValue*/);
3  
  new MultiSet<S> activeConnections;
4  
  Map<S, Bool> recentlyConnected = synchroMap(recentlyConnected_internal);
5  
  Set<S> set;
6  
  SimpleLiveValue<Int> connected2 = new(Int, 0); // filtered count
7  
  
8  
  void setAfterglowInterval(int ms) {
9  
    recentlyConnected_internal.setStandardExpiryTime(ms);
10  
  }
11  
12  
  void start() {
13  
    // TODO (maybe): add countdown to ExpiringMap instead
14  
    doEvery(1000, r {
15  
      bool changes;
16  
      synchronized(recentlyConnected) {
17  
        changes = recentlyConnected_internal.clean();
18  
      }
19  
      if (changes) updateValue();
20  
    });
21  
  }
22  
23  
  void updateValue {
24  
    Set<S> set1 = keys(activeConnections);
25  
    Set<S> set2 = keys(recentlyConnected);
26  
    set = mergeSets(set1, set2);
27  
    int n = l(set);
28  
    connected2.set(n);
29  
    print("Updated value to " + n + " (" + l(set1) + "/" + l(set2) + ")");
30  
  }
31  
  
32  
  void gotConnection(S computerID) {
33  
    if (computerID != null) {
34  
      activeConnections.add(computerID);
35  
      print("Have active connections from " + computerID + ": " + activeConnections.get(computerID));
36  
      updateValue();
37  
    }
38  
  }
39  
  
40  
  void lostConnection(S computerID) {
41  
    if (computerID != null) {
42  
      activeConnections.remove(computerID);
43  
      print("Disconnected. Have active connections from " + computerID + ": " + activeConnections.get(computerID));
44  
      recentlyConnected.put(computerID, true);
45  
      updateValue();
46  
    }
47  
  }
48  
  
49  
  Set<S> set() { ret set; }
50  
  int value() { ret connected2!; }
51  
52  
  /*void waitForChange(int value, int timeout) {
53  
    waitForVarToChange_withTimeout(connected2, value, timeout);
54  
  }*/
55  
}

Author comment

Began life as a copy of #1016548

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1016567
Snippet name: ConnectedInstances - robustly counts computers currently connected through network
Eternal ID of this version: #1016567/16
Text MD5: d7519002b51ae5ca457a77955aba83f5
Author: stefan
Category: javax / web
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-04 13:55:31
Source code size: 1743 bytes / 55 lines
Pitched / IR pitched: No / No
Views / Downloads: 472 / 1049
Version history: 15 change(s)
Referenced in: [show references]