Chapter 2
Chapter 2
COMMUNICATION
Contents
Message Passing: Introduction to Message Passing, Advantages and
features of Message Passing, Message Format, Message Buffering, Multi
Data gram Messaging, Group Communication
CHAPTER 3: Sunita Mahajan
Remote Procedure Call (RPC): Basic RPC Operations, Parameter Passing,
Extended RPC Models
CHAPTER 4: Sunita Mahajan
Remote Object Invocation: Distributed Objects, Binding a Client to an
Object, Static Vs Dynamic RMI, Parameter Passing, Java RMI
CHAPTER 4: Sunita Mahajan
Message Oriented Communication: Persistence and synchronicity in
communication, Message Oriented Transient and Persistent
Communications
Topic 2.5: Sudha Sadasivam
Vinaya Sawant 1
Chapter 2
3
Inter Process Communication
4
Message Passing v/s Shared Memory
Vinaya Sawant 2
Chapter 2
5
Message passing v/s Shared Memory Contd.
REMOTE PROCEDURE
Communication CALL (RPC)
Vinaya Sawant 3
Chapter 2
7
Introduction
• Middleware
Conventional Procedure Call: LPC
count = read (fd, buf, nbytes)
Vinaya Sawant 4
Chapter 2
10
Basic RPC Model
11
How RPC Works?: RPC Operation
Vinaya Sawant 5
Chapter 2
12
How RPC Works? (Cont’d)
Steps of RPC
13
Elements of RPC Implementation
To achieve transparency
Client
Client stub
RPC Runtime
Server stub
Server
Vinaya Sawant 6
Chapter 2
14
RPC Components
RPC Execution 15
Vinaya Sawant 7
Chapter 2
16
Stub Generation
Manual generation
Auto generation using Interface Definition
Language (IDL)
17
RPC Compilation
Vinaya Sawant 8
Chapter 2
18
RPC Implementation
RPC messages
Call / Request
Reply
19
RPC Call/ Request Message Format
Vinaya Sawant 9
Chapter 2
21
RPC Reply Message Format
22
Parameter Passing Semantics
Call-by-value semantic: copies all parameters
into a message before transmission
Marshalling
Call-by-reference semantic: passes pointers to
the parameters that are passed from the client
to the server
Call-by-copy/restore semantic: uses temporary
storage accessible to both programs
Vinaya Sawant 10
Chapter 2
23
Call-by-value Semantic: Example
24
Byte Ordering
Vinaya Sawant 11
Chapter 2
28
Extended RPC Models
Asynchronous RPC
29
Extended RPC Models
Deferred Synchronous RPC
One-way RPC
Vinaya Sawant 12
Chapter 2
30
Advantages of RPC
Easy to design and understand programs
Extends conventional procedure call to the client/server model
Helps the programmer to focus on the application instead of the
communication protocol
33
RPC Application Development
Defining the Protocol
Creating stub and skeleton
Defining Client and Server Application Code
Compiling and running the application
Vinaya Sawant 13
Chapter 2
34
1. IDL File
syntax:
program <program_name> {
version <version_name> {
return_type function_name(arguments) = 1;
} = 1;
} = 700001;
program SUMPROG {
version SUMVERS {
int sum(int, int) =1;
} =1;
}=377;
35
2. Stub and skeleton
java Jrpcgen -n -S sum.x
D:\MEIT\pracs\rpc\sum>java Jrpcgen -n -S sum.x
Jrpcgen: Invalid or incorrect serial number
D:\MEIT\pracs\rpc\sum>java Jrpcgen -n -S sum.x
Jrpcgen v6.0, Copyright 1997 - 2010 by Distinct Corporation
writing: sum.java
writing: sumServer.java
writing: sum_1_argument.java
sum_1_argument This is a subclass of the XDRType that performs
XDR encoding and decoding on the sum data
type.
sum.java This is the client stub.
sumServer.java This is the server stub.
Vinaya Sawant 14
Chapter 2
36
3. Server and Client Applications
Create the NetBeans projects
class myServer extends sumServer
Set JAR Library for djrpc.jar {
public myServer () throws RPCError
Server Code {
super ();
}
public int sum (int a , int b)
{
// Write function Definition
}
}
class serverThread extends Thread
{
myServer rpcServer;
public void run () {
try {
rpcServer = new myServer ();// run the RPC server
}
catch (RPCError e) {
System.err.println ("Unable to start server");
}
}
}
37
3. Server and Client Applications Contd.
Client Code
Create Interface
Add actionPerformed()
private void jButtonActionPerformed(java.awt.event.ActionEvent evt)
{
sum myClient = null;
try {
String server = JTextField1.getText ();
InetAddress addr = InetAddress.getByName (server);
myClient = new sum(addr, false);
//call the sum function
}
catch (Exception ex)
{
System.err.println ("Unable to make client call");
return;
}
}
Vinaya Sawant 15
Chapter 2
38
4. Run the Application
39
4. Run the Application Contd.
Vinaya Sawant 16
Chapter 2
40
4. Run the Application Contd.
41
RPC Practical Assignments
Sum
Sum of all numbers between range given
GCD and LCM
POWER
Area of rectangle
Largest of two
Prime
Factorial
Armstrong
Area of Circle
Reverse
Vinaya Sawant 17
Chapter 2
42
REMOTE METHOD INVOCATION
Communication (RMI)
Remote Object & Remote Interface
Distributed object concept
Remote objects reference
Remote interface
Vinaya Sawant 18
Chapter 2
45
Distributed / Remote Object
RMI v/s LMI
Remote Objects Reference
Remote Interface
Vinaya Sawant 19
Chapter 2
RMI Implementation
Design issues in RMI
Level of transparency
Marshalling
Message passing
Task of locating and contacting the remote object for the client
Level of Transparency: RMI Flow Diagram
Vinaya Sawant 20
Chapter 2
51
Architecture of RMI
52
RMI mechanism
Vinaya Sawant 21
Chapter 2
Components of RMI
RMI Execution
Vinaya Sawant 22
Chapter 2
57
Remote Method Invocation: Steps
Types of Objects
Vinaya Sawant 23
Chapter 2
Remote Invocation Readiness
Java RMI
Case Study
Vinaya Sawant 24
Chapter 2
65
Advantages
Integration of distributed object model
Seamless remote invocation on objects
Usage of applets on client side
Transparency of invocation
Ease of communication between distributed applications
Reliable communication between distributed applications
Ease of use
68
RMI Application: Components
Interface definition for the remote service
Implementation of the remote service
Stub and Skeleton files
A server to host the remote service
A RMI Naming service that allows clients to find
the remote service
A client program that needs the remote service
Vinaya Sawant 25
Chapter 2
69
Steps for Developing RMI Application
1. Write and compile Java code for interface
2. Write and compile Java code for
implementation class
3. Generate Stub and Skeleton class files from the
implementation class
4. Write Java code for a remote service host
program
5. Develop Java code for RMI client program
6. Install and run RMI application
71
Remote interface: requirements
Must be public
Must extend java.rmi.Remote
All Methods
Exception: java.rmi.RemoteException
May extend another interface
Vinaya Sawant 26
Chapter 2
72
Extending from non-remote interface in RMI
public interface A {
public A_method() throws
java.io.RemoteException;
}
public interface B extends A, java.rmi.Remote {
public B_method() throws
java.rmi.RemoteException;
}
73
RemoteException class
Failure in communication
Failure while marshalling / unmarshalling parameters
Vinaya Sawant 27
Chapter 2
74
RMI bank interface
public interface BankInterface extends java.rmi.Remote
{
public void deposit(float amount)
throws java.rmi.RemoteException;
public void withdraw(float amount)
throws java.rmi.RemoteException;
public float view_balance()
throws java.rmi.RemoteException;
}
75
RemoteObject class and subclasses
UnicastRemoteObject:
Used for exporting a remote object with JRMP and obtaining a stub that
communicates to the remote object.
methods to create remote objects and export them
Vinaya Sawant 28
Chapter 2
77
Steps for Developing RMI Application
1. Write and compile Java code for interface
2. Write and compile Java code for
implementation class
3. Generate Stub and Skeleton class files from the
implementation class
4. Write Java code for a remote service host
program
5. Develop Java code for RMI client program
6. Install and run RMI application
78
Step 1: Write and compile Java code for
interfaces
public interface Calculator extends java.rmi.Remote {
public long add (long a, long b)
throws java.rmi.RemoteException;
public long sub (long a, long b)
throws java.rmi.RemoteException;
public long mul (long a, long b)
throws java.rmi.RemoteException;
public long div (long a, long b)
throws java.rmi.RemoteException;
}
Vinaya Sawant 29
Chapter 2
Step 2: Write and compile Java code for 79
implementation class
The server is a simple unicast remote server.
Create server by extending java.rmi.server.UnicastRemoteObject.
public class CalculatorImpl extends
java.rmi.server.UnicastRemoteObject
implements Calculator
{
public CalculatorImpl() throws
java.rmi.RemoteException
{
super();
}
Step 2: contd.. 80
// Implement the remote methods
public long add(long a, long b) throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b) throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b) throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b) throws java.rmi.RemoteException {
return a / b;
}
}
Vinaya Sawant 30
Chapter 2
81
Step 3: Generate Stub and Skeleton class files from the
implementation class
rmic CalculatorImpl
82
Step 4: Write remote service host program: Server
The server must bind its name to the registry, the client will look
up the server name.
Use java.rmi.Naming class to bind the server name to registry.
In this example the name “CalculatorService”.
Vinaya Sawant 31
Chapter 2
83
Step 4: contd..
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099//CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer();
}
}
84
Step 5: Develop the client program
Lookup in registry
The server name is specified as URL in the from
(rmi://host:port/name )
Default RMI port is 1099.
The name specified in the URL must exactly
match the name that the server has bound to
the registry.
Vinaya Sawant 32
Chapter 2
85
Step 5: contd..
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup("rmi://localhost//CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}
catch (Exception e) {
System.out.println(e);
}
}
}
86
Step 6: Start the RMI registry and run
application
start rmiregistry
Run server
Run client
Vinaya Sawant 33
Chapter 2
87
Message
Passing
88
Introduction to Message Passing
Vinaya Sawant 34
Chapter 2
89
Message Passing v/s Shared Memory
91
Advantages and Disadvantages
of Message Passing
Hardware Match
Functionality
Performance: data locality
Responsibility of Programmer
Vinaya Sawant 35
Chapter 2
92
Features of Message Passing
Desirable Features
Uniform Semantics
Efficiency
Reliability
Correctness
Flexibility
Portability
Security
93
Message Passing Operation / Process
Vinaya Sawant 36
Chapter 2
94
Components of an IPC Message
96
IPC Message Format Contd
Vinaya Sawant 37
Chapter 2
IPC: Synchronous Communication
IPC: Asynchronous Communication
Vinaya Sawant 38
Chapter 2
IPC Primitives: Blocking and Non-
Blocking
106
Message Buffering Strategies
Null Buffer (No Buffering)
Single-Message Buffer
Infinite-Capacity Buffer
Finite-Bound Buffer
Vinaya Sawant 39
Chapter 2
Null Buffering
Null Buffering with Blocked Receiver
Null buffering with blocking mechanism
Vinaya Sawant 40
Chapter 2
Null Buffering with Non-blocked Receiver
Null buffering with non-blocking
mechanism
Message Buffering: Single Buffer
Single-message buffering
Vinaya Sawant 41
Chapter 2
Message Buffering: Multiple Message Buffer
Receiver overflow
handled using
Unsuccessful
communication
indication
Flow
control
mechanism
Multidatagram Messaging
Concept of MTU
Message sequencing and reassembly
Message contents
Message representation: tagged, untagged
Vinaya Sawant 42
Chapter 2
Message Data Transmission
Group Communication
Unicast –one to one communication
Many-to-one group communication
One-to-many or multicast group communication
Vinaya Sawant 43
Chapter 2
Unicast Group Communication
Many-to-One Communication
Vinaya Sawant 44
Chapter 2
Multicast Communication
Broadcast
Communication
Types of Groups
Closed group
Open group
Peer group
Hierarchical group
Vinaya Sawant 45
Chapter 2
Group Management
Centralized approach
Distributed approach
Group Addressing Message Delivery
High level naming • Send to all semantics
For large LANs/ MANs: send • Bulletin board
message to individual group semantics
members
Vinaya Sawant 46
Chapter 2
Reliability Mechanism
Classified based on number of receivers from which
sender expects a response
Message Ordering
Vinaya Sawant 47
Chapter 2
Message Ordering: Absolute Ordering
Message Ordering: Consistent Ordering
Vinaya Sawant 48
Chapter 2
Message Ordering: Causal Ordering
Communication
MESSAGE-ORIENTED COMMUNICATION
Vinaya Sawant 49
Chapter 2
137
Message Oriented Communication
RPC and RMI enhance transparency in Distributed Systems, but
can only be used when it is assumed that the receiver is
executing at the time the request is issued.
Likewise, the synchronous nature of RPC and RMI, by which a
client is blocked until its request is processed is not always
adequate.
Solution: – Message oriented communication
In order to understand the various alternatives in message
oriented communication, one must approach the concepts of
– Persistence – Synchronicity
138
Persistence and Synchronicity in
Communication
General Organization of a communication
system
Vinaya Sawant 50
Chapter 2
139
Persistent Communication
Persistent communication
Messages are stored until communication with next receiver is
possible
Examples: email, pony express
140
Transient Communication
Transient communication (the opposite of persistent
communication)
Message is stored only so long as sending/receiving application
are executing
Discard message if it can’t be delivered to next server/receiver
Example: transport-level communication services offer transient
communication
Example: Typical network router – discard message if it can’t be
delivered next router or destination
Vinaya Sawant 51
Chapter 2
141
Synchronicity
Asynchronous communication – Sender continues immediately
after it has submitted the message – Need a local buffer at the
sending host
Synchronous communication – Sender blocks until message is
stored in a local buffer at the receiving host or actually
delivered to the receiver
Variant: block until receiver processes the message
Six combinations of persistence and synchronicity
Different forms of
142
Communication
Persistent
Transient
Asynchronous
Synchronous
Vinaya Sawant 52
Chapter 2
143
Combinations
Persistent Asynchronous
Persistent Synchronous
Transient Asynchronous
Transient Synchronous
Receipt-based
Delivery-based
Response-based
Different forms of
144
Communication
Neepa Shah
Vinaya Sawant 53
Chapter 2
Message-oriented Transient 145
Communication
Many distributed systems are built on top of simple message-
oriented model – E.G: Berkeley sockets
Socket: a communication endpoint to which an application
can write data to be sent to the network, or where an
application can read incoming data
Message-oriented Transient 146
Communication
Message-passing interface (MPI)
Hardware independent
Designed for parallel applications (uses transient communication)
Key idea: communication between groups of processes – Each
endpoint is a (groupID, processID) pair
MPI primitives implement the most relevant transient/synchronicity
combinations
Vinaya Sawant 54
Chapter 2
147
Message-oriented Persistent
Communication
Message queuing systems
Support asynchronous persistent communication
Intermediate storage for message while sender/receiver are
inactive
Example application: email
Communicate by inserting messages in queues
Sender is only guaranteed that message will be eventually
inserted in recipient’s queue – No guarantees on when or if the
message will be read – “Loosely coupled communication”
148
Message-oriented Persistent
Communication
Message-Queuing Model
Vinaya Sawant 55
Chapter 2
Basic Interface to a queue in
149
MQS
150
General Architecture of a MQS
Requirements
Source and destination queue
Queue names
Mapping of queues to network locations
Queue manager
Relays
Vinaya Sawant 56
Chapter 2
151
Relationship between queue-level
addressing and network-level addressing
152
Message-oriented Persistent
Communication
General message-queuing systems vs. e-mail
E-mail aims at providing direct support for the end users (this is
why groupware is often based on e-mail systems)
General purpose message-queuing systems are set up to
enable persistent based communication between all kinds of
processes. Therefore they should also provide:
Guaranteed message delivery;
Message priorities;
Logging facilities;
Efficient multicasting;
Load balancing;
Fault tolerance;
• E.g.: IBM MQSeries
Vinaya Sawant 57
Chapter 2
153
General Architecture of a MQS
(Cont’d)
General Organization of a MQS with routers
154
General Architecture of a MQS
(Cont’d)
Message Brokers..gateway
Vinaya Sawant 58