0% found this document useful (0 votes)
81 views

RPC/RMI Tutorial: Prepared by Dr. Seyhan Ucar Adopted by Beakal Gizachew

The document provides an overview of remote procedure calls (RPC) and remote method invocation (RMI) in distributed systems. It discusses how RPC allows a program to call a subroutine that resides in a different address space or machine, hiding the distributed nature of the call. It describes how RMI extends this concept to object-oriented programming by allowing method calls on remote objects. The document outlines the client/server communication process using stubs and skeletons and provides an example RMI application for calculating mortgage payments to demonstrate how to implement and run a basic RMI program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

RPC/RMI Tutorial: Prepared by Dr. Seyhan Ucar Adopted by Beakal Gizachew

The document provides an overview of remote procedure calls (RPC) and remote method invocation (RMI) in distributed systems. It discusses how RPC allows a program to call a subroutine that resides in a different address space or machine, hiding the distributed nature of the call. It describes how RMI extends this concept to object-oriented programming by allowing method calls on remote objects. The document outlines the client/server communication process using stubs and skeletons and provides an example RMI application for calculating mortgage payments to demonstrate how to implement and run a basic RMI program.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

RPC/RMI Tutorial

Prepared by Dr. Seyhan Ucar adopted by Beakal Gizachew 1


Agenda
◼ RPC (Remote Procedure Call)
◼ RPC Protocol
◼ Client and Server Stubs
◼ RPC Overview
◼ Interaction in RMI
◼ RMI (Remote Method Invocation)
◼ RMI in Java
◼ References
◼ Sample Exercise

2
Remote Procedure Call
◼ Powerful technique for constructing distributed applications.
◼ Idea: mask distributed computing system using a “transparent”
abstraction
❑ Looks like normal procedurecall
❑ Hides all aspects of distributed interaction
❑ Supports an easy programming model
◼ Based on extending procedure call so that the called procedure does
not need to exist in the same address space as the calling procedure.
◼ By using RPC, programmers of distributed applications avoid the
details of
the interface with the network.
◼ RPC is at the core of many distributed systems today.

3 3
RPC Protocol

client server
“binds” to registers with
server name service

4
RPC Protocol cont.

client server
“binds” to registers with
server name service
prepares,
sends request
receives request

5
RPC Protocol cont.

client server
“binds” to registers with
server name service
prepares,
sends request
receives request
invokes handler
sends reply

6
RPC Protocol cont.

client server
“binds” to registers with
server name service
prepares,
sends request
receives request
invokes handler
sends reply
unpacks reply

7
Client and Server Stubs
◼ Client-side stub ◼ Server-side stub
❑ Looks like local server ❑ Looks like local client

• function • function to server


❑ Same interface as local ❑ Listens on a socket for

• function • message from client stub


❑ Bundles arguments into ❑ Un-bundles arguments to
message, sends to server- • local variables
side stub ❑ Makes a local function call

❑ Waits for reply, un-bundles to server


• results ❑ Bundles result into reply
❑ returns
message to client stub

8
Overview of RPC

Principle of RPC between a client and serverprogram.

9
Interaction in RMI

◼ RMI uses a standard mechanism (employed in RPC systems) for communicating with
remote objects: stubs and skeletons.
◼ A stub for a remote object acts as a client's local representative or proxy for the remote
object.
◼ The caller invokes a method on the local stub which is responsible for carrying out the
method call on the remote object.
◼ In RMI, a stub for a remote object implements the same set of remote interfaces that a
remote object implements. When a stub's method is invoked, it does the following:
◼ initiates a connection with the remote JVM containing the remote object
◼ marshals (writes and transmits) the parameters to the remote JVM
◼ waits for the result of the method invocation,
◼ unmarshals (reads) the return value or exception returned,
◼ returns the value to the caller.

10
RMI (Remote Method Invocation)

◼ Facilitates object function calls between Java Virtual Machines (JVMs).


◼ Java application programming interface that performs the object-
oriented equivalent of remote procedure call (RPC).
◼ JVMs can be located on separate computers - yet one JVM can invoke
methods belonging to an object stored in another JVM.
Local Machine(Client) Remote Machine (Server)

SampleServer remoteObject;
int s;

s = remoteObject.sum(1,2); 1,2
public int sum(int a,int b)
{
return a + b;
3 }

System.out.println(s);

11
Building and Running RMI Applications
I. Building RMI Applications
1) Implementing the Interfaces
2) Implementing the Server
3) Implementing the Client
II. Running RMI Applications
1) Run rmiregistry
2) Run Server
3) Run Client

12
Example- Demo:
Mortgage
Calculator

13
RMI Demo

◼ We'll create a service that can calculate the mortgage payment and the method is
available to public.
◼ The first thing we need to do is to agree upon an interface. An interface is a
description of the methods we will allow remote clients toinvoke.
◼ In Mortgage calculator, the interface will becalculatePayment.
◼ Let's consider exactly what we'll need.
❑ A method that accepts as parameters an double principal, double annualrate , and int terms,
calculatePayment(double principal, double annualRate, int terms)
❑ When clients invoked the remote method with provided parameters monthly payment will be
calculated.

14
1. Interface Implementation

15
2. Server Implementation

16
3. Client Implementation

17
How to Run RMI application
(from Windows Command line)

• Start rmiregistry (open the 1st command prompt then)


• user>> rmiregistry or user>> start rmiregistry it will run at the background
• Run the Server – (open 2nd command prompt)
• user>>javac Server.java
• user>>Java Server
• Run the Client (open the 3rd command prompt)
• user>>javac Client.java
• user>>java Client

18
References
◼ Book: Distributed Systems Principles and Paradigms, 2nd edition (Chapter 4,
Section 4.2), Andrew S. Tanenbaum, Maarten van Steen
◼ Hello RMI Tutorial
https://docs.oracle.com/javase/7/docs/technotes/guides/rmi/hello/hello-
world.html#define%60
◼ You tube demo tutorial https://www.youtube.com/watch?v=3fq4AdaiGFA
◼ PC Code example

19
QUESTIONS??

20
Example

◼ A client makes remote procedure calls to a server. The client takes 5 milliseconds
to compute the arguments for each request, and the server takes 10 milliseconds to
process each request. The local operating system processing time for each send or
receive operation is 0.5 milliseconds, and the network time to transmit each request
or reply message is 3 milliseconds.
Marshalling or unmarshalling takes 0.5 milliseconds permessage.
Calculate the time taken by the client to generate and return from two requests:

◼ a) if it is single-threaded
◼ b) if it has two threads that can make requests concurrently on a single processor.
(You can ignore context-switching times.)

21
Sample Answer
❑ a) time per call = calc. args + marshal args + OS send time + message transmission + OS
receive time + unmarshall args + execute server procedure + marshall results + OS send
time + message transmission + OS receive time + unmarshal args
= 5 + 4 * marshal / unmarshal + 4 * OS send / receive + 2 * message transmission +
execute server procedure = 5+ 4 * 0.5 + 4 * 0.5 +2 * 3 + 10 ms = 5+2+2+6+10 =25ms.
Time for two calls = 50 ms.

❑ b) threaded calls: client does calc. args + marshal args + OS send time (call 1)
= 5+.5+.5 = 6
then calc args + marshal args + OS send time (call 2) = 6
= 12 ms then waits for reply from first call
server gets first call after message transmission + OS receive time + unmarshal args
= 6+ 3+.5+.5 = 10 ms, takes 10+1 to execute, marshal, send at 21 ms

22
Thank you!

23

You might also like