Merge remote-tracking branch 'origin/development' into development

This commit is contained in:
Fleischer Hanno
2024-12-01 18:20:16 +01:00

View File

@@ -26,6 +26,7 @@ public class MdgaServer implements MessageListener<HostedConnection>, Connection
private static final Logger LOGGER = System.getLogger(MdgaServer.class.getName()); private static final Logger LOGGER = System.getLogger(MdgaServer.class.getName());
private Server myServer; private Server myServer;
private static int port;
private final ServerGameLogic logic; private final ServerGameLogic logic;
private final BlockingQueue<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>(); private final BlockingQueue<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>();
@@ -41,37 +42,38 @@ public class MdgaServer implements MessageListener<HostedConnection>, Connection
} }
/** /**
* Starts the Battleships server. * Constructor.
*
* @param port as the port for this server
*/ */
public static void main(String[] args) { public MdgaServer(int port) {
new MdgaServer().run(); MdgaServer.port = port;
}
/**
* Creates a new MdgaServer.
*/
public MdgaServer() {
LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS
logic = new ServerGameLogic(this, new Game()); logic = new ServerGameLogic(this, new Game());
} }
/**
*
*/
public void run() { public void run() {
startServer(); startServer();
this.connectionAdded(myServer, myServer.getConnection(0));
while (true) while (true)
processNextMessage(); processNextMessage();
} }
/**
*
*/
private void startServer() { private void startServer() {
try { try {
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
myServer = Network.createServer(1234); myServer = Network.createServer(port);
initializeSerializables(); initializeSerializables();
myServer.start(); myServer.start();
registerListeners(); registerListeners();
LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS LOGGER.log(Level.INFO, "Server started: {0}", myServer.isRunning()); //NON-NLS
} catch (IOException e) { }
catch (IOException e) {
LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS LOGGER.log(Level.ERROR, "Couldn't start server: {0}", e.getMessage()); //NON-NLS
exit(1); exit(1);
} }
@@ -186,8 +188,6 @@ private void messageReceived(HostedConnection source, ClientMessage message) {
@Override @Override
public void connectionAdded(Server server, HostedConnection hostedConnection) { public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
// ToDo: Synchronize data between server and client.
logic.getGame().addPlayer(hostedConnection.getId(), new Player());
} }
@Override @Override