Commit 9e8e4474 authored by Alexandre Ducarne's avatar Alexandre Ducarne
Browse files

Merge branch 'feature/com/refactorMessage' into 'int/v2'

pre-commit fix it

See merge request !47
parents d37a5448 624e74cc
...@@ -71,7 +71,6 @@ public class ClientApp extends Application { ...@@ -71,7 +71,6 @@ public class ClientApp extends Application {
@Override @Override
public void init() { public void init() {
initCores(); initCores();
comCoreClient.initWebSocketConnection("localhost", 8080);
} }
@Override @Override
......
...@@ -127,7 +127,7 @@ public class FXConnection { ...@@ -127,7 +127,7 @@ public class FXConnection {
this.loginTextFieldMissing.setVisible(true); this.loginTextFieldMissing.setVisible(true);
System.out.println("At least one field is missing."); System.out.println("At least one field is missing.");
} else { } else {
String hostname = this.loginTextFieldIPServerAddress + ":" + this.loginTextFieldPortNumber; String hostname = this.loginTextFieldIPServerAddress.getText() + ":" + this.loginTextFieldPortNumber.getText();
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
this.getModel().setUserLight(null); this.getModel().setUserLight(null);
......
...@@ -34,12 +34,17 @@ public class DataToComClientImpl implements IDataToCom { ...@@ -34,12 +34,17 @@ public class DataToComClientImpl implements IDataToCom {
*/ */
@Override @Override
public void connectUserOnline(UserLight userToConnect, String address) { public void connectUserOnline(UserLight userToConnect, String address) {
String port = address.split(":")[1];
String hostname = address.split(":")[0];
comCoreClient.initWebSocketConnection(hostname, Integer.parseInt(port));
comCoreClient.getMessageClientController().subscribeUser(userToConnect.getId().toString());
LoginUserToServerMessage message = new LoginUserToServerMessage(userToConnect, address); LoginUserToServerMessage message = new LoginUserToServerMessage(userToConnect, address);
comCoreClient.getMessageClientController().sendLoginUserToServer(message); comCoreClient.getMessageClientController().sendLoginUserToServer(message);
} }
@Override @Override
public void addNewGameAvailable(GameLight newGame) { public void addNewGameAvailable(GameLight newGame) {
comCoreClient.getMessageClientController().subscribeToRoom(newGame.getId().toString());
NewGameToServerMessage message = new NewGameToServerMessage(newGame); NewGameToServerMessage message = new NewGameToServerMessage(newGame);
comCoreClient.getMessageClientController().sendNewGameAvailableToServer(message); comCoreClient.getMessageClientController().sendNewGameAvailableToServer(message);
} }
......
package main.java.client.network; package main.java.client.network;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage; import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage;
import main.java.client.network.messages.clientToServer.LoginUserToServerMessage; import main.java.client.network.messages.clientToServer.LoginUserToServerMessage;
...@@ -8,8 +9,9 @@ import main.java.client.network.messages.clientToServer.NewGameToServerMessage; ...@@ -8,8 +9,9 @@ import main.java.client.network.messages.clientToServer.NewGameToServerMessage;
import main.java.client.network.messages.clientToServer.RequestGameListMessage; import main.java.client.network.messages.clientToServer.RequestGameListMessage;
import main.java.common.message.MessageMeta; import main.java.common.message.MessageMeta;
import main.java.server.network.messages.serverToClient.ChatMessageToClientMessage; import main.java.server.network.messages.serverToClient.ChatMessageToClientMessage;
import main.java.server.network.messages.serverToClient.NewUserAddedMessage;
import main.java.server.network.messages.serverToClient.GameListMessage; import main.java.server.network.messages.serverToClient.GameListMessage;
import main.java.server.network.messages.serverToClient.NewGameNotificationToClientMessage;
import main.java.server.network.messages.serverToClient.NewUserAddedMessage;
import main.java.server.network.messages.serverToClient.ServerStateOnConnectionMessage; import main.java.server.network.messages.serverToClient.ServerStateOnConnectionMessage;
/** /**
...@@ -104,6 +106,12 @@ public class MessageClientController { ...@@ -104,6 +106,12 @@ public class MessageClientController {
this.comCore.getComToDataClient().receivePlayersnGames(serverStateMessage.getUsersConnected(), this.comCore.getComToDataClient().receivePlayersnGames(serverStateMessage.getUsersConnected(),
serverStateMessage.getGamesOnline()); serverStateMessage.getGamesOnline());
break; break;
case "NewGameNotificationToClientMessage":
NewGameNotificationToClientMessage newGameMessage = (NewGameNotificationToClientMessage) message;
this.comCore.getComToDataClient().notifyNewGame(newGameMessage.getNewGame());
break;
case "NewUserAddedMessage": case "NewUserAddedMessage":
NewUserAddedMessage newUserMessage = (NewUserAddedMessage) message; NewUserAddedMessage newUserMessage = (NewUserAddedMessage) message;
this.comCore.getComToDataClient().addNewUser(newUserMessage.getUserToAdd()); this.comCore.getComToDataClient().addNewUser(newUserMessage.getUserToAdd());
...@@ -113,6 +121,17 @@ public class MessageClientController { ...@@ -113,6 +121,17 @@ public class MessageClientController {
GameListMessage gameListMessage = (GameListMessage) message; GameListMessage gameListMessage = (GameListMessage) message;
this.comCore.getComToDataClient().forwardGames(gameListMessage.getGameLights()); this.comCore.getComToDataClient().forwardGames(gameListMessage.getGameLights());
break; break;
default:
break;
}
} }
public void subscribeToRoom(String roomId) {
this.wsHandler.subscribeToRoom(roomId);
}
public void subscribeUser(String userId) {
this.wsHandler.subscribeUser(userId);
} }
} }
...@@ -40,7 +40,9 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter { ...@@ -40,7 +40,9 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
public void afterConnected(StompSession session, StompHeaders connectedHeaders) { public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
stompSession = session; stompSession = session;
LOGGER.info("New session established : " + session.getSessionId()); LOGGER.info("New session established : " + session.getSessionId());
synchronized (stompSession) {
stompSession.subscribe(RECEIVE_MESSAGE_TOPIC, this); stompSession.subscribe(RECEIVE_MESSAGE_TOPIC, this);
}
LOGGER.info("Subscribed to " + RECEIVE_MESSAGE_TOPIC); LOGGER.info("Subscribed to " + RECEIVE_MESSAGE_TOPIC);
} }
...@@ -85,13 +87,17 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter { ...@@ -85,13 +87,17 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
public void subscribeToRoom(String roomId) { public void subscribeToRoom(String roomId) {
final String destination = "/topic/" + roomId + "/room"; final String destination = "/topic/" + roomId + "/room";
synchronized (stompSession) {
stompSession.subscribe(destination, this); stompSession.subscribe(destination, this);
}
LOGGER.info("Subscribed to " + destination); LOGGER.info("Subscribed to " + destination);
} }
public void subscribeUser(String userId) { public void subscribeUser(String userId) {
final String destination = "/topic/" + userId + "/user"; final String destination = "/topic/" + userId + "/user";
synchronized (stompSession) {
stompSession.subscribe(destination, this); stompSession.subscribe(destination, this);
}
LOGGER.info("Subscribed to " + destination); LOGGER.info("Subscribed to " + destination);
this.currentUser = userId; this.currentUser = userId;
} }
......
package main.java.client.network.messages.serverToClient; package main.java.client.network.messages.serverToClient;
import main.java.client.network.ComCoreClient;
import main.java.common.dataModel.GameLight; import main.java.common.dataModel.GameLight;
import main.java.common.interfaces.client.IComToData; import main.java.common.interfaces.client.IComToData;
import main.java.common.message.MessageMeta; import main.java.common.message.MessageMeta;
...@@ -9,7 +10,7 @@ import main.java.common.message.MessageMeta; ...@@ -9,7 +10,7 @@ import main.java.common.message.MessageMeta;
*/ */
public class NewGameNotificationToClientMessage extends MessageMeta { public class NewGameNotificationToClientMessage extends MessageMeta {
private GameLight newGame; private GameLight newGame;
private ComCoreClient comCoreClient;
private IComToData iComToData; private IComToData iComToData;
public NewGameNotificationToClientMessage(GameLight newGame) { public NewGameNotificationToClientMessage(GameLight newGame) {
...@@ -21,7 +22,6 @@ public class NewGameNotificationToClientMessage extends MessageMeta { ...@@ -21,7 +22,6 @@ public class NewGameNotificationToClientMessage extends MessageMeta {
@Override @Override
public void processData() { public void processData() {
iComToData.notifyNewGame(this.newGame);
} }
public GameLight getNewGame() { public GameLight getNewGame() {
......
...@@ -9,6 +9,7 @@ import org.springframework.boot.SpringApplication; ...@@ -9,6 +9,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.Collections;
/** /**
* Class running the server app * Class running the server app
...@@ -29,7 +30,10 @@ public class ServerApp { ...@@ -29,7 +30,10 @@ public class ServerApp {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
LOGGER.debug("ServerApp"); LOGGER.debug("ServerApp");
SpringApplication.run(ServerApp.class, args); SpringApplication app = new SpringApplication(ServerApp.class);
app.setDefaultProperties(Collections
.singletonMap("server.port", "8080"));
app.run(args);
} }
private void initCores() { private void initCores() {
......
...@@ -21,7 +21,6 @@ public class DataToComServerImpl implements IDataToCom { ...@@ -21,7 +21,6 @@ public class DataToComServerImpl implements IDataToCom {
@Override @Override
public void notifyNewGame(GameLight newGameCreated) { public void notifyNewGame(GameLight newGameCreated) {
NewGameNotificationToClientMessage message = new NewGameNotificationToClientMessage(newGameCreated); NewGameNotificationToClientMessage message = new NewGameNotificationToClientMessage(newGameCreated);
MessageServerController.getInstance().sendNewGameNotification(message);
comCoreServer.getMessageServerController().sendNewGameNotification(message); comCoreServer.getMessageServerController().sendNewGameNotification(message);
} }
...@@ -42,7 +41,6 @@ public class DataToComServerImpl implements IDataToCom { ...@@ -42,7 +41,6 @@ public class DataToComServerImpl implements IDataToCom {
public void sendNewUserLists(UserLight playerToNotify, ArrayList<UserLight> playersOnline, public void sendNewUserLists(UserLight playerToNotify, ArrayList<UserLight> playersOnline,
ArrayList<GameLight> gamesOnline) { ArrayList<GameLight> gamesOnline) {
ServerStateOnConnectionMessage message = new ServerStateOnConnectionMessage(playerToNotify, playersOnline, gamesOnline); ServerStateOnConnectionMessage message = new ServerStateOnConnectionMessage(playerToNotify, playersOnline, gamesOnline);
MessageServerController.getInstance().sendListsToNewUser(message);
comCoreServer.getMessageServerController().sendListsToNewUser(message); comCoreServer.getMessageServerController().sendListsToNewUser(message);
} }
......
...@@ -6,6 +6,7 @@ import java.util.UUID; ...@@ -6,6 +6,7 @@ import java.util.UUID;
import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage; import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage;
import main.java.client.network.messages.clientToServer.LoginUserToServerMessage; import main.java.client.network.messages.clientToServer.LoginUserToServerMessage;
import main.java.client.network.messages.clientToServer.NewGameToServerMessage;
import main.java.common.dataModel.GameLight; import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.UserLight; import main.java.common.dataModel.UserLight;
import main.java.common.message.MessageMeta; import main.java.common.message.MessageMeta;
...@@ -86,8 +87,7 @@ public class MessageServerController { ...@@ -86,8 +87,7 @@ public class MessageServerController {
case "LoginUserToServerMessage": case "LoginUserToServerMessage":
// notify the data module of the new user // notify the data module of the new user
LoginUserToServerMessage loginMessage = (LoginUserToServerMessage) message; LoginUserToServerMessage loginMessage = (LoginUserToServerMessage) message;
MessageServerController.getInstance().getComCore().getComToData() comCore.getComToData().addAuthenticatedPlayer(loginMessage.getUserToAdd());
.addAuthenticatedPlayer(loginMessage.getUserToAdd());
// send lists to the new user // send lists to the new user
List<GameLight> gamesOnline = comCore.getComToData().getListGames(); List<GameLight> gamesOnline = comCore.getComToData().getListGames();
...@@ -113,18 +113,35 @@ public class MessageServerController { ...@@ -113,18 +113,35 @@ public class MessageServerController {
break; break;
case "RequestGameListMessage": case "RequestGameListMessage":
ComCoreServer comCore = MessageServerController.getInstance().getComCore();
GameListMessage gameListMessage = new GameListMessage(comCore.getComToData().getListGames()); GameListMessage gameListMessage = new GameListMessage(comCore.getComToData().getListGames());
MessageServerController.getInstance().sendGameList(gameListMessage); this.sendGameList(gameListMessage);
break;
case "NewGameToServerMessage":
NewGameToServerMessage newGameMessage = (NewGameToServerMessage) message;
comCore.getComToData().createGame(newGameMessage.getNewGame());
break;
default:
break; break;
} }
} }
/**
* Send a chat message to the clients of a game
*
* @param chatMessage the message to send
* @param gameId the ID of the room
*/
public void sendChatMessageToClients(ChatMessageToClientMessage chatMessage, UUID gameId) { public void sendChatMessageToClients(ChatMessageToClientMessage chatMessage, UUID gameId) {
//FIXME : when handling roomchat available, handle it to send chat message only on this chat room
wsController.sendToRoom(chatMessage, gameId.toString()); wsController.sendToRoom(chatMessage, gameId.toString());
} }
/**
* Notify the clients that a new game is created.
*
* @param message the message to send
*/
public void sendNewGameNotification(NewGameNotificationToClientMessage message) { public void sendNewGameNotification(NewGameNotificationToClientMessage message) {
wsController.sendToAll(message); wsController.sendToAll(message);
} }
......
...@@ -22,7 +22,6 @@ public class NewGameToServerMessage extends MessageMeta { ...@@ -22,7 +22,6 @@ public class NewGameToServerMessage extends MessageMeta {
@Override @Override
public void processData() { public void processData() {
iComToData.createGame(this.newGame);
} }
public GameLight getNewGame() { public GameLight getNewGame() {
......
package main.java.server.network.messages.serverToClient; package main.java.server.network.messages.serverToClient;
import main.java.client.network.ComCoreClient;
import main.java.common.dataModel.GameLight; import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.UserLight; import main.java.common.dataModel.UserLight;
import main.java.common.message.MessageMeta; import main.java.common.message.MessageMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
...@@ -15,9 +13,6 @@ public class ServerStateOnConnectionMessage extends MessageMeta { ...@@ -15,9 +13,6 @@ public class ServerStateOnConnectionMessage extends MessageMeta {
private List<UserLight> usersConnected; private List<UserLight> usersConnected;
private List<GameLight> gamesOnline; private List<GameLight> gamesOnline;
@Autowired(required = false)
private ComCoreClient comCoreClient;
public ServerStateOnConnectionMessage(UserLight receiver, List<UserLight> usersConnected, public ServerStateOnConnectionMessage(UserLight receiver, List<UserLight> usersConnected,
List<GameLight> gamesOnline) { List<GameLight> gamesOnline) {
this.receiver = receiver; this.receiver = receiver;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment