I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 306b3017 authored by zzz's avatar zzz
Browse files

Pluck of revision 493986d03c43bac019996ea0b10b8a6a40e92a32 from branch i2p.i2p.zzz.test

---------------------------------------------------------------------------------------
    * Browser Launch: Wait until the routerconsole is up before launching the browser
parent ce92b6cb
No related branches found
No related tags found
No related merge requests found
...@@ -12,9 +12,12 @@ package net.i2p.apps.systray; ...@@ -12,9 +12,12 @@ package net.i2p.apps.systray;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL; import java.net.URL;
import net.i2p.util.ShellCommand; import net.i2p.util.ShellCommand;
...@@ -33,6 +36,56 @@ public class UrlLauncher { ...@@ -33,6 +36,56 @@ public class UrlLauncher {
ShellCommand _shellCommand = new ShellCommand(); ShellCommand _shellCommand = new ShellCommand();
private static final int WAIT_TIME = 5*1000;
private static final int MAX_WAIT_TIME = 5*60*1000;
private static final int MAX_TRIES = 99;
/**
* Prevent bad user experience by waiting for the server to be there
* before launching the browser.
* @return success
*/
public boolean waitForServer(String urlString) {
URL url;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
return false;
}
String host = url.getHost();
int port = url.getPort();
if (port <= 0) {
port = url.getDefaultPort();
if (port <= 0)
return false;
}
SocketAddress sa;
try {
sa = new InetSocketAddress(host, port);
} catch (IllegalArgumentException iae) {
return false;
}
long done = System.currentTimeMillis() + MAX_WAIT_TIME;
for (int i = 0; i < MAX_TRIES; i++) {
try {
Socket test = new Socket();
// this will usually fail right away if it's going to fail since it's local
test.connect(sa, WAIT_TIME);
// it worked
try {
test.close();
} catch (IOException ioe) {}
return true;
} catch (Exception e) {}
if (System.currentTimeMillis() > done)
break;
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {}
}
return false;
}
/** /**
* Discovers the operating system the installer is running under and tries * Discovers the operating system the installer is running under and tries
* to launch the given URL using the default browser for that platform; if * to launch the given URL using the default browser for that platform; if
...@@ -49,6 +102,7 @@ public class UrlLauncher { ...@@ -49,6 +102,7 @@ public class UrlLauncher {
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");
waitForServer(url);
if (validateUrlFormat(url)) { if (validateUrlFormat(url)) {
if (osName.toLowerCase().indexOf("mac") > -1) { if (osName.toLowerCase().indexOf("mac") > -1) {
if (osName.toLowerCase().startsWith("mac os x")) { if (osName.toLowerCase().startsWith("mac os x")) {
...@@ -141,6 +195,7 @@ public class UrlLauncher { ...@@ -141,6 +195,7 @@ public class UrlLauncher {
*/ */
public boolean openUrl(String url, String browser) throws Exception { public boolean openUrl(String url, String browser) throws Exception {
waitForServer(url);
if (validateUrlFormat(url)) if (validateUrlFormat(url))
if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5)) if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5))
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment