List of utility methods to do HTTP Port Find
boolean | waitForPort(String host, int port) wait For Port for (int i = 0; i < RETRIES; i++) { if (isPortAvailable(host, port)) return true; try { Thread.sleep(WAIT_TIME_MS); } catch (InterruptedException e) { return false; |
HttpURLConnection | waitForResponseCode(int code, String name, String host, int port) wait For Response Code URL url = new URL("http://" + host + ":" + port + "/" + name); long until = System.currentTimeMillis() + WAIT_TIMEOUT; while (System.currentTimeMillis() < until) { HttpURLConnection connection = connect(url); try { if (connection.getResponseCode() == code) { return connection; } catch (FileNotFoundException e) { if (code == 404) { return connection; throw e; throw new RuntimeException("wait on url " + url + " for response code " + code + " timed out."); |
String | waitForRespose(String name, String host, int port) wait For Respose waitForResponseCode(200, name, host, port);
return getResponse(name, host, port);
|
boolean | waitForServerDown(String host, int port, long timeout) wait For Server Down long start = System.currentTimeMillis(); while (true) { try { send4LetterWord(host, port, "stat"); } catch (IOException e) { return true; if (System.currentTimeMillis() > start + timeout) { ... |
boolean | waitForService(int port) wait For Service final int MAX_TRY_COUNT = 10; int tryCount = 0; while (true) { if (isPortOccupied(port)) { return true; if (tryCount == MAX_TRY_COUNT) { System.err.println("Waiting for port " + port + " time out."); ... |
String | writeContactFile(int port, String appName) Write contact details to a file only readable by the owner process and privileged local users. String cookie = null; File contactFile = getConfigFile(appName + ".remote"); if (contactFile != null) { try { contactFile.createNewFile(); Runtime.getRuntime().exec("chmod 600 " + contactFile.getPath()); } catch (Exception e) { try { PrintStream out = new PrintStream(new FileOutputStream(contactFile)); InetAddress addr = InetAddress.getLocalHost(); String hexVal = Integer.toHexString((int) (Math.random() * 12345)); cookie = hexVal + addr.hashCode(); out.println(addr.getHostName() + " " + port + " " + cookie); } catch (Exception e) { return cookie; |