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).

1  
scope startVMLister.
2  
3  
static new TreeMap<Int, startVMLister_Data> startVMLister_vms;
4  
static Lock startVMLister_lock = lock();
5  
static O startVMLister_onChange;
6  
7  
sclass #Data {
8  
  long started;
9  
  int port;
10  
  S vmID;
11  
  bool hidden;
12  
}
13  
14  
static bool startVMLister() {
15  
  S bot = "VM Lister.";
16  
  if (!hasBot(bot)) {
17  
    makeBot(bot, f startVMLister_answer);
18  
    thread { startVMLister_scan(); }
19  
    true;
20  
  }
21  
  false;
22  
}
23  
24  
static S startVMLister_answer(S s) {
25  
  lock startVMLister_lock;
26  
  
27  
  new Matches m;
28  
  if "started vm * *" {
29  
    int port = parseInt($1);
30  
    startVMLister_vms.put(port, nu(startVMLister_Data, started := now(), +port,
31  
      vmID := $2));
32  
    pcallF(startVMLister_onChange);
33  
    ret "OK";
34  
  }
35  
  
36  
  if "hiding vm *" {
37  
    startVMLister_Data vm = startVMLister_vms.get(parseInt($1));
38  
    if (vm != null) vm.hidden = true;
39  
    pcallF(startVMLister_onChange);
40  
    ret "OK";
41  
  }
42  
  
43  
  if "unhiding vm *" {
44  
    startVMLister_Data vm = startVMLister_vms.get(parseInt($1));
45  
    if (vm != null) vm.hidden = false;
46  
    pcallF(startVMLister_onChange);
47  
    ret "OK";
48  
  }
49  
  
50  
  if "killing vm *" {
51  
    startVMLister_vms.remove(parseInt($1));
52  
    pcallF(startVMLister_onChange);
53  
    ret "OK";
54  
  }
55  
  
56  
  if "number of vms" ret lstr(startVMLister_vms);
57  
  if "number of hidden vms" ret str(startVMLister_numHiddenVMs());
58  
  if "all vms struct" ret struct(values(startVMLister_vms));
59  
  
60  
  if "scan vms" {
61  
    startVMLister_scan();
62  
    ret "OK, found " + n(startVMLister_vms, "VM");
63  
  }
64  
  
65  
  null;
66  
}
67  
68  
svoid startVMLister_scan ctex {
69  
  lock startVMLister_lock;
70  
  
71  
  new TreeMap<Int, startVMLister_Data> vms;
72  
  
73  
  for (DialogIO io : talkToAllVMs()) try {
74  
    new startVMLister_Data vm;
75  
    vm.port = io.getPort();
76  
    vm.vmID = io.ask("get vm id");
77  
    vm.hidden = isYes(io.ask("is hidden vm"));
78  
    vm.started = parseLongOpt(io.ask("vm start date"));
79  
    vms.put(vm.port, vm);
80  
  } finally {
81  
    io.close();
82  
  }
83  
  
84  
  copyMap(vms, startVMLister_vms);
85  
  pcallF(startVMLister_onChange);
86  
}
87  
88  
static int startVMLister_numVMs() {
89  
  lock startVMLister_lock;
90  
  ret l(startVMLister_vms);
91  
}
92  
93  
static L<Data> #hiddenVMs() {
94  
  lock startVMLister_lock;
95  
  ret objectsWhere(values(startVMLister_vms), hidden := true);
96  
}
97  
98  
static int #numHiddenVMs() {
99  
  ret l(hiddenVMs());
100  
}
101  
102  
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: 510 / 608
Version history: 16 change(s)
Referenced in: [show references]