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

58
LINES

< > BotCompany Repo | #1009722 // getBotAddress - old version

JavaX fragment (include)

static int getBotAddress_timeout = 5000;

static S getBotAddress(S searchPattern) {
  S subBot = "";
  int i = searchPattern.indexOf('/');
  if (i >= 0 && (isJavaIdentifier(searchPattern.substring(0, i)) || isInteger(searchPattern.substring(0, i)))) {
    subBot = searchPattern.substring(i);
    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 searchPattern + subBot;
    
  Integer port = findBot_cache.get(searchPattern);
  if (port != null) try {
    DialogIO io = talkTo("localhost", port);
    try {
      io.waitForLine(/*getBotAddress_timeout*/); // TODO: implement
      S line = io.readLineNoBlock();
      if (startsWithIgnoreCase(line, searchPattern))
        ret port + subBot;
    } finally {
      io.close();
    }
  } 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
      return p.port + subBot;
    }
  }
  
  // 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 = unstructureMap_verboseError(sendToLocalBot(p.port, "list bots"));
      for (Number vport : subBots.keySet()) {
        S name = subBots.get(vport);
        if (startsWithIgnoreCase(name, searchPattern))
          ret p.port + "/" + vport;
      }
    } catch (Exception e) { e.printStackTrace(); }
  }
        
  ret null;
}

Author comment

Began life as a copy of #1001767

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: #1009722
Snippet name: getBotAddress - old version
Eternal ID of this version: #1009722/1
Text MD5: 46e9936021000714ce3847b73295c84e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-15 17:08:02
Source code size: 1991 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 468 / 486
Referenced in: [show references]