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