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

61
LINES

< > BotCompany Repo | #1001226 // contactMyself

JavaX fragment (include)

1  
static long contactMyself_timeout = 5000;
2  
3  
static boolean contactMyself(S name) {
4  
  final S pivot = randomID(6);
5  
  
6  
  makeAndroid_forcePublic = true;
7  
  makeAndroid_allowVMInspection = false;
8  
  Android android = makeAndroid2("", stringfunc { pivot }, false);
9  
  ServerSocket server = startDialogServer_serverSocket;
10  
    
11  
  print("Checking possible name: " + name);
12  
  boolean result = contactMyself_isItMe(name, android.port, pivot, contactMyself_timeout);
13  
  print("  => " + result);
14  
  
15  
  try {
16  
    server.close();
17  
  } catch (IOException e) {
18  
    print("[internal] " + e);
19  
  }
20  
  // TODO: close the client connections to be sure
21  
  // (normally they are closed by client anyway)
22  
  
23  
  return result;
24  
}
25  
  
26  
!include #1000915 // Flag class
27  
  
28  
static boolean contactMyself_isItMe(final String name, final int port, final S pivot, long timeout) {
29  
  final new Flag ok;
30  
  final new Flag done;
31  
  
32  
  Thread t = new Thread("Talking to " + name + ":" + port) {
33  
    public void run() {
34  
      try {
35  
        DialogIO io = talkTo(name, port);
36  
        print("Connected to " + name + ":" + port + ".");
37  
        if (!io.waitForLine())
38  
          return;
39  
        S line = io.readLineNoBlock();
40  
        io.sendLine("please give pivot");
41  
        if (!io.waitForLine())
42  
          return;
43  
        line = io.readLineNoBlock();
44  
        print("Line read: " + quote(line));
45  
        io.close();
46  
        if (pivot.equals(line))
47  
          ok.raise();
48  
      } catch (RuntimeException e) {
49  
        print("[internal] " + e);
50  
      } finally {
51  
        done.raise();
52  
      }
53  
    }
54  
  };
55  
  t.start();
56  
  
57  
  done.waitUntilUp(timeout);
58  
  //print("Interrupting thread.");
59  
  t.interrupt();
60  
  return ok.isUp();
61  
}

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