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

63
LINES

< > BotCompany Repo | #1001961 // findBot with caching of sub-bots (developing)

JavaX fragment (include)

1  
static new Map<S, S> findBot_cache; // search pattern to address
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  
  if (subBot == null) {
23  
    S vport = findBot_cache.get(searchPattern);
24  
    if (vport != null) try {
25  
      DialogIO io = talkTo("localhost", port);
26  
      io.waitForLine(/*findBot_timeout*/); // TODO: implement
27  
      S line = io.readLineNoBlock();
28  
      if (indexOfIgnoreCase(line, searchPattern) == 0) {
29  
        call(io, "pushback", line); // put hello string back in
30  
        return talkToSubBot(subBot, io);
31  
    }
32  
  } catch (Exception e) {
33  
    e.printStackTrace();
34  
  }
35  
  
36  
  L<ProgramScan.Program> bots = quickBotScan();
37  
  
38  
  // find top-level bots
39  
  for (ProgramScan.Program p : bots) {
40  
    if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive
41  
      findBot_cache.put(searchPattern, p.port);
42  
      return talkToSubBot(subBot, talkTo("localhost", p.port));
43  
    }
44  
  }
45  
  
46  
  // find sub-bots
47  
  for (ProgramScan.Program p : bots) {
48  
    S botName = firstPartOfHelloString(p.helloString);
49  
    boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM.");
50  
    boolean shouldRecurse = startsWithIgnoreCase(botName, "Multi-Port") || isVM;
51  
        
52  
    if (shouldRecurse) try {
53  
      Map<Number, S> subBots = (Map) unstructure(sendToLocalBot(p.port, "list bots"));
54  
      for (Number vport : subBots.keySet()) {
55  
        S name = subBots.get(vport);
56  
        if (startsWithIgnoreCase(name, searchPattern))
57  
          ret talkToSubBot(vport.longValue(), talkTo("localhost", p.port));
58  
      }
59  
    } catch (Exception e) { e.printStackTrace(); }
60  
  }
61  
        
62  
  ret null;
63  
}

Author comment

Began life as a copy of #1001303

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001961
Snippet name: findBot with caching of sub-bots (developing)
Eternal ID of this version: #1001961/1
Text MD5: da7e7f16735d13108cd9651533344780
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-09 21:30:03
Source code size: 2404 bytes / 63 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 770 / 659
Referenced in: [show references]