Merge pull request #41 from PM2-IT21bWIN-ruiz-mach-krea/Server_Console_Display
Fixes issue #37
This commit is contained in:
		
						commit
						a75d3466ef
					
				| 
						 | 
					@ -14,7 +14,7 @@ public abstract class ConnectionHandler {
 | 
				
			||||||
    public static final String USER_ALL = "*";
 | 
					    public static final String USER_ALL = "*";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public enum State {
 | 
					    public enum State {
 | 
				
			||||||
        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED;
 | 
					        NEW, CONFIRM_CONNECT, CONNECTED, CONFIRM_DISCONNECT, DISCONNECTED, ERROR;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static String getDataTypeConnect() {
 | 
					    public static String getDataTypeConnect() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
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;
 | 
				
			||||||
| 
						 | 
					@ -75,11 +76,17 @@ public class Server {
 | 
				
			||||||
                 mutex.lock();
 | 
					                 mutex.lock();
 | 
				
			||||||
                 try {
 | 
					                 try {
 | 
				
			||||||
                     nameComplete.await();
 | 
					                     nameComplete.await();
 | 
				
			||||||
 | 
					                     if(connectionHandler.getState() == ConnectionHandler.State.ERROR) {
 | 
				
			||||||
 | 
					                         System.out.println(String.format("Connecting failed for new Client with IP:Port <%s:%d>.\nReason: Name already taken.",
 | 
				
			||||||
 | 
					                                 connection.getRemoteHost(),
 | 
				
			||||||
 | 
					                                 connection.getRemotePort()));
 | 
				
			||||||
 | 
					                     }
 | 
				
			||||||
 | 
					                     else {
 | 
				
			||||||
                         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>",
 | 
				
			||||||
                                 connectionHandler.getUserName(),
 | 
					                                 connectionHandler.getUserName(),
 | 
				
			||||||
                                 connection.getRemoteHost(),
 | 
					                                 connection.getRemoteHost(),
 | 
				
			||||||
                             connection.getRemotePort()
 | 
					                                 connection.getRemotePort()));
 | 
				
			||||||
                     ));
 | 
					                     }
 | 
				
			||||||
                 }
 | 
					                 }
 | 
				
			||||||
                finally {
 | 
					                finally {
 | 
				
			||||||
                     mutex.unlock();
 | 
					                     mutex.unlock();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -50,11 +50,15 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
 | 
				
			||||||
        return this.userName;
 | 
					        return this.userName;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public State getState() {
 | 
				
			||||||
 | 
					        return state;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void startReceiving() {
 | 
					    private void startReceiving() {
 | 
				
			||||||
            System.out.println("Starting Connection Handler for new User");
 | 
					            System.out.println("Starting Connection Handler for new User");
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                System.out.println("Start receiving data...");
 | 
					                System.out.println("Start receiving data...");
 | 
				
			||||||
                while (getConnection().isAvailable()) {
 | 
					                while (getConnection().isAvailable() && !(state == ERROR)) {
 | 
				
			||||||
                    String data = getConnection().receive();
 | 
					                    String data = getConnection().receive();
 | 
				
			||||||
                    processData(data);
 | 
					                    processData(data);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
| 
						 | 
					@ -72,7 +76,11 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
 | 
				
			||||||
            } catch (ClassNotFoundException e) {
 | 
					            } catch (ClassNotFoundException e) {
 | 
				
			||||||
                System.err.println("Received object of unknown type: " + e.getMessage());
 | 
					                System.err.println("Received object of unknown type: " + e.getMessage());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        if (state == ERROR) {
 | 
				
			||||||
 | 
					            System.out.println("Stopping Connection Handler for Rejected Client");
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
            System.out.println("Stopping Connection Handler for " + userName);
 | 
					            System.out.println("Stopping Connection Handler for " + userName);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -119,8 +127,17 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
 | 
				
			||||||
            if (type.equals(getDataTypeConnect())) {
 | 
					            if (type.equals(getDataTypeConnect())) {
 | 
				
			||||||
                if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
 | 
					                if (this.state != NEW) throw new ChatProtocolException("Illegal state for connect request: " + state);
 | 
				
			||||||
                if (sender == null || sender.isBlank()) sender = this.userName;
 | 
					                if (sender == null || sender.isBlank()) sender = this.userName;
 | 
				
			||||||
                if (connectionRegistry.containsKey(sender))
 | 
					                if (connectionRegistry.containsKey(sender)) {
 | 
				
			||||||
 | 
					                    mutex.lock();
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        state = ERROR;
 | 
				
			||||||
 | 
					                        nameComplete.signal();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    finally {
 | 
				
			||||||
 | 
					                        mutex.unlock();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    throw new ChatProtocolException("User name already taken: " + sender);
 | 
					                    throw new ChatProtocolException("User name already taken: " + sender);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                mutex.lock();
 | 
					                mutex.lock();
 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    this.userName = sender;
 | 
					                    this.userName = sender;
 | 
				
			||||||
| 
						 | 
					@ -167,11 +184,13 @@ public class ServerConnectionHandler extends ConnectionHandler implements Runnab
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } catch(ChatProtocolException e) {
 | 
					        } catch(ChatProtocolException e) {
 | 
				
			||||||
            System.out.println("Error while processing data" + e.getMessage());
 | 
					            System.out.println("Error while processing data " + e.getMessage());
 | 
				
			||||||
            sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
 | 
					            sendData(USER_NONE, userName, getDataTypeError(), e.getMessage());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void sendData(String sender, String receiver, String type, String payload) {
 | 
					    private void sendData(String sender, String receiver, String type, String payload) {
 | 
				
			||||||
        if (getConnection().isAvailable()) {
 | 
					        if (getConnection().isAvailable()) {
 | 
				
			||||||
            new StringBuilder();
 | 
					            new StringBuilder();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue