diff --git a/TestCase Analysis.pdf b/TestCase Analysis.pdf new file mode 100644 index 0000000..b59e395 Binary files /dev/null and b/TestCase Analysis.pdf differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..90e61e9 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -5,6 +5,8 @@ import java.io.*; import client.*; import common.*; +import java.util.Arrays; +import java.util.List; /** * This class constructs the UI for a chat client. It implements the @@ -41,17 +43,16 @@ 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) { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); + System.out.println("Cannot open connection. Awaiting command."); + // System.exit(1); } } @@ -64,6 +65,12 @@ public ClientConsole(String host, int port) */ public void accept() { + try{ + client.handleMessageFromClientUI("#login " + client.loginID);} + catch (NullPointerException f) {} + + + try { BufferedReader fromConsole = @@ -73,7 +80,15 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + + + //This is where I decide what to do with the code. + + if ( message.charAt(0) == '#' ){ + client.clientCommand(message);} + + else {client.handleMessageFromClientUI(message);} + } } catch (Exception ex) @@ -93,6 +108,28 @@ public void display(String message) { System.out.println("> " + message); } + + /** + * 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 server has shutdown."); + } + + /** + * 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) { + // connectionClosed(); + } //Class methods *************************************************** @@ -104,18 +141,42 @@ 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]; + host = args[1]; + port = Integer.parseInt(args[2]); } catch(ArrayIndexOutOfBoundsException e) { - host = "localhost"; + port = DEFAULT_PORT; + + try + { + loginID = args[0]; + host = args[1]; + } + catch(ArrayIndexOutOfBoundsException f) + { + host = "localhost"; + + try + { + loginID = args[0]; + } + catch (Exception g) + { + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(1); + } + } } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + 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..3387037 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,8 @@ import java.io.*; import ocsf.server.*; +import java.util.Arrays; +import java.util.List; /** * This class overrides some of the methods in the abstract @@ -39,6 +41,17 @@ public EchoServer(int port) //Instance methods ************************************************ + /** + * This method handles all data coming from the ServerConsole + * + * @param message The message from the ServerConsole. + */ + public void handleMessageFromServerConsole(String message) + { + this.sendToAllClients(message); + System.out.println(message); + } + /** * This method handles any messages received from the client. * @@ -48,8 +61,49 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + //modify so it does more than echo messages + String msgString = msg.toString(); + String[] messageArray = msgString.split(" "); + + switch(msgString.charAt(0)) { + case '#': + + switch(messageArray[0]) { + case "#login": //not done + + if (client.getInfo("firstContact")==null){ + client.setInfo("firstContact",false); + client.setInfo("loginID",messageArray[1]); + this.sendToAllClients(messageArray[1] + " has logged on."); + System.out.println(messageArray[1] + " has logged on."); + } + + else { + try { + client.sendToClient("You already logged-in our ID."); + } + catch (IOException w) {System.out.println("test");} + } + break; + + default: + if (client.getInfo("firstContact")==null){ + try { + client.sendToClient("Your first command to the Server must be #login."); + client.close(); + } + catch (IOException w) {} + } + //Here you would add more server command accessible by client. + break; + } + break; + + default: + System.out.println("Message received: " + msg + " from " + client); + this.sendToAllClients(client.getInfo("loginID") + " > " + msg); + break; + } } /** @@ -72,6 +126,124 @@ protected void serverStopped() ("Server has stopped listening for connections."); } + /** + * 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."); + } + + /** + * 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 client the connection with the client. + */ + synchronized protected void clientDisconnected(ConnectionToClient client) + { + System.out.println(client.getInfo("loginID") + " has disconnected."); + this.sendToAllClients(client.getInfo("loginID") + " 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) + { + clientDisconnected(client); + } + + /** + * This method contains all the server commands. + * Every commands begin with '#'. + * + * @param command word that needs to be actioned. + */ + public void serverCommand(String message){ + + //Make the message received into an array (necessary for setHost/setPort) + String[] messageArray = message.split(" "); + String[] commandArray = new String[]{"#quit","#start","#stop","#close","#getport","#setport"}; + + //If the command isn't in the 'known' list, give error message and command list. + if (!Arrays.asList(commandArray).contains(messageArray[2])){ + System.out.println("The command line you entered is 'INVALID', here is a list of all current commands."); + System.out.println("#quit // #start // #stop // #close // #getport // #setport"); + } + + switch(messageArray[2]) { + case "#quit": //not done + try + { + close(); + } + catch(IOException e) {System.out.println("exception");} + System.exit(0); + break; + + case "#start"://finished + //what happens in #start + if (!isListening()) + { + try + { + listen(); + } + catch (IOException e) {} + } break; + + case "#stop"://completed + this.stopListening(); + this.sendToAllClients("WARNING - Server has stopped listening for connections."); + break; + + case "#close"://completed + try + { + this.stopListening(); + this.close(); + } + catch(IOException e) {} + break; + + case "#getport"://completed + + try + { + System.out.println("SYSTEM ::: The current Port is: " + getPort()); + } + + catch (Exception a) + { + System.out.println("SYSTEM ::: There was an error in the getPort attempt"); + } + break; + + case "#setport": //completed + + try { + int newPort = Integer.parseInt(messageArray[3]); + + setPort(newPort); + System.out.println("SYSTEM ::: The new Port has been set to: " + getPort()); + } catch (Exception ex2) + { + System.out.println("SYSTEM ::: Array Out-of-Bound or your Port was not a valid Integer between 1 and 65535."); + } break; + } + } + //Class methods *************************************************** /** @@ -81,29 +253,29 @@ protected void serverStopped() * @param args[0] The port number to listen on. Defaults to 5555 * if no argument is entered. */ - public static void main(String[] args) + public static void main(int port) { - int port = 0; //Port to listen on + //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 - } + // 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); + //EchoServer sv = new EchoServer(port); - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); - } + // try + // { + // sv.listen(); //Start listening for connections + // } + // catch (Exception ex) + // { + // System.out.println("ERROR - Could not listen for clients!"); + // } } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..3927d41 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,171 @@ +// This file contains material supporting section 3.7 of the textbook: +// "Object Oriented Software Engineering" and is issued under the open-source +// license found at www.lloseng.com + +import java.io.*; +import client.*; +import common.*; +import java.util.Arrays; +import java.util.List; + +/** + * This class constructs the UI for a chat client. It implements the + * chat interface in order to activate the display() method. + * Warning: Some of the code here is cloned in ServerConsole + * + * @author François Bélanger + * @author Dr Timothy C. Lethbridge + * @author Dr Robert Laganière + * @version July 2000 + */ +public class ServerConsole implements ChatIF +{ + //Class variables ************************************************* + + /** + * The default port to connect on. + */ + final public static int DEFAULT_PORT = 5555; + + //Instance variables ********************************************** + + /** + * The instance of the client that created this ConsoleChat. + */ + ChatClient client; + + /** + * The instance of EchoServer for this Console. + */ + static EchoServer echo; + + //Constructors **************************************************** + + /** + * Constructs an instance of the ClientConsole UI. + * + * @param host The host to connect to. + * @param port The port to connect on. + */ + public ServerConsole(int port) + { + try + { + echo = new EchoServer(port); + } + catch(Exception e) + { + System.out.println("SYSTEM ::: Could not setup server"); + System.exit(1); + } + } + + + //Instance methods ************************************************ + + /** + * This method waits for input from the console. Once it is + * received, it sends it to the client's message handler. + */ + public void accept() + { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) + { + message = fromConsole.readLine(); + message = "SERVER MSG> " + message; + + + //This is where I decide what to do with the code. + + if ( message.charAt(12) == '#' ){ + echo.serverCommand(message);} + + else {echo.handleMessageFromServerConsole(message);} + + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } + + /** + * This method overrides the method in the ChatIF interface. It + * displays a message onto the screen. + * + * @param message The string to be displayed. + */ + public void display(String message) + { + System.out.println("> " + message); + } + + /** + * 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 server has shutdown."); + } + + /** + * 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) { + connectionClosed(); + } + + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of the Client UI. + * + * @param args[0] The host to connect to. + */ + public static void main(String[] args) + { + int port = 0; //The port number + + try + { + port = Integer.parseInt(args[0]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + + + + ServerConsole chat= new ServerConsole(port); + + try + { + echo.listen(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + } + + chat.accept(); //Wait for console data + } +} +//End of ConsoleChat class diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..ee8fa73 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -7,6 +7,8 @@ import ocsf.client.*; import common.*; import java.io.*; +import java.util.Arrays; +import java.util.List; /** * This class overrides some of the methods defined in the abstract @@ -26,6 +28,11 @@ public class ChatClient extends AbstractClient * the display method in the client. */ ChatIF clientUI; + + /** + * LoginID for the client. + */ + public String loginID; //Constructors **************************************************** @@ -38,12 +45,16 @@ 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.loginID = loginID; this.clientUI = clientUI; - openConnection(); + + try{ + openConnection();} + catch (Exception e){} } @@ -73,8 +84,8 @@ public void handleMessageFromClientUI(String message) catch(IOException e) { clientUI.display - ("Could not send message to server. Terminating client."); - quit(); + ("Cannot open connection. Awaiting command."); + //quit(); } } @@ -90,5 +101,124 @@ public void quit() catch(IOException e) {} System.exit(0); } + + /** + * 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("Connection 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) { + //connectionClosed(); + System.out.println("SERVER SHUTTING DOWN! DISCONNECTING!"); + System.out.println("Abnormal termination of connection."); + // quit(); + } + + /** + * This method contains all the client commands. + * Every commands begin with '#'. + * + * @param command word that needs to be actioned. + */ + public void clientCommand(String message){ + + //Make the message received into an array (necessary for setHost/setPort) + String[] messageArray = message.split(" "); + String[] commandArray = new String[]{"#quit","#login","#logoff","#gethost","#sethost","#getport","#setport"}; + + //If the command isn't in the 'known' list, give error message and command list. + if (!Arrays.asList(commandArray).contains(messageArray[0])){ + System.out.println("The command line you entered is 'INVALID', here is a list of all current commands."); + System.out.println("#quit // #login // #logoff // #gethost // #sethost // #getport // #setport"); + } + + switch(messageArray[0]) { + case "#quit": + quit(); + + case "#login": + //what happens in #login + if (isConnected()){ + System.out.println("SYSTEM ::: The client is already connected"); + return; + } + else { + try + { + openConnection(); + + try{ + handleMessageFromClientUI("#login " + loginID);} + catch (NullPointerException f) {} + + } + catch (IOException ex){System.out.println("SYSTEM ::: Failed to connect to Server");} + return; + } + + case "#logoff": + try + { + closeConnection(); + return; + } + catch(IOException e) {} + return; + + case "#gethost": + System.out.println("SYSTEM ::: The Host name is: " + getHost()); + return; + + case "#sethost": + + try + { + setHost(messageArray[1]); + System.out.println("SYSTEM ::: The new Host has been set to: " + getHost()); + } catch (Exception ex2) + { + System.out.println("SYSTEM ::: Array Out of Bound: Please try again with the new Host name."); + } return; + + case "#getport": + + try + { + System.out.println("SYSTEM ::: The current Port is: " + getPort()); + } + + catch (Exception a) + { + System.out.println("SYSTEM ::: There was an error in the getPort attempt"); + } + return; + + case "#setport": + + try { + int newPort = Integer.parseInt(messageArray[1]); + + setPort(newPort); + System.out.println("SYSTEM ::: The new Port has been set to: " + getPort()); + } catch (Exception ex2) + { + System.out.println("SYSTEM ::: Array Out-of-Bound or your Port was not a valid Integer between 1 and 65535."); + } return; + } + } + } //End of ChatClient class