Commit 6c0458e8 authored by alexandre.ducarne's avatar alexandre.ducarne
Browse files

Use Autowire

parent 3fbfbc96
package main.java.client;
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.stage.Stage;
import main.java.client.data.DataCoreClient;
import main.java.client.game.GameCore;
import main.java.client.main.controller.MainControllerFX;
import main.java.client.main.controller.MainCore;
import main.java.client.network.ComCoreClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.File;
/**
* Main Client application
*/
@SpringBootApplication
public class ClientApp extends Application {
private static final Logger LOGGER = LogManager.getLogger(ClientApp.class);
private static ComCoreClient comCoreClient;
private static DataCoreClient dataCoreClient;
public static MainCore mainCore;
private static GameCore gameCore;
public static final String APPLICATION_NAME = "Othello";
public static final int APPLICATION_WIDTH = 889;
public static final int APPLICATION_HEIGHT = 500;
// css definition
private static final File cssFile = new File("src/baleine/client/main/ressources/main.css");
public static final String CSS_FILE_PATH = "file:///" + ClientApp.cssFile.getAbsolutePath().replace("\\", "/");
public static final String APPLICATION_NAME = "Othello";
public static final int APPLICATION_WIDTH = 889;
public static final int APPLICATION_HEIGHT = 500;
private static final Logger LOGGER = LogManager.getLogger(ClientApp.class);
// css definition
private static final File cssFile = new File("src/baleine/client/main/ressources/main.css");
public static final String CSS_FILE_PATH = "file:///" + ClientApp.cssFile.getAbsolutePath().replace("\\", "/");
public static MainCore mainCore;
private static ComCoreClient comCoreClient;
private static DataCoreClient dataCoreClient;
private static GameCore gameCore;
private ConfigurableApplicationContext springContext;
private Parent rootNode;
private FXMLLoader fxmlLoader;
/**
* Run the client app (JavaFX)
*
* @param args additional args
* @throws Exception TODO: remove once the Websocket is initialized in the
* proper class
*/
public static void main(String[] args) {
LOGGER.debug("ClientApp");
initCores();
// TODO: call this method once user is connected to a server in ihm main.
comCoreClient.initWebSocketConnection("localhost", 8080);
launch(args);
}
/**
* Run the client app (JavaFX)
*
* @param args additional args
* @throws Exception TODO: remove once the Websocket is initialized in the
* proper class
*/
public static void main(String[] args) {
LOGGER.debug("ClientApp");
launch(args);
}
private static void initCores() {
// Com Core
comCoreClient = new ComCoreClient();
private static void initCores() {
// IHM Game Core
gameCore = new GameCore();
// Data Core
dataCoreClient = new DataCoreClient();
// IHM Main Core
mainCore = new MainCore(new MainControllerFX());
// IHM Game Core
gameCore = new GameCore();
dataCoreClient = new DataCoreClient();
comCoreClient = new ComCoreClient();
// IHM Main Core
mainCore = new MainCore(new MainControllerFX());
// Set implementations
dataCoreClient.setDataToCom(comCoreClient.getDataToComClientImpl());
dataCoreClient.setDataToMain(mainCore.getDataToMainImpl());
dataCoreClient.setDataToGame(gameCore.getDataToGame());
comCoreClient.setComToDataClient(dataCoreClient.getClientComToDataImpl());
mainCore.setMainToData(dataCoreClient.getMainToDataImpl());
gameCore.setGameToData(dataCoreClient.getGameToDataImpl());
// TODO missing: gameCore.setGameToData(dataCoreClient.getDataToGameImpl());
}
// Set implementations
dataCoreClient.setDataToCom(comCoreClient.getDataToComClientImpl());
dataCoreClient.setDataToMain(mainCore.getDataToMainImpl());
dataCoreClient.setDataToGame(gameCore.getDataToGame());
comCoreClient.setComToDataClient(dataCoreClient.getClientComToDataImpl());
mainCore.setMainToData(dataCoreClient.getMainToDataImpl());
gameCore.setGameToData(dataCoreClient.getGameToDataImpl());
}
@Override
public void init() {
initCores();
comCoreClient.initWebSocketConnection("localhost", 8080);
}
@Override
public void start(Stage stage) throws Exception {
System.out.println(ClientApp.APPLICATION_NAME + " is starting...");
ClientApp.mainCore.getFX().initialize("../fxml/login.fxml", stage);
}
@Override
public void start(Stage stage) throws Exception {
System.out.println(ClientApp.APPLICATION_NAME + " is starting...");
ClientApp.mainCore.getFX().initialize("../fxml/login.fxml", stage);
}
}
......@@ -4,16 +4,18 @@ import main.java.common.interfaces.client.IComToData;
import main.java.common.interfaces.client.IDataToCom;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
/**
* Communication core containing message controller and interfaces.
*/
@Component
public class ComCoreClient {
private static final Logger LOGGER = LogManager.getLogger(ComCoreClient.class);
private DataToComClientImpl dataToComClientImpl;
private IComToData comToDataClient;
private MessageClientController messageClientController;
private static final Logger LOGGER = LogManager.getLogger(ComCoreClient.class);
private DataToComClientImpl dataToComClientImpl;
private IComToData comToDataClient;
private MessageClientController messageClientController;
public ComCoreClient() {
dataToComClientImpl = new DataToComClientImpl(this);
......@@ -21,51 +23,51 @@ public class ComCoreClient {
messageClientController.setComCore(this);
}
/**
* Get the DataToCom interface implementation
*
* @return the implementation
*/
public IDataToCom getDataToComClientImpl() {
return dataToComClientImpl;
}
/**
* Get the DataToCom interface implementation
*
* @return the implementation
*/
public IDataToCom getDataToComClientImpl() {
return dataToComClientImpl;
}
/**
* Get the ComToData interface implementation
*
* @return the implementation
*/
public IComToData getComToDataClient() {
return comToDataClient;
}
/**
* Get the ComToData interface implementation
*
* @return the implementation
*/
public IComToData getComToDataClient() {
return comToDataClient;
}
public void setComToDataClient(IComToData comToDataClient) {
this.comToDataClient = comToDataClient;
}
public void setComToDataClient(IComToData comToDataClient) {
this.comToDataClient = comToDataClient;
}
/**
* Get the message controller associated to the client
*
* @return the message controller
*/
public MessageClientController getMessageClientController() {
return messageClientController;
}
/**
* Get the message controller associated to the client
*
* @return the message controller
*/
public MessageClientController getMessageClientController() {
return messageClientController;
}
/**
* Init the WebSocket connection process
*
* @param hostname the hostname url
* @param port the port
*/
public void initWebSocketConnection(String hostname, int port) {
try {
WebSocketStompSessionHandler webSocketStompSessionHandler = WebSocketStompSessionInitializer.init(hostname,
port);
this.messageClientController.setWsHandler(webSocketStompSessionHandler);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error when trying to setup WebSocket connection");
}
}
/**
* Init the WebSocket connection process
*
* @param hostname the hostname url
* @param port the port
*/
public void initWebSocketConnection(String hostname, int port) {
try {
WebSocketStompSessionHandler webSocketStompSessionHandler = WebSocketStompSessionInitializer.init(hostname,
port);
this.messageClientController.setWsHandler(webSocketStompSessionHandler);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error when trying to setup WebSocket connection");
}
}
}
package main.java.common.dataModel;
import java.util.Date;
import java.util.UUID;
public class UserLight extends UserMeta {
private String lastName;
private String firstName;
private Date dateOfBirth;
private int avatarId;
private int playedGames;
private int wonGames;
public UserLight(UUID id, String pseudo, String lastName, String firstName, Date dateOfBirth, int avatarId, int playedGames, int wonGames) {
super(id, pseudo);
this.lastName = lastName;
this.firstName = firstName;
this.dateOfBirth = dateOfBirth;
this.avatarId = avatarId;
this.playedGames = playedGames;
this.wonGames = wonGames;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getAvatarId() {
return avatarId;
}
public void setAvatarId(int avatarId) {
this.avatarId = avatarId;
}
public int getPlayedGames() {
return playedGames;
}
public void setPlayedGames(int playedGames) {
this.playedGames = playedGames;
}
public int getWonGames() {
return wonGames;
}
public void setWonGames(int wonGames) {
this.wonGames = wonGames;
}
public UserZero convertToUserZero() {
return new UserZero(this.getId(), this.getPseudo());
}
@Override
public String toString() {
return "UserLight{" +
"lastName='" + lastName + '\'' +
", firstName='" + firstName + '\'' +
", dateOfBirth=" + dateOfBirth +
", avatarId=" + avatarId +
", playedGames=" + playedGames +
", wonGames=" + wonGames +
'}';
}
private String lastName;
private String firstName;
private Date dateOfBirth;
private int avatarId;
private int playedGames;
private int wonGames;
public UserLight(UUID id, String pseudo, String lastName, String firstName, Date dateOfBirth, int avatarId, int playedGames, int wonGames) {
super(id, pseudo);
this.lastName = lastName;
this.firstName = firstName;
this.dateOfBirth = dateOfBirth;
this.avatarId = avatarId;
this.playedGames = playedGames;
this.wonGames = wonGames;
}
public UserLight(UUID id, String pseudo) {
super(id, pseudo);
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getAvatarId() {
return avatarId;
}
public void setAvatarId(int avatarId) {
this.avatarId = avatarId;
}
public int getPlayedGames() {
return playedGames;
}
public void setPlayedGames(int playedGames) {
this.playedGames = playedGames;
}
public int getWonGames() {
return wonGames;
}
public void setWonGames(int wonGames) {
this.wonGames = wonGames;
}
public UserZero convertToUserZero() {
return new UserZero(this.getId(), this.getPseudo());
}
@Override
public String toString() {
return "UserLight{" +
"lastName='" + lastName + '\'' +
", firstName='" + firstName + '\'' +
", dateOfBirth=" + dateOfBirth +
", avatarId=" + avatarId +
", playedGames=" + playedGames +
", wonGames=" + wonGames +
'}';
}
}
package main.java.server;
import main.java.server.data.DataCoreServer;
import main.java.server.network.ComCoreServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import main.java.server.data.DataCoreServer;
import main.java.server.network.ComCoreServer;
import javax.annotation.PostConstruct;
/**
* Class running the server app
*/
@SpringBootApplication
public class ServerApp {
private static final Logger LOGGER = LogManager.getLogger(ServerApp.class);
private static final Logger LOGGER = LogManager.getLogger(ServerApp.class);
@Autowired
private ComCoreServer comCoreServer;
@Autowired
private DataCoreServer dataCoreServer;
private static ComCoreServer comCoreServer;
private static DataCoreServer dataCoreServer;
/**
* Run the server app
*
* @param args additional args
*/
public static void main(String[] args) {
LOGGER.debug("ServerApp");
SpringApplication.run(ServerApp.class, args);
}
/**
* Run the server app
*
* @param args additional args
*/
public static void main(String[] args) {
LOGGER.debug("ServerApp");
initCores();
comCoreServer.initWebSocketConnection();
SpringApplication.run(ServerApp.class, args);
}
private void initCores() {
dataCoreServer.setDataToCom(comCoreServer.getDataToComServerImpl());
comCoreServer.setComToDataImpl(dataCoreServer.getComToDataServerImpl());
}
private static void initCores() {
comCoreServer = new ComCoreServer();
dataCoreServer = new DataCoreServer();
dataCoreServer.setDataToCom(comCoreServer.getDataToComServerImpl());
comCoreServer.setComToDataImpl(dataCoreServer.getComToDataServerImpl());
}
@PostConstruct
public void init() {
this.initCores();
comCoreServer.initWebSocketConnection();
}
}
package main.java.server.data;
import java.util.ArrayList;
import java.util.List;
import main.java.common.dataModel.Board;
import main.java.common.dataModel.GameHeavy;
import main.java.common.dataModel.Move;
import main.java.common.dataModel.UserLight;
import main.java.common.interfaces.server.IDataToCom;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class DataCoreServer {
private List<UserLight> listConnectedUsers;
private List<GameHeavy> listGameHeavy;
private ComToDataServerImpl comToDataServerImpl;
private IDataToCom dataToCom;
public DataCoreServer() {
comToDataServerImpl = new ComToDataServerImpl(this);
listConnectedUsers = new ArrayList<>();
listGameHeavy = new ArrayList<>();
}
public ComToDataServerImpl getComToDataServerImpl() {
return comToDataServerImpl;
}
public void setDataToCom(IDataToCom dataToCom) {
this.dataToCom = dataToCom;
}
private boolean isMoveValid(Move playerMove, Board boardGame) {
return false;
}
public List<UserLight> getListConnectedUsers() {
return listConnectedUsers;
}
public List<GameHeavy> getListGameHeavy() {
return listGameHeavy;
}
public IDataToCom getDataToCom() {
return dataToCom;
}
private List<UserLight> listConnectedUsers;
private List<GameHeavy> listGameHeavy;
private ComToDataServerImpl comToDataServerImpl;
private IDataToCom dataToCom;
public DataCoreServer() {
comToDataServerImpl = new ComToDataServerImpl(this);
listConnectedUsers = new ArrayList<>();
listGameHeavy = new ArrayList<>();
}
public ComToDataServerImpl getComToDataServerImpl() {
return comToDataServerImpl;
}
private boolean isMoveValid(Move playerMove, Board boardGame) {
return false;
}
public List<UserLight> getListConnectedUsers() {
return listConnectedUsers;
}
public List<GameHeavy> getListGameHeavy() {
return listGameHeavy;
}
public IDataToCom getDataToCom() {
return dataToCom;
}
public void setDataToCom(IDataToCom dataToCom) {
this.dataToCom = dataToCom;
}
}
......@@ -8,63 +8,70 @@ import org.apache.logging.log4j.Logger;
import main.java.common.dataModel.Message;
import main.java.common.interfaces.server.IComToData;
import main.java.common.interfaces.server.IDataToCom;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Communication core containing message controller and interfaces.
*/
@Component
public class ComCoreServer {
private static final Logger LOGGER = LogManager.getLogger(ComCoreServer.class);
private IComToData comToDataImpl;
private DataToComServerImpl dataToComServerImpl;
private MessageServerController messageServerController;