sclass MultiPort {
sclass Resp {
long virtualPort;
S botName;
O responder;
}
new List<Resp> responders;
Android3 android;
long nextVirtualPort = 1;
*() {
synchronized(getJavaX()) {
if (!getMultiPorts().isEmpty()) return;
if (!(Boolean) call(getJavaX(), "addMultiPort", this)) {
// XXX - hope this still works
Android3 android = new Android3("Multi-Port at " + getVMID() + ".");
android.useMultiPort = false;
android.incomingSilent = true;
android.console = false;
android.daemon = true;
makeAndroid3(android);
}
}
//print("Dynamic Multi-Port registered with VM. Now " + l(getMultiPorts()) + " multi-port(s).");
}
S answer(S s) {
new Matches m;
if (match3("list bots", s, m)) {
ret structure(listBotsWithPort());
}
if (match3("has bot *", s, m)) {
S name = m.unq(0);
ret findResponder(name) != null ? "yes" : "no";
}
if (match3("silent", s, m)) {
android.incomingSilent = true;
ret "OK.";
}
if (match3("unsilent", s, m)) {
android.incomingSilent = false;
ret "OK.";
}
if (match3("please forward to bot *: *", s, m)) {
S bot = unquote(m.m[0]);
S line = unquote(m.m[1]);
O responder = findResponder(bot);
if (responder == null)
ret "Error: bot not found";
else
ret (S) call(responder, "answer", line, litlist(s));
}
ret null;
}
synchronized L<S> listBots() {
new L<S> list;
for (Resp r : responders)
list.add(r.botName);
ret list;
}
synchronized Map<Long,S> listBotsWithPort() {
new Map<Long,S> map;
for (Resp r : responders)
map.put(r.virtualPort, r.botName);
ret map;
}
synchronized O findResponder(S botName) {
if (isInteger(botName)) {
long vport = parseLong(botName);
for (Resp r : responders)
if (r.virtualPort == vport)
ret r.responder;
} else {
for (Resp r : responders)
if (startsWithIgnoreCase(r.botName, botName))
ret r.responder;
}
ret null;
}
synchronized long addResponder(S botName, O responder) {
new Resp r;
r.virtualPort = nextVirtualPort++;
r.botName = botName;
r.responder = responder;
responders.add(r);
ret r.virtualPort;
}
synchronized void removeResponder(O responder) {
for (int i = 0; i < l(responders); i++)
if (responders.get(i).responder == responder) {
//print("Responder removed.");
responders.remove(i);
ret;
}
}
synchronized void removePort(long virtualPort) {
for (int i = 0; i < l(responders); i++)
if (responders.get(i).virtualPort == virtualPort) {
//print("Virtual port " + virtualPort + " removed.");
responders.remove(i);
ret;
}
}
}Began life as a copy of #1001672
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1006632 |
| Snippet name: | class MultiPort (multi-port as object) |
| Eternal ID of this version: | #1006632/2 |
| Text MD5: | a61ea907ed3de94d63d00ef0b6ca94e3 |
| Author: | stefan |
| Category: | javax / core |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-07-30 17:05:11 |
| Source code size: | 3032 bytes / 117 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1000 / 1795 |
| Version history: | 1 change(s) |
| Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |