parent
a189fedd76
commit
40e3e14666
|
@ -242,7 +242,6 @@ public class ChatWindowController {
|
|||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param event the event which occurred when Windows is closed
|
||||
*/
|
||||
public void handle(WindowEvent event) {
|
||||
|
|
|
@ -214,6 +214,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
|
||||
/**
|
||||
* Initiates the procedure to send a new message and sends one as such.
|
||||
*
|
||||
* @param data Data which has been transmitted
|
||||
*/
|
||||
private void caseMessage(Message data) {
|
||||
|
|
|
@ -17,6 +17,7 @@ public class ClientMessageList {
|
|||
|
||||
/**
|
||||
* Adds a new message to ArrayList and also informs Listener.
|
||||
*
|
||||
* @param message that should be added
|
||||
*/
|
||||
public void addMessage(Message message) {
|
||||
|
@ -37,12 +38,10 @@ public class ClientMessageList {
|
|||
for (Message message : messages) {
|
||||
if (showAll || message.matchesFilter(filter)) {
|
||||
switch (message.getType()) {
|
||||
case DATA_TYPE_MESSAGE ->
|
||||
result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
|
||||
case DATA_TYPE_MESSAGE -> result.append(String.format("[%s -> %s] %s\n", message.getSender(), message.getReceiver(), message.getText()));
|
||||
case DATA_TYPE_ERROR -> result.append(String.format("[ERROR] %s\n", message.getText()));
|
||||
case DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_CONNECT -> result.append(String.format("[INFO] %s\n", message.getText()));
|
||||
default ->
|
||||
result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
|
||||
default -> result.append(String.format("[ERROR] %s\n", "Unexpected message type: " + message.getType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,12 +3,11 @@ package ch.zhaw.pm2.multichat.protocol;
|
|||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* This abstract class is the superclass for ClientConnectionHandler and ServerConnectionHandler
|
||||
* It offers the DATA_TYPE Strings and a {@link State} enum for all valid connection states.
|
||||
* Shared methods are implemented in this class as well {@link ConnectionHandler#sendData(String, String, DATA_TYPE, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
||||
* It offers the DATA_TYPE for message and a {@link State} enum for all valid connection states.
|
||||
* Shared methods are implemented in this class as well {@link ConnectionHandler#sendData(String, String, DATA_TYPE, String)}
|
||||
*/
|
||||
public abstract class ConnectionHandler {
|
||||
private NetworkHandler.NetworkConnection<String> connection;
|
||||
|
@ -21,8 +20,9 @@ public abstract class ConnectionHandler {
|
|||
NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
|
||||
}
|
||||
|
||||
// DATA_TYPE of the messages
|
||||
public enum DATA_TYPE {
|
||||
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT,DATA_TYPE_MESSAGE,DATA_TYPE_ERROR
|
||||
DATA_TYPE_CONNECT, DATA_TYPE_CONFIRM, DATA_TYPE_DISCONNECT, DATA_TYPE_MESSAGE, DATA_TYPE_ERROR
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,7 +88,7 @@ public abstract class ConnectionHandler {
|
|||
protected void sendData(String sender, String receiver, DATA_TYPE type, String payload) {
|
||||
if (connection.isAvailable()) {
|
||||
try {
|
||||
connection.send(new Message(type,sender,receiver,payload));
|
||||
connection.send(new Message(type, sender, receiver, payload));
|
||||
} catch (SocketException e) {
|
||||
System.err.println("Connection closed: " + e.getMessage());
|
||||
} catch (EOFException e) {
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.io.Serializable;
|
|||
*/
|
||||
public class Message implements Serializable {
|
||||
private final ConnectionHandler.DATA_TYPE type;
|
||||
private String sender;
|
||||
private final String sender;
|
||||
private final String receiver;
|
||||
private final String text;
|
||||
|
||||
|
|
|
@ -85,7 +85,7 @@ public class Server {
|
|||
private void start() {
|
||||
System.out.println("Server started.");
|
||||
try {
|
||||
while (true) {
|
||||
while (networkServer.isAvailable()) {
|
||||
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
|
||||
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
|
||||
new Thread(connectionHandler).start();
|
||||
|
|
|
@ -64,8 +64,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
|||
*
|
||||
* @param connection representing the socket connection between server and client
|
||||
* @param registry map containing all active connections between server and clients
|
||||
|
||||
|
||||
*/
|
||||
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
|
||||
Map<String, ServerConnectionHandler> registry) {
|
||||
|
|
Loading…
Reference in New Issue