diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..78a05eb 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -5,7 +5,6 @@ import java.io.*; 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. @@ -41,11 +40,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) { @@ -100,23 +99,44 @@ public void display(String message) /** * This method is responsible for the creation of the Client UI. * - * @param args[0] The host to connect to. + * @param args[0] The login id + * @param args[1] The host to connect to. + * @param args[2] The port to connect to. */ public static void main(String[] args) { String host = ""; - int port = 0; //The port number - + int port ; //The port number + String loginid=""; + try{ + loginid=args[0]; + } + catch(Exception e){ + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(0); + return; + } + try { - host = args[0]; + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data + + try + { + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + ClientConsole cc= new ClientConsole(loginid,host, port); + cc.accept(); } } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..2178b67 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,10 +4,12 @@ import java.io.*; import ocsf.server.*; +import common.*; +import java.util.Scanner; /** - * 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,194 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer -{ - //Class variables ************************************************* - +public class EchoServer extends AbstractServer { + // Class variables ************************************************* + /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - - //Constructors **************************************************** - + + // Constructors **************************************************** + /** * Constructs an instance of the echo server. * * @param port The port number to connect on. */ - public EchoServer(int port) - { + public EchoServer(int port) { super(port); + } - - //Instance methods ************************************************ - + // Instance methods ************************************************ + /** * This method handles any messages received from the client. * - * @param msg The message 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); + public void handleMessageFromClient(Object msg, ConnectionToClient client) { + String message = (String) msg; + String[] mes = message.split(" "); + try { + if (mes[0].equals("#login")) { + if (client.getInfo("loginid") == null) { + String loginid = mes[1]; + client.setInfo("loginid", loginid); + this.sendToAllClients(client.getInfo("loginid") + " logged in"); + + } else { + + client.sendToClient("Error, please try again!"); + client.close(); + } + } + } catch (Exception ex) { + } + System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); + + this.sendToAllClients(client.getInfo("loginid") + "> " + msg); + } - + + public void handleMessageFromServerUI(String message) { + + if (message.charAt(0)=='#') { + switch (message.substring(0)) { + case "#close": + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); + serverClosed(); + break; + case "#stop": + serverStopped(); + sendToAllClients("WARNING - Server has stopped listening for connections."); + break; + case "#quit": + try { + close(); + } catch (IOException e) { + + } + System.out.println("server quits..."); + break; + case "#setport": + if (!isListening()) { + try { + int port = Integer.parseInt(message.substring(9)); + setPort(port); + System.out.println("Port is set to: " + port); + } catch (Exception e) { + System.out.println("invalid port, please try again"); + return; + } + } else { + System.out.println("You need to close server before set port"); + return; + } + break; + case "#start": + if (!isListening()) { + try { + listen(); + System.out.println("^^ logging you in ...^^"); + } catch (Exception e) { + System.out.println("Exception occurred when log in. please try again"); + return; + } + } else { + System.out.println("You need to close server before start server"); + return; + } + break; + case "#getport": + System.out.println("Your current port is: " + getPort()); + break; + default: + System.out.println("Invalid command"); + break; + } + } else { + System.out.println(message); + this.sendToAllClients("SERVER MSG> " + message); + + } + } + /** - * This method overrides the one in the superclass. Called - * when the server starts listening for connections. + * 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()); + @Override + 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. + * This method overrides the one in the superclass. Called called when the + * server is closed. */ - protected void serverStopped() - { - System.out.println - ("Server has stopped listening for connections."); + @Override + protected void serverClosed() { + System.out.println("Server has closed."); } - - //Class methods *************************************************** - + + @Override + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); + } + + @Override + protected synchronized void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); + + } + + @Override + protected synchronized void clientException(ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); + } + /** - * This method is responsible for the creation of - * the server instance (there is no UI in this phase). + * This method overrides the one in the superclass. Called when the server stops + * listening for connections. + */ + @Override + 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. + * @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 + public static void main(String[] args) { + int port = 0; // Port to listen on - try - { - port = Integer.parseInt(args[0]); //Get port from command line + try { + port = Integer.parseInt(args[0]); // Get port from command line + } catch (Throwable t) { + port = DEFAULT_PORT; // Set port to 5555 } - 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) - { + ServerConsole sc = new ServerConsole(sv); + + try { + sv.listen(); // Start listening for connections + } catch (Exception ex) { System.out.println("ERROR - Could not listen for clients!"); } + sc.accept(); } + } -//End of EchoServer class +// End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..0b5deeb --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,120 @@ +// 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 common.ChatIF; +import java.io.*; +/** + * This class constructs the UI for a chat server, followed ClientConsole.java. + * It implements the chatIF 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 { + +//Instance variables ********************************************** + + /** + * The instance of the client that created this ConsoleChat. + */ + EchoServer echoServer; + + +//Constructors **************************************************** + + /** + * Constructs an instance of the ServerConsole UI. + * + * @param port The port to connect on. + */ + public ServerConsole(int port) + { + echoServer = new EchoServer(port); + try + { + echoServer.listen(); + } + catch(IOException exception) + { + System.out.println("Could not listen for clients!" + + " Terminating client."); + System.exit(1); + } + } + + public ServerConsole(EchoServer server){ + this.echoServer = server; + } + //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(); + echoServer.handleMessageFromServerUI(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("SERVER MSG> " + message); + } + + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of the Server UI. + * + * @param args[0] The port to listen to. + */ + public static void main(String[] args) + { + + int port = 0; //The port number + + try + { + port = Integer.parseInt(args[0]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = 5555; + } + + ServerConsole sv = new ServerConsole(port); + sv.accept(); + +} +} +//End of ServerConsole class + + + diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..a861581 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -9,86 +9,164 @@ 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 ********************************************** - +public class ChatClient extends AbstractClient { + // Instance variables ********************************************** + /** - * The interface type variable. It allows the implementation of - * the display method in the client. + * The interface type variable. It allows the implementation of the display + * method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + String loginid; + + // Constructors **************************************************** - - //Constructors **************************************************** - /** * Constructs an instance of the chat client. * - * @param host The server to connect to. - * @param port The port number to connect on. + * @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 + + public ChatClient(String loginid, String host, int port, ChatIF clientUI) throws IOException { + super(host, port); // Call the superclass constructor this.clientUI = clientUI; - openConnection(); + this.loginid = loginid; + try { + openConnection(); + sendToServer("#login ID: " + loginid); + } catch (IOException e) { + System.out.println("Cannot open connection. Awaiting command."); + } } - - //Instance methods ************************************************ - + // 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) - { + public void handleMessageFromServer(Object msg) { clientUI.display(msg.toString()); } /** - * This method handles all data coming from the UI + * This method handles all data coming from the UI * - * @param message The message 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."); + public void handleMessageFromClientUI(String message) { + try { + if (message.charAt(0) == '#') { + switch (message) { + case "#quit": + quit(); + case "#logoff": + quit(); + case "#sethost": + if (!isConnected()) { + try { + String host = message.substring(0); + setHost(host); + System.out.println("Host is set to: " + host); + } catch (Exception e) { + clientUI.display("invalid host, please try again"); + return; + } + } else { + clientUI.display("You need to log off before set host"); + return; + } + break; + case "#setport": + if (!isConnected()) { + try { + int port = Integer.parseInt(message.substring(0)); + setPort(port); + System.out.println("Port is set to: " + port); + } catch (Exception e) { + clientUI.display("invalid port, please try again"); + return; + } + } else { + clientUI.display("You need to log off before set port"); + return; + } + break; + case "#login": + if (!isConnected()) { + try { + openConnection(); + clientUI.display("^^ logging you in ...^^"); + } catch (Exception e) { + clientUI.display("Exception occurred when log in. please try again"); + return; + } + } else { + clientUI.display("You already logged in."); + return; + } + break; + case "#gethost": + clientUI.display("Your current host is: " + getHost()); + break; + case "#getport": + clientUI.display("Your current port is: " + getPort()); + break; + default: + System.out.println("Invalid command"); + break; + } + } else + + sendToServer(message); + + } catch (IOException e) { + clientUI.display("Could not send message to server. Terminating client."); quit(); } } - + + public void connectionClosed() { + try { + if (!isConnected()) { + closeConnection(); + } + } catch (IOException e) { + connectionException(e); + clientUI.display("Abnormal termination of connection."); + } + } + + /* + * @param exception the exception raised. + */ + public void connectionException(Exception exception) { + + clientUI.display("Abnormal termination of connection."); + quit(); + } + /** * This method terminates the client. */ - public void quit() - { - try - { + public void quit() { + try { closeConnection(); + clientUI.display("client quits"); + } catch (IOException e) { } - catch(IOException e) {} System.exit(0); } } -//End of ChatClient class +// End of ChatClient class diff --git a/testlog.txt b/testlog.txt new file mode 100644 index 0000000..9e71871 --- /dev/null +++ b/testlog.txt @@ -0,0 +1,158 @@ +/** +* name: Xia(Cynthia) Sheng +* student number: 300091655 +* email: xshen061@uottawa.ca +**/ + +Testcase 2001: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. EchoServer +Server listening for connections on port 5555 + + +Testcase 2002: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole +ERROR - No login ID specified. Connection aborted. + +Testcase 2003: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole abc +Cannot open connection. Awaiting command... +Wait for client input) + +Testcase 2004: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc has logged on. + +client: +abc logged in. + +Testcase 2005: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +> Message received: Hello! from abc + +client: +abc logged in. +Hello +>Hello! + + +Testcase 2006: +Server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +A new client is attempting to connect to the server. +Message received: #login xyz from localhost (127.0.0.1) +xyz logged in. +Message received: xyz from xyz +Message received: abc from abc +serveruser +SERVER MSG>serveruser + +client: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole abc +abc logged in. +xyz> xyz +abc +abc>abc +SERVER MSG> serveruser + + +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole xyz +xyz logged in. +abc> abc +xyz +abc>xyz +SERVER MSG> serveruser + + +Testcase 2007: +server: +Server listening for connections on port 5555 +#quit +The server quits. +Server has stopped listening for connections. + +Testcase 2008: +Server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +#stop +Server has stopped listening for connections. +Message received: Hello! from abc +#start +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login xyz from localhost (127.0.0.1) +xyz logged in. +#quit +Server has stopped listening for connections. +> abc has disconnected from the server. +> abc has disconnected from the server. + +client: +abc logged in. +Hello! +abc> Hello! +>Abnormal termination of connection. + +xyz logged in +>Abnormal termination of connection. + + +testcase 2009: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +#stop +Server has stopped listening for connections. +#close +abc has disconnected from the server. + + +client: +abc logged in. +WARNING - Server has stopped listening for connections. +SERVER SHUTTING DOWN! DISCONNECTING! +> Abnormal termination of connection. + + +testcase 2010: +client: +#quit +Client quits. + + +testcase 2011: +client: +#logoff +Client quits. +#quit +Client quits. + +testcase 2013: +server: +Server listening for connections on port 1234 +#quit +server quits... +Server has stopped listening for connections. + + + + + + + + +