static DialogIO talkTo(int port) { return talkTo("localhost", port); } static int talkTo_defaultTimeout = 10000; // This is the CONNECT timeout static int talkTo_timeoutForReads = 0; // Timeout waiting for answers (0 = no timeout) static ThreadLocal> talkTo_byThread = new ThreadLocal; static DialogIO talkTo(S ip, int port) ctex { S full = ip + ":" + port; Map map = talkTo_byThread.get(); if (map != null && map.containsKey(full)) ret map.get(full); if (isLocalhost(ip) && port == vmPort()) ret talkToThisVM(); ret new talkTo_IO(ip, port); } sclass talkTo_IO extends DialogIO { S ip; int port; Socket s; Writer w; BufferedReader in; *(S *ip, int *port) ctex { s = new Socket; try { if (talkTo_timeoutForReads != 0) s.setSoTimeout(talkTo_timeoutForReads); s.connect(new InetSocketAddress(ip, port), talkTo_defaultTimeout); } catch (Throwable e) { fail("Tried talking to " + ip + ":" + port, e); } w = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); in = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8")); } boolean isLocalConnection() { return s.getInetAddress().isLoopbackAddress(); } boolean isStillConnected() { return !(eos || s.isClosed()); } void sendLine(String line) ctex { lock lock; w.write(line + "\n"); w.flush(); } S readLineImpl() ctex { ret in.readLine(); } public void close() { try { if (!noClose) s.close(); } catch (IOException e) { // whatever } } Socket getSocket() { return s; } }