From fb5e15dfb5a9241d126c36e963cf445e5733ff6b Mon Sep 17 00:00:00 2001 From: David Thomas Date: Sun, 7 Jun 2020 12:06:07 -0400 Subject: [PATCH] Assignment 1, changing OSCF from phase 1 to phase 2 with SimpleChat --- code/simplechat2/ChatClient.java | 172 +++++++++++ code/simplechat2/ClientConsole.java | 127 ++++++++ code/simplechat2/EchoServer.java | 239 +++++++++++++++ code/simplechat2/README.txt | 448 ++++++++++++++++++++++++++++ code/simplechat2/ServerConsole.java | 75 +++++ 5 files changed, 1061 insertions(+) create mode 100644 code/simplechat2/ChatClient.java create mode 100644 code/simplechat2/ClientConsole.java create mode 100644 code/simplechat2/EchoServer.java create mode 100644 code/simplechat2/README.txt create mode 100644 code/simplechat2/ServerConsole.java diff --git a/code/simplechat2/ChatClient.java b/code/simplechat2/ChatClient.java new file mode 100644 index 0000000..7d0d677 --- /dev/null +++ b/code/simplechat2/ChatClient.java @@ -0,0 +1,172 @@ +// 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 + +package client; +import ocsf.client.*; +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. + * + * @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. + */ + private ChatIF clientUI; + private String loginID = ""; + + + //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 loginID User entered loginID through cmd-line args + */ + + + + public ChatClient(String host, int port, ChatIF clientUI, String loginID) throws IOException{ + super(host, port); //Call the superclass constructor + this.clientUI = clientUI; //assign rest of instance variables + this.loginID = loginID; + if (loginID.equals("")){ //if login ID is blank, refuse connection + clientUI.display("Error - No login ID specified. Connection aborted."); + quit(); + } + else{ //if login ID was entered, connect, and send loginID to server + + try{ + openConnection(); + sendToServer("#login "+loginID); + clientUI.display(loginID+" has logged on."); + } + catch(Exception ex){clientUI.display("Cannot open connection. Awaiting command");} + } + } + + + + //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 from the UI. + */ + public void handleMessageFromClientUI(String message){ + try{ + //series of commands user can enter through the ClientUI + if (message.equals("#quit")){ + quit(); + } + + else if ( message.equals("#logoff") ){ + //if already not connected, display error message + if (!isConnected()){ + clientUI.display("You are not connected to the server."); + } + else{ + closeConnection(); + clientUI.display("You have been disconnected from "+getHost()+"."); + } + } + + else if (message.equals("#login")){ + //if you are already logged in, display error message. + if (isConnected()){ + clientUI.display("You are already connected."); + } + else{ + openConnection(); + clientUI.display("You have connected to "+getHost()+"."); + } + } + + else if (message.equals("#gethost")){ + clientUI.display("Current host: "+getHost()); + } + + else if (message.equals("#getport")){ + clientUI.display("Current port: "+getPort()); + } + + else if (message.contains("#setport")){ + if (!isConnected()){ + int newPort = Integer.parseInt( message.substring(9) ); + setPort(newPort); + clientUI.display("Port set to: "+getPort()); + } + else{ + clientUI.display("Cannot set port while connected to server."); + } + } + + + else if (message.contains("#sethost")){ + if (!isConnected()){ + String newHost = new String(); + newHost = message.substring(9) ; + setHost(newHost); + clientUI.display("Host set to: "+getHost()); + } + else{ + clientUI.display("Cannot set host while connected to server."); + } + } + //if it is not a command, treat as a regular message to server + else{ + sendToServer(message);} + } + + //catch exceptions and disconnect from server + catch(IOException e){ + clientUI.display("Could not send message to server. Terminating client."); + quit(); + } + } + + + //disconnect from server if server shuts down while connected. + public void connectionException(Exception exception){ + String exceptionString = exception.toString(); + System.out.println("Abnormal termination of connection."); + + } + + /** + * This method terminates the client. + */ + public void quit(){ + try{ + closeConnection(); + } + catch(IOException e) {} + System.exit(0); + } +} +//End of ChatClient class diff --git a/code/simplechat2/ClientConsole.java b/code/simplechat2/ClientConsole.java new file mode 100644 index 0000000..cccefd1 --- /dev/null +++ b/code/simplechat2/ClientConsole.java @@ -0,0 +1,127 @@ +// 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.*; + +/** + * 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 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. + * @param loginID The login ID to connect with. + */ + public ClientConsole(String host, int port, String loginID) + { + try{ + client = new ChatClient(host, port, this, loginID); + } + + 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 port to connect to. + */ + public static void main(String[] args) + { + String loginID = ""; + String host = ""; + int port = 0; //The port number + + try{ + loginID = args[0]; + host = args[1]; + port = Integer.parseInt(args[2]); + + } + catch(ArrayIndexOutOfBoundsException e) + { + host = "localhost"; + port = DEFAULT_PORT; + } + + + + ClientConsole chat= new ClientConsole(host, port, loginID); + chat.accept(); //Wait for console data + } +} +//End of ConsoleChat class diff --git a/code/simplechat2/EchoServer.java b/code/simplechat2/EchoServer.java new file mode 100644 index 0000000..4edb4d8 --- /dev/null +++ b/code/simplechat2/EchoServer.java @@ -0,0 +1,239 @@ +// 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 common.*; +import ocsf.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 + * @author François Bélanger + * @author Paul Holden + * @version July 2000 + */ +public class EchoServer extends AbstractServer +{ + //Instance variables ********************************************** + + /** + * The interface type variable. It allows the implementation of + * the display method in the client. + */ + private ChatIF serverUI; + /** + * The boolean isOpen variable. Allows the system to keep track if + if the server is open or closed. + */ + + private boolean isOpen = false; + + //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. + * @param serverUI The ServerConsole instance. + */ + + public EchoServer(int port){ + super(port); + } + public EchoServer(int port, ChatIF serverUI) throws IOException{ + super(port); + this.serverUI = serverUI; + listen(); + } + + + //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){ + String mesg = (String)msg; //cast Obj msg to String, used to concatenate later + //if it is the #login command to the server, do this... + if (mesg.contains("#login")){ //if it has the keyword "#login" + String loginID = mesg.substring(7); //splice the loginID from it + client.setInfo("#login",loginID); //assign loginID as a "#login" for this instance of client + System.out.println("> Message received: "+mesg+" from "+client); + System.out.println("> "+loginID+" has logged on."); + + } + //if it is NOT the #login command, treat as an echo + else{ + String loginSTR = (String) client.getInfo("#login"); //cast getInfo data to String + System.out.println("> Message received: "+mesg+" from "+loginSTR); + this.sendToAllClients(loginSTR+": "+mesg); + } + } + + + public void handleMessageFromServerUI(String message){ + try{ + + if (message.equals("#quit")){ + quit(); + } + + else if (message.equals("#stop")){ + stopListening(); + sendToAllClients("WARNING - Server has stopped listening for connections"); + } + + else if (message.equals("#close")){ + sendToAllClients("WARNING - Server has stopped listening for connections"); + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); + close(); + + + } + + else if (message.equals("#getport")){ + serverUI.display("Current port: "+getPort()); + } + + else if (message.contains("#setport")){ + if (!isOpen){ + int newPort = Integer.parseInt( message.substring(9) ); + setPort(newPort); + } + else{ + serverUI.display("Cannot set port while connected to server."); + } + } + + else if (message.equals("#start")){ + listen(); + } + + //if the server is not responding to a command, treat it as a generic server msg from server + else{ + sendToAllClients("SERVER MSG> "+message);} + } + catch(IOException e) + { + serverUI.display + ("Could not send message to all clients. Terminating server."); + quit(); + } + } + + /** + * 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()); + isOpen = true; + } + /** + * This method overrides the one in the superclass. Called + * when the is server closed, but not terminated. Sets instance variable isOpen + */ + protected void serverClosed(){ + isOpen = 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."); + } + + /** + * This method overrides the one in the superclass. Called + * when a client connects. + */ + protected void clientConnected(ConnectionToClient client){ + System.out.println("> A new client is attempting to connect to the server"); + } + + /** + * This method overrides the one in the superclass. Called + * when a client disconnects. + */ + synchronized protected void clientDisconnected(ConnectionToClient client){ + String loginSTR = (String) client.getInfo("#login"); + sendToAllClients("> "+loginSTR+" has disconnected from the server.");} + + /** + * This method overrides the one in the superclass. Called + * to shut the server down and terminates the program. + */ + public void quit(){ + try{ + close(); + } + catch(IOException e) {} + System.exit(0); + } +public void accept() + { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) + { + message = fromConsole.readLine(); + handleMessageFromServerUI(message); + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } + + + 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(); + sv.accept(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + } + } + +} +//End of EchoServer class diff --git a/code/simplechat2/README.txt b/code/simplechat2/README.txt new file mode 100644 index 0000000..6073fc8 --- /dev/null +++ b/code/simplechat2/README.txt @@ -0,0 +1,448 @@ +DAVID THOMAS +300137596 +dthom131@uottawa.ca + + +TEST CASES + +SERVER CONSOLE + + +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +^CDavids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +localhost (127.0.0.1) has connected to the server. +> Message received: #login David from localhost (127.0.0.1) +David has logged on. +sick +cool +> Message received: cool from David +^CDavids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +localhost (127.0.0.1) has connected to the server. +> Message received: #login David from localhost (127.0.0.1) +David has logged on. +^CDavids-Computer:simplechat1 DavidThomas$ javac *.java +EchoServer.java:170: error: ')' expected + System.out.println("> "client+" has disconnected from the server.");} + ^ +EchoServer.java:170: error: not a statement + System.out.println("> "client+" has disconnected from the server.");} + ^ +EchoServer.java:170: error: ';' expected + System.out.println("> "client+" has disconnected from the server.");} + ^ +3 errors +Davids-Computer:simplechat1 DavidThomas$ javac *.java +EchoServer.java:82: error: cannot find symbol + System.out.println("> Message received: "+mesg+" from "+loginID); + ^ + symbol: variable loginID + location: class EchoServer +EchoServer.java:83: error: cannot find symbol + this.sendToAllClients(loginID+": "+mesg); + ^ + symbol: variable loginID + location: class EchoServer +2 errors +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +David has logged on. +> A new client is attempting to connect to the server +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +David has logged on. +^CDavids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +> Message received: Hello! from David +> A new client is attempting to connect to the server +> Message received: #login Bobby from localhost (127.0.0.1) +> Bobby has logged on. +> Message received: hi from Bobby +> Message received: hey from David +> Message received: hi from Bobby +stfu +#quit +Server has stopped listening for connections. +> null has disconnected from the server. +> null has disconnected from the server. +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#stop +Server has stopped listening for connections. +> Message received: gfgj from David +#start +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login Bobby from localhost (127.0.0.1) +> Bobby has logged on. +> Message received: hg from Bobby +> Message received: from Bobby +> Message received: from Bobby +> Message received: from Bobby +#quit +Server has stopped listening for connections. +> null has disconnected from the server. +> null has disconnected from the server. +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#stop +Server has stopped listening for connections. +#start +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login Bobby from localhost (127.0.0.1) +> Bobby has logged on. +#quit +Server has stopped listening for connections. +> null has disconnected from the server. +> null has disconnected from the server. +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#stop +Server has stopped listening for connections. +#quit +> null has disconnected from the server. +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#stop +Server has stopped listening for connections. +#start +Server listening for connections on port 5555 +#close +Server has stopped listening for connections. +> null has disconnected from the server. +#quit +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#close +Server has stopped listening for connections. +> null has disconnected from the server. +@^[[25~# +#quit +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#close +Server has stopped listening for connections. +> null has disconnected from the server. +#quit +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#close +Server has stopped listening for connections. +> null has disconnected from the server. +#quit +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#close +Server has stopped listening for connections. +> null has disconnected from the server. +#start +Server listening for connections on port 5555 +> A new client is attempting to connect to the server + + +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +#quit +Server has stopped listening for connections. +Davids-Computer:simplechat1 DavidThomas$ javac *.java +./client/ChatClient.java:122: error: ')' expected + clientUI.display("Port set to: +"getPort()); + ^ +./client/ChatClient.java:122: error: -> expected + clientUI.display("Port set to: +"getPort()); + ^ +./client/ChatClient.java:122: error: not a statement + clientUI.display("Port set to: +"getPort()); + ^ +./client/ChatClient.java:135: error: ')' expected + clientUI.display("Host set to: +"getHost()); + ^ +./client/ChatClient.java:135: error: -> expected + clientUI.display("Host set to: +"getHost()); + ^ +./client/ChatClient.java:135: error: not a statement + clientUI.display("Host set to: +"getHost()); + ^ +6 errors +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +#close +Server has stopped listening for connections. +^[[A^[[B +java ServerConsole 1234 +#quit +Davids-Computer:simplechat1 DavidThomas$ java ServerConsole 1234 +Server listening for connections on port 1234 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +> A new client is attempting to connect to the server +> A new client is attempting to connect to the server +hi +#quit +Server has stopped listening for connections. +> null has disconnected from the server. +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +#quit +Server has stopped listening for connections. +Davids-Computer:simplechat1 DavidThomas$ javac *.java +Davids-Computer:simplechat1 DavidThomas$ java EchoServer +Server listening for connections on port 5555 +> A new client is attempting to connect to the server +> Message received: #login David from localhost (127.0.0.1) +> David has logged on. +> A new client is attempting to connect to the server +> Message received: #login Davi from localhost (127.0.0.1) +> Davi has logged on. +> A new client is attempting to connect to the server +> Message received: #login Bobby from localhost (127.0.0.1) +> Bobby has logged on. +**************************************************************************** + + +CLIENT 1 + +Last login: Sun Jun 7 11:13:55 on ttys000 + +The default interactive shell is now zsh. +To update your account to use zsh, please run `chsh -s /bin/zsh`. +For more details, please visit https://support.apple.com/kb/HT208050. +Davids-Computer:~ DavidThomas$ cd /Users/DavidThomas/Documents/SEG2105/Assignment1/Lloseng/code/simplechat1 +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole +> You have not provided a login ID. Awaiting command. +^CDavids-Computer:simplechat1 DavidThomas$ java ClientConsole +> Error - No login ID specified. Connection aborted. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +Error: Can't setup connection! Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +Error: Can't setup connection! Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> Cannot open connection. Awaiting command +^CDavids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> SERVER MSG> sick +> SERVER MSG> cool +cool +> David: cool +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +#logoff +> You have been disconnected from localhost. +#login +> You have connected to localhost. +#quit +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +Hello! +> David: Hello! +> Bobby: hi +hey +> David: hey +> Bobby: hi +> SERVER MSG> stfu +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +gfgj +> David: gfgj +> Bobby: hg +> Bobby: +> Bobby: +> Bobby: +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> WARNING - Server has stopped listening for connections +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> WARNING - Server has stopped listening for connections +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> > WARNING - Server has stopped listening for connections +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> SERVER SHUTTING DOWN! DISCONNECTING! +> Abnormal termination of connection. +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> SERVER SHUTTING DOWN! DISCONNECTING! +> Abnormal termination of connection. +Cannot connect to server: java.io.EOFException. Terminating client. +^[[A +> Could not send message to server. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +d +> Could not send message to server. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +> WARNING - Server has stopped listening for connections +> SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +#login +> You have connected to localhost. +#quit +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> David has logged on. +#logoff +> You have been disconnected from localhost. +#quit +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> Cannot open connection. Awaiting command +#sethost david's comp +#setport +Unexpected error while reading from console! +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David +> Cannot open connection. Awaiting command +#sethost david's comp +> Host set to: david's comp +#setport 1234 +> Port set to: 1234 +#quit +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David localhost 1234 +> David has logged on. +> SERVER MSG> hi +#quit +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David localhost 1234 +> Cannot open connection. Awaiting command +#setport 5555 +> Port set to: 5555 +#login +> Could not send message to server. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole David localhost +> David has logged on. +#quit +Davids-Computer:simplechat1 DavidThomas$ + +**************************************************************************** + +CLIENT 2 + +Last login: Sun Jun 7 11:14:01 on ttys001 + +The default interactive shell is now zsh. +To update your account to use zsh, please run `chsh -s /bin/zsh`. +For more details, please visit https://support.apple.com/kb/HT208050. +Davids-Computer:~ DavidThomas$ cd /Users/DavidThomas/Documents/SEG2105/Assignment1/Lloseng/code/simplechat1 +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Davi +> Cannot open connection. Awaiting command +#setport 1234 +> Port set to: 1234 +#login +> You have connected to localhost. +> SERVER MSG> hi +#logoff +> You have been disconnected from localhost. +^[[A^[[A +> Could not send message to server. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Davi +> Davi has logged on. + + +**************************************************************************** + +CLIENT 3 + +Last login: Sun Jun 7 11:14:03 on ttys002 + +The default interactive shell is now zsh. +To update your account to use zsh, please run `chsh -s /bin/zsh`. +For more details, please visit https://support.apple.com/kb/HT208050. +Davids-Computer:~ DavidThomas$ cd /Users/DavidThomas/Documents/SEG2105/Assignment1/Lloseng/code/simplechat1 +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Bobby +> Bobby has logged on. +hi +> Bobby: hi +> David: hey +hi +> Bobby: hi +> SERVER MSG> stfu +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Bobby +hg + + + +> Bobby has logged on. +> Bobby: hg +> Bobby: +> Bobby: +> Bobby: +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Bobby +> Bobby has logged on. +Cannot connect to server: java.io.EOFException. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Bobby +> Cannot open connection. Awaiting command +#setport 1234 +> Port set to: 1234 +#login +> You have connected to localhost. +> SERVER MSG> hi +Abnormal termination of connection. +^[[A +> Could not send message to server. Terminating client. +Davids-Computer:simplechat1 DavidThomas$ java ClientConsole Bobby +> Bobby has logged on. +#logoff +> You have been disconnected from localhost. + diff --git a/code/simplechat2/ServerConsole.java b/code/simplechat2/ServerConsole.java new file mode 100644 index 0000000..491287a --- /dev/null +++ b/code/simplechat2/ServerConsole.java @@ -0,0 +1,75 @@ +import java.io.*; +import common.*; +import ocsf.server.*; + +public class ServerConsole implements ChatIF{ + + /*INSTANCE VARIABLES*/ + EchoServer server; //instance of a server + + /*CLASS VARIABLES*/ + final public static int DEFAULT_PORT = 5555; //default port value + + + /*CONSTRUCTORS*/ + /** + * @param port The port the server starts on. + */ + public ServerConsole(int port)throws IOException{ + server = new EchoServer(port, this); + } + + /** + * This method waits for input from the console. Once it is + * received, it sends it to the server's message handler. + */ + 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!"); + } + } + + public void display(String message){ + System.out.println("SERVER MSG> "+message); + } + + public static void main(String[] args) throws IOException + { + 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 + } + + ServerConsole sv = new ServerConsole(port); + + try + { + sv.accept(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + } + } +} \ No newline at end of file