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

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology.
CSE 240 Lecture 8. © Lethbridge/Laganière 2001 Chapter 5: Modelling with classes2 Overview Begin discussing Chapter 3 - Reuse.
Software Reuse, Frameworks, and Client-server Systems September 8, 2005 Motivation for Software Reuse Frameworks Definition Object Oriented Model Client-server.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
System Programming Practical session 10 Java sockets.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Exceptions Problems with error reporting so far –Either ignored exceptions or terminated program on first error. –Error handling and regular code mixed.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
Io package as Java’s basic I/O system continue’d.
The Little Caesars Cory, Sean, Seth, James, Madison.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Assignment 3 A Client/Server Application: Chatroom.
Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Taken from Chapter 3 of Object-Oriented Software Engineering: Practical Software Development using UML and Java by T. Lethbridge and R. Laganiere Basing.
Internet Software Development Lecture 14: Basing Software Development on Reusable Technology From: Object-Oriented Software Engineering, by Lethbridge.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology 1.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
IBM TSpaces Lab 3 Transactions Event Registration.
Java Server Programming Web Interface for Java Programs.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Basing software development on Reusable Technology.
Java Programming II Java Network (I) Java Programming II.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology.
Echo Networking COMP
Threads in Java Two ways to start a thread
Lecture 21 Sockets 1 (Not in D&D) Date.
Clients and Servers 19-Nov-18.
ATS Application Programming: Java Programming
Exception Handling Contents
Exceptions.
Distributed Computing
Multiplexing/Demux.
Exceptions.
CMSC 202 Exceptions.
Presentation transcript:

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 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 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 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 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 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 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 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 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 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 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 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 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.