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.

Slides:



Advertisements
Similar presentations
COS 461 Fall 1997 Network Objects u first good implementation: DEC SRC Network Objects for Modula-3 u recent implementation: Java RMI (Remote Method Invocation)
Advertisements

RMI Varun SainiYing Chen. What is RMI? RMI is the action of invoking a method of a remote interface on a remote object. It is used to develop applications.
Remote Method Invocation (RMI) Mixing RMI and sockets
Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
15-May-15 RMI Remote Method Invocation. 2 “The network is the computer” Consider the following program organization: If the network is the computer, we.
Remote Method Invocation in Java Bennie Lewis EEL 6897.
Advanced Programming Rabie A. Ramadan Lecture 4. A Simple Use of Java Remote Method Invocation (RMI) 2.
Java Remote Method Invocation (RMI) In Java we implement object systems: O1O2 O3 thread 1thread 2 execution scheme JVM 1JVM 2 distribution scheme.
Remote Method Invocation
FONG CHAN SING (143334) WONG YEW JOON (143388). JAVA RMI is a distributive system programming interface introduced in JDK 1.1. A library that allows an.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
DISTRIBUTED FILE SYSTEM USING RMI
Java RMI. What is RMI? RMI is an RPC system for an object based language. Objects provide a natural granularity for the binding of functions. –RMI allows.
Sockets  Defined as an “endpoint for communication.”  Concatenation of IP address + port.  Used for server-client communication.  Server waits for.
EEC-681/781 Distributed Computing Systems Lecture 5 Wenbing Zhao Department of Electrical and Computer Engineering Cleveland State University
Introduction to Remote Method Invocation (RMI)
How Does Remote Method Invocation Work? –Systems that use RMI for communication typically are divided into two categories: clients and servers. A server.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets Rethinking out tic-tac-toe game.
COMPS311F Li Tak Sing Lectures
Java RMI Essentials Based on Mastering RMI Rickard Oberg.
1 Java Programming II Java Network II (Distributed Objects in Java)
CS 584 Lecture 18 l Assignment » Glenda assignment extended to the Java RMI Deadline » No Java RMI Assignment l Test » Friday, Saturday, Monday.
+ A Short Java RMI Tutorial Usman Saleem
15 - RMI. Java RMI Architecture Example Deployment RMI is a part of J2SE (standard edition), but is used by J2EE) A J2EE server is not nessesary for using.
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Java RMI: Remote Method Invocation January 2000 Nancy McCracken Syracuse University.
1 Java RMI G53ACC Chris Greenhalgh. 2 Contents l Java RMI overview l A Java RMI example –Overview –Walk-through l Implementation notes –Argument passing.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
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.
Client Call Back Client Call Back is useful for multiple clients to keep up to date about changes on the server Example: One auction server and several.
Java Remote Method Invocation RMI. Idea If objects communicate with each other on one JVM why not do the same on several JVM’s? If objects communicate.
RMI Continued IS Outline  Review of RMI  Programming example.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 43 Remote Method Invocation.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 17/10/2007.
Java Remote Method Invocation (RMI) Overview of RMI Java RMI allowed programmer to execute remote function class using the same semantics as local functions.
Li Tak Sing Lectures COMPS311F 1. Remote Method Invocation(RMI) For distributed computing. A Java object on one system can invoke a method in an.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Creating Applications Using RMI Lesson 1B / Slide 1 of 19 Network and Distributed Programming in Java Pre-assessment Questions 1.Which of the following.
Remote Method Invocation A Client Server Approach.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
Java RMI. RMI Any object whose methods can be invoked from another Java VM is called a remote object.
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Distributed programming in Java Faculty:Nguyen Ngoc Tu Session 5 - RMI.
CSC 480 Software Engineering Lab 6 – RMI Nov 8, 2002.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Using RMI The Example of A Remote Calculator 1. Parts of A Working RMI System A working RMI system is composed of several parts. –Interface definitions.
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 29 Remote Method.
Java Distributed Computing
Principles of Software Development
Java Distributed Computing
Java Remote Method Invocation (RMI)
Broker in practice: Middleware
Remote Method Invocation
What is RMI? Remote Method Invocation
Remote Method Invocation
Lesson 3 Remote Method Invocation (RMI) Mixing RMI and sockets
Java RMI (more) CS-328 Internet Programming.
Network and Distributed Programming in Java
Chapter 40 Remote Method Invocation
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday.
Java Remote Method Invocation
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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 a server's request. For example, if we want to use RMI to write a chat server, when a client send in a message, we need to send it to all connecting clients. In this case, we need the server to be able to initiate the communication with the clients. In order for the server to initiate a request, the client should also be a Remote object. The server should have a copy of the stub. This can be done by invoking a remote method by the client which passes itself as a parameter to the server.

