Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only.

Similar presentations


Presentation on theme: "1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only."— Presentation transcript:

1 1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only “hooks” in AbstractClient and AbstractServer, i.e., implement only the methods that are marked as changeable by the framework.  For client testing, we will build a simple console-based interface that interacts with the user.

2 2 Let’s look at client first What are the hooks in AbstractClient we implement? protected void connectionClosed() {}//optional protected void connectionException(Exception exception) {}//optional protected void connectionEstablished() {}//optional protected abstract void handleMessageFromServer(Object msg);//required What are the instance methods we can call? final public void openConnection() throws IOException final public void sendToServer(Object msg) throws IOException final public void closeConnection() throws IOException

3 3 Here’s strategy we will employ 1.We will define a class, ClientConsole, that does the actual I/O with user. This will consist of reading from console and writing to console. 2.We will define a class, ChatClient that extends AbstractClient. 3.When we build a ClientConsole, we will have it also build a ChatClient. We will pass a pointer (callback) to ChatClient that points back to ClientConsole. In essence, each will point at the other, allowing each to call methods of the other. Whew. I’ll try to draw it on the board. 4.We should avoid all threading in our code – that is taken care of by the framework.

4 4 Will look at ChatClient first Reminder: ChatClient is the class that deals most directly with the network. It has methods for reading from and writing to the server over sockets. We will attempt to keep ChatClient separated from the user interface: this is viewed as a good design decision. In fact, we will attempt to make the user interface “pluggable” so that either console-based interfaces or GUI-based interfaces will work with ChatClient. (Later, we will hook a “robot” into this piece!) The ChatIF interface is used as part of this pluggable mechanism. ChatClient will be happy with any interface that implements the ChatIF interface. The interface will be passed into ChatClient through its constructor.

5 5 ChatClient - header public class ChatClient extends AbstractClient { //Instance variables ********************************************** /** * The interface type variable. It allows the implementation of * the display method in the client. */ ChatIF clientUI; //why use interface here? //clientUI used for callback. public ChatClient(String host, int port, ChatIF clientUI) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; openConnection(); //framework supplies this method }

6 6 ChatClient - methods //Instance methods ************************************************ public void handleMessageFromServer(Object msg) //framework method that needs to be implemented here { clientUI.display(msg.toString()); //passed on the UI for handling } public void handleMessageFromClientUI(String message) //method called by clientUI { try { sendToServer(message); //method supplied by framework } catch(IOException e) { clientUI.display("Could not send message to server. Terminating client."); quit(); } } /** * This method terminates the client. */ public void quit() { try { closeConnection(); //method supplied by framework } catch(IOException e) {} System.exit(0); } } //End of ChatClient class

7 7 ClientConsole - header public class ClientConsole implements ChatIF { final public static int DEFAULT_PORT = 5555; ChatClient client; //so can do (callback) method calls on the client public ClientConsole(String host, int port) { try { client= new ChatClient(host, port, this); //build the ChatClient object and give it a pointer to us } catch(IOException exception) { System.out.println("Error: Can't setup connection!" + " Terminating client."); System.exit(1); } //=================================================================================================== public interface ChatIF { /** * Method that when overriden is used to display objects onto * a UI. */ public abstract void display(String message); }

8 8 ClientConsole - 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); //callback to ChatClient } catch (Exception ex) { System.out.println ("Unexpected error while reading from console!"); } /** * This method implements the method in the ChatIF interface. It * displays a message onto the screen. */ public void display(String message) //allows ChatClient to get stuff displayed { System.out.println("> " + message); }

9 9 ClientConsole – main method public static void main(String[] args) { String host = ""; int port = 0; //The port number try//not sure I like this style { host = args[0]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); chat.accept(); //Wait for console data }

10 10 That Was Pretty Easy! We built a chat client on top of the ocsf framework. Most of the effort was building the actual user interface pieces, not the low level chat code. Let’s now build a simple server. All it does it echo chat to whoever is signed up.

11 11 EchoServer - body public class EchoServer extends AbstractServer { final public static int DEFAULT_PORT = 5555; public EchoServer(int port) { super(port); } public void handleMessageFromClient(Object msg, ConnectionToClient client) { System.out.println("Message received: " + msg + " from " + client); this.sendToAllClients(msg); //framework supplies this method } protected void serverStarted() { System.out.println ("Server listening for connections on port " + getPort()); } protected void serverStopped() { System.out.println ("Server has stopped listening for connections."); }

12 12 EchoServer – main method 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) //superclass of Error and Exception { port = DEFAULT_PORT; //Set port to 5555 } EchoServer sv = new EchoServer(port); try { sv.listen(); //Start listening for connections } catch (Exception ex) { System.out.println("ERROR - Could not listen for clients!"); } //End of EchoServer class

13 13 That Was Easy, Too! Built a server with very little code. Let’s test it to see how it works. I’ll crank up the server and a couple of clients.


Download ppt "1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only."

Similar presentations


Ads by Google