diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..82eeddc 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,11 +41,11 @@ 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 id, String host, int port) { try { - client= new ChatClient(host, port, this); + client = new ChatClient(id, host, port, this); } catch(IOException exception) { @@ -73,7 +73,51 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + char first = message.charAt(0); + char command = '#'; + if (first == command) { + String msg = message.substring(1); + String[] cmd = msg.split(" "); + if (cmd[0].equals("quit")) { + System.out.println("The client is quit."); + client.quit(); + }else if (cmd[0].equals("logoff")) { + System.out.println("The client is logoff."); + client.closeConnection(); + }else if (cmd[0].equals("sethost")) { + if (!client.isConnected()) { + client.setHost(cmd[1]); + System.out.println("The host set to: " + cmd[1]); + } else { + System.out.println("The client is still connected, so host cannot be set!"); + } + }else if (cmd[0].equals("setport")) { + if (!client.isConnected()) { + client.setPort(Integer.parseInt(cmd[1])); + System.out.println("The port set to: " + cmd[1]); + } else { + System.out.println("The client is still connected, so port cannot be set!"); + } + }else if (cmd[0].equals("login")) { + if (!client.isConnected()) { + try { + System.out.println(cmd[1] + " is login."); + client.openConnection(); + client.handleMessageFromClientUI("#login " + client.getId()); + } catch (Exception e) { + System.out.println("No id! Connection cannot be established."); + } + } else { + System.out.println("It is already connected, so the client cannot login!"); + } + }else if (cmd[0].equals("gethost")) { + System.out.println("The host is: " + client.getHost()); + }else if (cmd[0].equals("getport")) { + System.out.println("The port is: " + client.getPort()); + } + }else { + client.handleMessageFromClientUI(message); + } } } catch (Exception ex) @@ -106,16 +150,37 @@ public static void main(String[] args) { String host = ""; int port = 0; //The port number + String id = ""; try { - host = args[0]; + id = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(0); + } + + try + { + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + try + { + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + ClientConsole chat = new ClientConsole(id, host, port); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..084c668 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -23,6 +23,8 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; + + private boolean cls = false; //Constructors **************************************************** @@ -47,9 +49,26 @@ public EchoServer(int port) */ public void handleMessageFromClient (Object msg, ConnectionToClient client) - { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + { + if (msg.toString().startsWith("#login ")) { + if (client.getInfo("id") == null) { + String message = String.valueOf(msg); + String[] cmd = message.split(" "); + if (cmd[0].equals("#login")) { + client.setInfo("id", cmd[1]); + this.sendToAllClients(client.getInfo("id") + " is logged in."); + } + } else if (client.getInfo("id") != null) { + try + { + client.sendToClient("You need to login before you can chat!"); + client.close(); + } + catch (IOException e) {} + } + } + System.out.println("Message received: " + msg + " from " + client.getInfo("id")); + this.sendToAllClients(client.getInfo("id") + "> " + msg); } /** @@ -71,7 +90,28 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } + + protected void clientConnected(ConnectionToClient client) { + System.out.println(client.getInfo("id") + " is connected. "); + } + + synchronized protected void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("id") + " is disconnected. "); + } + synchronized protected void clientException( + ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("id") + " is disconnected. "); + } + + protected void serverClosed() { + cls = true; + } + + protected boolean serverIsClosed() { + return cls; + } + //Class methods *************************************************** /** @@ -95,6 +135,8 @@ public static void main(String[] args) } EchoServer sv = new EchoServer(port); + + ServerConsole sc = new ServerConsole(sv); try { @@ -104,6 +146,8 @@ public static void main(String[] args) { System.out.println("ERROR - Could not listen for clients!"); } + + sc.accept(); } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..f8b6b88 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,79 @@ +import java.io.*; +import common.*; + +public class ServerConsole implements ChatIF { + + EchoServer server; + + public ServerConsole(EchoServer server) { + this.server = server; + } + + public void accept() { + try { + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); + String message; + while (true) { + message = fromConsole.readLine(); + char first = message.charAt(0); + char command = '#'; + if (first == command) { + String msg = message.substring(1); + String[] cmd = msg.split(" "); + if (cmd[0].equals("quit")) { + System.out.println("The server is quit."); + try { + server.close(); + } + catch(IOException e) {} + System.exit(0); + } else if (cmd[0].equals("stop")) { + server.sendToAllClients("WARNING - Server has stopped listening for connections."); + server.stopListening(); + } else if (cmd[0].equals("close")) { + server.sendToAllClients("WARNING - The server has stopped listening for connections" + + "\n" + "SERVER SHUTTING DOWN! DISCONNECTING!"); + server.close(); + } else if (cmd[0].equals("setport")) { + if (server.serverIsClosed()) { + server.setPort(Integer.parseInt(cmd[1])); + System.out.println("The port set to: " + cmd[1]); + } else { + System.out.println("The server is not closed, so port cannot be set!"); + } + } else if (cmd[0].equals("start")) { + if (!server.isListening()) { + server.listen(); + System.out.println("The server is started."); + } else { + System.out.println("The server is not stopped, so it cannot start!"); + } + }else if (cmd[0].equals("getport")) { + System.out.println("The port is: " + server.getPort()); + } + }else { + server.sendToAllClients( "SERVER MSG>" + message); + } + } + } + catch (Exception ex) { + System.out.println("Unexpected error while reading from console!"); + } + } + + public void display(String message) { + System.out.println("> " + message); + } + + public static void main(String[] args) { + EchoServer server = new EchoServer(0); + try + { + server.setPort(Integer.parseInt(args[0])); + } + catch(ArrayIndexOutOfBoundsException e) + { + server.setPort(5555); + } + } +} \ No newline at end of file diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..13f3724 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -27,6 +27,7 @@ public class ChatClient extends AbstractClient */ ChatIF clientUI; + String id; //Constructors **************************************************** @@ -38,12 +39,14 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) + public ChatClient( String id, String host, int port, ChatIF clientUI) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; + this.id = id; openConnection(); + sendToServer("#login " + id); } @@ -90,5 +93,18 @@ public void quit() catch(IOException e) {} System.exit(0); } + + + public void ConnectionClosed() { + System.out.println("The connection is closed."); + } + + public void ConnectionException(){ + System.out.println("Abnormal termination of connection."); + } + + public String getId() { + return id; + } } //End of ChatClient class