Merge branch 'main' into State_Sync
# Conflicts: # client/src/main/java/ch/zhaw/pm2/multichat/client/ChatWindowController.java # client/src/main/java/ch/zhaw/pm2/multichat/client/ClientConnectionHandler.java # client/src/main/java/ch/zhaw/pm2/multichat/client/ClientMessageList.java
This commit is contained in:
		
						commit
						88d4493cfb
					
				| 
						 | 
					@ -21,7 +21,6 @@ import java.util.regex.Pattern;
 | 
				
			||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
					import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ChatWindowController {
 | 
					public class ChatWindowController {
 | 
				
			||||||
    private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
 | 
					 | 
				
			||||||
    private ClientConnectionHandler connectionHandler;
 | 
					    private ClientConnectionHandler connectionHandler;
 | 
				
			||||||
    private ClientMessageList messages;
 | 
					    private ClientMessageList messages;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,45 +66,40 @@ public class ChatWindowController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void connect() {
 | 
					    private void connect() {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            messages = new ClientMessageList(this); // clear message list
 | 
					            messages.clear(); // clear message list
 | 
				
			||||||
 | 
					            redrawMessageList();
 | 
				
			||||||
            startConnectionHandler();
 | 
					            startConnectionHandler();
 | 
				
			||||||
            connectionHandler.connect();
 | 
					            connectionHandler.connect();
 | 
				
			||||||
        } catch(ChatProtocolException | IOException e) {
 | 
					        } catch(ChatProtocolException | IOException e) {
 | 
				
			||||||
            writeError(e.getMessage());
 | 
					            addError(e.getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void disconnect() {
 | 
					    private void disconnect() {
 | 
				
			||||||
        if (connectionHandler == null) {
 | 
					        if (connectionHandler == null) {
 | 
				
			||||||
            writeError("No connection handler");
 | 
					            addError("No connection handler");
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            connectionHandler.disconnect();
 | 
					            connectionHandler.disconnect();
 | 
				
			||||||
        } catch (ChatProtocolException e) {
 | 
					        } catch (ChatProtocolException e) {
 | 
				
			||||||
            writeError(e.getMessage());
 | 
					            addError(e.getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @FXML
 | 
					    @FXML
 | 
				
			||||||
    private void message() {
 | 
					    private void message() {
 | 
				
			||||||
        if (connectionHandler == null) {
 | 
					 | 
				
			||||||
            writeError("No connection handler");
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        String messageString = messageField.getText().strip();
 | 
					        String messageString = messageField.getText().strip();
 | 
				
			||||||
        Matcher matcher = messagePattern.matcher(messageString);
 | 
					 | 
				
			||||||
        if (matcher.find()) {
 | 
					 | 
				
			||||||
            String receiver = matcher.group(1);
 | 
					 | 
				
			||||||
            String message = matcher.group(2);
 | 
					 | 
				
			||||||
            if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL;
 | 
					 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
                connectionHandler.message(receiver, message);
 | 
					            if (connectionHandler == null) {
 | 
				
			||||||
            } catch (ChatProtocolException e) {
 | 
					                addError("No connection handler");
 | 
				
			||||||
                writeError(e.getMessage());
 | 
					            } else if (!connectionHandler.message(messageString)) {
 | 
				
			||||||
            }
 | 
					                addError("Not a valid message format.");
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
            writeError("Not a valid message format.");
 | 
					                messageField.clear();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (ChatProtocolException e) {
 | 
				
			||||||
 | 
					            addError(e.getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -123,7 +117,6 @@ public class ChatWindowController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // register window close handler
 | 
					        // register window close handler
 | 
				
			||||||
        rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
 | 
					        rootPane.getScene().getWindow().addEventHandler(WindowEvent.WINDOW_CLOSE_REQUEST, windowCloseHandler);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void terminateConnectionHandler() {
 | 
					    private void terminateConnectionHandler() {
 | 
				
			||||||
| 
						 | 
					@ -210,46 +203,30 @@ public class ChatWindowController {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void addMessage(String sender, String receiver, String message) {
 | 
					    public void addMessage(String sender, String receiver, String message) {
 | 
				
			||||||
        messages.addMessage(ClientMessageList.MessageType.MESSAGE, sender, receiver, message);
 | 
					        messages.addMessage(new Message(Message.MessageType.MESSAGE, sender, receiver, message));
 | 
				
			||||||
        this.redrawMessageList();
 | 
					        this.redrawMessageList();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void addInfo(String message) {
 | 
					    public void addInfo(String message) {
 | 
				
			||||||
        messages.addMessage(ClientMessageList.MessageType.INFO, null, null, message);
 | 
					        messages.addMessage(new Message(Message.MessageType.INFO, null, null, message));
 | 
				
			||||||
        this.redrawMessageList();
 | 
					        this.redrawMessageList();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void addError(String message) {
 | 
					    public void addError(String message) {
 | 
				
			||||||
        messages.addMessage(ClientMessageList.MessageType.ERROR, null, null, message);
 | 
					        messages.addMessage(new Message(Message.MessageType.ERROR, null, null, message));
 | 
				
			||||||
        this.redrawMessageList();
 | 
					        this.redrawMessageList();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void clearMessageArea() {
 | 
					 | 
				
			||||||
        this.messageArea.clear();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private void redrawMessageList() {
 | 
					    private void redrawMessageList() {
 | 
				
			||||||
        Platform.runLater(() -> messages.writeFilteredMessages(filterValue.getText().strip()));
 | 
					        this.messageArea.clear();
 | 
				
			||||||
 | 
					        Platform.runLater(() -> this.messageArea.setText(messages.getFilteredMessages(filterValue.getText().strip())));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void writeError(String message) {
 | 
					 | 
				
			||||||
        this.messageArea.appendText(String.format("[ERROR] %s\n", message));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void writeInfo(String message) {
 | 
					 | 
				
			||||||
        this.messageArea.appendText(String.format("[INFO] %s\n", message));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public void writeMessage(String sender, String reciever, String message) {
 | 
					 | 
				
			||||||
        this.messageArea.appendText(String.format("[%s -> %s] %s\n", sender, reciever, message));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    class WindowCloseHandler implements EventHandler<WindowEvent> {
 | 
					    class WindowCloseHandler implements EventHandler<WindowEvent> {
 | 
				
			||||||
        public void handle(WindowEvent event) {
 | 
					        public void handle(WindowEvent event) {
 | 
				
			||||||
            applicationClose();
 | 
					            applicationClose();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,6 +11,8 @@ import java.io.EOFException;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.net.SocketException;
 | 
					import java.net.SocketException;
 | 
				
			||||||
import java.util.Scanner;
 | 
					import java.util.Scanner;
 | 
				
			||||||
 | 
					import java.util.regex.Matcher;
 | 
				
			||||||
 | 
					import java.util.regex.Pattern;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
					import static ch.zhaw.pm2.multichat.client.ClientConnectionHandler.State.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -32,6 +34,7 @@ public class ClientConnectionHandler implements Runnable {
 | 
				
			||||||
    private SimpleStringProperty serverAddress;
 | 
					    private SimpleStringProperty serverAddress;
 | 
				
			||||||
    private ObjectProperty<State> stateObjectProperty;
 | 
					    private ObjectProperty<State> stateObjectProperty;
 | 
				
			||||||
    private ClientMessageList messageList;
 | 
					    private ClientMessageList messageList;
 | 
				
			||||||
 | 
					    private final Pattern messagePattern = Pattern.compile( "^(?:@(\\w*))?\\s*(.*)$" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    enum State {
 | 
					    enum State {
 | 
				
			||||||
        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
 | 
					        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
 | 
				
			||||||
| 
						 | 
					@ -227,6 +230,22 @@ public class ClientConnectionHandler implements Runnable {
 | 
				
			||||||
    public void message(String receiver, String message) throws ChatProtocolException {
 | 
					    public void message(String receiver, String message) throws ChatProtocolException {
 | 
				
			||||||
        if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get());
 | 
					        if (stateObjectProperty.get() != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + stateObjectProperty.get());
 | 
				
			||||||
        this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message);
 | 
					        this.sendData(userName.get(), receiver, DATA_TYPE_MESSAGE,message);
 | 
				
			||||||
 | 
					    public boolean message(String messageString) throws ChatProtocolException {
 | 
				
			||||||
 | 
					        if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message: " + state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Matcher matcher = messagePattern.matcher(messageString);
 | 
				
			||||||
 | 
					        if (matcher.find()) {
 | 
				
			||||||
 | 
					            String receiver = matcher.group(1);
 | 
				
			||||||
 | 
					            String message = matcher.group(2);
 | 
				
			||||||
 | 
					            if(message.length() < 1){
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (receiver == null || receiver.isBlank()) receiver = ClientConnectionHandler.USER_ALL;
 | 
				
			||||||
 | 
					            this.sendData(userName, receiver, DATA_TYPE_MESSAGE,message);
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,58 +1,35 @@
 | 
				
			||||||
package ch.zhaw.pm2.multichat.client;
 | 
					package ch.zhaw.pm2.multichat.client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javafx.beans.property.SimpleListProperty;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.Objects;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ClientMessageList {
 | 
					public class ClientMessageList {
 | 
				
			||||||
    private final List<MessageType> typeList = new ArrayList<>();
 | 
					    private List<Message> messages = new ArrayList<>();
 | 
				
			||||||
	private final List<String> senderList = new ArrayList<>();
 | 
					 | 
				
			||||||
	private final List<String> receiverList = new ArrayList<>();
 | 
					 | 
				
			||||||
	private final SimpleListProperty<String> messageList = new SimpleListProperty<>();
 | 
					 | 
				
			||||||
	private final ChatWindowController gui;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public ClientMessageList(ChatWindowController gui) {
 | 
					    public void addMessage(Message message) {
 | 
				
			||||||
		this.gui = gui;
 | 
						    messages.add(message);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void addMessage(MessageType type, String sender, String receiver, String message) {
 | 
						public String getFilteredMessages(String filter) {
 | 
				
			||||||
	    typeList.add(type);
 | 
					        StringBuilder result = new StringBuilder();
 | 
				
			||||||
    	senderList.add(sender);
 | 
					 | 
				
			||||||
    	receiverList.add(receiver);
 | 
					 | 
				
			||||||
    	messageList.add(message);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public void writeFilteredMessages(String filter) {
 | 
					 | 
				
			||||||
		boolean showAll = filter == null || filter.isBlank();
 | 
							boolean showAll = filter == null || filter.isBlank();
 | 
				
			||||||
		gui.clearMessageArea();
 | 
							for(Message message : messages) {
 | 
				
			||||||
		for(int i=0; i<senderList.size(); i++) {
 | 
								if(showAll || message.matchesFilter(filter))
 | 
				
			||||||
		    String sender = Objects.requireNonNullElse(senderList.get(i),"");
 | 
					 | 
				
			||||||
		    String receiver = Objects.requireNonNullElse(receiverList.get(i),"");
 | 
					 | 
				
			||||||
		    String message = Objects.requireNonNull(messageList.get(i), "");
 | 
					 | 
				
			||||||
			if(showAll ||
 | 
					 | 
				
			||||||
					sender.contains(filter) ||
 | 
					 | 
				
			||||||
					receiver.contains(filter) ||
 | 
					 | 
				
			||||||
					message.contains(filter))
 | 
					 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
			    switch (typeList.get(i)) {
 | 
					                switch (message.getType()) {
 | 
				
			||||||
                    case MESSAGE: gui.writeMessage(senderList.get(i), receiverList.get(i), messageList.get(i)); break;
 | 
					                    case MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
 | 
				
			||||||
                    case ERROR: gui.writeError(messageList.get(i)); break;
 | 
					                    case ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
 | 
				
			||||||
                    case INFO: gui.writeInfo(messageList.get(i)); break;
 | 
					                    case INFO -> result.append(String.format("[INFO] %s\n", message.getText()));
 | 
				
			||||||
                    default: gui.writeError("Unexpected message type: " + typeList.get(i));
 | 
					                    default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					        return result.toString();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public SimpleListProperty<String> getMessageListProperty() {
 | 
					    public void clear() {
 | 
				
			||||||
		return messageList;
 | 
					        messages = new ArrayList<>();
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public enum MessageType {
 | 
					 | 
				
			||||||
        INFO, MESSAGE, ERROR;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,7 @@ import javafx.scene.layout.Pane;
 | 
				
			||||||
import javafx.stage.Stage;
 | 
					import javafx.stage.Stage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ClientUI extends Application {
 | 
					public class ClientUI extends Application {
 | 
				
			||||||
 | 
					    private ClientMessageList clientMessageList = new ClientMessageList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void start(Stage primaryStage) {
 | 
					    public void start(Stage primaryStage) {
 | 
				
			||||||
| 
						 | 
					@ -25,16 +26,20 @@ public class ClientUI extends Application {
 | 
				
			||||||
            controller.chatWindowController(messageList,connectionHandler);
 | 
					            controller.chatWindowController(messageList,connectionHandler);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Pane rootPane = loader.load();
 | 
					            Pane rootPane = loader.load();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            ChatWindowController chatWindowController = loader.getController();
 | 
				
			||||||
 | 
					            chatWindowController.setMessages(clientMessageList);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // fill in scene and stage setup
 | 
					            // fill in scene and stage setup
 | 
				
			||||||
            Scene scene = new Scene(rootPane);
 | 
					            Scene scene = new Scene(rootPane);
 | 
				
			||||||
            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
 | 
					            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // configure and show stage
 | 
					            // configure and show stage
 | 
				
			||||||
            primaryStage.setScene(scene);
 | 
					            primaryStage.setScene(scene);
 | 
				
			||||||
            primaryStage.setMinWidth(420);
 | 
					 | 
				
			||||||
            primaryStage.setMinHeight(250);
 | 
					            primaryStage.setMinHeight(250);
 | 
				
			||||||
            primaryStage.setTitle("Multichat Client");
 | 
					            primaryStage.setTitle("Multichat Client");
 | 
				
			||||||
            primaryStage.show();
 | 
					            primaryStage.show();
 | 
				
			||||||
 | 
					            primaryStage.setMinWidth(primaryStage.getWidth()); //use automatically computed size as Minimum Size.
 | 
				
			||||||
        } catch(Exception e) {
 | 
					        } catch(Exception e) {
 | 
				
			||||||
            System.err.println("Error starting up UI" + e.getMessage());
 | 
					            System.err.println("Error starting up UI" + e.getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,71 @@
 | 
				
			||||||
 | 
					package ch.zhaw.pm2.multichat.client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					A Message object represents one Message of a client. Can be stored in ClientMessageList.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class Message {
 | 
				
			||||||
 | 
					    private MessageType type;
 | 
				
			||||||
 | 
					    private String sender;
 | 
				
			||||||
 | 
					    private String receiver;
 | 
				
			||||||
 | 
					    private String text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Constructor of Message. Needs all Information about a Message to save them.
 | 
				
			||||||
 | 
					     * @param type Message (if it's a message typed by a user), Error or Information (if it is generated automatically, in this case sender and reciever will be null)
 | 
				
			||||||
 | 
					     * @param sender The User who has sent the message.
 | 
				
			||||||
 | 
					     * @param receiver The User who should recieve the message.
 | 
				
			||||||
 | 
					     * @param text The Text of the message.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public Message(MessageType type, String sender, String receiver, String text) {
 | 
				
			||||||
 | 
					        this.type = type;
 | 
				
			||||||
 | 
					        this.sender = sender;
 | 
				
			||||||
 | 
					        this.receiver = receiver;
 | 
				
			||||||
 | 
					        this.text = text;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Checks if the Filter String is contained in one of the datafields sender, receiver or text
 | 
				
			||||||
 | 
					     * @param filter The Filter String
 | 
				
			||||||
 | 
					     * @return true if it the Filter String is contained in a datafield.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public boolean matchesFilter(String filter){
 | 
				
			||||||
 | 
					        return (sender != null && sender.contains(filter)) ||
 | 
				
			||||||
 | 
					            (receiver != null && receiver.contains(filter)) ||
 | 
				
			||||||
 | 
					            (text != null && text.contains(filter));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return The type of the message.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public MessageType getType() {
 | 
				
			||||||
 | 
					        return type;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return The User who has sent the Message.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public String getSender() {
 | 
				
			||||||
 | 
					        return sender;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return The Reciever who recieves the Message.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public String getReceiver() {
 | 
				
			||||||
 | 
					        return receiver;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @return The Text of the Message.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public String getText() {
 | 
				
			||||||
 | 
					        return text;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Enummeration of Message Types.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public enum MessageType {
 | 
				
			||||||
 | 
					        INFO, MESSAGE, ERROR;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,10 +1,18 @@
 | 
				
			||||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<?import javafx.geometry.*?>
 | 
					<?import javafx.geometry.Insets?>
 | 
				
			||||||
<?import javafx.scene.control.*?>
 | 
					<?import javafx.scene.control.Button?>
 | 
				
			||||||
<?import javafx.scene.layout.*?>
 | 
					<?import javafx.scene.control.Label?>
 | 
				
			||||||
 | 
					<?import javafx.scene.control.Menu?>
 | 
				
			||||||
 | 
					<?import javafx.scene.control.MenuBar?>
 | 
				
			||||||
 | 
					<?import javafx.scene.control.MenuItem?>
 | 
				
			||||||
 | 
					<?import javafx.scene.control.TextArea?>
 | 
				
			||||||
 | 
					<?import javafx.scene.control.TextField?>
 | 
				
			||||||
 | 
					<?import javafx.scene.layout.BorderPane?>
 | 
				
			||||||
 | 
					<?import javafx.scene.layout.HBox?>
 | 
				
			||||||
 | 
					<?import javafx.scene.layout.VBox?>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
 | 
					<BorderPane fx:id="rootPane" minWidth="-Infinity" prefHeight="500.0" prefWidth="420.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.multichat.client.ChatWindowController">
 | 
				
			||||||
   <top>
 | 
					   <top>
 | 
				
			||||||
      <VBox BorderPane.alignment="CENTER">
 | 
					      <VBox BorderPane.alignment="CENTER">
 | 
				
			||||||
         <children>
 | 
					         <children>
 | 
				
			||||||
| 
						 | 
					@ -68,7 +76,7 @@
 | 
				
			||||||
      </HBox>
 | 
					      </HBox>
 | 
				
			||||||
   </bottom>
 | 
					   </bottom>
 | 
				
			||||||
   <center>
 | 
					   <center>
 | 
				
			||||||
      <TextArea fx:id="messageArea" editable="false">
 | 
					      <TextArea fx:id="messageArea" editable="false" wrapText="true">
 | 
				
			||||||
         <BorderPane.margin>
 | 
					         <BorderPane.margin>
 | 
				
			||||||
            <Insets left="5.0" right="5.0" />
 | 
					            <Insets left="5.0" right="5.0" />
 | 
				
			||||||
         </BorderPane.margin>
 | 
					         </BorderPane.margin>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -305,7 +305,7 @@ public class NetworkHandler {
 | 
				
			||||||
         * @param data  data object of type T to be submitted through the connection.
 | 
					         * @param data  data object of type T to be submitted through the connection.
 | 
				
			||||||
         * @throws IOException if an error occurs (e.g. connection interrupted while sending, ...)
 | 
					         * @throws IOException if an error occurs (e.g. connection interrupted while sending, ...)
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        public void send(T data) throws IOException {
 | 
					        public synchronized void send(T data) throws IOException {
 | 
				
			||||||
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
 | 
					            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
 | 
				
			||||||
            outputStream.writeObject(data);
 | 
					            outputStream.writeObject(data);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,7 +67,7 @@ public class Server {
 | 
				
			||||||
            while (true) {
 | 
					            while (true) {
 | 
				
			||||||
                 NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
 | 
					                 NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
 | 
				
			||||||
                 ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
 | 
					                 ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
 | 
				
			||||||
                 connectionHandler.startReceiving();
 | 
					                 new Thread(connectionHandler).start();
 | 
				
			||||||
                 System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
 | 
					                 System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
 | 
				
			||||||
                     connectionHandler.getUserName(),
 | 
					                     connectionHandler.getUserName(),
 | 
				
			||||||
                     connection.getRemoteHost(),
 | 
					                     connection.getRemoteHost(),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,7 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
 | 
					import static ch.zhaw.pm2.multichat.server.ServerConnectionHandler.State.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ServerConnectionHandler {
 | 
					public class ServerConnectionHandler implements Runnable{
 | 
				
			||||||
    private static final AtomicInteger connectionCounter = new AtomicInteger(0);
 | 
					    private static final AtomicInteger connectionCounter = new AtomicInteger(0);
 | 
				
			||||||
    private final int connectionId = connectionCounter.incrementAndGet();
 | 
					    private final int connectionId = connectionCounter.incrementAndGet();
 | 
				
			||||||
    private final NetworkHandler.NetworkConnection<String> connection;
 | 
					    private final NetworkHandler.NetworkConnection<String> connection;
 | 
				
			||||||
| 
						 | 
					@ -33,6 +33,11 @@ public class ServerConnectionHandler {
 | 
				
			||||||
    private String userName = "Anonymous-"+connectionId;
 | 
					    private String userName = "Anonymous-"+connectionId;
 | 
				
			||||||
    private State state = NEW;
 | 
					    private State state = NEW;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void run() {
 | 
				
			||||||
 | 
					        startReceiving();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    enum State {
 | 
					    enum State {
 | 
				
			||||||
        NEW, CONNECTED, DISCONNECTED;
 | 
					        NEW, CONNECTED, DISCONNECTED;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue