General Codestyle and higlighted todos

This commit is contained in:
Leonardo Brandenberger
2022-04-16 08:19:44 +02:00
parent 2acfa708e9
commit c732e82260
7 changed files with 145 additions and 106 deletions
@@ -1,6 +1,5 @@
package ch.zhaw.pm2.multichat.server;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import java.io.IOException;
@@ -16,19 +15,17 @@ import java.util.concurrent.locks.ReentrantLock;
* After initialising the server:
* 1. Starts a Socketserver using the logic given in {@link NetworkHandler.NetworkServer#createServer()}
* 2. The server starts to listen for incoming connection using the Logic given in {@link NetworkHandler.NetworkServer#waitForConnection()}
* 3. New conections will be atached to a Connectionhandler: {@link ServerConnectionHandler} and placed in a Map containing all active connections {@link Server#connections}
*
* 3. New connections will be attached to a Connection handler: {@link ServerConnectionHandler} and placed in a Map containing all active connections {@link Server#connections}
*/
public class Server {
// Server connection
private NetworkHandler.NetworkServer<String> networkServer;
private final NetworkHandler.NetworkServer<String> networkServer;
// Connection registry
private Map<String,ServerConnectionHandler> connections = new HashMap<>();
private final Map<String, ServerConnectionHandler> connections = new HashMap<>();
/**
*
* @param args
*/
public static void main(String[] args) {
@@ -71,6 +68,7 @@ public class Server {
/**
* The Constructor to create a new instance.
*
* @param serverPort to listen for incoming connections.
* @throws IOException thrown if an I/O error occurs when opening the socket.
*/
@@ -91,11 +89,11 @@ public class Server {
System.out.println("Server started.");
try {
while (true) {
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete);
new Thread(connectionHandler).start();
NetworkHandler.NetworkConnection<String> connection = networkServer.waitForConnection();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections, mutex, nameComplete);
new Thread(connectionHandler).start();
}
} catch(SocketException e) {
} catch (SocketException e) {
System.out.println("Server connection terminated");
} catch (IOException e) {
System.err.println("Communication error " + e);
@@ -2,7 +2,9 @@ package ch.zhaw.pm2.multichat.server;
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
import java.io.EOFException;
@@ -19,38 +21,38 @@ import java.util.concurrent.locks.ReentrantLock;
/**
* This class represents the connection between the server and a client and offers the serverside logic.
* The ServerConnectionHandler receives data send from the client aswell as sends data to the client.
*
* <p>
* The ServeConnectionHandler offers following functionality:
*
* <p>
* Evaluating connection attempts from a client by:
* 1. Checks if Username is valid (Not used)
* 2. Saves Username in {@link ServerConnectionHandler#userName}
* 3. Saves the connection in the {@link ServerConnectionHandler#connectionRegistry}
*
* <p>
* Processes disconnections from a client by:
* 1. Removing the connection from the {@link ServerConnectionHandler#connectionRegistry}
* 2. Terminates the socket by calling {@link NetworkHandler.NetworkConnection#close()}
*
* <p>
* Processes Messages send from a client by:
* 1. Evaluating the reciever by differentiating between broadcast or unicast.
* 2. Sending the message accordingly.
*
* <p>
* To use this class, start a new instance and start it in a thread.
* To constructor needs following parameter:
* 1. {@link ch.zhaw.pm2.multichat.protocol.NetworkHandler.NetworkConnection} representing the socket connection between server and client
* 2. {@link Map<String,ServerConnectionHandler>} registry to check for all active connections
* 3. {@link ReentrantLock @link Condition} to lock server thread to evaluate connection.
* */
public class ServerConnectionHandler extends ConnectionHandler implements Runnable{
*/
public class ServerConnectionHandler extends ConnectionHandler implements Runnable {
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
private final int connectionId = connectionCounter.incrementAndGet();
private final Map<String,ServerConnectionHandler> connectionRegistry;
private final Map<String, ServerConnectionHandler> connectionRegistry;
private ReentrantLock mutex;
private final ReentrantLock mutex;
private Condition nameComplete;
private final Condition nameComplete;
private String userName = "Anonymous-"+connectionId;
private String userName = "Anonymous-" + connectionId;
private State state = NEW;
/**
@@ -62,14 +64,15 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* Constructor to intitialize the connection
* @param connection representing the socket connection between server and clinet
* @param registry map containing all active connections between server and clients
* @param mutex to lock thread
* Constructor to initialize the connection
*
* @param connection representing the socket connection between server and client
* @param registry map containing all active connections between server and clients
* @param mutex to lock thread
* @param nameComplete condition to call threads
*/
public ServerConnectionHandler(NetworkHandler.NetworkConnection<String> connection,
Map<String,ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
Map<String, ServerConnectionHandler> registry, ReentrantLock mutex, Condition nameComplete) {
super();
setConnection(connection);
Objects.requireNonNull(connection, "Connection must not be null");
@@ -80,7 +83,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
}
/**
*
* @return the username of the connected client
*/
public String getUserName() {
@@ -88,39 +90,38 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
}
/**
*
* @return state of the connection. Poosible states are see {@link ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State}
* @return state of the connection. Possible states are see {@link ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State}
*/
public State getState() {
return state;
}
/**
* This methods runs in a whileloop as long as the socket between server and client is available
* This methods runs in a while-loop as long as the socket between server and client is available
* and the connection State is not ERROR.
*/
private void startReceiving() {
System.out.println("Starting Connection Handler for new User");
try {
System.out.println("Start receiving data...");
while (getConnection().isAvailable() && !(state == ERROR)) {
String data = getConnection().receive();
processData(data);
}
System.out.println("Stopped recieving data");
} catch (SocketException e) {
System.out.println("Connection terminated locally");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (EOFException e) {
System.out.println("Connection terminated by remote");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (IOException e) {
System.err.println("Communication error: " + e);
} catch (ClassNotFoundException e) {
System.err.println("Received object of unknown type: " + e.getMessage());
System.out.println("Starting Connection Handler for new User");
try {
System.out.println("Start receiving data...");
while (getConnection().isAvailable() && !(state == ERROR)) {
String data = getConnection().receive();
processData(data);
}
System.out.println("Stopped receiving data");
} catch (SocketException e) {
System.out.println("Connection terminated locally");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (EOFException e) {
System.out.println("Connection terminated by remote");
connectionRegistry.remove(userName);
System.out.println("Unregistered because client connection terminated: " + userName + " " + e.getMessage());
} catch (IOException e) {
System.err.println("Communication error: " + e);
} catch (ClassNotFoundException e) {
System.err.println("Received object of unknown type: " + e.getMessage());
}
if (state == ERROR) {
System.out.println("Stopping Connection Handler for Rejected Client");
} else {
@@ -145,24 +146,25 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
}
/**
* This method gets called when socket recieves data. The method checks for the data type and reacts accordingly
* This method gets called when socket receives data. The method checks for the data type and reacts accordingly
* If data type:
* 1. Connect => checks if username is valid. if valid sends response to client with confirmation.
* If username not valid quits connection by changing status to ERROR.
* 2. Confirm => Server should not recieve this kind of message. STDOUT informs about it.
* If username not valid quits connection by changing status to ERROR.
* 2. Confirm => Server should not receive this kind of message. STDOUT informs about it.
* 3. Disconnect => Disconnects connection by removing connection from registry and calling method to terminate socket.
* 4. Message => Checks if broadcast or unicast. Sends data accordingly
* 5. Error => STDERR message
* @param data recieved by the server
*
* @param data received by the server
*/
private void processData(String data) {
private void processData(String data) {
try {
Scanner scanner = new Scanner(data);
StringBuilder sender = new StringBuilder();
StringBuilder reciever = new StringBuilder();
StringBuilder receiver = new StringBuilder();
StringBuilder type = new StringBuilder();
StringBuilder payload = new StringBuilder();
super.processData(scanner,sender,reciever,type,payload);
super.processData(scanner, sender, receiver, type, payload);
// dispatch operation based on type parameter
if (type.toString().equals(getDataTypeConnect())) {
@@ -172,14 +174,14 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
} else if (type.toString().equals(getDataTypeDisconnect())) {
caseDisconnect();
} else if (type.toString().equals(getDataTypeMessage())) {
caseMessage(sender.toString(), reciever.toString(), type.toString(), payload.toString());
caseMessage(sender.toString(), receiver.toString(), type.toString(), payload.toString());
} else if (type.toString().equals(getDataTypeError())) {
System.err.println("Received error from client (" + sender + "): " + payload);
} else {
System.err.println("Unknown data type received: " + type);
}
} catch(ChatProtocolException e) {
} catch (ChatProtocolException e) {
System.out.println("Error while processing data " + e.getMessage());
sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
}
@@ -188,6 +190,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
/**
* This method is called by method {@link ServerConnectionHandler#processData(String)}
* Checks if username is valid. if valid sends response to client with confirmation.
*
* @param sender of the payload
* @throws ChatProtocolException if username not valid
*/
@@ -205,7 +208,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
//if username valid
this.userName = sender;
connectionRegistry.put(userName, this);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successfull for " + userName);
sendData(USER_NONE, userName, getDataTypeConfirm(), "Registration successful for " + userName);
this.state = CONNECTED;
System.out.println(String.format("Connected new Client %s with IP:Port <%s:%d>",
userName,
@@ -216,7 +219,8 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
/**
* This method is called by method {@link ServerConnectionHandler#processData(String)}
* Disconnects connection by removing connection from registry and calling method {@link ServerConnectionHandler#stopReceiving()} to terminate socket.
* @throws ChatProtocolException if state allready DISCONNECTED.
*
* @throws ChatProtocolException if state already DISCONNECTED.
*/
private void caseDisconnect() throws ChatProtocolException {
if (state == DISCONNECTED)
@@ -232,27 +236,28 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
/**
* This method is called by method {@link ServerConnectionHandler#processData(String)}
* Checks if broadcast or unicast. Sends data accordingly
* @param sender who sent data
* @param reciever to recieve data
* @param type of message
* @param payload data to transmit
*
* @param sender who sent data
* @param receiver to receive data
* @param type of message
* @param payload data to transmit
* @throws ChatProtocolException if state not equal to CONNECT
*/
private void caseMessage(String sender, String reciever, String type, String payload) throws ChatProtocolException{
private void caseMessage(String sender, String receiver, String type, String payload) throws ChatProtocolException {
if (state != CONNECTED) throw new ChatProtocolException("Illegal state for message request: " + state);
if (USER_ALL.equals(reciever)) {
if (USER_ALL.equals(receiver)) {
for (ServerConnectionHandler handler : connectionRegistry.values()) {
handler.sendData(sender, reciever, type, payload);
handler.sendData(sender, receiver, type, payload);
}
} else {
ServerConnectionHandler handler = connectionRegistry.get(reciever);
ServerConnectionHandler handler = connectionRegistry.get(receiver);
if (handler != null) {
handler.sendData(sender, reciever, type, payload);
if(!reciever.equals(sender)){
sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
handler.sendData(sender, receiver, type, payload);
if (!receiver.equals(sender)) {
sendData(sender, receiver, type, payload); //send message to sender if it's a direct message and sender is not receiver.
}
} else {
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + receiver);
}
}
}