Commit 624e74cc authored by alexandre.ducarne's avatar alexandre.ducarne
Browse files

Add ws support when creating games

Use data typed in main form for port and host
Add config to change server port
parent d37a5448
......@@ -71,7 +71,6 @@ public class ClientApp extends Application {
@Override
public void init() {
initCores();
comCoreClient.initWebSocketConnection("localhost", 8080);
}
@Override
......
......@@ -127,7 +127,7 @@ public class FXConnection {
this.loginTextFieldMissing.setVisible(true);
System.out.println("At least one field is missing.");
} else {
String hostname = this.loginTextFieldIPServerAddress + ":" + this.loginTextFieldPortNumber;
String hostname = this.loginTextFieldIPServerAddress.getText() + ":" + this.loginTextFieldPortNumber.getText();
try {
long start = System.currentTimeMillis();
this.getModel().setUserLight(null);
......
......@@ -34,12 +34,17 @@ public class DataToComClientImpl implements IDataToCom {
*/
@Override
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);
comCoreClient.getMessageClientController().sendLoginUserToServer(message);
}
@Override
public void addNewGameAvailable(GameLight newGame) {
comCoreClient.getMessageClientController().subscribeToRoom(newGame.getId().toString());
NewGameToServerMessage message = new NewGameToServerMessage(newGame);
comCoreClient.getMessageClientController().sendNewGameAvailableToServer(message);
}
......
package main.java.client.network;
import java.util.Optional;
import java.util.UUID;
import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage;
import main.java.client.network.messages.clientToServer.LoginUserToServerMessage;
......@@ -8,8 +9,9 @@ import main.java.client.network.messages.clientToServer.NewGameToServerMessage;
import main.java.client.network.messages.clientToServer.RequestGameListMessage;
import main.java.common.message.MessageMeta;
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.NewGameNotificationToClientMessage;
import main.java.server.network.messages.serverToClient.NewUserAddedMessage;
import main.java.server.network.messages.serverToClient.ServerStateOnConnectionMessage;
/**
......@@ -88,7 +90,7 @@ public class MessageClientController {
*
* @param message the message to send
*/
public void sendGameListToData(GameListMessage message) {
public void sendGameListToData(GameListMessage message) {
wsHandler.sendMessage(message);
}
......@@ -104,15 +106,32 @@ public class MessageClientController {
this.comCore.getComToDataClient().receivePlayersnGames(serverStateMessage.getUsersConnected(),
serverStateMessage.getGamesOnline());
break;
case "NewGameNotificationToClientMessage":
NewGameNotificationToClientMessage newGameMessage = (NewGameNotificationToClientMessage) message;
this.comCore.getComToDataClient().notifyNewGame(newGameMessage.getNewGame());
break;
case "NewUserAddedMessage":
NewUserAddedMessage newUserMessage = (NewUserAddedMessage) message;
this.comCore.getComToDataClient().addNewUser(newUserMessage.getUserToAdd());
break;
NewUserAddedMessage newUserMessage = (NewUserAddedMessage) message;
this.comCore.getComToDataClient().addNewUser(newUserMessage.getUserToAdd());
break;
case "GameListMessage":
GameListMessage gameListMessage = (GameListMessage) message;
this.comCore.getComToDataClient().forwardGames(gameListMessage.getGameLights());
GameListMessage gameListMessage = (GameListMessage) message;
this.comCore.getComToDataClient().forwardGames(gameListMessage.getGameLights());
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 {
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
stompSession = session;
LOGGER.info("New session established : " + session.getSessionId());
stompSession.subscribe(RECEIVE_MESSAGE_TOPIC, this);
synchronized (stompSession) {
stompSession.subscribe(RECEIVE_MESSAGE_TOPIC, this);
}
LOGGER.info("Subscribed to " + RECEIVE_MESSAGE_TOPIC);
}
......@@ -85,13 +87,17 @@ public class WebSocketStompSessionHandler extends StompSessionHandlerAdapter {
public void subscribeToRoom(String roomId) {
final String destination = "/topic/" + roomId + "/room";
stompSession.subscribe(destination, this);
synchronized (stompSession) {
stompSession.subscribe(destination, this);
}
LOGGER.info("Subscribed to " + destination);
}
public void subscribeUser(String userId) {
final String destination = "/topic/" + userId + "/user";
stompSession.subscribe(destination, this);
synchronized (stompSession) {
stompSession.subscribe(destination, this);
}
LOGGER.info("Subscribed to " + destination);
this.currentUser = userId;
}
......
package main.java.client.network.messages.serverToClient;
import main.java.client.network.ComCoreClient;
import main.java.common.dataModel.GameLight;
import main.java.common.interfaces.client.IComToData;
import main.java.common.message.MessageMeta;
......@@ -9,7 +10,7 @@ import main.java.common.message.MessageMeta;
*/
public class NewGameNotificationToClientMessage extends MessageMeta {
private GameLight newGame;
private ComCoreClient comCoreClient;
private IComToData iComToData;
public NewGameNotificationToClientMessage(GameLight newGame) {
......@@ -21,7 +22,6 @@ public class NewGameNotificationToClientMessage extends MessageMeta {
@Override
public void processData() {
iComToData.notifyNewGame(this.newGame);
}
public GameLight getNewGame() {
......
......@@ -9,6 +9,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import java.util.Collections;
/**
* Class running the server app
......@@ -29,7 +30,10 @@ public class ServerApp {
*/
public static void main(String[] args) {
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() {
......
......@@ -21,7 +21,6 @@ public class DataToComServerImpl implements IDataToCom {
@Override
public void notifyNewGame(GameLight newGameCreated) {
NewGameNotificationToClientMessage message = new NewGameNotificationToClientMessage(newGameCreated);
MessageServerController.getInstance().sendNewGameNotification(message);
comCoreServer.getMessageServerController().sendNewGameNotification(message);
}
......@@ -42,7 +41,6 @@ public class DataToComServerImpl implements IDataToCom {
public void sendNewUserLists(UserLight playerToNotify, ArrayList<UserLight> playersOnline,
ArrayList<GameLight> gamesOnline) {
ServerStateOnConnectionMessage message = new ServerStateOnConnectionMessage(playerToNotify, playersOnline, gamesOnline);
MessageServerController.getInstance().sendListsToNewUser(message);
comCoreServer.getMessageServerController().sendListsToNewUser(message);
}
......
......@@ -6,6 +6,7 @@ import java.util.UUID;
import main.java.client.network.messages.clientToServer.ChatMessageToServerMessage;
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.UserLight;
import main.java.common.message.MessageMeta;
......@@ -86,8 +87,7 @@ public class MessageServerController {
case "LoginUserToServerMessage":
// notify the data module of the new user
LoginUserToServerMessage loginMessage = (LoginUserToServerMessage) message;
MessageServerController.getInstance().getComCore().getComToData()
.addAuthenticatedPlayer(loginMessage.getUserToAdd());
comCore.getComToData().addAuthenticatedPlayer(loginMessage.getUserToAdd());
// send lists to the new user
List<GameLight> gamesOnline = comCore.getComToData().getListGames();
......@@ -95,7 +95,7 @@ public class MessageServerController {
ServerStateOnConnectionMessage msg = new ServerStateOnConnectionMessage(loginMessage.getUserToAdd(),
usersConnected, gamesOnline);
this.sendListsToNewUser(msg);
//notify others of the fact a new user is available
NewUserAddedMessage userToAdd = new NewUserAddedMessage(loginMessage.getUserToAdd());
this.notifyNewUserAdded(userToAdd);
......@@ -111,23 +111,40 @@ public class MessageServerController {
this.sendChatMessageToClients(new ChatMessageToClientMessage(chatMessage.getChatMessage()),
chatMessage.getGameId());
break;
case "RequestGameListMessage":
ComCoreServer comCore = MessageServerController.getInstance().getComCore();
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;
}
}
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());
}
/**
* 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) {
wsController.sendToRoom(chatMessage, gameId.toString());
}
public void sendNewGameNotification(NewGameNotificationToClientMessage message) {
wsController.sendToAll(message);
}
/**
* Notify the clients that a new game is created.
*
* @param message the message to send
*/
public void sendNewGameNotification(NewGameNotificationToClientMessage message) {
wsController.sendToAll(message);
}
/**
* Send a GameListMessage to clients
......
......@@ -22,7 +22,6 @@ public class NewGameToServerMessage extends MessageMeta {
@Override
public void processData() {
iComToData.createGame(this.newGame);
}
public GameLight getNewGame() {
......
package main.java.server.network.messages.serverToClient;
import main.java.client.network.ComCoreClient;
import main.java.common.dataModel.GameLight;
import main.java.common.dataModel.UserLight;
import main.java.common.message.MessageMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
......@@ -15,9 +13,6 @@ public class ServerStateOnConnectionMessage extends MessageMeta {
private List<UserLight> usersConnected;
private List<GameLight> gamesOnline;
@Autowired(required = false)
private ComCoreClient comCoreClient;
public ServerStateOnConnectionMessage(UserLight receiver, List<UserLight> usersConnected,
List<GameLight> gamesOnline) {
this.receiver = receiver;
......
Markdown is supported
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