mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Fixed bug where discovery wasn't working under windows
- getLocalHost() was returning the networks public IP address, changed this to getByName() for the local IP Tags: #story[1247]
This commit is contained in:
@@ -3,7 +3,10 @@ package seng302.gameServer;
|
|||||||
import javax.jmdns.JmDNS;
|
import javax.jmdns.JmDNS;
|
||||||
import javax.jmdns.ServiceInfo;
|
import javax.jmdns.ServiceInfo;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +36,7 @@ public class ServerAdvertiser {
|
|||||||
private Hashtable<String ,String> props;
|
private Hashtable<String ,String> props;
|
||||||
|
|
||||||
private ServerAdvertiser() throws IOException{
|
private ServerAdvertiser() throws IOException{
|
||||||
jmdnsInstance = JmDNS.create(InetAddress.getLocalHost());
|
jmdnsInstance = JmDNS.create(InetAddress.getByName(getLocalHostIp()));
|
||||||
|
|
||||||
props = new Hashtable<>();
|
props = new Hashtable<>();
|
||||||
props.put("map", "");
|
props.put("map", "");
|
||||||
@@ -115,4 +118,39 @@ public class ServerAdvertiser {
|
|||||||
if (serviceInfo != null)
|
if (serviceInfo != null)
|
||||||
jmdnsInstance.unregisterService(serviceInfo);
|
jmdnsInstance.unregisterService(serviceInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the local host ip address.
|
||||||
|
*
|
||||||
|
* @return the localhost ip address
|
||||||
|
*/
|
||||||
|
public static String getLocalHostIp() {
|
||||||
|
String ipAddress = null;
|
||||||
|
try {
|
||||||
|
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
||||||
|
while (e.hasMoreElements()) {
|
||||||
|
NetworkInterface ni = e.nextElement();
|
||||||
|
if (ni.isLoopback())
|
||||||
|
continue;
|
||||||
|
if(ni.isPointToPoint())
|
||||||
|
continue;
|
||||||
|
if(ni.isVirtual())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
||||||
|
while(addresses.hasMoreElements()) {
|
||||||
|
InetAddress address = addresses.nextElement();
|
||||||
|
if(address instanceof Inet4Address) { // skip all ipv6
|
||||||
|
ipAddress = address.getHostAddress();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if (ipAddress == null) {
|
||||||
|
System.out.println("[HOST] Cannot obtain local host ip address.");
|
||||||
|
}
|
||||||
|
return ipAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static seng302.gameServer.ServerAdvertiser.getLocalHostIp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listens for servers on the local network
|
* Listens for servers on the local network
|
||||||
*/
|
*/
|
||||||
@@ -86,7 +88,7 @@ public class ServerListener{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ServerListener() throws IOException {
|
private ServerListener() throws IOException {
|
||||||
jmdns = JmDNS.create(InetAddress.getLocalHost());
|
jmdns = JmDNS.create(InetAddress.getByName(getLocalHostIp()));
|
||||||
listener = new GameServeMonitor();
|
listener = new GameServeMonitor();
|
||||||
jmdns.addServiceListener(ServerAdvertiser.SERVICE_TYPE, listener);
|
jmdns.addServiceListener(ServerAdvertiser.SERVICE_TYPE, listener);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import javafx.scene.control.ListView;
|
|||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.layout.GridPane;
|
import javafx.scene.layout.GridPane;
|
||||||
|
import seng302.gameServer.ServerAdvertiser;
|
||||||
import seng302.gameServer.ServerDescription;
|
import seng302.gameServer.ServerDescription;
|
||||||
import seng302.visualiser.GameClient;
|
import seng302.visualiser.GameClient;
|
||||||
import seng302.visualiser.ServerListener;
|
import seng302.visualiser.ServerListener;
|
||||||
@@ -94,7 +95,7 @@ public class StartScreenController implements Initializable, ServerListenerDeleg
|
|||||||
public void hostButtonPressed() {
|
public void hostButtonPressed() {
|
||||||
// new GameState(getLocalHostIp());
|
// new GameState(getLocalHostIp());
|
||||||
gameClient = new GameClient(holder);
|
gameClient = new GameClient(holder);
|
||||||
gameClient.runAsHost(getLocalHostIp(), 4942);
|
gameClient.runAsHost(ServerAdvertiser.getLocalHostIp(), 4942);
|
||||||
// try {
|
// try {
|
||||||
//// String ipAddress = InetAddress.getLocalHost().getHostAddress();
|
//// String ipAddress = InetAddress.getLocalHost().getHostAddress();
|
||||||
//// new GameState(ipAddress);
|
//// new GameState(ipAddress);
|
||||||
@@ -155,42 +156,7 @@ public class StartScreenController implements Initializable, ServerListenerDeleg
|
|||||||
// this.controller = controller;
|
// this.controller = controller;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the local host ip address and sets this ip to ClientState.
|
|
||||||
* Only runs by the host.
|
|
||||||
*
|
|
||||||
* @return the localhost ip address
|
|
||||||
*/
|
|
||||||
private String getLocalHostIp() {
|
|
||||||
String ipAddress = null;
|
|
||||||
try {
|
|
||||||
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
|
||||||
while (e.hasMoreElements()) {
|
|
||||||
NetworkInterface ni = e.nextElement();
|
|
||||||
if (ni.isLoopback())
|
|
||||||
continue;
|
|
||||||
if(ni.isPointToPoint())
|
|
||||||
continue;
|
|
||||||
if(ni.isVirtual())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
|
||||||
while(addresses.hasMoreElements()) {
|
|
||||||
InetAddress address = addresses.nextElement();
|
|
||||||
if(address instanceof Inet4Address) { // skip all ipv6
|
|
||||||
ipAddress = address.getHostAddress();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
if (ipAddress == null) {
|
|
||||||
System.out.println("[HOST] Cannot obtain local host ip address.");
|
|
||||||
}
|
|
||||||
// ClientState.setHostIp(ipAddress);
|
|
||||||
return ipAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the server list with new information
|
* Update the server list with new information
|
||||||
|
|||||||
Reference in New Issue
Block a user