Libraryless. Click here for Pure Java version (1511L/10K/36K).
1 | // Bot Ref Bot knows which bots are online on some machine. |
2 | |
3 | !747 |
4 | |
5 | m {
|
6 | static int interval = 4000; |
7 | |
8 | static L<ProgramScan.Program> bots = new ArrayList<ProgramScan.Program>(); |
9 | |
10 | p {
|
11 | makeAndroid3("Bot Ref Bot.");
|
12 | |
13 | while (true) {
|
14 | scan(); |
15 | sleep(interval); |
16 | } |
17 | } |
18 | |
19 | static void scan() {
|
20 | setBots(quickBotScan()); |
21 | } |
22 | |
23 | static synchronized void setBots(L<ProgramScan.Program> l) {
|
24 | bots = l; |
25 | } |
26 | |
27 | // returned list is thread-safe (immutable even) |
28 | static synchronized L<ProgramScan.Program> getBots() {
|
29 | ret bots; |
30 | } |
31 | |
32 | static synchronized S answer(S s, L<S> history) {
|
33 | new Matches m; |
34 | |
35 | if (match3("please forward to bot *: *", s, m)) {
|
36 | S bot = m.unq(0); |
37 | S line = m.unq(1); |
38 | |
39 | DialogIO io = findBotFromList(bot); |
40 | if (io != null) {
|
41 | S hello = io.readLine(); |
42 | if (startsWithIgnoreCase(hello, bot)) {
|
43 | io.sendLine(line); |
44 | S answer = io.readLine(); |
45 | io.close(); |
46 | ret answer; |
47 | } |
48 | } |
49 | |
50 | // default case |
51 | print("not in list, fallback");
|
52 | ret sendToLocalBot(bot, line); // TODO: keep connections |
53 | } |
54 | |
55 | ret useQuestionBot(s, history); |
56 | } |
57 | |
58 | static S useQuestionBot(S s, L<S> history) {
|
59 | ret sendToLocalBot("Questions Bot", "how can question * to bot * be answered?", s, getProgramID());
|
60 | } |
61 | |
62 | static DialogIO findBotFromList(S botName) {
|
63 | for (ProgramScan.Program p : getBots()) |
64 | if (startsWithIgnoreCase(p.helloString, botName)) |
65 | ret talkTo(p.port); |
66 | ret null; |
67 | } |
68 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1001493 |
| Snippet name: | Bot Ref Bot |
| Eternal ID of this version: | #1001493/1 |
| Text MD5: | 61da5d3dcfcdb3711468b41f0edba539 |
| Transpilation MD5: | 9beca85b20143d07991631b3cb7dd845 |
| Author: | stefan |
| Category: | |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-10-21 17:33:18 |
| Source code size: | 1634 bytes / 68 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 905 / 990 |
| Referenced in: | [show references] |