diff --git a/code/ocsf/client/AbstractClient.class b/code/ocsf/client/AbstractClient.class new file mode 100644 index 0000000..13b37d6 Binary files /dev/null and b/code/ocsf/client/AbstractClient.class differ diff --git a/code/ocsf/client/AbstractClient.java b/code/ocsf/client/AbstractClient.java index e905636..cf4fec1 100644 --- a/code/ocsf/client/AbstractClient.java +++ b/code/ocsf/client/AbstractClient.java @@ -246,14 +246,16 @@ final public void run() { handleMessageFromServer(msg); } } catch (Exception exception) { + if (!readyToStop) { try { closeAll(); } catch (Exception ex) { } - + connectionException(exception); } + } finally { clientReader = null; } @@ -268,6 +270,7 @@ final public void run() { * attempting to reconnect. */ protected void connectionClosed() { + } /** diff --git a/code/ocsf/server/AbstractServer$1.class b/code/ocsf/server/AbstractServer$1.class new file mode 100644 index 0000000..ee21e47 Binary files /dev/null and b/code/ocsf/server/AbstractServer$1.class differ diff --git a/code/ocsf/server/AbstractServer.class b/code/ocsf/server/AbstractServer.class new file mode 100644 index 0000000..4a118fe Binary files /dev/null and b/code/ocsf/server/AbstractServer.class differ diff --git a/code/ocsf/server/ConnectionToClient.class b/code/ocsf/server/ConnectionToClient.class new file mode 100644 index 0000000..e592699 Binary files /dev/null and b/code/ocsf/server/ConnectionToClient.class differ diff --git a/code/simplechat1/ClientConsole.class b/code/simplechat1/ClientConsole.class new file mode 100644 index 0000000..4fdd431 Binary files /dev/null and b/code/simplechat1/ClientConsole.class differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..335b9a1 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -40,12 +40,13 @@ public class ClientConsole implements ChatIF * * @param host The host to connect to. * @param port The port to connect on. + * @param loginID The loginID user gave */ - public ClientConsole(String host, int port) + public ClientConsole(String host, int port, String loginID) { try { - client= new ChatClient(host, port, this); + client= new ChatClient(host, port, this, loginID); } catch(IOException exception) { @@ -106,17 +107,34 @@ public static void main(String[] args) { String host = ""; int port = 0; //The port number + String loginID = ""; - try - { - host = args[0]; + //Checks if the first argument is a loginID + try{ + loginID = args[0]; + }catch(ArrayIndexOutOfBoundsException e){ + System.out.println("No login ID detected, program will not exit."); + System.exit(0); } - catch(ArrayIndexOutOfBoundsException e) - { + + //Saves the inputted host or uses default if non is given + try{ + host = args[1]; + } + catch(ArrayIndexOutOfBoundsException e){ host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data + + //Saves the inputted port or uses default if non is given + try{ + port = Integer.parseInt(args[2]); //Get port from command line + ClientConsole chat= new ClientConsole(host, port, loginID); + chat.accept(); + } + catch(Throwable t){ + ClientConsole chat= new ClientConsole(host, DEFAULT_PORT, loginID); //Set port to 5555 + chat.accept(); + } } } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.class b/code/simplechat1/EchoServer.class new file mode 100644 index 0000000..0ffdbd7 Binary files /dev/null and b/code/simplechat1/EchoServer.class differ diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..6f343b7 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,8 @@ import java.io.*; import ocsf.server.*; +import client.*; +import common.*; /** * This class overrides some of the methods in the abstract @@ -23,6 +25,7 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; + ChatIF serverUI; //Constructors **************************************************** @@ -31,9 +34,10 @@ public class EchoServer extends AbstractServer * * @param port The port number to connect on. */ - public EchoServer(int port) + public EchoServer(int port, ChatIF serverUI) throws IOException { super(port); + this.serverUI = serverUI; } @@ -48,8 +52,13 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { + if(msg.toString().contains("#")){ + if(msg.toString().contains("login")){ + client.setInfo("LoginID", msg.toString().replace("#login","")); + } + } System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + this.sendToAllClients("(" + client.getInfo("LoginID") + "): " + msg); } /** @@ -71,6 +80,67 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } + + + //This method overrides the superclass to display when clients are connected + protected void clientConnected(ConnectionToClient client){ + serverUI.display("#login" + client + " has connected to this server."); + } + + //This method overrides the superclass to display when clients are disconnected + synchronized protected void clientDisconnected(ConnectionToClient client){ + serverUI.display("#login" + client + " has disconnected from the server."); + } + + + //This method handles all user input and excutes the desire effects + public void handleMessageFromServerUI(String message) + { + if(message.contains("#")){ + if(message.contains("quit")){ + try{ + close(); + }catch(IOException e){} + System.exit(0); + } + + if(message.contains("stop")){ + stopListening(); + } + + if(message.contains("close")){ + try{ + close(); + }catch(IOException e){} + } + + if(message.contains("setport")){ + if(isListening()){ + serverUI.display("This command can not be issued while the server is still on."); + }else{ + setPort(Integer.parseInt(message.replace("#setport",""))); + } + } + + if(message.contains("start")){ + if(isListening()){ + serverUI.display("This command can not be issued while the server is still on."); + }else{ + try{ + listen(); + } + catch(IOException e) {} + } + } + + if(message.contains("getport")){ + serverUI.display(String.valueOf(getPort())); + } + }else{ + message = "SERVER MSG> "+ message; + this.sendToAllClients(message); + } + } //Class methods *************************************************** @@ -94,11 +164,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.server.listen(); //Start listening for connections } catch (Exception ex) { diff --git a/code/simplechat1/ServerConsole.class b/code/simplechat1/ServerConsole.class new file mode 100644 index 0000000..96fdb23 Binary files /dev/null and b/code/simplechat1/ServerConsole.class differ diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..6325223 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,126 @@ +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 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) + { + try + { + server = new EchoServer(port, this); + } + 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 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!"); + } + } + + /** + * 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) + { + 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 chat= new ServerConsole(DEFAULT_PORT); + chat.accept(); //Wait for console data + + try + { + chat.server.listen(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + } + } +} +//End of ConsoleChat class diff --git a/code/simplechat1/client/ChatClient.class b/code/simplechat1/client/ChatClient.class new file mode 100644 index 0000000..c74a293 Binary files /dev/null and b/code/simplechat1/client/ChatClient.class differ diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..797f0fb 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -38,12 +38,13 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) + public ChatClient(String host, int port, ChatIF clientUI, String LoginID) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; openConnection(); + sendToServer("#login <"+ LoginID +">"); } @@ -64,19 +65,80 @@ public void handleMessageFromServer(Object msg) * * @param message The message from the UI. */ + + + //This method handles all user input and excutes the desire effects public void handleMessageFromClientUI(String message) { - try - { - sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); + if(message.contains("#")){ + if(message.contains("quit")){ + quit(); + } + + if(message.contains("logoff")){ + try{ + closeConnection(); + } + catch(IOException e) {} + } + + if(message.contains("sethost")){ + if(isConnected()){ + clientUI.display("This command can not be issued while you are still logged on."); + }else{ + setHost(message.replace("#sethost","")); + } + } + + if(message.contains("setport")){ + if(isConnected()){ + clientUI.display("This command can not be issued while you are still logged on."); + }else{ + setPort(Integer.parseInt(message.replace("#setport",""))); + } + } + + if(message.contains("login")){ + if(isConnected()){ + clientUI.display("This command can not be issued while you are still logged on."); + }else{ + try{ + openConnection(); + } + catch(IOException e) {} + } + } + + if(message.contains("gethost")){ + clientUI.display(getHost()); + } + + if(message.contains("getport")){ + clientUI.display(String.valueOf(getPort())); + } + + + }else{ + try{ + sendToServer(message); + } + catch(IOException e){ + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } } } + + //This method overrides the superclass to display the shutting down of server + protected void connectionClosed() { + clientUI.display("The server you are connected to has shut down."); + } + + //This method overrides the superclass to display an error message + protected void connectionException(Exception exception) { + clientUI.display("An error has occured."); + } /** * This method terminates the client. diff --git a/code/simplechat1/common/ChatIF.class b/code/simplechat1/common/ChatIF.class new file mode 100644 index 0000000..4457d73 Binary files /dev/null and b/code/simplechat1/common/ChatIF.class differ