mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Implemented server re-registration when a server closes / updates
- When a server is closed, it will disappear from the server list - When a player joins a server, the number of spaces left will decrease - Servers now disappear instead of duplicating - Added tests for ServerDescription - Added documentation for new classes Tags: #story[1247]
This commit is contained in:
@@ -1,17 +1,26 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.gameServer.messages.*;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.PolarTable;
|
||||
import seng302.model.ServerYacht;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.stream.xml.parser.RegattaXMLData;
|
||||
import seng302.utilities.GeoUtility;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.utilities.XMLParser;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.net.ServerSocket;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
@@ -42,6 +51,41 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
|
||||
private Logger logger = LoggerFactory.getLogger(MainServerThread.class);
|
||||
|
||||
private void startAdvertisingServer(){
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db;
|
||||
Document doc;
|
||||
XMLGenerator generator = new XMLGenerator();
|
||||
|
||||
try {
|
||||
db = dbf.newDocumentBuilder();
|
||||
String regatta = generator.getRegattaAsXml();
|
||||
StringReader stringReader = new StringReader(regatta);
|
||||
InputSource is = new InputSource(stringReader);
|
||||
doc = db.parse(is);
|
||||
} catch (ParserConfigurationException | IOException | SAXException e) {
|
||||
logger.warn("Couldn't load race regatta");
|
||||
return;
|
||||
}
|
||||
|
||||
RegattaXMLData regattaXMLData = XMLParser.parseRegatta(doc);
|
||||
|
||||
Integer spacesLeft = GameState.getSpacesLeft();
|
||||
|
||||
// No spaces left on server
|
||||
if (spacesLeft < 1){
|
||||
return;
|
||||
}
|
||||
|
||||
// Start advertising server
|
||||
try{
|
||||
ServerAdvertiser.getInstance().setMapName(regattaXMLData.getCourseName()).setSpacesLeft(spacesLeft);
|
||||
ServerAdvertiser.getInstance().registerGame(PORT, regattaXMLData.getRegattaName());
|
||||
} catch (IOException e) {
|
||||
logger.warn("Could not register server");
|
||||
}
|
||||
}
|
||||
|
||||
public MainServerThread() {
|
||||
new GameState("localhost");
|
||||
try {
|
||||
@@ -50,13 +94,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
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");
|
||||
}
|
||||
|
||||
startAdvertisingServer();
|
||||
|
||||
PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv"));
|
||||
GameState.addMarkPassListener(this::broadcastMessage);
|
||||
@@ -189,6 +227,12 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
}
|
||||
});
|
||||
serverToClientThread.addDisconnectListener(this::clientDisconnected);
|
||||
|
||||
try {
|
||||
ServerAdvertiser.getInstance().setSpacesLeft(GameState.getSpacesLeft());
|
||||
} catch (IOException e) {
|
||||
logger.warn("Couldn't update advertisement");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,6 +259,13 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
}
|
||||
}
|
||||
serverToClientThreads.remove(closedConnection);
|
||||
|
||||
try {
|
||||
ServerAdvertiser.getInstance().setSpacesLeft(GameState.getSpacesLeft());
|
||||
} catch (IOException e) {
|
||||
logger.warn("Couldn't update advertisement");
|
||||
}
|
||||
|
||||
closedConnection.terminate();
|
||||
}
|
||||
|
||||
@@ -222,7 +273,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
try {
|
||||
ServerAdvertiser.getInstance().unregister();
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error unregistering server");
|
||||
logger.warn("Error unregistered server");
|
||||
}
|
||||
|
||||
initialiseBoatPositions();
|
||||
|
||||
Reference in New Issue
Block a user