mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
ServerManager logging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.toop;
|
||||
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.toop.UI.GameSelectorWindow;
|
||||
import org.toop.eventbus.EventRegistry;
|
||||
import org.toop.eventbus.Events;
|
||||
@@ -17,8 +18,8 @@ public class Main {
|
||||
private static boolean running = false;
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
// Logging.disableAllLogs();
|
||||
// Logging.enableAllLogsForClass(EventRegistry.class);
|
||||
Logging.disableAllLogs();
|
||||
Logging.enableLogsForClass(ServerManager.class, Level.ALL);
|
||||
initSystems();
|
||||
registerEvents();
|
||||
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
package org.toop.UI;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.toop.eventbus.Events;
|
||||
import org.toop.eventbus.GlobalEventBus;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
||||
public class GameSelectorWindow extends JFrame {
|
||||
private static final Logger logger = LogManager.getLogger(GameSelectorWindow.class);
|
||||
|
||||
private JPanel mainMenu;
|
||||
private JTextField nameTextField;
|
||||
private JTextField name2TextField;
|
||||
@@ -70,22 +75,36 @@ public class GameSelectorWindow extends JFrame {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
|
||||
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
||||
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event.
|
||||
serverId,
|
||||
nameTextField.getText(),
|
||||
name2TextField.getText(),
|
||||
ticTacToeGame
|
||||
));
|
||||
String ticTacToeGameId;
|
||||
try {
|
||||
ticTacToeGameId = ticTacToeGame.get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} // TODO: Better error handling to not crash the system.
|
||||
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ReceivedMessage.class,
|
||||
event -> {
|
||||
if (event.message().equalsIgnoreCase("ok")) {
|
||||
logger.info("received ok from server.");
|
||||
} else if (event.message().toLowerCase().startsWith("gameid")) {
|
||||
String gameId = event.message().toLowerCase().replace("gameid ", "");
|
||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand("start_game " + gameId));
|
||||
}
|
||||
else {
|
||||
logger.info("{}", event.message());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", nameTextField.getText(), name2TextField.getText()));
|
||||
|
||||
// CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
|
||||
// GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event.
|
||||
// serverId,
|
||||
// nameTextField.getText(),
|
||||
// name2TextField.getText(),
|
||||
// ticTacToeGame
|
||||
// ));
|
||||
// String ticTacToeGameId;
|
||||
// try {
|
||||
// ticTacToeGameId = ticTacToeGame.get();
|
||||
// } catch (InterruptedException | ExecutionException ex) {
|
||||
// throw new RuntimeException(ex);
|
||||
// } // TODO: Better error handling to not crash the system.
|
||||
|
||||
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId)); // TODO: attempt connecting to the server with given ip, port and name.
|
||||
|
||||
frame.remove(mainMenu);
|
||||
UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this);
|
||||
|
||||
@@ -11,9 +11,10 @@ import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class UIGameBoard extends JFrame {
|
||||
private static final Logger logger = LogManager.getLogger(UIGameBoard.class);
|
||||
|
||||
private final int TICTACTOE_SIZE = 3;
|
||||
private final int REVERSI_SIZE = 8;
|
||||
private static final Logger logger = LogManager.getLogger(Main.class);
|
||||
private JLabel name;
|
||||
private JLabel ip;
|
||||
private JLabel gameName;
|
||||
|
||||
@@ -45,12 +45,12 @@ public class ServerManager {
|
||||
server = new TicTacToeServer(Integer.parseInt(port));
|
||||
}
|
||||
else {
|
||||
logger.error("Manager could not create a TcpServer for {}", gameType);
|
||||
logger.error("Manager could not create a server for game type: {}", gameType);
|
||||
return null;
|
||||
}
|
||||
this.servers.put(serverId, server);
|
||||
new Thread(server, "Server-" + serverId).start();
|
||||
logger.info("Connected to server {} at {}", serverId, port);
|
||||
logger.info("Created server with id: {}, port: {}", serverId, port);
|
||||
return serverId;
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to start server", e);
|
||||
|
||||
@@ -76,6 +76,33 @@ public class ParsedCommand {
|
||||
TicTacToeServerCommand commandEnum = TicTacToeServerCommand.getCommand(segments[0]);
|
||||
|
||||
switch (commandEnum) {
|
||||
case CREATE_GAME -> {
|
||||
if (segments.length == 3 && !segments[1].isEmpty() && !segments[2].isEmpty()) {
|
||||
this.command = commandEnum;
|
||||
this.arguments = new ArrayList<>(2);
|
||||
this.arguments.add(segments[1]);
|
||||
this.arguments.add(segments[2]);
|
||||
this.returnMessage = TicTacToeServerMessage.OK;
|
||||
this.isValidCommand = true;
|
||||
this.isServerCommand = true;
|
||||
this.errorMessage = null;
|
||||
this.originalCommand = receivedCommand;
|
||||
return;
|
||||
}
|
||||
}
|
||||
case END_GAME, START_GAME -> {
|
||||
if (segments.length == 2 && !segments[1].isEmpty()) {
|
||||
this.command = commandEnum;
|
||||
this.arguments = new ArrayList<>(1);
|
||||
this.arguments.add(segments[1]);
|
||||
this.returnMessage = TicTacToeServerMessage.OK;
|
||||
this.isValidCommand = true;
|
||||
this.isServerCommand = true;
|
||||
this.errorMessage = null;
|
||||
this.originalCommand = receivedCommand;
|
||||
return;
|
||||
}
|
||||
}
|
||||
case MOVE -> {
|
||||
if (segments.length == 2 && !segments[1].isEmpty()) {
|
||||
this.command = commandEnum;
|
||||
|
||||
@@ -104,9 +104,24 @@ public class TicTacToeServer extends TcpServer {
|
||||
}
|
||||
|
||||
private void handleServerCommand(ParsedCommand command) {
|
||||
if (command.isValidCommand) {
|
||||
this.sendQueue.offer("ok");
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
||||
if (command.command == TicTacToeServerCommand.CREATE_GAME) {
|
||||
String gameId = this.newGame((String) command.arguments.getFirst(), (String) command.arguments.get(1));
|
||||
this.sendQueue.offer("game created successfully|gameid " + gameId);
|
||||
} else if (command.command == TicTacToeServerCommand.START_GAME) {
|
||||
this.runGame((String) command.arguments.getFirst());
|
||||
this.sendQueue.offer("svr game is running successfully");
|
||||
} else if (command.command == TicTacToeServerCommand.END_GAME) {
|
||||
this.endGame((String) command.arguments.getFirst());
|
||||
this.sendQueue.offer("svr game ended successfully");
|
||||
} else if (command.command == TicTacToeServerCommand.LOGIN) {
|
||||
this.sendQueue.offer("svr login successful");
|
||||
} else if (command.command == TicTacToeServerCommand.SUBSCRIBE) {
|
||||
this.sendQueue.offer("svr added to ");
|
||||
}
|
||||
}
|
||||
|
||||
public void forwardGameMessages(TicTacToe game) {
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.toop.server.backend.tictactoe;
|
||||
import java.util.EnumSet;
|
||||
|
||||
public enum TicTacToeServerCommand {
|
||||
CREATE_GAME,
|
||||
START_GAME,
|
||||
END_GAME,
|
||||
/**
|
||||
* Login, "username"
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user