connection = networkServer.waitForConnection();
+ ServerConnectionHandler connectionHandler = new ServerConnectionHandler(connection, connections);
+ 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);
diff --git a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java
index 9a7cb91..d246c66 100644
--- a/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java
+++ b/server/src/main/java/ch/zhaw/pm2/multichat/server/ServerConnectionHandler.java
@@ -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;
@@ -12,45 +14,40 @@ import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.locks.Condition;
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.
- *
+ * The ServerConnectionHandler receives data send from the client as well as sends data to the client.
+ *
* The ServeConnectionHandler offers following functionality:
- *
+ *
* 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}
- *
+ *
* 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()}
- *
- * Processes Messages send from a client by:
- * 1. Evaluating the reciever by differentiating between broadcast or unicast.
+ *
+ * Process Messages send from a client by:
+ * 1. Evaluating the receiver by differentiating between broadcast or unicast.
* 2. Sending the message accordingly.
- *
+ *
* 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} 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 connectionRegistry;
+ private final Map connectionRegistry;
- private ReentrantLock mutex;
-
- private Condition nameComplete;
-
- private String userName = "Anonymous-"+connectionId;
+ private String userName = "Anonymous-" + connectionId;
private State state = NEW;
/**
@@ -62,65 +59,48 @@ 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
- * @param nameComplete condition to call threads
+ * 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
+
+
*/
public ServerConnectionHandler(NetworkHandler.NetworkConnection connection,
- Map registry, ReentrantLock mutex, Condition nameComplete) {
+ Map registry) {
super();
setConnection(connection);
Objects.requireNonNull(connection, "Connection must not be null");
Objects.requireNonNull(registry, "Registry must not be null");
this.connectionRegistry = registry;
- this.mutex = mutex;
- this.nameComplete = nameComplete;
}
/**
- *
- * @return the username of the connected client
- */
- public String getUserName() {
- return this.userName;
- }
-
- /**
- *
- * @return state of the connection. Poosible 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
+ * These 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 +125,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 +153,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 +169,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 +187,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 +198,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 +215,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);
}
}
}