diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..345c282 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,18 +41,9 @@ 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); - } - catch(IOException exception) - { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); - } + client= new ChatClient(host, port, this,loginId); } @@ -100,22 +91,39 @@ 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] login id + * @param args[1] The host to connect to. + * @param args[2] port */ public static void main(String[] args) { + String loginId = ""; + try { + loginId = args[0]; + } catch (Throwable e) { + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(0); + } String host = ""; int port = 0; //The port number try { - host = args[0]; + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + try + { + port = Integer.parseInt(args[2]); + } + catch(Throwable e) + { + port = 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..782aefe 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -3,10 +3,12 @@ // license found at www.lloseng.com import java.io.*; + +import common.ChatIF; import ocsf.server.*; /** - * This class overrides some of the methods in the abstract + * 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 @@ -15,95 +17,198 @@ * @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 +public class EchoServer extends AbstractServer { + //Class variables ************************************************* + + /** + * The default port to listen on. + */ + final public static int DEFAULT_PORT = 5555; + private ChatIF serverUI; + private boolean closed; + //Constructors **************************************************** + + /** + * Constructs an instance of the echo server. + * + * @param port The port number to connect on. + */ + public EchoServer(int port, ChatIF serverUI) { + super(port); + this.serverUI = serverUI; + } + + + //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.getInfo("loginId")); + String message = (String) msg; + if(message.startsWith("#login")) { + try { + String loginId = message.split(" ")[1]; + if(client.getInfo("loginId") != null) { + client.sendToClient("Error - You has already logined."); + } else { + client.setInfo("loginId",loginId); + message = String.format("%s has logged on.",loginId); + System.out.println(message); + client.sendToClient(message); + } + }catch (Throwable e) {} + } else { + Object temp = client.getInfo("loginId"); + if (temp == null) { + try { + client.close(); + } catch (IOException e) { + } + } else { + this.sendToAllClients((String) temp + "> " + 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()); + closed = false; + } + + /** + * 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."); + } + + public void handleMessageFromServerUI(String message) { + if (message.startsWith("#quit")) { + try { + close(); + System.out.println("The server quits."); + } catch (IOException e) { + } + System.exit(0); + } else if (message.startsWith("#stop")) { + stopListening(); + sendToAllClients("WARNING - Server has stopped listening for connections."); + } else if (message.startsWith("#close")) { + try { + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); + close(); + } catch (IOException e) { + } + } else if (message.startsWith("#setport")) { + if (!closed) { + System.out.println("Only allowed if the server is closed"); + return; + } + try { + int port = Integer.parseInt(message.split("\\s+")[1]); + setPort(port); + System.out.printf("port set to: %d.\n",port); + } catch (Exception e) { + serverUI.display("Invalid port."); + } + } else if (message.startsWith("#start")) { + try { + listen(); + } catch (IOException e) { + } + } else if (message.startsWith("#getport")) { + serverUI.display("current port: " + getPort()); + } else if (message.startsWith("#")) { + serverUI.display("Unknown command."); + } else { + message = "SERVER MSG> " + message; + serverUI.display(message); + sendToAllClients(message); + } + } + + //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!"); +// } +// } + 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 = 5555; //Set port to 5555 + } + ServerConsole serverConsole = new ServerConsole(port); + serverConsole.accept(); + } + + + @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."); } - catch(Throwable t) - { - port = DEFAULT_PORT; //Set port to 5555 + + @Override + protected synchronized void clientException(ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("loginId") + " has disconnected."); + sendToAllClients(client.getInfo("loginId") + " has disconnected."); } - - EchoServer sv = new EchoServer(port); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); + + @Override + protected void serverClosed() { + closed = true; } - } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..b679bdf --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,49 @@ +import common.ChatIF; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class ServerConsole implements ChatIF { + + private EchoServer echoServer; + + public ServerConsole(int port) { + echoServer = new EchoServer(port,this); + try + { + echoServer.listen(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + System.exit(0); + } + } + + 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!"); + } + } + + @Override + public void display(String message) { + System.out.println(message); + } + +} diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..857b322 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -25,7 +25,8 @@ 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,12 +39,17 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException + public ChatClient(String host, int port, ChatIF clientUI,String loginId) { super(host, port); //Call the superclass constructor this.clientUI = clientUI; - openConnection(); + try { + openConnection(); + this.loginId = loginId; + sendToServer("#login " + loginId); + } catch (IOException e) { + System.out.println("Cannot open connection. Awaiting command."); + } } @@ -56,7 +62,7 @@ public ChatClient(String host, int port, ChatIF clientUI) */ public void handleMessageFromServer(Object msg) { - clientUI.display(msg.toString()); + System.out.println(msg.toString()); } /** @@ -66,15 +72,61 @@ public void handleMessageFromServer(Object msg) */ public void handleMessageFromClientUI(String message) { - try - { - sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); + if(message.equals("#quit")) { quit(); + } else if(message.equals("#logoff")) { + try { + closeConnection(); + } catch (IOException ignored) { } + } else if(message.startsWith("#sethost ")) { + if(isConnected()) { + clientUI.display("Only allowed if the client is logged off"); + return; + } + try { + String host = message.split("\\s+")[1]; + setHost(host); + System.out.println("Host set to: " + host); + } catch (Exception e) { + clientUI.display("Invalid host."); + } + } else if(message.startsWith("#setport ")) { + if(isConnected()) { + clientUI.display("Only allowed if the client is logged off"); + return; + } + try { + int port = Integer.parseInt(message.split("\\s+")[1]); + setPort(port); + System.out.printf("port set to: %d.\n",port); + } catch (Exception e) { + clientUI.display("Invalid port."); + } + } else if(message.equals("#login")) { + if(isConnected()) { + clientUI.display("Only allowed if the client is not already connected."); + return; + } + try { + openConnection(); + sendToServer("#login " + loginId); + } catch (IOException e) { + clientUI.display("login error"); + } + } else if(message.equals("#gethost")) { + clientUI.display("current host: " + getHost()); + } else if(message.equals("#getport")) { + clientUI.display("current port: " + getPort()); + } else if(message.startsWith("#")) { + clientUI.display("Unknown command."); + } else { + try { + sendToServer(message); + } catch (IOException e) { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } } } @@ -83,12 +135,26 @@ public void handleMessageFromClientUI(String message) */ public void quit() { - try - { - closeConnection(); + if(isConnected()) { + try { + closeConnection(); + } catch (IOException e) { + } } - catch(IOException e) {} + System.out.println("Client terminates."); System.exit(0); } + + @Override + protected void connectionClosed() { + System.out.println("Abnormal termination of connection."); + } + + + @Override + protected void connectionException(Exception exception) { + System.out.println("Abnormal termination of connection."); + } + } //End of ChatClient class diff --git a/testcase_logs b/testcase_logs new file mode 100644 index 0000000..345fceb --- /dev/null +++ b/testcase_logs @@ -0,0 +1,308 @@ +/** +* name: yanqi huang +* student number: 8288435 +* email: yhuan126@uottawa.ca +**/ + + +Testcase 2001: +$ java EchoServer +Server listening for connections on port 5555 +^C% + +Testcase 2002: +$ java ClientConsole +ERROR - No login ID specified. Connection aborted. + +Testcase 2003: +$ java ClientConsole abc +Cannot open connection. Awaiting command. +^C% + +Testcase 2004: +$ java EchoServer +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. + + +$ java ClientConsole abc +> abc has logged on. + + +Testcase 2005: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +Message received: hello from abc + +$ java ClientConsole abc +abc has logged on. +hello +abc> hello + + +Testcase 2006: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +A new client is attempting to connect to the server. +Message received: #login def from null +def has logged on. +Message received: I am def from def +Message received: I am abc from abc +I am admin +SERVER MSG> I am admin + +$ java ClientConsole def +def has logged on. +I am def +def> I am def +abc> I am abc +SERVER MSG> I am admin + +$ java ClientConsole abc +abc has logged on. +def> I am def +I am abc +abc> I am abc +SERVER MSG> I am admin + + + +Testcase 2007: +$ java EchoServer +Server listening for connections on port 5555 +#quit +The server quits. +Server has stopped listening for connections. + + +Testcase 2008: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +#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 def from null +def has logged on. +#quit +Server has stopped listening for connections. +null disconnected. +null disconnected. +The server quits. + + +$ java ClientConsole abc +abc has logged on. +WARNING - Server has stopped listening for connections. +hello +abc> hello +> the server has shut down, and quitting. + + +$ java ClientConsole def +def has logged on. +> the server has shut down, and quitting. + + +Testcase 2009: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +#stop +Server has stopped listening for connections. +#close +abc has disconnected. +^C% + +$ java ClientConsole abc +abc has logged on. +WARNING - Server has stopped listening for connections. +SERVER SHUTTING DOWN! DISCONNECTING! +> Abnormal termination of connection. +^C% + + +Testcase 2009: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +#close +Server has stopped listening for connections. +abc has disconnected. +#start +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +^C% + +$ java ClientConsole abc +abc has logged on. +SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +#login +abc has logged on. +Abnormal termination of connection. +^C% + + +Testcase 2010: +$ java ClientConsole abc +Cannot open connection. Awaiting command. +#quit +Client terminates. + +Testcase 2011: +$ java ClientConsole abc +abc has logged on. +#logoff +Abnormal termination of connection. +#quit +Client terminates. + +Testcase 2012: +$ java ClientConsole abc +Cannot open connection. Awaiting command. +#sethost 192.168.1.13 +Host set to: 192.168.1.13 +#setport 1234 +port set to: 1234. +#quit +Client terminates. + +Testcase 2013: +$ java EchoServer 1234 +Server listening for connections on port 1234 +#quit +The server quits. +Server has stopped listening for connections. + + +Testcase 2014: +$ java EchoServer 1234 +Server listening for connections on port 1234 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +a client disconnected. +^C% + + +$ java ClientConsole abc localhost 1234 +abc has logged on. +^C% + + +Testcase 2015: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +Message received: hello from abc +hello +SERVER MSG> hello +#close +Server has stopped listening for connections. +abc has disconnected. +#setport 1234 +port set to: 1234. +#start +Server listening for connections on port 1234 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +a client disconnected. +#quit +The server quits. +Server has stopped listening for connections. + + +$ java ClientConsole abc +abc has logged on. +hello +abc> hello +SERVER MSG> hello +SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +#setport 1234 +port set to: 1234. +#login +abc has logged on. +#quit +Abnormal termination of connection. +Client terminates. + + +Testcase 2016: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +a client disconnected. +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +a client disconnected. + + +$ java ClientConsole abc +abc has logged on. +#logoff +Abnormal termination of connection. +#sethost 127.0.0.1 +Host set to: 127.0.0.1 +#login +abc has logged on. +#quit +Abnormal termination of connection. +Client terminates. + + +Testcase 2017: +$ java EchoServer +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +A new client is attempting to connect to the server. +Message received: #login abc from null +abc has logged on. +abc has disconnected. +abc has disconnected. +^C% + +$ java ClientConsole abc +abc has logged on. +#quit +Abnormal termination of connection. +Client terminates. + +$ java ClientConsole abc +abc has logged on. +abc has disconnected. +#logoff +Abnormal termination of connection. +^C% + + +Testcase 2018: omit +Testcase 2019: omit