From f430b45b3f6a8d7a4e97b611280c99ee6a938fff Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 06:43:51 -0400 Subject: [PATCH 1/9] Update EchoServer.java --- code/simplechat1/EchoServer.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..b531bf1 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -71,6 +71,16 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } + @Override + protected void clientConnected(ConnectionToClient client) + { + System.out.println("Server is connected to " + client + ".") + } + synchronized protected void clientDisconnected(ConnectionToClient client) + { + System.out.println("Server has disconnected from " + client + ".") + + } //Class methods *************************************************** From 7f68acb5f4e8ec0c4a677adf63deb0a0b04e473d Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 09:11:18 -0400 Subject: [PATCH 2/9] Update ChatClient.java --- code/simplechat1/client/ChatClient.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index e091a1b..7b8d275 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -76,14 +76,19 @@ public void handleMessageFromClientUI(String message) ("Could not send message to server. Terminating client."); quit(); } + } + protected void connectionEstablished(){ + System.out.println("Connection to the server has been established") + } + protected void connectionclosed(){ + system.out.println("The server's connection has been closed") } protected void connectionException(Exception exception) { - //if connection is closed, throw this exception and print "The connections lost due to server shutting down" + //if connection is closed, throw this exception and print "The connections lost due to server shutting down" + System.out.println("The connection is lost due to server shutting down") + quit(); } - @Override - public void connectionClosed(){ - // - } + /** * This method terminates the client. From 144f9e9ddf5fbc5f7432f793d74ee852ed39959e Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 09:20:24 -0400 Subject: [PATCH 3/9] Client connect/dissconnect --- code/simplechat1/EchoServer.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index b531bf1..21e5497 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -35,6 +35,7 @@ public EchoServer(int port) { super(port); } + //Instance methods ************************************************ @@ -45,6 +46,12 @@ public EchoServer(int port) * @param msg The message received from the client. * @param client The connection from which the message originated. */ + protected void clientConnected(ConnectionToClient client){ + System.out.println("A client has connected to the server."); + } + synchronized protected void clientDisconnected(ConnectionToClient client){ + System.out.println("A client has disconnected from the server"); + } public void handleMessageFromClient (Object msg, ConnectionToClient client) { From 162b1078ed7e6cefb530eb339eb4682e719e576a Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 09:36:31 -0400 Subject: [PATCH 4/9] Part of #6a an incomplete implementation of all the commands of 6a --- code/simplechat1/client/ChatClient.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index 7b8d275..f9a4fc2 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -65,7 +65,7 @@ public void handleMessageFromServer(Object msg) * @param message The message from the UI. */ public void handleMessageFromClientUI(String message) - { + /*{ try { sendToServer(message); @@ -76,6 +76,18 @@ public void handleMessageFromClientUI(String message) ("Could not send message to server. Terminating client."); quit(); } + */ + if (message[0] ="#"){ + if message.equals("#quit") + quit(); + else if message.equals("#logoff") + closeConnection(); + else if message.equals("#gethost") + clientUI.display(" The host is " + getHost()); + else if message.equals("#getport") + clientUI.display(" The host is " + getPort()); + // couldn't figure out how to implement the rest in time. + } } protected void connectionEstablished(){ System.out.println("Connection to the server has been established") From 100b3d5ff0ff54b60b76b9081c61bab61298058d Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 09:39:00 -0400 Subject: [PATCH 5/9] Update EchoServer.java --- code/simplechat1/EchoServer.java | 1 - 1 file changed, 1 deletion(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 21e5497..5bc7a78 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,7 +4,6 @@ import java.io.*; import ocsf.server.*; - /** * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. From 063a44fae5b2393a286ac6688c35f411078c9535 Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 09:48:28 -0400 Subject: [PATCH 6/9] typo --- code/simplechat1/client/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index f9a4fc2..ded7446 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -85,7 +85,7 @@ public void handleMessageFromClientUI(String message) else if message.equals("#gethost") clientUI.display(" The host is " + getHost()); else if message.equals("#getport") - clientUI.display(" The host is " + getPort()); + clientUI.display(" The port is " + getPort()); // couldn't figure out how to implement the rest in time. } } From 5b7c6e83bdd5d661dc43b0e53402a8948014f8a1 Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 10:04:00 -0400 Subject: [PATCH 7/9] Create ServerConsole.java --- code/simplechat1/ServerConsole.java | 109 ++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 code/simplechat1/ServerConsole.java diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..5a79ddd --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,109 @@ +import java.io.*; +import client.*; +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 ClientConsole UI. + * + * @param host The host to connect to. + * @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: cannot listen for clients"); + 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 host to connect to. + */ + public static void main(String[] args) + { + String host = ""; + int port = 0; //The port number + + try + { + host = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + host = "localhost"; + } + ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + chat.accept(); //Wait for console data + } +} +//End of ServerConsole class From df7f27192bf02ea6f98cf3e514de1810a7ca813a Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 10:06:13 -0400 Subject: [PATCH 8/9] Update ClientConsole.java --- code/simplechat1/ClientConsole.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..05b5bc0 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -73,7 +73,7 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + client.handleMessageFromServerUI(message); } } catch (Exception ex) @@ -104,19 +104,13 @@ public void display(String message) */ public static void main(String[] args) { - String host = ""; int port = 0; //The port number - try - { - host = args[0]; - } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; - } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data + + port = Integer.parseInt(args[0]); + + ServerConsole server= new ServerConsole(host, port); + server.accept(); //Wait for console data } } //End of ConsoleChat class From 4ae788091dd973b03ffd955a4ec248448808f281 Mon Sep 17 00:00:00 2001 From: michelleoke <66561101+michelleoke@users.noreply.github.com> Date: Sun, 21 Jun 2020 10:12:17 -0400 Subject: [PATCH 9/9] incomplete implementation of 6c --- code/simplechat1/EchoServer.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 5bc7a78..ebe2231 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,7 @@ import java.io.*; import ocsf.server.*; +import common.*; /** * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. @@ -17,7 +18,7 @@ public class EchoServer extends AbstractServer { //Class variables ************************************************* - + ChatIF serverUI; /** * The default port to listen on. */ @@ -87,8 +88,19 @@ synchronized protected void clientDisconnected(ConnectionToClient client) System.out.println("Server has disconnected from " + client + ".") } - - //Class methods *************************************************** + public void handleMessageFromServerUI(String message){ + if (message[0] ="#"){ + if message.equals("#quit") + this.close(); + else if message.equals("#stop") + this.stopListening(); + else if message.equals("#close") + close(); + else if message.equals("#getport") + clientUI.display(" The port is " + getPort()); + else if message.equals("#start") + this.listen(); + } /** * This method is responsible for the creation of