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)

static new Map<S, S> findBot_cache; // search pattern to address
static int findBot_timeout = 5000;

static DialogIO findBot(S searchPattern) {
  // first split off sub-bot suffix
  S subBot = null;
  int i = searchPattern.indexOf('/');
  if (i >= 0 && (isJavaIdentifier(searchPattern.substring(0, i)) || isInteger(searchPattern.substring(0, i)))) {
    subBot = searchPattern.substring(i+1);
    searchPattern = searchPattern.substring(0, i);
    if (!isInteger(searchPattern))
      searchPattern = "Multi-Port at " + searchPattern + ".";
  }
  
  // assume it's a port if it's an integer
  if (isInteger(searchPattern))
    ret talkToSubBot(subBot, talkTo(parseInt(searchPattern)));
    
  if (eq(searchPattern, "remote"))
    ret talkToSubBot(subBot, talkTo("second.tinybrain.de", 4999));
    
  if (subBot == null) {
    S vport = findBot_cache.get(searchPattern);
    if (vport != null) try {
      DialogIO io = talkTo("localhost", port);
      io.waitForLine(/*findBot_timeout*/); // TODO: implement
      S line = io.readLineNoBlock();
      if (indexOfIgnoreCase(line, searchPattern) == 0) {
        call(io, "pushback", line); // put hello string back in
        return talkToSubBot(subBot, io);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  L<ProgramScan.Program> bots = quickBotScan();
  
  // find top-level bots
  for (ProgramScan.Program p : bots) {
    if (indexOfIgnoreCase(p.helloString, searchPattern) == 0) { // strict matching - start of hello string only, but case-insensitive
      findBot_cache.put(searchPattern, p.port);
      return talkToSubBot(subBot, talkTo("localhost", p.port));
    }
  }
  
  // find sub-bots
  for (ProgramScan.Program p : bots) {
    S botName = firstPartOfHelloString(p.helloString);
    boolean isVM = startsWithIgnoreCase(p.helloString, "This is a JavaX VM.");
    boolean shouldRecurse = startsWithIgnoreCase(botName, "Multi-Port") || isVM;
        
    if (shouldRecurse) try {
      Map<Number, S> subBots = (Map) unstructure(sendToLocalBot(p.port, "list bots"));
      for (Number vport : subBots.keySet()) {
        S name = subBots.get(vport);
        if (startsWithIgnoreCase(name, searchPattern))
          ret talkToSubBot(vport.longValue(), talkTo("localhost", p.port));
      }
    } catch (Exception e) { e.printStackTrace(); }
  }
        
  ret null;
}

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: 768 / 656
Referenced in: [show references]