From 36e3e9fc4519f30cbb9e44f10fa903f8a7434231 Mon Sep 17 00:00:00 2001 From: Xiaoxi Jia <60756026+1998Charles@users.noreply.github.com> Date: Fri, 19 Jun 2020 00:48:32 -0400 Subject: [PATCH] Modified ClientConsole, EchoServer, ChatClient; Added Xiaoxi_Jia_Testresults, ServerConsole --- Xiaoxi_Jia_Testresults.txt | 313 ++++++++++++++++++++++++ code/simplechat1/ClientConsole.java | 40 ++- code/simplechat1/EchoServer.java | 298 +++++++++++++++++++--- code/simplechat1/ServerConsole.java | 89 +++++++ code/simplechat1/client/ChatClient.java | 137 ++++++++++- 5 files changed, 840 insertions(+), 37 deletions(-) create mode 100644 Xiaoxi_Jia_Testresults.txt create mode 100644 code/simplechat1/ServerConsole.java diff --git a/Xiaoxi_Jia_Testresults.txt b/Xiaoxi_Jia_Testresults.txt new file mode 100644 index 0000000..9bda98c --- /dev/null +++ b/Xiaoxi_Jia_Testresults.txt @@ -0,0 +1,313 @@ +Name: Xiaoxi Jia +ID: 300000227 +E-mail: xjia033@uottawa.ca + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +^C% +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole +Error: Can't setup connection! LoginID is mandatory. Terminating client. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +Error: Can't setup connection! Terminating client. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Xiaoxi_Jia has disconnected. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Message received: Hello, World. from localhost (127.0.0.1) Xiaoxi_Jia +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +Hello, World. +> Xiaoxi_Jia: Hello, World. +The server has shut down. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +A new client is attempting to connect to the server. +#login User_1 has logged on. +Message received: Hello! from localhost (127.0.0.1) Xiaoxi_Jia +Message received: Hi! from localhost (127.0.0.1) User_1 + +SERVER MSG> +User_1 has disconnected. +Xiaoxi_Jia has disconnected. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +Hello! +> Xiaoxi_Jia: Hello! +> User_1: Hi! +> SERVER MSG> +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole User_1 +The connection has established. +> Xiaoxi_Jia: Hello! +Hi! +> User_1: Hi! +> SERVER MSG> +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +#quit +The server has closed. +Server has stopped listening for connections. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +#stop +Server has stopped listening for connections. +Message received: Hello? from localhost (127.0.0.1) Xiaoxi_Jia +#start +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login User_1 has logged on. +#quit +Server has stopped listening for connections. +Xiaoxi_Jia is disconnected by server. +User_1 is disconnected by server. +The server has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +Hello? +> Xiaoxi_Jia: Hello? +The server has shut down. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole User_1 +The connection has established. +The server has shut down. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +#stop +Server has stopped listening for connections. +#close +Xiaoxi_Jia is disconnected by server. +The server has closed. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +The server has shut down. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +#close +Server has stopped listening for connections. +Xiaoxi_Jia is disconnected by server. +The server has closed. +#start +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Xiaoxi_Jia has disconnected. +#quit +The server has closed. +Server has stopped listening for connections. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +The server has shut down. +#login +The connection has established. +^C% jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole +Error: Can't setup connection! LoginID is mandatory. Terminating client. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Xiaoxi_Jia has disconnected. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +#logoff +The connection has closed. +#quit +The connection has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Xiaoxi_Jia has disconnected. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole 1234 +Server listening for connections on port 1234 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +Xiaoxi_Jia has disconnected. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. +#gethost +The current host name is localhost. +#getport +The current port number is 5555. +#logoff +The connection has closed. +#sethost 192.168.1.163 +#setport 1234 +#login +The connection has established. +#quit +The connection has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole 1234 +Server listening for connections on port 1234 +#quit +The server has closed. +Server has stopped listening for connections. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole 1234 +Server listening for connections on port 1234 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia 192.168.1.163 1234 +The connection has established. + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +A new client is attempting to connect to the server. +#login User_1 has logged on. +#close +Server has stopped listening for connections. +Xiaoxi_Jia is disconnected by server. +User_1 is disconnected by server. +The server has closed. +#setport 1234 +#start +Server listening for connections on port 1234 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +A new client is attempting to connect to the server. +#login User_1 has logged on. +User_1 has disconnected. +Xiaoxi_Jia has disconnected. +#quit +The server has closed. +Server has stopped listening for connections. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia 192.168.1.163 +The connection has established. +The server has shut down. +#setport 1234 +#login +The connection has established. +#quit +The connection has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole User_1 +The connection has established. +The server has shut down. +#setport 1234 +#login +The connection has established. +#quit +The connection has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + + + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ServerConsole +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +#login Xiaoxi_Jia has logged on. +A new client is attempting to connect to the server. +#login User_1 has logged on. +A new client is attempting to connect to the server. +#login User_2 has logged on. +User_1 has disconnected. +User_2 has disconnected. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole Xiaoxi_Jia +The connection has established. + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole User_1 +The connection has established. +#quit +The connection has closed. +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % + +jiaxiaoxi@MacBook-Pro-Xi simplechat1 % java -classpath '../../code:.' ClientConsole User_2 +The connection has established. +#logoff +The connection has closed. + diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..fefa3ad 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,11 +41,11 @@ 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 loginID, String host, int port) { try { - client= new ChatClient(host, port, this); + client= new ChatClient(loginID, host, port, this); } catch(IOException exception) { @@ -104,18 +104,50 @@ public void display(String message) */ public static void main(String[] args) { + String loginID = ""; String host = ""; int port = 0; //The port number try { - host = args[0]; + loginID = args[0]; } catch(ArrayIndexOutOfBoundsException e) + { + System.out.println("Error: Can't setup connection!" + + " LoginID is mandatory." + + " Terminating client."); + System.exit(1); + } + + try + { + host = args[1]; + } + catch(ArrayIndexOutOfBoundsException e) + { + host = "localhost"; + } + catch(NumberFormatException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + try + { + port = Integer.parseInt(args[2]); //Obtains the port number from the command line. + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; //Handles the case that the port is omitted from the command line. + } + catch(NumberFormatException e) //Handles invalid input for the port number. + { + System.out.println("Error: Can't setup connection!" + + " Terminating client."); + System.exit(1); + } + ClientConsole chat = new ClientConsole(loginID, host, port); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..cefe0e9 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,7 @@ import java.io.*; import ocsf.server.*; +import common.*; /** * This class overrides some of the methods in the abstract @@ -18,12 +19,26 @@ public class EchoServer extends AbstractServer { //Class variables ************************************************* - + /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; + + //Instance variables ********************************************** + + /** + * The interface type variable. It allows the implementation of + * the display method in the server. + */ + ChatIF serverUI; + /** + * The boolean type variable. It checks whether the #login + * is firstly received as the first command. + */ + private boolean firstChecker = false; + //Constructors **************************************************** /** @@ -31,9 +46,11 @@ public class EchoServer extends AbstractServer * * @param port The port number to connect on. */ - public EchoServer(int port) + public EchoServer(int port, ChatIF serverUI) throws IOException { super(port); + this.serverUI = serverUI; + listen(); } @@ -46,12 +63,78 @@ public EchoServer(int port) * @param client The connection from which the message originated. */ public void handleMessageFromClient - (Object msg, ConnectionToClient client) + (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + if (firstChecker = true){ + try + { + String string = new String(""); + string = string + msg; + char[] msgArray = string.toCharArray(); + + if (msgArray[0] == '#') { + String [] stringArray = string.split("\\s+"); + if (stringArray[0].equals("#login") && stringArray.length == 2) { + + if (client.getInfo("ID") == null){ + System.out.println(msg + " has logged on."); + client.setInfo("ID", stringArray[1]); + firstChecker = true; + + } + else + { + try + { + client.sendToClient("SERVER MSG> Error: Could not reset ID information."); + } + catch(IOException e) + {} + // client.close(); + } + } + + else + { + System.out.println("Message received: " + msg + " from " + client + " " + client.getInfo("ID").toString()); + this.sendToAllClients(client.getInfo("ID").toString() + ": " + msg); + } + } + + + else + { + System.out.println("Message received: " + msg + " from " + client + " " + client.getInfo("ID").toString()); + this.sendToAllClients(client.getInfo("ID").toString() + ": " + msg); + } + } + + catch(ArrayIndexOutOfBoundsException e) + { + System.out.println("Message received: " + msg + " from " + client + " " + client.getInfo("ID").toString()); + this.sendToAllClients(client.getInfo("ID").toString() + ": " + msg); + } + } + else + { + try + { + client.sendToClient("SERVER MSG> Error: #login command MUST be the first command received."); + client.close(); + } + catch(IOException e) + { + try + { + client.close(); + } + catch(Exception ex) + {} + } + } } - + + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. @@ -59,7 +142,7 @@ public EchoServer(int port) protected void serverStarted() { System.out.println - ("Server listening for connections on port " + getPort()); + ("Server listening for connections on port " + getPort()); } /** @@ -69,41 +152,200 @@ protected void serverStarted() protected void serverStopped() { System.out.println - ("Server has stopped listening for connections."); + ("Server has stopped listening for connections."); } - //Class methods *************************************************** + // //Class methods *************************************************** + // /** + // * This method is responsible for the creation of + // * the server instance (there is no UI in this phase). + // * + // * @param args[0] The port number to listen on. Defaults to 5555 + // * if no argument is entered. + // */ + // public static void main(String[] args) + // { + // int port = 0; //Port to listen on + + // try + // { + // port = Integer.parseInt(args[0]); //Get port from command line + // } + // catch(Throwable t) + // { + // port = DEFAULT_PORT; //Set port to 5555 + // } + + // EchoServer sv = new EchoServer(port); + + // try + // { + // sv.listen(); //Start listening for connections + // } + // catch (Exception ex) + // { + // System.out.println("ERROR - Could not listen for clients!"); + // } + // } + +//Was the end of EchoServer class + +// METHODS DESIGNED TO BE OVERRIDDEN BY CONCRETE SUBCLASSES --------- + + /** + * Hook method called each time a new client connection is + * accepted. The default implementation does nothing. + * @param client the connection connected to the client. + */ + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); //Indicates a new client is attempting to connect to the server.. + } + /** - * This method is responsible for the creation of - * the server instance (there is no UI in this phase). + * Hook method called each time a client disconnects. + * The default implementation does nothing. The method + * may be overridden by subclasses but should remains synchronized. * - * @param args[0] The port number to listen on. Defaults to 5555 - * if no argument is entered. + * @param client the connection with the client. */ - public static void main(String[] args) - { - int port = 0; //Port to listen on + synchronized protected void clientDisconnected( + ConnectionToClient client) { + System.out.println(client.getInfo("ID").toString() + " is disconnected by server."); //Indicates a client has disconnected. + } + + /** + * Hook method called each time an exception is thrown in a + * ConnectionToClient thread. + * The method may be overridden by subclasses but should remains + * synchronized. + * + * @param client the client that raised the exception. + * @param Throwable the exception thrown. + */ + synchronized protected void clientException( + ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("ID").toString() + " has disconnected."); //Indicates the client has disconnected. + } + + /** + * Hook method called when the server stops accepting + * connections because an exception has been raised. + * The default implementation does nothing. + * This method may be overriden by subclasses. + * + * @param exception the exception raised. + */ + protected void listeningException(Throwable exception) { + System.out.println("The server stops accepting."); + } + + /** + * Hook method called when the server is clased. + * The default implementation does nothing. This method may be + * overriden by subclasses. When the server is closed while still + * listening, serverStopped() will also be called. + */ + protected void serverClosed() { + System.out.println("The server has closed."); + } + /** + * This method handles all data coming from the UI + * + * @param message The message from the UI. + */ + public void handleMessageFromServerUI(String message) + { try { - port = Integer.parseInt(args[0]); //Get port from command line + char[] messageArray = message.toCharArray(); + + if (messageArray[0] == '#') { + String [] stringArray = message.split("\\s+"); + if (stringArray[0].equals("#setport")) { + if (isListening() == false && getNumberOfClients() == 0){ + setPort(Integer.parseInt(stringArray[1])); + } + else { + System.out.println("Only allowed if the server is closed."); + } + } + + else if (stringArray[0] != "#setport" && stringArray[0] != "#setport"){ + switch(message){ + case "#quit" : + quit(); + break; + case "#stop" : + stopListening(); + break; + case "#close" : + close(); + break; + case "#start" : + if (isListening() == false) + { + listen(); + } + else + { + System.out.println("Only allowed if the server is stopped."); + } + break; + case "#getport" : + System.out.println("The current port number is " + getPort() + "."); + break; + default : + sendToAllClients("SERVER MSG> " + message); + serverUI.display(message); + } + } + else { + sendToAllClients("SERVER MSG> " + message); + serverUI.display(message); + } + } + else { + sendToAllClients("SERVER MSG> " + message); + serverUI.display(message); + } } - catch(Throwable t) + + catch(IOException e) { - port = DEFAULT_PORT; //Set port to 5555 + serverUI.display + ("Could not send message to clients. Terminating server."); + quit(); } - - EchoServer sv = new EchoServer(port); - - try + + catch(ArrayIndexOutOfBoundsException e) { - sv.listen(); //Start listening for connections - } - catch (Exception ex) + try + { + sendToAllClients("SERVER MSG> " + message); + serverUI.display(message); + } + catch(Exception ex) + { + serverUI.display + ("Could not send message to clients. Terminating server."); + quit(); + } + } + } + /** + * This method terminates the client. + */ + public void quit() + { + try { - System.out.println("ERROR - Could not listen for clients!"); + close(); } + catch(IOException e) {} + System.exit(0); } } -//End of EchoServer class + + diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..4b06f4b --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,89 @@ +import java.io.*; +import common.*; +import ocsf.server.*; + +public class ServerConsole implements ChatIF +{ + + final public static int DEFAULT_PORT = 5555; + + EchoServer server; + + public ServerConsole(int port){ + + try + { + server = new EchoServer(port, this); + } + catch(Exception exception) + { + System.out.println("Error: Can't setup server!" + + " Terminating server."); + System.exit(1); + } + + } + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of + * the server instance (there is a UI in this phase). + * + * @param args[0] The port number to listen on. Defaults to 5555 + * if no argument is entered. + */ + + public static void main(String[] args) + { + int port = 0; //Port to listen on + + try + { + port = Integer.parseInt(args[0]); //Get port from command line + } + catch(Throwable t) + { + port = DEFAULT_PORT; //Set port to 5555 + } + + // EchoServer sv = new EchoServer(port); + // try + // { + // sv.listen(); //Start listening for connections + // } + // catch (Exception ex) + // { + // System.out.println("ERROR - Could not listen for clients!"); + // } + + ServerConsole echo = new ServerConsole(port); + echo.accept(); + } + + public void display(String message) + { + System.out.println("SERVER MSG> " + message); + } + + public void accept() + { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) + { + message = fromConsole.readLine(); + server.handleMessageFromServerUI(message); + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } +} \ No newline at end of file diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..42ba531 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -8,6 +8,7 @@ import common.*; import java.io.*; + /** * This class overrides some of the methods defined in the abstract * superclass in order to give more functionality to the client. @@ -25,7 +26,9 @@ public class ChatClient extends AbstractClient * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + + private String loginID; //Constructors **************************************************** @@ -38,11 +41,12 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) + public ChatClient(String loginID, String host, int port, ChatIF clientUI) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; + this.loginID = loginID; openConnection(); } @@ -68,14 +72,78 @@ public void handleMessageFromClientUI(String message) { try { - sendToServer(message); + char[] messageArray = message.toCharArray(); + + if (messageArray[0] == '#') { + String [] stringArray = message.split("\\s+"); + if (stringArray[0].equals("#sethost")) { + if (isConnected() == false){ + setHost(stringArray[1]); + } + else{ + System.out.println("Only allowed if the client is logged off."); + } + } + + else if (stringArray[0].equals("#setport")) { + if (isConnected() == false){ + setPort(Integer.parseInt(stringArray[1])); + } + else { + System.out.println("Only allowed if the client is logged off."); + } + } + + else if (stringArray[0] != "#setport" && stringArray[0] != "#setport"){ + switch(message){ + case "#quit" : + quit(); + break; + case "#logoff" : + closeConnection(); + break; + case "#login" : + openConnection(); + break; + case "#gethost" : + System.out.println("The current host name is " + getHost() + "."); + break; + case "#getport" : + System.out.println("The current port number is " + getPort() + "."); + break; + default : + sendToServer(message); + } + } + else { + sendToServer(message); + } + } + else { + sendToServer(message); + } } + catch(IOException e) { clientUI.display - ("Could not send message to server. Terminating client."); + ("Could not send message to server. Terminating client."); quit(); } + + catch(ArrayIndexOutOfBoundsException e) + { + try + { + sendToServer(message); + } + catch(IOException ex) + { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } + } } /** @@ -90,5 +158,64 @@ public void quit() catch(IOException e) {} System.exit(0); } + +//WAS the end of ChatClient class + +// ACCESSING METHODS ------------------------------------------------ + + /** + * Returns the login ID. + * + * @return the login ID. + */ + final public String getloginID() + { + return loginID; + } + + // METHODS DESIGNED TO BE OVERRIDDEN BY CONCRETE SUBCLASSES --------- + + /** + * Hook method called after the connection has been closed. The default + * implementation does nothing. The method may be overriden by subclasses to + * perform special processing such as cleaning up and terminating, or + * attempting to reconnect. + */ + protected void connectionClosed() { + System.out.println("The connection has closed."); //Indicates the connection has closed. + } + + /** + * Hook method called each time an exception is thrown by the client's + * thread that is waiting for messages from the server. The method may be + * overridden by subclasses. + * + * @param exception + * the exception raised. + */ + protected void connectionException(Exception exception) { + System.out.println("The server has shut down."); //Indicates the server has shut down. + // quit(); //Quits the client console. + } + + /** + * Hook method called after a connection has been established. The default + * implementation does nothing. It may be overridden by subclasses to do + * anything they wish. + */ + protected void connectionEstablished() { + System.out.println("The connection has established."); //Indicates the connection has established. + try + { + sendToServer("#login " + getloginID()); + } + catch(IOException e) + { + clientUI.display + ("Could not set login ID. Terminating client."); + quit(); + } + } } -//End of ChatClient class + +