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

102
LINES

< > BotCompany Repo | #1009926 // startVMLister

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9702L/66K).

scope startVMLister.

static new TreeMap<Int, startVMLister_Data> startVMLister_vms;
static Lock startVMLister_lock = lock();
static O startVMLister_onChange;

sclass #Data {
  long started;
  int port;
  S vmID;
  bool hidden;
}

static bool startVMLister() {
  S bot = "VM Lister.";
  if (!hasBot(bot)) {
    makeBot(bot, f startVMLister_answer);
    thread { startVMLister_scan(); }
    true;
  }
  false;
}

static S startVMLister_answer(S s) {
  lock startVMLister_lock;
  
  new Matches m;
  if "started vm * *" {
    int port = parseInt($1);
    startVMLister_vms.put(port, nu(startVMLister_Data, started := now(), +port,
      vmID := $2));
    pcallF(startVMLister_onChange);
    ret "OK";
  }
  
  if "hiding vm *" {
    startVMLister_Data vm = startVMLister_vms.get(parseInt($1));
    if (vm != null) vm.hidden = true;
    pcallF(startVMLister_onChange);
    ret "OK";
  }
  
  if "unhiding vm *" {
    startVMLister_Data vm = startVMLister_vms.get(parseInt($1));
    if (vm != null) vm.hidden = false;
    pcallF(startVMLister_onChange);
    ret "OK";
  }
  
  if "killing vm *" {
    startVMLister_vms.remove(parseInt($1));
    pcallF(startVMLister_onChange);
    ret "OK";
  }
  
  if "number of vms" ret lstr(startVMLister_vms);
  if "number of hidden vms" ret str(startVMLister_numHiddenVMs());
  if "all vms struct" ret struct(values(startVMLister_vms));
  
  if "scan vms" {
    startVMLister_scan();
    ret "OK, found " + n(startVMLister_vms, "VM");
  }
  
  null;
}

svoid startVMLister_scan ctex {
  lock startVMLister_lock;
  
  new TreeMap<Int, startVMLister_Data> vms;
  
  for (DialogIO io : talkToAllVMs()) try {
    new startVMLister_Data vm;
    vm.port = io.getPort();
    vm.vmID = io.ask("get vm id");
    vm.hidden = isYes(io.ask("is hidden vm"));
    vm.started = parseLongOpt(io.ask("vm start date"));
    vms.put(vm.port, vm);
  } finally {
    io.close();
  }
  
  copyMap(vms, startVMLister_vms);
  pcallF(startVMLister_onChange);
}

static int startVMLister_numVMs() {
  lock startVMLister_lock;
  ret l(startVMLister_vms);
}

static L<Data> #hiddenVMs() {
  lock startVMLister_lock;
  ret objectsWhere(values(startVMLister_vms), hidden := true);
}

static int #numHiddenVMs() {
  ret l(hiddenVMs());
}

end scope

Author comment

Began life as a copy of #1009802

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009926
Snippet name: startVMLister
Eternal ID of this version: #1009926/17
Text MD5: 606cf45916387748c693e0d814f8daeb
Transpilation MD5: 9dc3099960845a7a52d0b8b174a66cda
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-29 21:36:54
Source code size: 2353 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 511 / 608
Version history: 16 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)