Skip to content

Commit 2b7fb1b

Browse files
author
rjmatthews62
committed
Improved fix for issue 591
1 parent 8eec5a9 commit 2b7fb1b

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

android/ScriptingLayer/src/com/googlecode/android_scripting/interpreter/InterpreterProcess.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818

1919
import com.googlecode.android_scripting.Analytics;
2020
import com.googlecode.android_scripting.AndroidProxy;
21+
import com.googlecode.android_scripting.Log;
2122
import com.googlecode.android_scripting.Process;
23+
import com.googlecode.android_scripting.SimpleServer;
2224
import com.googlecode.android_scripting.jsonrpc.RpcReceiverManagerFactory;
2325

26+
import java.net.InetSocketAddress;
27+
import java.net.SocketException;
28+
import java.net.UnknownHostException;
29+
2430
/**
2531
* This is a skeletal implementation of an interpreter process.
2632
*
@@ -66,13 +72,28 @@ public Interpreter getInterpreter() {
6672
}
6773

6874
public String getHost() {
69-
return mProxy.getAddress().getHostName();
75+
String result = mProxy.getAddress().getHostName();
76+
if (result.equals("0.0.0.0")) { // Wildcard.
77+
try {
78+
return SimpleServer.getPublicInetAddress().getHostName();
79+
} catch (UnknownHostException e) {
80+
Log.i("public address", e);
81+
e.printStackTrace();
82+
} catch (SocketException e) {
83+
Log.i("public address", e);
84+
}
85+
}
86+
return result;
7087
}
7188

7289
public int getPort() {
7390
return mProxy.getAddress().getPort();
7491
}
7592

93+
public InetSocketAddress getAddress() {
94+
return mProxy.getAddress();
95+
}
96+
7697
public String getSecret() {
7798
return mProxy.getSecret();
7899
}

android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptProcessMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
import android.os.Bundle;
2525
import android.os.IBinder;
2626
import android.view.ContextMenu;
27+
import android.view.ContextMenu.ContextMenuInfo;
2728
import android.view.LayoutInflater;
2829
import android.view.Menu;
2930
import android.view.MenuItem;
3031
import android.view.View;
3132
import android.view.ViewGroup;
32-
import android.view.ContextMenu.ContextMenuInfo;
3333
import android.widget.AdapterView;
3434
import android.widget.BaseAdapter;
3535
import android.widget.ListView;

android/Utils/src/com/googlecode/android_scripting/SimpleServer.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,26 @@ public int getNumberOfConnections() {
117117
return mConnectionThreads.size();
118118
}
119119

120-
private InetAddress getPublicInetAddress() throws UnknownHostException, SocketException {
120+
public static InetAddress getPublicInetAddress() throws UnknownHostException, SocketException {
121+
122+
InetAddress candidate = null;
121123
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
122124
for (NetworkInterface netint : Collections.list(nets)) {
125+
if (netint.isLoopback() || !netint.isUp()) { // Ignore if localhost or not active
126+
continue;
127+
}
123128
Enumeration<InetAddress> addresses = netint.getInetAddresses();
124129
for (InetAddress address : Collections.list(addresses)) {
125-
if (!address.getHostAddress().equals("127.0.0.1") && address instanceof Inet4Address) {
126-
return address;
130+
if (address instanceof Inet4Address) {
131+
return address; // Prefer ipv4
127132
}
133+
candidate = address; // Probably an ipv6
128134
}
129135
}
130-
return InetAddress.getLocalHost();
136+
if (candidate != null) {
137+
return candidate; // return ipv6 address if no suitable ipv6
138+
}
139+
return InetAddress.getLocalHost(); // No damn matches. Give up, return local host.
131140
}
132141

133142
/**
@@ -162,7 +171,8 @@ public InetSocketAddress startLocal(int port) {
162171
public InetSocketAddress startPublic(int port) {
163172
InetAddress address;
164173
try {
165-
address = getPublicInetAddress();
174+
// address = getPublicInetAddress();
175+
address = null;
166176
mServer = new ServerSocket(port, 5 /* backlog */, address);
167177
} catch (Exception e) {
168178
Log.e("Failed to start server.", e);

0 commit comments

Comments
 (0)