diff --git a/OcsfPhase2.log b/OcsfPhase2.log new file mode 100644 index 0000000..c42b867 --- /dev/null +++ b/OcsfPhase2.log @@ -0,0 +1,4 @@ +$0=OcsfPhase2 +Name = Nick Bailuc +Student Number = 300014335 +Email Address = nbail040@uottawa.ca diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..21b218a 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -7,116 +7,150 @@ import common.*; /** - * This class constructs the UI for a chat client. It implements the + * This class constructs the UI for a chat client. It implements the * chat interface in order to activate the display() method. * Warning: Some of the code here is cloned in ServerConsole * * @author François Bélanger - * @author Dr Timothy C. Lethbridge + * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière * @version July 2000 */ public class ClientConsole implements ChatIF { - //Class variables ************************************************* - - /** - * The default port to connect on. - */ - final public static int DEFAULT_PORT = 5555; - - //Instance variables ********************************************** - - /** - * The instance of the client that created this ConsoleChat. - */ - ChatClient client; + //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 **************************************************** + + //Constructors **************************************************** - /** - * Constructs an instance of the ClientConsole UI. - * - * @param host The host to connect to. - * @param port The port to connect on. - */ - public ClientConsole(String host, int port) - { - try - { - client= new ChatClient(host, port, this); - } - catch(IOException exception) - { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); - } - } + /** + * Constructs an instance of the ClientConsole UI. + * + * @param host The host to connect to. + * @param port The port to connect on. + */ + public ClientConsole(String host, int port) + { + try + { + client= new ChatClient(host, port, this); + } + catch(IOException exception) + { + System.out.println("Error: Can't setup connection!" + + " Terminating client."); + System.exit(1); + } + } - - //Instance methods ************************************************ - - /** - * This method waits for input from the console. Once it is - * received, it sends it to the client's message handler. - */ - public void accept() - { - try - { - BufferedReader fromConsole = - new BufferedReader(new InputStreamReader(System.in)); - String message; + + //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!"); - } - } + while (true) + { + message = fromConsole.readLine(); + client.handleMessageFromClientUI(message); + if (message.charAt(0) == '#') + {/*TODO lang.String seems to not recognize full sys-messages. + * *TODO Attempt to stdin with different character encoding. + * *TODO Try escaping # or specifying ASCII number 0x23 */ + if (message == "#quit") + { + System.out.println("Client terminated"); + connectionClosed(); + System.exit(0); // Exit code 0 represents user exit + } + if (message == "#logoff") connectionClosed(); + if (message == "#sethost")//TODO Parse string with tokenizer + { + String newHost = "";//TODO Add string tokenizer +// setHost(newHost); + } + if (message == "#setport") {}//setPort(newPort); +// if (message == "#gethost") getHost(); +// if (message == "#getport") getPort(); + } + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + + +/* catch(Exception exception) + { + //connectionException(Exception exception); + System.out.println("Server has shut down"); + }*/ + } + + protected void connectionClosed() + { + System.out.println("Server has shut down"); +// System.exit(2); // Exit code 2 represents server shutdown + //connectionException(exception); + } - /** - * 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); - } + /** + * This method overrides the method in the ChatIF interface. It + * displays a message onto the screen. + * + * @param message The string to be displayed. + */ + public void display(String message) + { + System.out.println("> " + message); + } - - //Class methods *************************************************** - - /** - * This method is responsible for the creation of the Client UI. - * - * @param args[0] The host to connect to. - */ - public static void main(String[] args) - { - String host = ""; - int port = 0; //The port number + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of the Client UI. + * + * @param args[0] The host to connect to. + */ + public static void main(String[] args) + { + String host = ""; + int port = Integer.parseInt(args[1]); //stdin > port - try - { - host = args[0]; - } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; - } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data - } + try + { + host = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + host = "localhost"; + } + ClientConsole chat= new ClientConsole(host, port); + chat.accept(); //Wait for console data + } } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..c41e865 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -6,104 +6,115 @@ 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 - */ +**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 { - //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); - } + //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 + + //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."); + } + + protected void clientConnected(ConnectionToClient client) + { + System.out.println("A client has connected via localhost:" + getPort()); + } + + synchronized protected void clientDisconnected(ConnectionToClient client) + {//TODO Method not called during ClientConsole:KeyboardInterrupt + System.out.println("Client has disconnected"); + } + + + //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 - } + 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!"); - } - } + 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/ocsf/client/AbstractClient.java b/code/simplechat1/ocsf/client/AbstractClient.java similarity index 100% rename from code/ocsf/client/AbstractClient.java rename to code/simplechat1/ocsf/client/AbstractClient.java diff --git a/code/ocsf/client/AdaptableClient.java b/code/simplechat1/ocsf/client/AdaptableClient.java similarity index 100% rename from code/ocsf/client/AdaptableClient.java rename to code/simplechat1/ocsf/client/AdaptableClient.java diff --git a/code/ocsf/client/ObservableClient.java b/code/simplechat1/ocsf/client/ObservableClient.java similarity index 100% rename from code/ocsf/client/ObservableClient.java rename to code/simplechat1/ocsf/client/ObservableClient.java diff --git a/code/ocsf/index.html b/code/simplechat1/ocsf/index.html similarity index 100% rename from code/ocsf/index.html rename to code/simplechat1/ocsf/index.html diff --git a/code/ocsf/server/AbstractServer.java b/code/simplechat1/ocsf/server/AbstractServer.java similarity index 100% rename from code/ocsf/server/AbstractServer.java rename to code/simplechat1/ocsf/server/AbstractServer.java diff --git a/code/ocsf/server/AdaptableServer.java b/code/simplechat1/ocsf/server/AdaptableServer.java similarity index 100% rename from code/ocsf/server/AdaptableServer.java rename to code/simplechat1/ocsf/server/AdaptableServer.java diff --git a/code/ocsf/server/ConnectionToClient.java b/code/simplechat1/ocsf/server/ConnectionToClient.java similarity index 100% rename from code/ocsf/server/ConnectionToClient.java rename to code/simplechat1/ocsf/server/ConnectionToClient.java diff --git a/code/ocsf/server/ObservableOriginatorServer.java b/code/simplechat1/ocsf/server/ObservableOriginatorServer.java similarity index 100% rename from code/ocsf/server/ObservableOriginatorServer.java rename to code/simplechat1/ocsf/server/ObservableOriginatorServer.java diff --git a/code/ocsf/server/ObservableServer.java b/code/simplechat1/ocsf/server/ObservableServer.java similarity index 100% rename from code/ocsf/server/ObservableServer.java rename to code/simplechat1/ocsf/server/ObservableServer.java diff --git a/code/ocsf/server/OriginatorMessage.java b/code/simplechat1/ocsf/server/OriginatorMessage.java similarity index 100% rename from code/ocsf/server/OriginatorMessage.java rename to code/simplechat1/ocsf/server/OriginatorMessage.java