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
|
||||
* 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 {
|
||||
private ClientConnectionHandler connectionHandler;
|
||||
|
@ -56,7 +56,7 @@ public class ChatWindowController {
|
|||
}
|
||||
|
||||
/**
|
||||
* Takes a Connectionhandler object and stores it used as Model and also starts a Listener for it.
|
||||
* Takes a Connection handler object and stores it used as Model and also starts a Listener for it.
|
||||
*
|
||||
* @param connectionHandler that will be set and used as Model.
|
||||
*/
|
||||
|
@ -241,7 +241,7 @@ public class ChatWindowController {
|
|||
*/
|
||||
class WindowCloseHandler implements EventHandler<WindowEvent> {
|
||||
/**
|
||||
* TODO
|
||||
* //TODO
|
||||
*
|
||||
* @param event the event which occurred
|
||||
*/
|
||||
|
@ -251,7 +251,7 @@ public class ChatWindowController {
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO missing
|
||||
* //TODO missing
|
||||
*/
|
||||
|
||||
public void startConnectionHandlerListener() {
|
||||
|
@ -285,7 +285,7 @@ public class ChatWindowController {
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* //TODO
|
||||
*/
|
||||
private void messageListener() {
|
||||
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 SimpleStringProperty userName;
|
||||
private SimpleObjectProperty<State> state;
|
||||
private ClientMessageList messages;
|
||||
private SimpleStringProperty serverAddress;
|
||||
private SimpleIntegerProperty serverPort;
|
||||
private final SimpleStringProperty userName;
|
||||
private final SimpleObjectProperty<State> state;
|
||||
private final ClientMessageList
|
||||
messages;
|
||||
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) {
|
||||
super();
|
||||
this.messages = messages;
|
||||
|
@ -40,6 +46,14 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
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 {
|
||||
state.set(NEW);
|
||||
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.
|
||||
* 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() {
|
||||
System.out.println("Starting Connection Handler");
|
||||
|
@ -112,7 +126,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
String data = getConnection().receive();
|
||||
processData(data);
|
||||
}
|
||||
System.out.println("Stopped recieving data");
|
||||
System.out.println("Stopped receiving data");
|
||||
} catch (SocketException e) {
|
||||
System.out.println("Connection terminated locally");
|
||||
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.
|
||||
*/
|
||||
|
@ -153,22 +167,22 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
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())) {
|
||||
System.err.println("Illegal connect request from server");
|
||||
} 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())) {
|
||||
caseDisconnect(sender.toString(), reciever.toString(), payload.toString());
|
||||
caseDisconnect(sender.toString(), receiver.toString(), payload.toString());
|
||||
} 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())) {
|
||||
caseError(sender.toString(), reciever.toString(), payload.toString());
|
||||
caseError(sender.toString(), receiver.toString(), payload.toString());
|
||||
} else {
|
||||
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) {
|
||||
this.userName.set(reciever);
|
||||
this.userName.set(receiver);
|
||||
this.serverPort.set(getConnection().getRemotePort());
|
||||
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);
|
||||
this.setState(CONNECTED);
|
||||
} 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);
|
||||
this.setState(DISCONNECTED);
|
||||
} else {
|
||||
|
@ -229,7 +243,7 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
}
|
||||
|
||||
/**
|
||||
* Stores the message as an error message and displays it as such aswell.
|
||||
* Stores the message as an error message and displays it as such as well.
|
||||
*
|
||||
* @param sender the sender name of the message
|
||||
* @param receiver the receiver name of the message
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -252,7 +266,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* //TODO
|
||||
*
|
||||
* @throws ChatProtocolException
|
||||
*/
|
||||
public void disconnect() throws ChatProtocolException {
|
||||
|
@ -263,7 +278,8 @@ public class ClientConnectionHandler extends ConnectionHandler implements Runnab
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* //TODO
|
||||
*
|
||||
* @param messageString
|
||||
* @return
|
||||
* @throws ChatProtocolException
|
||||
|
|
|
@ -6,17 +6,29 @@ import java.util.ArrayList;
|
|||
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 {
|
||||
private List<Message> messages = new ArrayList<>();
|
||||
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) {
|
||||
messages.add(message);
|
||||
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) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
boolean showAll = filter == null || filter.isBlank();
|
||||
|
@ -35,11 +47,19 @@ public class ClientMessageList {
|
|||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrites the Arraylist of messages, clearing it, also informs all Listeners.
|
||||
*/
|
||||
public void clear() {
|
||||
messages = new ArrayList<>();
|
||||
changed.set(!changed.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter Method to check the current value of SimpleBooleanProperty changed.
|
||||
*
|
||||
* @return the current value of changed
|
||||
*/
|
||||
public SimpleBooleanProperty getChangedProperty() {
|
||||
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 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.
|
||||
*/
|
||||
public Message(MessageType type, String sender, String receiver, String text) {
|
||||
|
@ -25,10 +25,10 @@ public class Message {
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks if the Filter String is contained in one of the datafields 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
|
||||
* @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) {
|
||||
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() {
|
||||
return receiver;
|
||||
|
|
|
@ -8,7 +8,7 @@ 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 aswell {@link ConnectionHandler#sendData(String, String, String, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
||||
* Shared methods are implemented in this class as well {@link ConnectionHandler#sendData(String, String, String, String)} {@link ConnectionHandler#processData(Scanner, StringBuilder, StringBuilder, StringBuilder, StringBuilder)}
|
||||
*/
|
||||
public abstract class ConnectionHandler {
|
||||
private NetworkHandler.NetworkConnection<String> connection;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue