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 diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..ebe2231 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,7 +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. @@ -18,7 +18,7 @@ public class EchoServer extends AbstractServer { //Class variables ************************************************* - + ChatIF serverUI; /** * The default port to listen on. */ @@ -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) { @@ -71,8 +78,29 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - - //Class methods *************************************************** + @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 + ".") + + } + 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 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 diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index e091a1b..ded7446 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,14 +76,31 @@ 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 port 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") + } + 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.