Fixed discovery bug, implemented server list, added server parameters

- Resolved DNS bug by updating to a newer version of JmDNS
- Added server list, this is populated with new servers as they are discovered
- Added map name and spaces remaining to server advertisement

Tags: #story[1247]
This commit is contained in:
Michael Rausch
2017-08-31 01:11:17 +12:00
parent 262f27fa8a
commit 0c5d661995
10 changed files with 220 additions and 35 deletions
@@ -1,5 +1,8 @@
package seng302.gameServer;
import javafx.application.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import seng302.gameServer.messages.*;
import seng302.model.GeoPoint;
import seng302.model.Player;
@@ -37,6 +40,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
private ServerSocket serverSocket = null;
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
private Logger logger = LoggerFactory.getLogger(MainServerThread.class);
public MainServerThread() {
new GameState("localhost");
@@ -45,6 +49,15 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
} catch (IOException e) {
serverLog("IO error in server thread handler upon trying to make new server socket", 0);
}
// Start advertising server
try{
ServerAdvertiser.getInstance().registerGame(PORT, "PP Test Server", 10, "Random Map");
} catch (IOException e) {
logger.warn("Could not register server");
}
PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv"));
GameState.addMarkPassListener(this::broadcastMessage);
terminated = false;
@@ -206,6 +219,12 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
}
public void startGame() {
try {
ServerAdvertiser.getInstance().unregister();
} catch (IOException e) {
logger.warn("Error unregistering server");
}
initialiseBoatPositions();
Timer t = new Timer();
@@ -4,11 +4,12 @@ import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Hashtable;
public class ServerAdvertiser {
private static String SERVICE = "_partyatsea_";
private static String SERVICE = "_partyatsea";
private static String PROTOCOL = "_tcp";
public static String SERVICE_TYPE = SERVICE + "." + PROTOCOL + ".local";
public static String SERVICE_TYPE = SERVICE + "." + PROTOCOL + ".local.";
private static Integer PROTO_VERSION = 1;
@@ -16,7 +17,7 @@ public class ServerAdvertiser {
private static JmDNS jmdnsInstance = null;
private ServerAdvertiser() throws IOException{
jmdnsInstance = JmDNS.create(InetAddress.getLocalHost(), InetAddress.getByName(InetAddress.getLocalHost().getHostName()).toString());
jmdnsInstance = JmDNS.create(InetAddress.getLocalHost());
}
public static ServerAdvertiser getInstance() throws IOException {
@@ -27,14 +28,30 @@ public class ServerAdvertiser {
return instance;
}
public void registerGame(Integer portNo, String serverName, Integer spacesLeft, String mapName) throws IOException {
String serviceData = packageServerData(spacesLeft, mapName, PROTO_VERSION);
ServiceInfo serviceInfo = ServiceInfo.create(SERVICE_TYPE, serverName, portNo, serviceData);
public void registerGame(Integer portNo, String serverName, Integer spacesLeft, String mapName) {
Hashtable<String ,String> props = new Hashtable<>();
jmdnsInstance.registerService(serviceInfo);
props.put("map", mapName);
props.put("spacesLeft", spacesLeft.toString());
ServiceInfo serviceInfo = ServiceInfo.create(SERVICE_TYPE, serverName, portNo, 0, 0, props);
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
try {
jmdnsInstance.registerService(serviceInfo);
} catch (IOException e) {
System.out.println("Failed");
}
}
}, 0
);
}
private String packageServerData(Integer spacesLeft, String mapName, Integer version){
return spacesLeft.toString() + "|" + mapName + "|" + version.toString();
public void unregister(){
jmdnsInstance.unregisterAllServices();
jmdnsInstance = null;
}
}
@@ -0,0 +1,39 @@
package seng302.gameServer;
public class ServerDescription {
private String address;
private Integer portNum;
private String serverName;
private String mapName;
private Integer spacesLeft;
public ServerDescription(String serverName, String mapName, Integer spacesLeft, String address, Integer portNum){
this.serverName = serverName;
this.mapName = mapName;
this.spacesLeft = spacesLeft;
this.address = address;
this.portNum = portNum;
}
public String getName() {
return serverName;
}
public String getMapName() {
return mapName;
}
public Integer portNumber() {
return portNum;
}
public String getAddress(){
return address;
}
public Integer spacesLeft() {
return spacesLeft;
}
}