Passing the client to the server The first step is to create a client interface: public interface Client extends Remote { public void callBack(...) throws RemoteException; } The server should have a method which allows the client to pass itself as the parameter: public interface Server extends Remote { public void connect(Client client) throws RemoteException; }

Implementation of the client The implementation of the client should consists of a statement that invoke the connect method of the server that passes itself to the server:.... server.connect(this); // A statement in the client....

Implementation of the server The server should use an attribute to store the connected client: public class ServerImp implements Server, Serializable { private client; public void connect(Client c) throws RemoteException { client=c; }.... //whenever there is a need to contact the client, we can: client.callBack(..);.... }

A Chat server using RMI You can download the program at 0/src/r.zip The server is stored in the package RMIChat In the above file, there is another server in the package RMIChat2 which we will talk about later. The program has four files: ChatServer: interface for the chat server ChatServerImp: implementation of the chat server ChatClient: interface for the chat client ChatClientImp: implementation of the chat client

ChatServer public interface ChatServer extends java.rmi.Remote { public void send(String st) throws java.rmi.RemoteException; //the client uses it to send a //message to the server public void connect(ChatClient c) throws java.rmi.RemoteException; //the client sends itself to the //server public void disconnect(ChatClient c) throws java.rmi.RemoteException; //the client disconnect from the //server }

ChatServerImp It contains a JFrame and a JTextArea for the displaying all messages from the clients. The bottom line displays all connected clients.

Declaration of ChatServerImp public class ChatServerImp extends UnicastRemoteObject implements ChatServer{..... }

Some Attributes of ChatServerImp Vector clients; //this stores all connected clients

Some Methods of ChatServerImp public void send(String st) throws RemoteException { Vector problem = new Vector (); textarea.append(st+"\n"); synchronized (clients) { for (ChatClient c : clients) { try { c.toClient(st); } catch (Exception e) { problem.add(c); }

Some Methods of ChatServerImp } for (ChatClient c : problem) { clients.remove(c); } displayClients(); }

Some Methods of ChatServerImp public void connect(ChatClient c) throws RemoteException { clients.add(c); displayClients(); }

Some Methods of ChatServerImp public void disconnect(ChatClient c) throws RemoteException { clients.remove(c); displayClients(); }

The main method of ChatServerImp public static void main(String st[]) throws RemoteException { ChatServer s = new ChatServerImp(); java.rmi.registry.Registry r = java.rmi.registry.LocateRegistry.getRegistry(); r.rebind("chat", s); }

ChatClient public interface ChatClient extends java.rmi.Remote { public void toClient(String st) throws java.rmi.RemoteException; //this is the callback for the //server to send a string to the client public String name() throws java.rmi.RemoteException; //this is another callback for the server to get the name of //the client. }

ChatClientImp The bottom textfield is for the user to type in the name. The connect bottom is used to connect to the chat server. Once the connection is made, the button becomes the disconnection button.

ChatClientImp There is a textfield at the top to allow the user to type a message. When the user presses the send button, the message would be sent to the server. The message broadcasted from the server would be displayed on the middle textarea.

Declaration of ChatClientImp public class ChatClientImp extends UnicastRemoteObject implements ChatClient {...

Some Attributes of ChatClientImp JTextField name; //the textfield at the bottom of the //window that allows the user to type in the name JTextArea textarea; //the textarea for the display of //messages broadcasted from the server. ChatServer server; //the chat server

The action listener of the send button public void actionPerformed(ActionEvent e) { try { server.send(name.getText() + ":" + textfield.getText()); textfield.setText(""); } catch (RemoteException ex) { }

The action listener of the connect button public void actionPerformed(ActionEvent e) { try { if (connect.getText().equals("connect")) { Registry registry = java.rmi.registry.LocateRegistry.getRegistry("localhost"); server = (ChatServer) registry.lookup("chat"); server.connect(ChatClientImp.this);...

The action listener of the connect button } else { connect.setText("connect"); server.disconnect(ChatClientImp.this); } } catch (NotBoundException ex) { }

Some methods of ChatClientImp public String name() throws RemoteException { return name.getText(); } public void toClient(String st) throws java.rmi.RemoteException { textarea.append(st + "\n"); }

The main method of ChatClientImp public static void main(String st[]) throws RemoteException { new ChatClientImp(); } Note that we do not need to register the client with any RMI registries. The client would be sent to the server via a method call. So the server would not need to get the client from an RMI registry.

Some features of this RMI server There is no need to handle multithreading in the RMI version of the server. Multithreading is handled by the internal mechanism of RMI. Although we do not create threads, we still need to make the server thread safe as RMI is multithreaded. Therefore, we use the synchronized keyword whenever needed.

Problems of the RMI ChatServer Although we have a button for the user to disconnect from the server, it has problem for the server to keep track of clients that have quit execution without informing the server. In the socket version of the ChatServer, when a client quit execution, a network exception would be thrown at the server side so that the server would know that the client has quit. This is not the case for RMI. The server would not know that a client has quit execution.

The java.rmi.server.Unreferenced interface This interface has one method: public void unreferenced() throws RemoteException After a remote object has implemented this interface, then the unreferenced method will be invoked when it is not referenced anywhere.

The java.rmi.server.Unreferenced interface There are sever ways that an external reference to a remote object disappear: The client quits execution. The reference to the object is set to null. For example: ChatServer server=registry.lookup("chat");..... server=null; //the remote object is not referenced by server The variable that references the object is garbage collected.

How to know that a client has quit? For every client that is connected to the server, we pass to it a newly created object that has implemented the Unreferenced interface. Then, when the client has quit, the object is not reference anywhere as we only pass the object to one client. In this way, the corresponding unreferenced method would be invoked.

But there is a delay before unreferenced is called Note that the unreferenced method would only be invoked for a delay of 10 minutes after all referencing clients have quit.

Modified Chat server You can find the server in the same file for the original server. But this time, the server is in the RMIChat2 package. A new Contact interface is needed for this purpose: public interface Contact extends java.rmi.Remote { } The interface has no method. It only use is to have the object implementing this also implements the Unreferenced interface.

Implementation of Contact public class ContactImp extends UnicastRemoteObject implements Contact, Unreferenced { private ChatClient client; public ContactImp(ChatClient c) throws RemoteException { client = c; } public void unreferenced() { clients.remove(client); displayClients(); }

Changes to ChatServerImp The connect method is changed as following: public Contact connect(ChatClient c) throws RemoteException { clients.add(c); displayClients(); return new ContactImp(c); }

Changes to ChatClient The following attribute is added: private Contact contact; The code to connect to the server is changed as following: Registry registry = java.rmi.registry.LocateRegistry.getRegistry("localhost"); server = (ChatServer) registry.lookup("chat2"); contact=server.connect(ChatClientImp.this);