Mpdule ChatCommands
Pom
<pre lang="Java">
<pre><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>NetworkChat</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ChatCommands</artifactId>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
</project>
<pre lang="Java">
<pre lang="Java">
package clientserver;
public enum CommandType {
AUTH,
AUTH_OK,
AUTH_ERROR,
PRIVATE_MESSAGE,
PUBLIC_MESSAGE,
INFO_MESSAGE,
ERROR,
END,
UPDATE_USERS_LIST
}
package clientserver;
import clientserver.сommands.*;
import java.io.Serializable;
import java.util.List;
public class Command implements Serializable {
private CommandType type;
private Object data;
public CommandType getType() {
return type;
}
public Object getData() {
return data;
}
public static Command authCommand(String login, String password) {
Command command = new Command();
command.type = CommandType.AUTH;
command.data = new AuthCommandData(login, password);
return command;
}
public static Command authOkCommand(String username) {
Command command = new Command();
command.type = CommandType.AUTH_OK;
command.data = new AuthOkCommandData(username);
return command;
}
public static Command authErrorCommand(String authErrorMessage) {
Command command = new Command();
command.type = CommandType.AUTH_ERROR;
command.data = new AuthErrorCommandData(authErrorMessage);
return command;
}
public static Command errorCommand(String errorMessage) {
Command command = new Command();
command.type = CommandType.ERROR;
command.data = new ErrorCommandData(errorMessage);
return command;
}
public static Command messageInfoCommand(String message, String sender) {
Command command = new Command();
command.type = CommandType.INFO_MESSAGE;
command.data = new MessageInfoCommandData(message, sender);
return command;
}
public static Command publicMessageCommand(String username, String message) {
Command command = new Command();
command.type = CommandType.PUBLIC_MESSAGE;
command.data = new PublicMessageCommandData(username, message);
return command;
}
public static Command privateMessageCommand(String receiver, String message) {
Command command = new Command();
command.type = CommandType.PRIVATE_MESSAGE;
command.data = new PrivateMessageCommandData(receiver, message);
return command;
}
public static Command updateUsersListCommand(List<String> users) {
Command command = new Command();
command.type = CommandType.UPDATE_USERS_LIST;
command.data = new UpdateUsersListCommandData(users);
return command;
}
public static Command endCommand() {
Command command = new Command();
command.type = CommandType.END;
return command;
}
}
Module ChatClient
NetworkClient
<pre lang="Java">
public class NetworkClient extends Application {
private Stage primaryStage;
private Stage authStage;
private Network network;
private ChatController chatController;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
network = new Network();
if (!network.connect()) {
showErrorMessage("Проблемы с соединением", "", "Ошибка подключения к серверу");
return;
}
openAuthWindow();
createMainChatWindow();
}
private void openAuthWindow() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(NetworkClient.class.getResource("views/auth-view.fxml"));
Parent root = loader.load();
authStage = new Stage();
authStage.setTitle("Авторизация");
authStage.initModality(Modality.WINDOW_MODAL);
authStage.initOwner(primaryStage);
Scene scene = new Scene(root);
authStage.setScene(scene);
authStage.show();
AuthController authController = loader.getController();
authController.setNetwork(network);
authController.setNetworkClient(this);
}
public void createMainChatWindow() throws java.io.IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(NetworkClient.class.getResource("views/chat-view.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Messenger");
primaryStage.setScene(new Scene(root, 600, 400));
chatController = loader.getController();
chatController.setNetwork(network);
primaryStage.setOnCloseRequest(windowEvent -> network.close());
}
public static void showErrorMessage(String title, String message, String errorMessage) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(message);
alert.setContentText(errorMessage);
alert.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
public void openMainChatWindow() {
authStage.close();
primaryStage.show();
primaryStage.setTitle(network.getUsername());
primaryStage.setAlwaysOnTop(true);
chatController.setLabel(network.getUsername());
network.waitMessage(chatController);
}
}
Network
public class Network {
private static final String SERVER_ADRESS = "localhost";
private static final int SERVER_PORT = 8189;
private final String host;
private final int port;
private ObjectOutputStream dataOutputStream;
private ObjectInputStream dataInputStream;
private Socket socket;
private String username;
public ObjectOutputStream getDataOutputStream() {
return dataOutputStream;
}
public ObjectInputStream getDataInputStream() {
return dataInputStream;
}
public Network() {
this(SERVER_ADRESS, SERVER_PORT);
}
public Network(String host, int port) {
this.host = host;
this.port = port;
}
public boolean connect() {
try {
socket = new Socket(host, port);
dataOutputStream = new ObjectOutputStream(socket.getOutputStream());
dataInputStream = new ObjectInputStream(socket.getInputStream());
return true;
} catch (IOException e) {
System.out.println("Соединение не было установлено!");
e.printStackTrace();
return false;
}
}
public void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void waitMessage(ChatController chatController) {
Thread thread = new Thread( () -> {
try { while (true) {
Command command = readCommand();
if(command == null) {
NetworkClient.showErrorMessage("Error","Ошибка серверва", "Получена неверная команда");
continue;
}
switch (command.getType()) {
case INFO_MESSAGE: {
MessageInfoCommandData data = (MessageInfoCommandData) command.getData();
String message = data.getMessage();
String sender = data.getSender();
String formattedMessage = sender != null ? String.format("%s: %s", sender, message) : message;
Platform.runLater(() -> {
chatController.appendMessage(formattedMessage);
});
break;
}
case ERROR: {
ErrorCommandData data = (ErrorCommandData) command.getData();
String errorMessage = data.getErrorMessage();
Platform.runLater(() -> {
NetworkClient.showErrorMessage("Error", "Server error", errorMessage);
});
break;
}
case UPDATE_USERS_LIST: {
UpdateUsersListCommandData data = (UpdateUsersListCommandData) command.getData();
Platform.runLater(() -> chatController.updateUsers(data.getUsers()));
break;
}
default:
Platform.runLater(() -> {
NetworkClient.showErrorMessage("Error","Unknown command from server!", command.getType().toString());
});
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Соединение потеряно!");
}
});
thread.setDaemon(true);
thread.start();
}
public String sendAuthCommand(String login, String password) {
try {
Command authCommand = Command.authCommand(login, password);
dataOutputStream.writeObject(authCommand);
Command command = readCommand();
if (command == null) {
return "Ошибка чтения команды с сервера";
}
switch (command.getType()) {
case AUTH_OK: {
AuthOkCommandData data = (AuthOkCommandData) command.getData();
this.username = data.getUsername();
return null;
}
case AUTH_ERROR:
case ERROR: {
AuthErrorCommandData data = (AuthErrorCommandData) command.getData();
return data.getErrorMessage();
}
default:
return "Unknown type of command: " + command.getType();
}
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
public String getUsername() {
return username;
}
public void sendMessage(String message) throws IOException {
sendMessage(Command.publicMessageCommand(username, message));
}
public void sendMessage(Command command) throws IOException {
dataOutputStream.writeObject(command);
}
public void sendPrivateMessage(String message, String recipient) throws IOException {
Command command = Command.privateMessageCommand(recipient, message);
sendMessage(command);
}
private Command readCommand() throws IOException {
try {
return (Command) dataInputStream.readObject();
} catch (ClassNotFoundException e) {
String errorMessage = "Получен неизвестный объект";
System.err.println(errorMessage);
e.printStackTrace();
sendMessage(Command.errorCommand(errorMessage));
return null;
}
}
}
ChatClient pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>NetworkChat</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ChatClient</artifactId>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>16-ea+5</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>ChatCommands</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.5</version>
<configuration>
<mainClass>
client.NetworkClient
</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
What I have tried:
Therese two modules can't find dependencies. Everything is in the Pom. There are dependencies from MVN repository and I did all the setting in ItelliJ Idea. What should I do?