diff --git a/Test Cases Phase 2 PDF.pdf b/Test Cases Phase 2 PDF.pdf new file mode 100644 index 0000000..090a2cd Binary files /dev/null and b/Test Cases Phase 2 PDF.pdf differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..a73715e 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,15 +41,15 @@ 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) { - System.out.println("Error: Can't setup connection!" + System.out.println("Can't setup connection!" + " Terminating client."); System.exit(1); } @@ -104,18 +104,42 @@ public void display(String message) */ public static void main(String[] args) { - String host = ""; + String loginid; + String host; int port = 0; //The port number try { - host = args[0]; + loginid = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + System.out.println("ERROR: No login ID provided."); + System.exit(1); + return; + + } + + //set host + try + { + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + // set port + try + { + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException 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..1f0c6a7 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -15,7 +15,7 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer +public class EchoServer extends AbstractServer { //Class variables ************************************************* @@ -48,8 +48,36 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + String message = msg.toString(); + if (message.startsWith("#")) { + String[] splitMessage = message.substring(1).split(" "); + if (splitMessage.length > 1 && splitMessage[0]==("login")) { + if (client.getInfo("loginid") == null) { + client.setInfo("loginid", splitMessage[1]); + System.out.println(client.getInfo("loginid")+" has logged on."); + this.sendToAllClients(client.getInfo("loginid") + " has logged on."); + } else { + try { + client.sendToClient("ERROR: Your username has been set already."); + } catch (IOException e) { + System.out.print("An error has occurred."); + } + } + + } + } else { + if (client.getInfo("loginid") == null) { + try { + client.sendToClient("ERROR: You must set a username."); + client.close(); + } catch (IOException e) { + System.out.print("An error has occurred."); + } + } else { + System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); + this.sendToAllClients(client.getInfo("loginid") + " > " + message); + } + } } /** @@ -71,6 +99,87 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } + + public void clientConnected(ConnectionToClient client){ + if(client.getInfo("loginid")!=null){ + System.out.println + (client.getInfo("loginid") + " has connected to the chat."); + } + else{ + System.out.println("A client has connected to the chat."); + } + } + + @Override + public void clientDisconnected(ConnectionToClient client){ + System.out.println + (client.getInfo("loginid") + " has disconnected from the chat."); + this.sendToAllClients(client.getInfo("loginid") + " has disconnected from the chat."); + } + + public void handleMessageFromServer(String message) + { + if(message.startsWith("#")){ + String[] splitMessage = message.split(" "); + switch (splitMessage[0]){ + case "#quit": + try { + System.out.println("Quitting..."); + this.close(); + System.out.println("Server successfully quit."); + } + catch(IOException exception){ + System.out.println("An error occurred when trying to close the server."); + } + break; + case "#stop": + this.sendToAllClients("WARNING: the server has stopped listening for connections."); + this.stopListening(); + break; + case "#close": + this.stopListening(); + try { + this.close(); + System.out.println("The server is now closed."); + } + catch(IOException exception){ + System.out.println("An error occurred when trying to close the server."); + } + break; + case "#setport": + if (!this.isListening()) { + super.setPort(Integer.parseInt(splitMessage[1])); + System.out.println("New port: " + Integer.parseInt(splitMessage[1])); + } else { + System.out.println("An error occurred when changing the port."); + } + break; + case "#start": + if (!this.isListening()) { + try{this.listen(); + System.out.println("The server has begun listening for connections."); + } + catch(IOException exception){ + System.out.println("An unexpected error occurred."); + } + } + else{ + System.out.println("The server has already started."); + } + break; + case "#getport": + System.out.println("Current port: " + this.getPort()); + break; + default: + System.out.println("Invalid command."); + break; + } + } + else{ + sendToAllClients("SERVER MSG>" + message); + } + + } //Class methods *************************************************** @@ -94,11 +203,11 @@ public static void main(String[] args) port = DEFAULT_PORT; //Set port to 5555 } - EchoServer sv = new EchoServer(port); + ServerConsole sv = new ServerConsole(port); try { - sv.listen(); //Start listening for connections + sv.accept(); //Start listening for connections } catch (Exception ex) { diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..51f24dd --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,120 @@ + +import java.io.*; +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 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 ClientConsole UI. + * + * @param port The port to connect on. + */ + public ServerConsole(int port) + { + server = new EchoServer(port); + try + { + server.listen(); + } + catch(IOException exception) + { + System.out.println("Error!!!!: Can't setup connection!" + + " Terminating server."); + 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(); + server.handleMessageFromServer(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("SERVER MSG > " + 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) + { + int port; + + // set port + try + { + port = Integer.parseInt(args[0]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + ServerConsole chat= new ServerConsole(port); + chat.accept(); //Wait for console data + } +} +//End of ServerConsole class diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..63f9eba 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -38,12 +38,15 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) + public ChatClient(String loginid, String host, int port, ChatIF clientUI) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; openConnection(); + + this.sendToServer("#login " + loginid); + } @@ -66,16 +69,83 @@ public void handleMessageFromServer(Object msg) */ public void handleMessageFromClientUI(String message) { - try - { - sendToServer(message); + if(message.startsWith("#")){ + String[] splitMessage = message.split(" "); + switch (splitMessage[0]){ + case "#quit": + System.out.println("Quitting server..."); + quit(); + break; + case "#logoff": + try + { + System.out.println("Logging off..."); + closeConnection(); + System.out.println("Connection closed.");} + catch (IOException e) + { System.out.println("There was an error trying to close connection."); } + break; + case "#sethost": + if(!isConnected()){ + this.setHost(splitMessage[1]); + System.out.println("New host has been set."); + } + else{ + System.out.println("You cannot set host while connected."); + } + break; + case "#setport": + if(!isConnected()){ + this.setPort(Integer.parseInt(splitMessage[1])); + System.out.println("New port has been set."); + } + else{ + System.out.println("You cannot set port while connected."); + } + break; + case "#login": + try + { openConnection(); + System.out.println("The connection has been opened.");} + catch (IOException e) + { System.out.println("There was an error trying to open connection."); } + break; + case "#gethost": + System.out.println("Currrent host: " + getHost()); + break; + case "#getport": + System.out.println("Currrent port: " + getPort()); + break; + default: + System.out.println("Invalid command."); + break; + } + } + else{ + try + { + sendToServer(message); + } + catch(IOException e) + { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } + } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); } + + + // quit if the connection is closed + public void ConnectionClosed(){ + System.out.println("The server has shut down."); + } + + // quit if there is a connection exception + public void connectionException(Exception exception) { + System.out.println("There has been a connection error. Shutting down..."); + quit(); } /** diff --git a/code/ocsf/client/AbstractClient.java b/code/simplechat1/ocsf/client/AbstractClient.java similarity index 99% rename from code/ocsf/client/AbstractClient.java rename to code/simplechat1/ocsf/client/AbstractClient.java index e905636..b8260ef 100644 --- a/code/ocsf/client/AbstractClient.java +++ b/code/simplechat1/ocsf/client/AbstractClient.java @@ -278,6 +278,7 @@ protected void connectionClosed() { * @param exception * the exception raised. */ + protected void connectionException(Exception exception) { } @@ -286,6 +287,7 @@ protected void connectionException(Exception exception) { * implementation does nothing. It may be overridden by subclasses to do * anything they wish. */ + protected void connectionEstablished() { } diff --git a/code/ocsf/client/AdaptableClient.java b/code/simplechat1/ocsf/client/AdaptableClient.java similarity index 99% rename from code/ocsf/client/AdaptableClient.java rename to code/simplechat1/ocsf/client/AdaptableClient.java index 143592e..bf9a1ec 100644 --- a/code/ocsf/client/AdaptableClient.java +++ b/code/simplechat1/ocsf/client/AdaptableClient.java @@ -57,6 +57,8 @@ final protected void connectionClosed() * * @param exception the exception raised. */ + + final protected void connectionException(Exception exception) { client.connectionException(exception); 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 diff --git a/desktop.ini b/desktop.ini new file mode 100644 index 0000000..279a260 --- /dev/null +++ b/desktop.ini @@ -0,0 +1,2 @@ +[.ShellClassInfo] +LocalizedResourceName=@Lloseng,0