diff --git a/Testcase Logs.pdf b/Testcase Logs.pdf new file mode 100644 index 0000000..72f61bc Binary files /dev/null and b/Testcase Logs.pdf differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..d3cc04e 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -1,122 +1,130 @@ + // 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 java.util.Scanner; import client.*; import common.*; /** - * 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 + * 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 Timothy C. Lethbridge * @author Dr Robert Laganière * @version July 2000 */ -public class ClientConsole 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; - - - //Constructors **************************************************** - - /** - * Constructs an instance of the ClientConsole UI. - * - * @param host The host to connect to. - * @param port The port to connect on. - */ - public ClientConsole(String host, int port) - { - try - { - client= new ChatClient(host, port, this); - } - catch(IOException exception) - { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - 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(); - client.handleMessageFromClientUI(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); - } - - - //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) - { - String host = ""; - int port = 0; //The port number - - try - { - host = args[0]; - } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; - } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data - } +public class ClientConsole 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; + + // Constructors **************************************************** + + /** + * Constructs an instance of the ClientConsole UI. + * + * @param host The host to connect to. + * @param port The port to connect on. + */ + public ClientConsole(String host, int port, String id) { + try { + client = new ChatClient(host, port, this, id); + } catch (IOException exception) { + System.out.println("Error: Can't setup connection!" + " Terminating client."); + 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(); + client.handleMessageFromClientUI(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); + } + + // 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) { + String host = ""; + int port = DEFAULT_PORT; // The port number + +// Scanner inputScanner = new Scanner(System.in); +// System.out.println("Enter login ID: "); +// String id = inputScanner.nextLine(); +// if(id.isEmpty()) { +// System.out.println("ERROR - No login ID specified. Connection aborted."); +// return; +// } + + String id; + try { + id = args[0]; + } catch (Exception e) { + System.out.println("ERROR - No login ID specified. Connection aborted."); + return; + } + + try { + if (Integer.valueOf(args[1]) > 0 && Integer.valueOf((args[1])) < 100000) { + port = Integer.valueOf((args[1])); + } else { + System.out.println("No valid port specified. Will use default port"); + } + } catch (Exception e) { + System.out.println("No valid port specified. Will use default port"); + } + + try { + host = args[2]; + } catch (ArrayIndexOutOfBoundsException e) { + host = "localhost"; + } + + ClientConsole chat = new ClientConsole(host, port, id); + chat.accept(); // Wait for console data + } } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..b08fb2f 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,13 +1,15 @@ + // 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 java.util.Arrays; import ocsf.server.*; /** - * This class overrides some of the methods in the abstract - * superclass in order to give more functionality to the server. + * This class overrides some of the methods in the abstract superclass in order + * to give more functionality to the server. * * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière @@ -15,95 +17,105 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer -{ - //Class variables ************************************************* - - /** - * The default port to listen on. - */ - final public static int DEFAULT_PORT = 5555; - - //Constructors **************************************************** - - /** - * Constructs an instance of the echo server. - * - * @param port The port number to connect on. - */ - public EchoServer(int port) - { - super(port); - } - - - //Instance methods ************************************************ - - /** - * This method handles any messages received from the client. - * - * @param msg The message received from the client. - * @param client The connection from which the message originated. - */ - public void handleMessageFromClient - (Object msg, ConnectionToClient client) - { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); - } - - /** - * This method overrides the one in the superclass. Called - * when the server starts listening for connections. - */ - protected void serverStarted() - { - System.out.println - ("Server listening for connections on port " + getPort()); - } - - /** - * This method overrides the one in the superclass. Called - * when the server stops listening for connections. - */ - protected void serverStopped() - { - System.out.println - ("Server has stopped listening for connections."); - } - - //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 - } +public class EchoServer extends AbstractServer { + // Class variables ************************************************* + + /** + * The interface type variable. It allows the implementation of the display + * method in the server. + */ + ChatIF serverUI; + + /** + * The default port to listen on. + */ + final public static int DEFAULT_PORT = 5555; + + // Constructors **************************************************** + + /** + * Constructs an instance of the echo server. + * + * @param port The port number to connect on. + */ + public EchoServer(int port) { + super(port); + } + + // Instance methods ************************************************ + + /** + * This method handles any messages received from the client. + * + * @param msg The message received from the client. + * @param client The connection from which the message originated. + */ + public void handleMessageFromClient(Object msg, ConnectionToClient client) { + if (!((String) msg).isEmpty() && ((String) msg).startsWith("#login ")) { + String id = ((String) msg).substring(7); + client.setInfo("clientID" ,id); + } else { + System.out.println("Message received: "+ client.getInfo("clientID")+ ": " + msg + " from " + client); + this.sendToAllClients(msg); + } + } + + /** + * This method overrides the one in the superclass. Called when the server + * starts listening for connections. + */ + protected void serverStarted() { + System.out.println("Server listening for connections on port " + getPort()); + } + + /** + * This method overrides the one in the superclass. Called when the server stops + * listening for connections. + */ + protected void serverStopped() { + System.out.println("Server has stopped listening for connections."); + } + + // 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!"); + } + } + + /** + * Method called whenever a client connects. Informs server that a client has been connected + */ + protected void clientConnected(ConnectionToClient client) { + System.out.println(client + " is now connected!"); + } - EchoServer sv = new EchoServer(port); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); - } - } + /** + * Method called whenever a client Disconnects. Informs server that a client has been Disconnected + */ + synchronized protected void clientDisconnected(ConnectionToClient client) { + System.out.println(client + " has Disconnected."); + } + } //End of EchoServer class diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..a421fa6 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -9,86 +9,137 @@ import java.io.*; /** - * This class overrides some of the methods defined in the abstract - * superclass in order to give more functionality to the client. + * This class overrides some of the methods defined in the abstract superclass + * in order to give more functionality to the client. * * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganiè * @author François Bélanger * @version July 2000 */ -public class ChatClient extends AbstractClient -{ - //Instance variables ********************************************** - - /** - * The interface type variable. It allows the implementation of - * the display method in the client. - */ - ChatIF clientUI; +public class ChatClient extends AbstractClient { + // Instance variables ********************************************** + + /** + * The interface type variable. It stores the client ID entered by the user. + */ + String clientID; - - //Constructors **************************************************** - - /** - * Constructs an instance of the chat client. - * - * @param host The server to connect to. - * @param port The port number to connect on. - * @param clientUI The interface type variable. - */ - - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException - { - super(host, port); //Call the superclass constructor - this.clientUI = clientUI; - openConnection(); - } + /** + * The interface type variable. It allows the implementation of the display + * method in the client. + */ + ChatIF clientUI; - - //Instance methods ************************************************ - - /** - * This method handles all data that comes in from the server. - * - * @param msg The message from the server. - */ - public void handleMessageFromServer(Object msg) - { - clientUI.display(msg.toString()); - } + // Constructors **************************************************** + + /** + * Constructs an instance of the chat client. + * + * @param host The server to connect to. + * @param port The port number to connect on. + * @param clientUI The interface type variable. + * @param clientID The interface type variable. + */ + + public ChatClient(String host, int port, ChatIF clientUI, String clientID) throws IOException { + super(host, port); // Call the superclass constructor + this.clientUI = clientUI; + this.clientID = clientID; + openConnection(); + } + + // Instance methods ************************************************ + + /** + * This method handles all data that comes in from the server. + * + * @param msg The message from the server. + */ + public void handleMessageFromServer(Object msg) { + clientUI.display(msg.toString()); + } + + /** + * This method handles all data coming from the UI + * + * @param message The message or command from the UI. + */ + public void handleMessageFromClientUI(String message) { + try { + if ( ! ((String) message).isEmpty() && ((String) message).startsWith("#")) { + if (((String) message).equals("#quit")) { + quit(); + } + if (((String) message).equals("#logoff")) { + closeConnection(); + } + if (((String) message).equals("#login")) { + openConnection(); + } + if (((String) message).startsWith("#sethost ")) { + setHost(((String) message).substring(9)); + clientUI.display("Host set to: " + getHost()); + } + if (((String) message).startsWith("#setport ")) { + setPort(Integer.valueOf(((String) message).substring(9))); + clientUI.display("Port set to: " + getPort()); + } + if (((String) message).equals("#gethost")) { + clientUI.display(getHost()); + } + if (((String) message).equals("#getport")) { + clientUI.display(Integer.toString(getPort())); + } + } else { + sendToServer(message); + } + } catch (IOException e) { + clientUI.display("Could not send message to server. Terminating client."); + quit(); + } + } + + /** + * This method terminates the client. + */ + public void quit() { + try { + closeConnection(); + } catch (IOException e) { + } + System.exit(0); + } + + // my methods. + + /** + * Informs the user that connection with server has been terminated. + */ + protected void connectionClosed() { + clientUI.display("Connection closed."); + } + + /** + * Informs the user that there has been an exception while connecting to server. + * @param e the exception being thrown + */ + protected void connectionException(Exception e) { + if (!isConnected()) { + quit(); + } + } + + /** + * Informs the user that connection with server has been established. + */ + protected void connectionEstablished() { + try { + sendToServer("#login " + clientID); + } catch (IOException e) { + clientUI.display("problem sending ID to server"); + } + } - /** - * This method handles all data coming from the UI - * - * @param message The message from the UI. - */ - public void handleMessageFromClientUI(String message) - { - try - { - sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); - } - } - - /** - * This method terminates the client. - */ - public void quit() - { - try - { - closeConnection(); - } - catch(IOException e) {} - System.exit(0); - } } //End of ChatClient class