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

62
LINES

< > BotCompany Repo | #1001303 // findBot with caching (CURRENT)

JavaX fragment (include)

1  
static Map<S, Integer> findBot_cache = synchroHashMap();
2  
static int findBot_timeout = 5000;
3  
4  
static DialogIO findBot(S searchPattern) {
5  
  // first split off sub-bot suffix
6  
  S subBot = null;
7  
  int i = searchPattern.indexOf('/');
8  
  if (i >= 0 && (isJavaIdentifier(searchPattern.substring(0, i)) || isInteger(searchPattern.substring(0, i)))) {
9  
    subBot = searchPattern.substring(i+1);
10  
    searchPattern = searchPattern.substring(0, i);
11  
    if (!isInteger(searchPattern))
12  
      searchPattern = "Multi-Port at " + searchPattern + ".";
13  
  }
14  
  
15  
  // assume it's a port if it's an integer
16  
  if (isInteger(searchPattern))
17  
    ret talkToSubBot(subBot, talkTo(parseInt(searchPattern)));
18  
    
19  
  if (eq(searchPattern, "remote"))
20  
    ret talkToSubBot(subBot, talkTo("second.tinybrain.de", 4999));
21  
    
22  
  Integer port = findBot_cache.get(searchPattern);
23  
  if (port != null) try {
24  
    DialogIO io = talkTo("localhost", port);
25  
    io.waitForLine(/*findBot_timeout*/); // TODO: implement
26  
    S line = io.readLineNoBlock();
27  
    if (indexOfIgnoreCase(line, searchPattern) == 0) {
28  
      call(io, "pushback", line); // put hello string back in
29  
      return talkToSubBot(subBot, io);
30  
    }
31  
  } catch (Exception e) {
32  
    e.printStackTrace();
33  
  }
34  
  
35  
  L<ProgramScan.Program> bots = quickBotScan();
36  
  
37  
  // find top-level bots
38  
  for (ProgramScan.Program p : bots) {
39  
    if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive
40  
      findBot_cache.put(searchPattern, p.port);
41  
      return talkToSubBot(subBot, talkTo("localhost", p.port));
42  
    }
43  
  }
44  
  
45  
  // find sub-bots
46  
  for (ProgramScan.Program p : bots) {
47  
    S botName = firstPartOfHelloString(p.helloString);
48  
    boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM.");
49  
    boolean shouldRecurse = startsWithIgnoreCase(botName, "Multi-Port") || isVM;
50  
        
51  
    if (shouldRecurse) pcall-short {
52  
      Map<Number, S> subBots = (Map) unstructure(sendToLocalBotQuietly(p.port, "list bots"));
53  
      for (Number vport : subBots.keySet()) {
54  
        S name = subBots.get(vport);
55  
        if (startsWithIgnoreCase(name, searchPattern))
56  
          ret talkToSubBot(vport.longValue(), talkTo("localhost", p.port));
57  
      }
58  
    }
59  
  }
60  
        
61  
  ret null;
62  
}

Author comment

Began life as a copy of #1001203

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001303
Snippet name: findBot with caching (CURRENT)
Eternal ID of this version: #1001303/2
Text MD5: 0a9a30493e9f728dcb1abd32df3ceef9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-12 22:24:51
Source code size: 2329 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 748 / 2755
Version history: 1 change(s)
Referenced in: [show references]