fixes for localhost ip

This commit is contained in:
BetaSteward 2011-06-22 23:18:43 -04:00
parent 05be0a14ed
commit 73939bd72c
3 changed files with 38 additions and 35 deletions

View file

@ -28,6 +28,15 @@
package mage.remote; package mage.remote;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
@ -64,6 +73,13 @@ public class Connection {
} }
public String getURI() { public String getURI() {
if (host.equals("localhost")) {
try {
return "bisocket://" + getLocalAddress().getHostAddress() + ":" + port;
} catch (SocketException ex) {
// just use localhost if can't find local ip
}
}
return "bisocket://" + host + ":" + port; return "bisocket://" + host + ":" + port;
} }
@ -160,5 +176,21 @@ public class Connection {
public void setProxyPassword(String proxyPassword) { public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword; this.proxyPassword = proxyPassword;
} }
public static InetAddress getLocalAddress() throws SocketException {
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
NetworkInterface iface = interfaces.nextElement( );
if (iface.isLoopback())
continue;
for (InterfaceAddress addr: iface.getInterfaceAddresses())
{
InetAddress iaddr = addr.getAddress();
if (iaddr instanceof Inet4Address) {
return iaddr;
}
}
}
return null;
}
} }

View file

@ -101,7 +101,6 @@ public class Session {
public boolean connect() { public boolean connect() {
sessionState = SessionState.CONNECTING; sessionState = SessionState.CONNECTING;
try { try {
// System.setSecurityManager(null);
// System.setProperty("http.nonProxyHosts", "code.google.com"); // System.setProperty("http.nonProxyHosts", "code.google.com");
// System.setProperty("socksNonProxyHosts", "code.google.com"); // System.setProperty("socksNonProxyHosts", "code.google.com");
// //
@ -135,7 +134,7 @@ public class Session {
this.sessionId = callbackClient.getSessionId(); this.sessionId = callbackClient.getSessionId();
boolean registerResult = false; boolean registerResult = false;
if (connection.getPassword().isEmpty()) if (connection.getPassword() == null)
registerResult = server.registerClient(connection.getUsername(), sessionId, client.getVersion()); registerResult = server.registerClient(connection.getUsername(), sessionId, client.getVersion());
else else
registerResult = server.registerAdmin(connection.getPassword(), sessionId, client.getVersion()); registerResult = server.registerAdmin(connection.getPassword(), sessionId, client.getVersion());

View file

@ -31,17 +31,13 @@ package mage.server;
import mage.server.util.PluginClassLoader; import mage.server.util.PluginClassLoader;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import mage.game.match.MatchType; import mage.game.match.MatchType;
import mage.game.tournament.TournamentType; import mage.game.tournament.TournamentType;
import mage.interfaces.MageServer; import mage.interfaces.MageServer;
import mage.remote.Connection;
import mage.server.game.DeckValidatorFactory; import mage.server.game.DeckValidatorFactory;
import mage.server.game.GameFactory; import mage.server.game.GameFactory;
import mage.server.game.PlayerFactory; import mage.server.game.PlayerFactory;
@ -107,10 +103,12 @@ public class Main {
adminPassword = arg.replace(adminPasswordArg, ""); adminPassword = arg.replace(adminPasswordArg, "");
} }
} }
String host = getServerAddress(); String host = config.getServerAddress();
int port = config.getPort(); int port = config.getPort();
String locatorURI = "bisocket://" + host + ":" + port;
try { try {
if (host.equals("localhost"))
host = Connection.getLocalAddress().getHostAddress();
String locatorURI = "bisocket://" + host + ":" + port;
InvokerLocator serverLocator = new InvokerLocator(locatorURI); InvokerLocator serverLocator = new InvokerLocator(locatorURI);
server = new MageTransporterServer(serverLocator, new MageServerImpl(adminPassword, testMode), MageServer.class.getName(), new MageServerInvocationHandler()); server = new MageTransporterServer(serverLocator, new MageServerImpl(adminPassword, testMode), MageServer.class.getName(), new MageServerInvocationHandler());
server.start(); server.start();
@ -178,32 +176,6 @@ public class Main {
} }
} }
private static String getServerAddress() {
try {
String foundIP = "";
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
NetworkInterface iface = interfaces.nextElement( );
if (iface.isLoopback())
continue;
for (InterfaceAddress addr: iface.getInterfaceAddresses())
{
InetAddress iaddr = addr.getAddress();
if (iaddr instanceof Inet4Address) {
foundIP = iaddr.getHostAddress();
break;
}
}
if (foundIP.length() > 0)
break;
}
if (foundIP.length() > 0)
return foundIP;
} catch (SocketException ex) {
logger.warn("Could not get server address: ", ex);
}
return null;
}
private static Class<?> loadPlugin(Plugin plugin) { private static Class<?> loadPlugin(Plugin plugin) {
try { try {