Communication Basics,: RPC & Rmi
Communication Basics,: RPC & Rmi
1
Communication Models
1. Remote Procedure Call (RPC)
• Client/Server application
2. Remote Method Invocation (RMI)
• Distributed objects
3. Message-Oriented Middleware (MOM)
• High-level message-queuing
4. Streams
• Communication of continuous media (having timing
constraints)
2
Layered Protocols (1)
application protocol
application 7
presentation protocol
presentation 6
session protocol
session 5
transport protocol
transport 4
network protocol
network 3
data link protocol
data link 2
physical protocol
physical 1
network
5
Data Link Layer: Example
time A B Event
0 data 0 A sends data message 0
7
Transport Layer
Important: The transport layer provides the
actual communication facilities for most
distributed systems.
Responsibility: deliver messages without loss;
– The messages are broken into smaller pieces, and each piece is
assigned a sequence number.
– if necessary retransmit messages.
Standard Internet protocols:
– TCP: connection-oriented, reliable, stream-oriented
– UDP: connectionless, unreliable (best-effort) datagram
communication; essentially IP with minor additions.
8
Client-Server TCP
application protocol
application 6
middleware protocol
Middleware 5
transport protocol
transport 4
network protocol
network 3
data link protocol
data link 2
physical protocol
physical 1
network
An adapted reference model for networked communication.
12
Remote Procedure Call (RPC)
The procedures such as send and receive do
not conceal the communication
More sophisticated is allowing programs to call
procedures located on other machines.
13
Conventional Procedure Call
int read (int fd, char *buf, int bytes);
SP
(a) (b)
15
Basic RPC Operation
Goal: to make a remote procedure call look as
much as possible like a local one
Observations:
– Application developers are familiar with simple local
procedure call model
– Well-engineered procedures operate in isolation
(black box)
– There is no fundamental reason not to execute
procedures on a remote machine
16
Client and Server Model
17
Key Components in RPC
Client stub:
A piece of code that transforms the procedure
call to a request message that is to be delivered
to the receiver by the underlying network
services
– It blocks until the reply comes back
Server stub:
A piece of code that transforms requests coming
in over the network into local procedure calls
– The server stub is blocked waiting for incoming
messages.
18
Steps of a Remote Procedure Call
1. Client procedure calls client stub in normal way
2. Client stub builds message, calls local OS
3. Client's OS sends message to remote OS
4. Remote OS gives message to server stub
5. Server stub unpacks parameters, calls server
6. Server does work, returns result to the stub
7. Server stub packs it in message, calls local OS
8. Server's OS sends message to client's OS
9. Client's OS gives message to client stub
10. Stub unpacks result, returns to client
19
RPC: Parameter Passing
client machine server machine
server process
client process implementation of add
k = add(i, j)
k = add(i, j)
21
Passing Value Parameters
Two parameters to the RPC:
• int 5;
• string “VELI”;
address increases
5 5 0
0 0 0
0 0 0
0 0 5
V V I
E E L
L L E
I I V
23
RPC Protocol
24
Interface
• An interface consists of a collection of procedures that
can be called by a client, and which are implemented by
a server.
• An interface is generally available in the same
programming language as the one in which the client or
server is written (not necessarily though).
• It, often, looks like a ANSI function declaration
• Interfaces are often specified by means of an
Interface Definition Language (IDL).
• An interface specified in an IDL, is then subsequently
compiled into a client and server stub (e.g. rmic
generates client stub given an interface in Java RMI)
25
Variations: Asynchronous RPC
Essence: client needs not to block when there is nothing
to return.
2-12
a) traditional RPC
b) asynchronous RPC 26
Asynchronous RPC with Return Value
Deferred synchronous RPC: A client and server
interacting through two asynchronous RPCs
29
Remote Method Invocation
• Distributed objects
• Remote method invocation
• Parameter passing
30
Distributed Objects (1)
• Data(state) and operations are encapsulated in an
object.
• Operations on object data are implemented as methods,
and are accessible through interfaces
• Object offers only interfaces to its clients
• Object server is responsible for a collection of objects
• Client stub (proxy) implements interface on the client
side
• Server stub (skeleton) handles un/marshalling and
method invocation (probably returning results)
• The state of the objects are not distributed remote
objects
31
Distributed Objects (2)
34
Client-to-Object Binding: Explicit
distr_object* obj_ref; // Declare a systemwide
// object reference
local_object* obj_ptr; // Declare a pointer to
// local objects
obj_ref = …; // Initialize the
// reference to a
// distributed object
obj_ptr = bind(obj_ref); // Explicitly bind and
// obtain a pointer to the
// local proxy
obj_ptr->do_something(); // Invoke a method on the
// local proxy
37
Implementing Object References (3)
39
Dynamic vs. Static RMI
Example:
Static Invocation:
fobject.append(int i)
Dynamic Invocation:
invoke(fobject, id(append), int i)
40
Parameter Passing (1)
Object as a parameter: Two cases:
• Object-by-reference: when it refers to a remote
object, an object reference is passed when invoking a
remote method.
• Object-by-value: reference refers to a local object
– It is in the address space of the client
– The state and the methods of the object are to be marshaled
– Server unmarshalls the object; creating a copy of the original
object
• This distinction may lead to some problems due to the
transparency
• In Java, reference to a local object and reference to a
remote object are of different data types. But similarly
treated. 41
Parameter Passing (2)
Machine A Machine B
Local remote
ref L1 Local object ref R1 Remote object
O1 O2
Remote invocation
with L1 and R1 as server code
parameters (method imp)
• The situation when passing an object by reference or
by value 42
Java RMI: Object model
• Distributed objects are integral part of the language
• Distributed object is defined as remote object, whose
state resides on a single remote machine
• Interfaces are implemented by means of a proxy
• A proxy appear as a local object in the client’s address
space
• Remote objects cannot be cloned (neither their proxies
at each client) (can be cloned only by the server)
• Each object can be constructed as a monitor, by
declaring a method to be synchronized
– Access to object’s internal data is completely serialized.
– protecting remote objects through synchronized methods is
generally not possible.
43
Java RMI: Remote Object Invocation
• Any primitive or object type can be passed as a
parameter to an RMI, provided that the type can be
marshaled
• In Java terminology, objects must be serializable
• Platform dependent objects (e.g. file descriptors,
sockets) are not serializable
• Local objects are passed by value, remote objects by
reference
• Object reference contains
– network address,
– endpoint of the server
– local identifier for the actual object in the server’s address
space
– protocol stack for communication
44
Components of Remote Object
• Each object in Java is an instance of a class which
contains an implementation of one or more interfaces
• Server-class: implementation of server-side code
– Contains implementation of the object (description of the
object’s state, implementation of the methods on the state)
– Server side stub, skeleton, is generated from the interface
specification of the object (obsolete now)
• Client-class: implementation of client-side code
– Contains the client-side stub, called proxy, generated from the
interface specification of the remote object
– Remote object reference (i.e. network address of the server,
the port number etc.) is always stored as part of the state of a
proxy.
– Proxies are serializable since they are, in essence, local objects
45
Marshalling a Proxy
• A proxy can be used as a reference to a remote
object
• Marshalling a proxy may be inefficient since it
can lead to very large references
• In Java, when marshalling a proxy, an
implementation handle is generated
• Implementation handle specifies precisely which
classes are needed to construct the proxy
• Some of these classes may first need to be
downloaded from a remote site
• Since Java code is completely portable passing
proxies is possible
46
Java RMI: Example (1)
---- Hello.java File (interface) ------
import java.rmi.Remote;
import java.rmi.RemoteException;
47
Java RMI: Example (2)
--- HelloImpl.java File ------
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
48
Java RMI: Example (3)
--- HelloImpl.java File (cont.) ------
public static void main(String args[]) {
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
HelloImpl obj = new HelloImpl();
// Bind this object instance to the name
// "HelloServer"
Naming.rebind("//localhost/HelloServer", obj);
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.util.Date;
52
Java RMI: Example (7)
---- HelloClient.java File (cont.) ------
System.setSecurityManager(new RMISecurityManager());
if (argv.length != 1) {
try {
serverName =java.net.InetAddress.getLocalHost().getHostName();
}
catch(Exception e) {
e.printStackTrace();
}
}
else {
serverName = argv[0];
}
if (serverName == "") {
System.out.println("usage: java SimpleRMIClient <IP
address of host running RMI server>");
System.exit(0);
}
53
Java RMI: Example (8)
---- HelloClient.java File (cont.) ------
try {
//explicitly bind server object to object in client
obj = (Hello) Naming.lookup("//" + serverName + "/HelloServer");
//Print success message
System.out.println("RMI connection successful");
//Call method on server and put result into String message
message = obj.sayHello();
//Print message on server
System.out.println("Message on server is " + message);
}
catch(Exception e) {
System.out.println("Exception occurred: " + e);
System.exit(0);
}
}
}
54
Java RMI: Example (9)
Server side:
– HelloImpl.java
– Hello.java (interface)
– HelloImpl_stub.java (needed for registry)
– Skeleton is deprecated in version 1.2
Client side:
– HelloClient.java (client object invoking remote
method)
– HelloImpl_stub.java (proxy) – can be downloaded
remotely
– Hello.java (interface)
55