General Codestyle and higlighted todos
This commit is contained in:
parent
2acfa708e9
commit
c732e82260
|
@ -20,7 +20,7 @@ import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
/**
|
/**
|
||||||
* Class Representing the Controller Element of the Window, also Contains the Elements that contacts the View Elements via
|
* Class Representing the Controller Element of the Window, also Contains the Elements that contacts the View Elements via
|
||||||
* Listeners and Observable Objects.
|
* Listeners and Observable Objects.
|
||||||
* To Contact the Model Elements needed it also holds references to messages and the Connectionhandler.
|
* To Contact the Model Elements needed it also holds references to message and the Connectionhandler.
|
||||||
*/
|
*/
|
||||||
public class ChatWindowController {
|
public class ChatWindowController {
|
||||||
private ClientConnectionHandler connectionHandler;
|
private ClientConnectionHandler connectionHandler;
|
||||||
|
@ -241,7 +241,7 @@ public class ChatWindowController {
|
||||||
*/
|
*/
|
||||||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||||
/**
|
/**
|
||||||
* TODO
|
* //TODO
|
||||||
*
|
*
|
||||||
* @param event the event which occurred
|
* @param event the event which occurred
|
||||||
*/
|
*/
|
||||||
|
@ -251,7 +251,7 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO missing
|
* //TODO missing
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public void startConnectionHandlerListener() {
|
public void startConnectionHandlerListener() {
|
||||||
|
@ -285,7 +285,7 @@ public class ChatWindowController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO
|
* //TODO
|
||||||
*/
|
*/
|
||||||
private void messageListener() {
|
private void messageListener() {
|
||||||
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
|
messages.getChangedProperty().addListener(new ChangeListener<Boolean>() {
|
||||||
|
|
|
@ -25,12 +25,18 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
|
|
||||||
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
|
private final Pattern messagePattern = Pattern.compile("^(?:@(\\S*))?\\s*(.*)$");
|
||||||
|
|
||||||
private SimpleStringProperty userName;
|
private final SimpleStringProperty userName;
|
||||||
private SimpleObjectProperty<State> state;
|
private final SimpleObjectProperty<State> state;
|
||||||
private ClientMessageList messages;
|
private final ClientMessageList
|
||||||
private SimpleStringProperty serverAddress;
|
messages;
|
||||||
private SimpleIntegerProperty serverPort;
|
private final SimpleStringProperty serverAddress;
|
||||||
|
private final SimpleIntegerProperty serverPort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor initializes ConnectionHandler by Setting default values for the fields and stores the given messages itself.
|
||||||
|
*
|
||||||
|
* @param messages
|
||||||
|
*/
|
||||||
public ClientConnectionHandler(ClientMessageList messages) {
|
public ClientConnectionHandler(ClientMessageList messages) {
|
||||||
super();
|
super();
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
|
@ -40,6 +46,14 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
this.userName = new SimpleStringProperty(null);
|
this.userName = new SimpleStringProperty(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* //TODO complete javadoc
|
||||||
|
*
|
||||||
|
* @param serverAddress
|
||||||
|
* @param serverPort
|
||||||
|
* @param userName
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
|
public void initialize(String serverAddress, int serverPort, String userName) throws IOException {
|
||||||
state.set(NEW);
|
state.set(NEW);
|
||||||
this.serverAddress.set(serverAddress);
|
this.serverAddress.set(serverAddress);
|
||||||
|
@ -102,7 +116,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that is started by the run method, starts a connection handler.
|
* Method that is started by the run method, starts a connection handler.
|
||||||
* Figures out if connection is avaible if not determines the error cause and gives an error acordingly.
|
* Figures out if connection is available if not determines the error cause and gives an error accordingly.
|
||||||
*/
|
*/
|
||||||
private void startReceiving() {
|
private void startReceiving() {
|
||||||
System.out.println("Starting Connection Handler");
|
System.out.println("Starting Connection Handler");
|
||||||
|
@ -112,7 +126,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
String data = getConnection().receive();
|
String data = getConnection().receive();
|
||||||
processData(data);
|
processData(data);
|
||||||
}
|
}
|
||||||
System.out.println("Stopped recieving data");
|
System.out.println("Stopped receiving data");
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.out.println("Connection terminated locally");
|
System.out.println("Connection terminated locally");
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
|
@ -145,7 +159,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method which processes data and determines its type then uses the corresponding method to proccess it.
|
* Method which processes data and determines its type then uses the corresponding method to process it.
|
||||||
*
|
*
|
||||||
* @param data that is received in a form of a String and then used depending on its determined cause.
|
* @param data that is received in a form of a String and then used depending on its determined cause.
|
||||||
*/
|
*/
|
||||||
|
@ -153,22 +167,22 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
try {
|
try {
|
||||||
Scanner scanner = new Scanner(data);
|
Scanner scanner = new Scanner(data);
|
||||||
StringBuilder sender = new StringBuilder();
|
StringBuilder sender = new StringBuilder();
|
||||||
StringBuilder reciever = new StringBuilder();
|
StringBuilder receiver = new StringBuilder();
|
||||||
StringBuilder type = new StringBuilder();
|
StringBuilder type = new StringBuilder();
|
||||||
StringBuilder payload = 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
|
// dispatch operation based on type parameter
|
||||||
if (type.toString().equals(getDataTypeConnect())) {
|
if (type.toString().equals(getDataTypeConnect())) {
|
||||||
System.err.println("Illegal connect request from server");
|
System.err.println("Illegal connect request from server");
|
||||||
} else if (type.toString().equals(getDataTypeConfirm())) {
|
} else if (type.toString().equals(getDataTypeConfirm())) {
|
||||||
caseConfirm(sender.toString(), reciever.toString(), payload.toString());
|
caseConfirm(sender.toString(), receiver.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||||
caseDisconnect(sender.toString(), reciever.toString(), payload.toString());
|
caseDisconnect(sender.toString(), receiver.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
} else if (type.toString().equals(getDataTypeMessage())) {
|
||||||
caseMessage(sender.toString(), reciever.toString(), payload.toString());
|
caseMessage(sender.toString(), receiver.toString(), payload.toString());
|
||||||
} else if (type.toString().equals(getDataTypeError())) {
|
} else if (type.toString().equals(getDataTypeError())) {
|
||||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
caseError(sender.toString(), receiver.toString(), payload.toString());
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Unknown data type received: " + type);
|
System.out.println("Unknown data type received: " + type);
|
||||||
}
|
}
|
||||||
|
@ -178,16 +192,16 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void caseConfirm(String sender, String reciever, String payload) {
|
private void caseConfirm(String sender, String receiver, String payload) {
|
||||||
if (state.get() == CONFIRM_CONNECT) {
|
if (state.get() == CONFIRM_CONNECT) {
|
||||||
this.userName.set(reciever);
|
this.userName.set(receiver);
|
||||||
this.serverPort.set(getConnection().getRemotePort());
|
this.serverPort.set(getConnection().getRemotePort());
|
||||||
this.serverAddress.set(getConnection().getRemoteHost());
|
this.serverAddress.set(getConnection().getRemoteHost());
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO, sender, reciever, payload));
|
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(CONNECTED);
|
this.setState(CONNECTED);
|
||||||
} else if (state.get() == CONFIRM_DISCONNECT) {
|
} else if (state.get() == CONFIRM_DISCONNECT) {
|
||||||
messages.addMessage(new Message(Message.MessageType.INFO, sender, reciever, payload));
|
messages.addMessage(new Message(Message.MessageType.INFO, sender, receiver, payload));
|
||||||
System.out.println("CONFIRM: " + payload);
|
System.out.println("CONFIRM: " + payload);
|
||||||
this.setState(DISCONNECTED);
|
this.setState(DISCONNECTED);
|
||||||
} else {
|
} else {
|
||||||
|
@ -241,7 +255,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects TODO
|
* //Connects TODO
|
||||||
*
|
*
|
||||||
* @throws ChatProtocolException Error that is thrown if the state is not set to NEW
|
* @throws ChatProtocolException Error that is thrown if the state is not set to NEW
|
||||||
*/
|
*/
|
||||||
|
@ -252,7 +266,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO
|
* //TODO
|
||||||
|
*
|
||||||
* @throws ChatProtocolException
|
* @throws ChatProtocolException
|
||||||
*/
|
*/
|
||||||
public void disconnect() throws ChatProtocolException {
|
public void disconnect() throws ChatProtocolException {
|
||||||
|
@ -263,7 +278,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO
|
* //TODO
|
||||||
|
*
|
||||||
* @param messageString
|
* @param messageString
|
||||||
* @return
|
* @return
|
||||||
* @throws ChatProtocolException
|
* @throws ChatProtocolException
|
||||||
|
|
|
@ -6,17 +6,29 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that is use
|
* Class that is used to store the messages in an ArrayList
|
||||||
|
* And also as a Model that informs the controller via Listeners that can be initialized
|
||||||
*/
|
*/
|
||||||
public class ClientMessageList {
|
public class ClientMessageList {
|
||||||
private List<Message> messages = new ArrayList<>();
|
private List<Message> messages = new ArrayList<>();
|
||||||
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
|
private final SimpleBooleanProperty changed = new SimpleBooleanProperty(false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new message to ArrayList and also informs Listener.
|
||||||
|
* @param message that should be added
|
||||||
|
*/
|
||||||
public void addMessage(Message message) {
|
public void addMessage(Message message) {
|
||||||
messages.add(message);
|
messages.add(message);
|
||||||
changed.set(!changed.get());
|
changed.set(!changed.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies a given filter over all messages and returns the result as a new String.
|
||||||
|
* Method is also in charge of applying the correct prefix to the message according to its type.
|
||||||
|
*
|
||||||
|
* @param filter is the applied filter on all the messages that are stored.
|
||||||
|
* @return String that matches the given filter.
|
||||||
|
*/
|
||||||
public String getFilteredMessages(String filter) {
|
public String getFilteredMessages(String filter) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
boolean showAll = filter == null || filter.isBlank();
|
boolean showAll = filter == null || filter.isBlank();
|
||||||
|
@ -35,11 +47,19 @@ public class ClientMessageList {
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overwrites the Arraylist of messages, clearing it, also informs all Listeners.
|
||||||
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
messages = new ArrayList<>();
|
messages = new ArrayList<>();
|
||||||
changed.set(!changed.get());
|
changed.set(!changed.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter Method to check the current value of SimpleBooleanProperty changed.
|
||||||
|
*
|
||||||
|
* @return the current value of changed
|
||||||
|
*/
|
||||||
public SimpleBooleanProperty getChangedProperty() {
|
public SimpleBooleanProperty getChangedProperty() {
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class Message {
|
||||||
*
|
*
|
||||||
* @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 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 sender The User who has sent the message.
|
||||||
* @param receiver The User who should recieve the message.
|
* @param receiver The User who should receive the message.
|
||||||
* @param text The Text of the message.
|
* @param text The Text of the message.
|
||||||
*/
|
*/
|
||||||
public Message(MessageType type, String sender, String receiver, String text) {
|
public Message(MessageType type, String sender, String receiver, String text) {
|
||||||
|
@ -28,7 +28,7 @@ public class Message {
|
||||||
* Checks if the Filter String is contained in one of the data fields sender, receiver or text
|
* Checks if the Filter String is contained in one of the data fields sender, receiver or text
|
||||||
*
|
*
|
||||||
* @param filter The Filter String
|
* @param filter The Filter String
|
||||||
* @return true if it the Filter String is contained in a datafield.
|
* @return true if it is the Filter String is contained in a data field.
|
||||||
*/
|
*/
|
||||||
public boolean matchesFilter(String filter) {
|
public boolean matchesFilter(String filter) {
|
||||||
return (sender != null && sender.contains(filter)) ||
|
return (sender != null && sender.contains(filter)) ||
|
||||||
|
@ -51,7 +51,7 @@ public class Message {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The Reciever who recieves the Message.
|
* @return The Receiver who receives the Message.
|
||||||
*/
|
*/
|
||||||
public String getReceiver() {
|
public String getReceiver() {
|
||||||
return receiver;
|
return receiver;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package ch.zhaw.pm2.multichat.server;
|
package ch.zhaw.pm2.multichat.server;
|
||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -16,19 +15,17 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||||
* After initialising the server:
|
* After initialising the server:
|
||||||
* 1. Starts a Socketserver using the logic given in {@link NetworkHandler.NetworkServer#createServer()}
|
* 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()}
|
* 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 {
|
public class Server {
|
||||||
|
|
||||||
// Server connection
|
// Server connection
|
||||||
private NetworkHandler.NetworkServer<String> networkServer;
|
private final NetworkHandler.NetworkServer<String> networkServer;
|
||||||
|
|
||||||
// Connection registry
|
// Connection registry
|
||||||
private Map<String,ServerConnectionHandler> connections = new HashMap<>();
|
private final Map<String, ServerConnectionHandler> connections = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -71,6 +68,7 @@ public class Server {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Constructor to create a new instance.
|
* The Constructor to create a new instance.
|
||||||
|
*
|
||||||
* @param serverPort to listen for incoming connections.
|
* @param serverPort to listen for incoming connections.
|
||||||
* @throws IOException thrown if an I/O error occurs when opening the socket.
|
* @throws IOException thrown if an I/O error occurs when opening the socket.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -2,7 +2,9 @@ package ch.zhaw.pm2.multichat.server;
|
||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
import ch.zhaw.pm2.multichat.protocol.ChatProtocolException;
|
||||||
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
import ch.zhaw.pm2.multichat.protocol.ConnectionHandler;
|
||||||
|
|
||||||
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
import static ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State.*;
|
||||||
|
|
||||||
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
import ch.zhaw.pm2.multichat.protocol.NetworkHandler;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
|
@ -19,36 +21,36 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||||
/**
|
/**
|
||||||
* This class represents the connection between the server and a client and offers the serverside logic.
|
* 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 aswell as sends data to the client.
|
||||||
*
|
* <p>
|
||||||
* The ServeConnectionHandler offers following functionality:
|
* The ServeConnectionHandler offers following functionality:
|
||||||
*
|
* <p>
|
||||||
* Evaluating connection attempts from a client by:
|
* Evaluating connection attempts from a client by:
|
||||||
* 1. Checks if Username is valid (Not used)
|
* 1. Checks if Username is valid (Not used)
|
||||||
* 2. Saves Username in {@link ServerConnectionHandler#userName}
|
* 2. Saves Username in {@link ServerConnectionHandler#userName}
|
||||||
* 3. Saves the connection in the {@link ServerConnectionHandler#connectionRegistry}
|
* 3. Saves the connection in the {@link ServerConnectionHandler#connectionRegistry}
|
||||||
*
|
* <p>
|
||||||
* Processes disconnections from a client by:
|
* Processes disconnections from a client by:
|
||||||
* 1. Removing the connection from the {@link ServerConnectionHandler#connectionRegistry}
|
* 1. Removing the connection from the {@link ServerConnectionHandler#connectionRegistry}
|
||||||
* 2. Terminates the socket by calling {@link NetworkHandler.NetworkConnection#close()}
|
* 2. Terminates the socket by calling {@link NetworkHandler.NetworkConnection#close()}
|
||||||
*
|
* <p>
|
||||||
* Processes Messages send from a client by:
|
* Processes Messages send from a client by:
|
||||||
* 1. Evaluating the reciever by differentiating between broadcast or unicast.
|
* 1. Evaluating the reciever by differentiating between broadcast or unicast.
|
||||||
* 2. Sending the message accordingly.
|
* 2. Sending the message accordingly.
|
||||||
*
|
* <p>
|
||||||
* To use this class, start a new instance and start it in a thread.
|
* To use this class, start a new instance and start it in a thread.
|
||||||
* To constructor needs following parameter:
|
* To constructor needs following parameter:
|
||||||
* 1. {@link ch.zhaw.pm2.multichat.protocol.NetworkHandler.NetworkConnection} representing the socket connection between server and client
|
* 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
|
* 2. {@link Map<String,ServerConnectionHandler>} registry to check for all active connections
|
||||||
* 3. {@link ReentrantLock @link Condition} to lock server thread to evaluate connection.
|
* 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 static final AtomicInteger connectionCounter = new AtomicInteger(0);
|
||||||
private final int connectionId = connectionCounter.incrementAndGet();
|
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;
|
private State state = NEW;
|
||||||
|
@ -62,8 +64,9 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor to intitialize the connection
|
* Constructor to initialize the connection
|
||||||
* @param connection representing the socket connection between server and clinet
|
*
|
||||||
|
* @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
|
||||||
* @param mutex to lock thread
|
* @param mutex to lock thread
|
||||||
* @param nameComplete condition to call threads
|
* @param nameComplete condition to call threads
|
||||||
|
@ -80,7 +83,6 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the username of the connected client
|
* @return the username of the connected client
|
||||||
*/
|
*/
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
|
@ -88,15 +90,14 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* @return state of the connection. Possible states are see {@link ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State}
|
||||||
* @return state of the connection. Poosible states are see {@link ch.zhaw.pm2.multichat.protocol.ConnectionHandler.State}
|
|
||||||
*/
|
*/
|
||||||
public State getState() {
|
public State getState() {
|
||||||
return state;
|
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.
|
* and the connection State is not ERROR.
|
||||||
*/
|
*/
|
||||||
private void startReceiving() {
|
private void startReceiving() {
|
||||||
|
@ -107,7 +108,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
String data = getConnection().receive();
|
String data = getConnection().receive();
|
||||||
processData(data);
|
processData(data);
|
||||||
}
|
}
|
||||||
System.out.println("Stopped recieving data");
|
System.out.println("Stopped receiving data");
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.out.println("Connection terminated locally");
|
System.out.println("Connection terminated locally");
|
||||||
connectionRegistry.remove(userName);
|
connectionRegistry.remove(userName);
|
||||||
|
@ -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:
|
* If data type:
|
||||||
* 1. Connect => checks if username is valid. if valid sends response to client with confirmation.
|
* 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.
|
* 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.
|
* 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.
|
* 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
|
* 4. Message => Checks if broadcast or unicast. Sends data accordingly
|
||||||
* 5. Error => STDERR message
|
* 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 {
|
try {
|
||||||
Scanner scanner = new Scanner(data);
|
Scanner scanner = new Scanner(data);
|
||||||
StringBuilder sender = new StringBuilder();
|
StringBuilder sender = new StringBuilder();
|
||||||
StringBuilder reciever = new StringBuilder();
|
StringBuilder receiver = new StringBuilder();
|
||||||
StringBuilder type = new StringBuilder();
|
StringBuilder type = new StringBuilder();
|
||||||
StringBuilder payload = 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
|
// dispatch operation based on type parameter
|
||||||
if (type.toString().equals(getDataTypeConnect())) {
|
if (type.toString().equals(getDataTypeConnect())) {
|
||||||
|
@ -172,7 +174,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
} else if (type.toString().equals(getDataTypeDisconnect())) {
|
||||||
caseDisconnect();
|
caseDisconnect();
|
||||||
} else if (type.toString().equals(getDataTypeMessage())) {
|
} 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())) {
|
} else if (type.toString().equals(getDataTypeError())) {
|
||||||
System.err.println("Received error from client (" + sender + "): " + payload);
|
System.err.println("Received error from client (" + sender + "): " + payload);
|
||||||
} else {
|
} else {
|
||||||
|
@ -188,6 +190,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
/**
|
/**
|
||||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
||||||
* Checks if username is valid. if valid sends response to client with confirmation.
|
* Checks if username is valid. if valid sends response to client with confirmation.
|
||||||
|
*
|
||||||
* @param sender of the payload
|
* @param sender of the payload
|
||||||
* @throws ChatProtocolException if username not valid
|
* @throws ChatProtocolException if username not valid
|
||||||
*/
|
*/
|
||||||
|
@ -205,7 +208,7 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
//if username valid
|
//if username valid
|
||||||
this.userName = sender;
|
this.userName = sender;
|
||||||
connectionRegistry.put(userName, this);
|
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;
|
this.state = CONNECTED;
|
||||||
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>",
|
||||||
userName,
|
userName,
|
||||||
|
@ -216,7 +219,8 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
/**
|
/**
|
||||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
* 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.
|
* 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 {
|
private void caseDisconnect() throws ChatProtocolException {
|
||||||
if (state == DISCONNECTED)
|
if (state == DISCONNECTED)
|
||||||
|
@ -232,27 +236,28 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
|
||||||
/**
|
/**
|
||||||
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
* This method is called by method {@link ServerConnectionHandler#processData(String)}
|
||||||
* Checks if broadcast or unicast. Sends data accordingly
|
* Checks if broadcast or unicast. Sends data accordingly
|
||||||
|
*
|
||||||
* @param sender who sent data
|
* @param sender who sent data
|
||||||
* @param reciever to recieve data
|
* @param receiver to receive data
|
||||||
* @param type of message
|
* @param type of message
|
||||||
* @param payload data to transmit
|
* @param payload data to transmit
|
||||||
* @throws ChatProtocolException if state not equal to CONNECT
|
* @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 (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()) {
|
for (ServerConnectionHandler handler : connectionRegistry.values()) {
|
||||||
handler.sendData(sender, reciever, type, payload);
|
handler.sendData(sender, receiver, type, payload);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ServerConnectionHandler handler = connectionRegistry.get(reciever);
|
ServerConnectionHandler handler = connectionRegistry.get(receiver);
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.sendData(sender, reciever, type, payload);
|
handler.sendData(sender, receiver, type, payload);
|
||||||
if(!reciever.equals(sender)){
|
if (!receiver.equals(sender)) {
|
||||||
sendData(sender, reciever, type, payload); //send message to sender if it's a direct message and sender is not receiver.
|
sendData(sender, receiver, type, payload); //send message to sender if it's a direct message and sender is not receiver.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + reciever);
|
this.sendData(USER_NONE, userName, getDataTypeError(), "Unknown User: " + receiver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue