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

61
LINES

< > BotCompany Repo | #1001226 // contactMyself

JavaX fragment (include)

static long contactMyself_timeout = 5000;

static boolean contactMyself(S name) {
  final S pivot = randomID(6);
  
  makeAndroid_forcePublic = true;
  makeAndroid_allowVMInspection = false;
  Android android = makeAndroid2("", stringfunc { pivot }, false);
  ServerSocket server = startDialogServer_serverSocket;
    
  print("Checking possible name: " + name);
  boolean result = contactMyself_isItMe(name, android.port, pivot, contactMyself_timeout);
  print("  => " + result);
  
  try {
    server.close();
  } catch (IOException e) {
    print("[internal] " + e);
  }
  // TODO: close the client connections to be sure
  // (normally they are closed by client anyway)
  
  return result;
}
  
!include #1000915 // Flag class
  
static boolean contactMyself_isItMe(final String name, final int port, final S pivot, long timeout) {
  final new Flag ok;
  final new Flag done;
  
  Thread t = new Thread("Talking to " + name + ":" + port) {
    public void run() {
      try {
        DialogIO io = talkTo(name, port);
        print("Connected to " + name + ":" + port + ".");
        if (!io.waitForLine())
          return;
        S line = io.readLineNoBlock();
        io.sendLine("please give pivot");
        if (!io.waitForLine())
          return;
        line = io.readLineNoBlock();
        print("Line read: " + quote(line));
        io.close();
        if (pivot.equals(line))
          ok.raise();
      } catch (RuntimeException e) {
        print("[internal] " + e);
      } finally {
        done.raise();
      }
    }
  };
  t.start();
  
  done.waitUntilUp(timeout);
  //print("Interrupting thread.");
  t.interrupt();
  return ok.isUp();
}

Author comment

Began life as a copy of #1001225

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: #1001226
Snippet name: contactMyself
Eternal ID of this version: #1001226/1
Text MD5: b606e4aa16a3c9cf9b296fea3fa9a23e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-03 19:33:19
Source code size: 1724 bytes / 61 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 506 / 723
Referenced in: [show references]