diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..0237a42 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -24,6 +24,7 @@ public class ClientConsole implements ChatIF * The default port to connect on. */ final public static int DEFAULT_PORT = 5555; + String loginid; //Instance variables ********************************************** @@ -41,11 +42,13 @@ public class ClientConsole implements ChatIF * @param host The host to connect to. * @param port The port to connect on. */ - public ClientConsole(String host, int port) + public ClientConsole(String host, int port, String loginid) { + loginid = loginid; try { - client= new ChatClient(host, port, this); + client = new ChatClient(host, port, this); + client.handleMessageFromClientUI("#login " + loginid); } catch(IOException exception) { @@ -73,8 +76,14 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); - } + if (message.toString().charAt(0) == '#'){ + commands(message.split(" ")); + + } + else { + client.handleMessageFromClientUI(message); + } + } } catch (Exception ex) { @@ -82,6 +91,47 @@ public void accept() ("Unexpected error while reading from console!"); } } + + protected void commands(String[] splitMessage) { + if (splitMessage[0] == "#quit") { + try{ + client.closeConnection(); + client.quit(); + } + catch(Exception e) { + } + } + else if (splitMessage[0] == "#logoff"){ + try { + client.closeConnection(); + } + catch(Exception e) { + } + } + else if (splitMessage[0] == "#sethost"){ + if(!client.isConnected()){ + client.setHost(splitMessage[1]); + } + } + else if (splitMessage[0] == "#setport"){ + if(!client.isConnected()) { + client.setPort(Integer.parseInt(splitMessage[1])); + } + } + else if (splitMessage[0] == "#login"){ + try{ + client.openConnection(); + } + catch(IOException e) { + } + } + else if (splitMessage[0] == "#gethost"){ + System.out.println("Host: "+client.getHost()); + } + else if (splitMessage[0] == "#getport"){ + System.out.println("Port: "+client.getPort()); + } + } /** * This method overrides the method in the ChatIF interface. It @@ -105,17 +155,18 @@ public void display(String message) public static void main(String[] args) { String host = ""; + String loginID = ""; int port = 0; //The port number - try - { - host = args[0]; + if(args.length==0){ + System.out.println("ERROR - No login ID specified. Connection Aborted."); + System.exit(1); } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; + else if(args.length == 1){ + loginID = args[0]; + host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + ClientConsole chat= new ClientConsole(host, DEFAULT_PORT, loginID); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..6ff77a0 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -58,6 +58,16 @@ public void handleMessageFromServer(Object msg) { clientUI.display(msg.toString()); } + + protected void connectionException(Exception exception) { + clientUI.display("WARNING - Server has stopped listening for connections"); + clientUI.display("SERVER SHUTTING DOWN! DISCONNECTING!"); + clientUI.display("Abnormal termination of connection."); + } + + protected void connectionClosed() { + System.out.println("Disconnected from server."); + } /** * This method handles all data coming from the UI