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
@@ -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;
}
}