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)

sclass ConnectedInstances {
  ExpiringMap2<S, Bool> recentlyConnected_internal = new ExpiringMap2(5000/*, r updateValue*/);
  new MultiSet<S> activeConnections;
  Map<S, Bool> recentlyConnected = synchroMap(recentlyConnected_internal);
  Set<S> set;
  SimpleLiveValue<Int> connected2 = new(Int, 0); // filtered count
  
  void setAfterglowInterval(int ms) {
    recentlyConnected_internal.setStandardExpiryTime(ms);
  }

  void start() {
    // TODO (maybe): add countdown to ExpiringMap instead
    doEvery(1000, r {
      bool changes;
      synchronized(recentlyConnected) {
        changes = recentlyConnected_internal.clean();
      }
      if (changes) updateValue();
    });
  }

  void updateValue {
    Set<S> set1 = keys(activeConnections);
    Set<S> set2 = keys(recentlyConnected);
    set = mergeSets(set1, set2);
    int n = l(set);
    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();
    }
  }
  
  Set<S> set() { ret set; }
  int value() { ret connected2!; }

  /*void waitForChange(int value, int timeout) {
    waitForVarToChange_withTimeout(connected2, value, timeout);
  }*/
}

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: 471 / 1048
Version history: 15 change(s)
Referenced in: [show references]