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(); }