diff --git a/Test2001.PNG b/Test2001.PNG new file mode 100644 index 0000000..c755432 Binary files /dev/null and b/Test2001.PNG differ diff --git a/Test2002.PNG b/Test2002.PNG new file mode 100644 index 0000000..5913d59 Binary files /dev/null and b/Test2002.PNG differ diff --git a/Test2003.PNG b/Test2003.PNG new file mode 100644 index 0000000..a046f83 Binary files /dev/null and b/Test2003.PNG differ diff --git a/Test2004.PNG b/Test2004.PNG new file mode 100644 index 0000000..6478d04 Binary files /dev/null and b/Test2004.PNG differ diff --git a/Test2005.PNG b/Test2005.PNG new file mode 100644 index 0000000..9d6579a Binary files /dev/null and b/Test2005.PNG differ diff --git a/Test2006.PNG b/Test2006.PNG new file mode 100644 index 0000000..1ce2970 Binary files /dev/null and b/Test2006.PNG differ diff --git a/Test2007.PNG b/Test2007.PNG new file mode 100644 index 0000000..e9ad430 Binary files /dev/null and b/Test2007.PNG differ diff --git a/Test2008.PNG b/Test2008.PNG new file mode 100644 index 0000000..5f9d2b0 Binary files /dev/null and b/Test2008.PNG differ diff --git a/Test2009.PNG b/Test2009.PNG new file mode 100644 index 0000000..12116cb Binary files /dev/null and b/Test2009.PNG differ diff --git a/Test2010.PNG b/Test2010.PNG new file mode 100644 index 0000000..65c28ec Binary files /dev/null and b/Test2010.PNG differ diff --git a/Test2011.PNG b/Test2011.PNG new file mode 100644 index 0000000..cfbe2f2 Binary files /dev/null and b/Test2011.PNG differ diff --git a/Test2012.PNG b/Test2012.PNG new file mode 100644 index 0000000..f1070e2 Binary files /dev/null and b/Test2012.PNG differ diff --git a/Test2013.PNG b/Test2013.PNG new file mode 100644 index 0000000..3c69050 Binary files /dev/null and b/Test2013.PNG differ diff --git a/Test2014.PNG b/Test2014.PNG new file mode 100644 index 0000000..6732f39 Binary files /dev/null and b/Test2014.PNG differ diff --git a/Test2017.PNG b/Test2017.PNG new file mode 100644 index 0000000..30eff6f Binary files /dev/null and b/Test2017.PNG differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..dcde4ce 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -1,6 +1,6 @@ // 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 +// license found at www.lloseng.com import java.io.*; import client.*; @@ -9,74 +9,75 @@ /** * 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 + * 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 +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 port The port to connect on + * @param user The username to connect with. */ - public ClientConsole(String host, int port) + public ClientConsole(String user, String host, int port) { - try + try { - client= new ChatClient(host, port, this); - } - catch(IOException exception) + client= new ChatClient(user, host, port, this); + } + catch(IOException exception) { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); + System.out.println("Cannot open connection. Awaiting command."); + } } - + //Instance methods ************************************************ - + /** - * This method waits for input from the console. Once it is + * This method waits for input from the console. Once it is * received, it sends it to the client's message handler. */ - public void accept() + public void accept() { try { - BufferedReader fromConsole = + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - while (true) + while (true) { message = fromConsole.readLine(); + client.handleMessageFromClientUI(message); } - } - catch (Exception ex) + } + catch (Exception ex) { System.out.println ("Unexpected error while reading from console!"); @@ -89,33 +90,69 @@ public void accept() * * @param message The string to be displayed. */ - public void display(String message) + public void display(String message) { System.out.println("> " + message); } - + protected void connectionClosed() { + System.out.println(" The server is currently shut down.the client quit"); + } + + public void connectionException(Exception exception) { + System.out.println("WARNING - The server has stopped listening for connections"); + } + + //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) + public static void main(String[] args) { + String user = ""; String host = ""; int port = 0; //The port number try { - host = args[0]; + user = args[0]; + } catch(ArrayIndexOutOfBoundsException e) { + System.err.print("ERROR - No login ID specified. Connection aborted."); + System.exit(1); + } + + try + { + host = args[1]; + } + catch (ArrayIndexOutOfBoundsException e) { + host = "localhost"; + } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + try + { + port = Integer.parseInt(args[2]); + } + catch (Throwable t) { + + port = DEFAULT_PORT; + + } + + + ClientConsole chat= new ClientConsole(user ,host, port); + + + chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..ab9d179 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,12 +1,14 @@ // 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 +// license found at www.lloseng.com import java.io.*; import ocsf.server.*; +import common.*; +import java.util.Scanner; /** - * 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,53 +17,154 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer +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) + 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) + public void handleMessageFromClient(Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + + + + if (msg.toString().startsWith("#")) { + String[] pa = msg.toString().substring(1).split(" "); + if (pa[0].equalsIgnoreCase("login") && pa.length > 1) { + if (client.getInfo("user") == null) { + client.setInfo("user", pa[1]); + System.out.println("Message received: " + msg + " from " + client.getInfo("user")); + this.sendToAllClients(client.getInfo("user") + " has logged on ! "); + + } else { + try { + client.sendToClient("Your name has already been set "); + } catch (IOException e) {} + } + + } + } else { + if (client.getInfo("user") == null) { + try { + client.sendToClient(" Please set a username before messaging the server ! "); + + } catch (IOException e) {} + + } - + + System.out.println("Message received: " + msg + " from " + client.getInfo("user")); + + this.sendToAllClients(client.getInfo("user") + " > " + msg); + +} + +} + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. */ + + public void handleMessageFromServerConsole(String message) { + if(message.startsWith("#")) { + + int nPort; + + Scanner sc = new Scanner(System.in); + + + if(message.equals("#quit")) { + try + { + this.close(); + } catch (IOException e) { + System.exit(1); + } + System.exit(0); + } + + if(message.equals("#stop")) { + this.stopListening(); + } + + if(message.equals("#close")) { + try + { + this.close(); + } + catch(IOException e) {} + } + + + if(message.equals("#setport")) { + if(!this.isListening() ) { + System.out.println("Please enter a port"); + nPort = sc.nextInt(); + super.setPort(nPort); + System.out.println("The current host is " + this.getPort()); + } else { + System.out.println("Sorry, but to setup the port, you must be logged off"); + } + + } + + if(message.equals("#getport")) { + System.out.println("The current port is " + this.getPort()); + } + + if(message.equals("#start")) { + if (!this.isListening()) { + try { + this.listen(); + } catch (IOException e) {} + } else { + System.out.println("We are already started and listening for clients!."); + + } + + + + } + + + + } + + } 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. @@ -71,39 +174,39 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - + + protected void clientConnected(ConnectionToClient client) { + System.out.println(" A new client is attempting to connect to the server. Say hello ! "); + System.out.println(client.getInfo("user") + " has logged on ! "); + + + } + + synchronized protected void clientDisconnected(ConnectionToClient client) { + + System.out.println(" Oh no ! Someone leave ! Say byebye ! "); + System.out.println(client.getInfo("user") + " has logged off ! "); + this.sendToAllClients(client.getInfo("user") + " has logged off ! "); + + } + + synchronized protected void clientException( + ConnectionToClient client, Throwable exception) { + String message = client + " disconnect "; + System.out.println(client + " disconnect "); + this.sendToAllClients(message); + } + } + + //Class methods *************************************************** - + /** - * This method is responsible for the creation of + * 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 + * @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!"); - } - } -} //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..31b0476 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,93 @@ +import java.io.*; +import common.*; + +public class ServerConsole implements ChatIF { + //Class variables ************************************************* + + /** + * The default port to connect on. + */ + final public static int DEFAULT_PORT = 5555; + + //Instance variables ********************************************** + + /** + * The instance of the client that created this ConsoleChat. + */ + EchoServer server; + + + //Constructors **************************************************** + + /** + * Constructs an instance of the ServerConsole UI. + * + * @param port The port to connect on. + */ + public ServerConsole(int port) { + server = new EchoServer(port); + try { + server.listen(); + } catch (IOException e) { + System.out.println("ERROR - Could not listen for clients!"); + } + } + + + //Instance methods ************************************************ + + /** + * This method waits for input from the console. Once it is + * received, it sends it to all of the clients + */ + public void accept() { + try { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) { + message = fromConsole.readLine(); + server.handleMessageFromServerConsole(message); + this.display(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 du serveur > " + message); + } + + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of the Client UI. + * + * @param args The port to use for connection + */ + 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 + } + + ServerConsole serv = new ServerConsole(port); + serv.accept(); + } + +} +//End of ConsoleChat class diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..5e1a740 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -1,12 +1,13 @@ // 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 +// license found at www.lloseng.com package client; import ocsf.client.*; import common.*; import java.io.*; +import java.util.Scanner; /** * This class overrides some of the methods defined in the abstract @@ -20,16 +21,18 @@ public class ChatClient extends AbstractClient { //Instance variables ********************************************** - + /** - * The interface type variable. It allows the implementation of + * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + + private String user; //username + - //Constructors **************************************************** - + /** * Constructs an instance of the chat client. * @@ -37,47 +40,150 @@ public class ChatClient extends AbstractClient * @param port The port number to connect on. * @param clientUI The interface type variable. */ - - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException + + public ChatClient(String user, String host, int port, ChatIF clientUI) + throws IOException { super(host, port); //Call the superclass constructor + this.user = user; this.clientUI = clientUI; - openConnection(); - } + this.openConnection(); + this.sendToServer("#login " + user ); //Automatic ID identification +} + + +public String getUser() { //getter for username + return user; +} + +public void connectionException(Exception exception) { + System.out.println("WARNING - The server has stopped listening for connections"); + } + + - //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."); - quit(); + + if(message.startsWith("#")) { //# is for command line + + String nHost; + int nPort; + + Scanner sc = new Scanner(System.in); + + + if(message.equals("#quit")) { + System.out.print("Shut down"); + quit(); + } + + if(message.equals("#login")) { + if(!isConnected()) { + + try + { + + this.openConnection(); + System.out.println("log in..."); + + + } + catch (IOException e) {} + + } else { + System.out.println(" You already connected ! "); + } + + } + + if(message.equals("#logoff")) { + try + { + closeConnection(); + System.out.println("login off..."); + + } + catch(IOException e) { + System.out.println("Error"); + } + + + } + + + if(message.equals("#gethost")) { + System.out.println("The current host is " + this.getHost()); + + } + + if(message.equals("#getport")) { + System.out.println("The current port is " + this.getPort()); + } + + if(message.equals("#sethost")) { + if(!this.isConnected()) { + System.out.println("Please entrer a host name"); + nHost = sc.nextLine(); + this.setHost(nHost); + System.out.println("The new host is " + nHost); + + } else { + + System.out.println("Sorry, but to setup the host, you must be logged off"); + + } + + } + + if(message.equals("#setport")) { + if(!this.isConnected()) { + System.out.println("Please entrer a port"); + nPort = sc.nextInt(); + this.setPort(nPort); + System.out.println("The new port is " + nPort); + + } else { + + System.out.println("Sorry, but to setup the port, you must be logged off"); + + } + + } + + } else { //if the message isn't a command line + + try + { + sendToServer(message); + } + catch(IOException e) + { + clientUI.display("Could not send message to server. Terminating client."); + quit(); + } + } + } - + /** * This method terminates the client. */