move all proxy checking into common class(DRY) and add the ability to selectively disable or change the timeout time

Former-commit-id: 368d9c3423
Former-commit-id: 26922abcb88820e6ae4d894ce3eece295eb925ba
This commit is contained in:
idk
2022-09-07 11:45:49 -04:00
parent 7ed828391e
commit fac9329b4b
6 changed files with 81 additions and 139 deletions

View File

@ -1,8 +1,6 @@
package net.i2p.i2pfirefox;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
/**
@ -21,7 +19,6 @@ import java.util.ArrayList;
*/
public class I2PChromium extends I2PCommonBrowser {
private final String[] CHROMIUM_SEARCH_PATHS = CHROMIUM_FINDER();
private final int DEFAULT_TIMEOUT = 200;
private Process p = null;
/**
@ -483,71 +480,6 @@ public class I2PChromium extends I2PCommonBrowser {
}
}
/**
* Waits for an HTTP proxy on port 4444 to be ready.
* Returns false on timeout of 200 seconds.
*
* @return true if the proxy is ready, false if it is not.
* @since 0.0.1
*/
public boolean waitForProxy() { return waitForProxy(DEFAULT_TIMEOUT); }
/**
* Waits for an HTTP proxy on port 4444 to be ready.
* Returns false on timeout of the specified number of seconds.
*
* @param timeout the number of seconds to wait for the proxy to be ready.
* @return true if the proxy is ready, false if it is not.
* @since 0.0.1
*/
public boolean waitForProxy(int timeout) {
return waitForProxy(timeout, 4444);
}
/**
* Waits for an HTTP proxy on the specified port to be ready.
* Returns false on timeout of the specified number of seconds.
*
* @param timeout the number of seconds to wait for the proxy to be ready.
* @param port the port to wait for the proxy to be ready on.
* @return true if the proxy is ready, false if it is not.
* @since 0.0.1
*/
public boolean waitForProxy(int timeout, int port) {
return waitForProxy(timeout, port, "localhost");
}
/**
* Waits for an HTTP proxy on the specified port to be ready.
* Returns false on timeout of the specified number of seconds.
*
* @param timeout the number of seconds to wait for the proxy to be ready.
* @param port the port to wait for the proxy to be ready on.
* @param host the host to wait for the proxy to be ready on.
* @return true if the proxy is ready, false if it is not.
* @since 0.0.1
*/
public boolean waitForProxy(int timeout, int port, String host) {
for (int i = 0; i < timeout; i++) {
if (checkifPortIsOccupied(port, host)) {
return true;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
private boolean checkifPortIsOccupied(int port, String host) {
try {
Socket socket = new Socket(host, port);
socket.close();
return true;
} catch (IOException e) {
return false;
}
}
public Process launchAndDetatch(boolean privateWindow, String[] url) {
validateUserDir();
if (waitForProxy()) {