UNIT II COMMUNICATION IN DISTRIBUTED SYSTEM 10
System Model – Inter process Communication – the API for internet protocols – External data representation and Multicast communication. Network virtualization: Overlay networks. Case study: MPI Remote Method Invocation And Objects: Remote Invocation – Introduction – Request-reply protocols – Remote procedure call – Remote method invocation. Case study: Java RMI – Group communication – Publish-subscribe systems – Message queues – Shared memory approaches – Distributed objects – Case study: Enterprise Java Beans -from objects to components.
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
CS6601 DISTRIBUTED SYSTEMS
1. CS6601 DISTRIBUTED SYSTEMS
UNIT – II
Dr.A.Kathirvel, Professor, Computer Science and Engg.
M N M Jain Engineering College, Chennai
2. Unit - II
COMMUNICATION IN DISTRIBUTED
SYSTEM
System Model – Inter process Communication – the API for internet
protocols – External data representation and Multicast
communication. Network virtualization: Overlay networks. Case
study: MPI Remote Method Invocation And Objects: Remote
Invocation – Introduction – Request-reply protocols – Remote
procedure call – Remote method invocation. Case study: Java RMI –
Group communication – Publish-subscribe systems – Message
queues – Shared memory approaches – Distributed objects – Case
study: Enterprise Java Beans -from objects to components.
Coulouris, Dollimore, Kindberg and Blair
Distributed Systems:Concepts and Design Edition 5, Addison-Wesley 2012
4. Fig4.2:Sockets and ports
message
agreed portany port socketsocket
Internet address = 138.37.88.249Internet address = 138.37.94.248
other ports
client server
4
5. UDP client sends a message to the
server and gets a reply
import java.net.*;
import java.io.*;
public class UDPClient{
public static void main(String args[]){
// args give message contents and server hostname
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket();
byte [] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request = new DatagramPacket(m, m.length(), aHost, serverPort);
aSocket.send(request);
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply: " + new String(reply.getData()));
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
}catch (IOException e){System.out.println("IO: " + e.getMessage());}
}finally {if(aSocket != null) aSocket.close();}
} }
5
6. UDP server repeatedly receives a
request and sends it back to the client
import java.net.*;
import java.io.*;
public class UDPServer{
public static void main(String args[]){
DatagramSocket aSocket = null;
try{
aSocket = new DatagramSocket(6789);
byte[] buffer = new byte[1000];
while(true){
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
aSocket.receive(request);
DatagramPacket reply = new DatagramPacket(request.getData(),
request.getLength(), request.getAddress(), request.getPort());
aSocket.send(reply);
}
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
}catch (IOException e) {System.out.println("IO: " + e.getMessage());}
}finally {if(aSocket != null) aSocket.close();}
}}
6
7. TCP client makes connection to server,
sends request and receives reply
import java.net.*;
import java.io.*;
public class TCPClient {
public static void main (String args[]) {
// arguments supply message and hostname of destination
Socket s = null;
try{
int serverPort = 7896;
s = new Socket(args[1], serverPort);
DataInputStream in = new DataInputStream( s.getInputStream());
DataOutputStream out =new DataOutputStream( s.getOutputStream());
out.writeUTF(args[0]); // UTF is a string encoding see Sn 4.3
String data = in.readUTF();
System.out.println("Received: "+ data) ;
}catch (UnknownHostException e){ System.out.println("Sock:"+e.getMessage());
}catch (EOFException e){System.out.println("EOF:"+e.getMessage());
}catch (IOException e){System.out.println("IO:"+e.getMessage());}
}finally {if(s!=null) try {s.close();}catch (IOException
e){System.out.println("close:"+e.getMessage());}}
}}
7
8. TCP server makes a connection for each
client and then echoes the client’s request
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main (String args[]) {
try{ int serverPort = 7896;
ServerSocket listenSocket = new ServerSocket(serverPort);
while(true) {
Socket clientSocket = listenSocket.accept();
Connection c = new Connection(clientSocket); }
} catch(IOException e) {System.out.println("Listen :"+e.getMessage());}
}
}
// this continues on the next slide
8
9. class Connection extends Thread {
DataInputStream in;
DataOutputStream out;
Socket clientSocket;
public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
in = new DataInputStream( clientSocket.getInputStream());
out =new DataOutputStream( clientSocket.getOutputStream());
this.start();
} catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}
public void run(){
try { // an echo server
String data = in.readUTF();
out.writeUTF(data);
} catch(EOFException e) {System.out.println("EOF:"+e.getMessage());
} catch(IOException e) {System.out.println("IO:"+e.getMessage());}
} finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
}
}
9
10. CORBA CDR for constructed types
Type Representation
sequence length (unsigned long) followed by elements in order
string length (unsigned long) followed by characters in order (can also
can have wide characters)
array array elements in order (no length specified because it is fixed)
struct in the order of declaration o f the comp onents
enumerated unsigned long (the values are specified by the order declared)
union type tag followed by the selected m emb er
10
11. CORBA CDR message
The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1984}
0–3
4–7
8–11
12–15
16–19
20-23
24–27
5
"Smit"
"h___"
6
"Lond"
"on__"
1984
index in
sequence of bytes 4 bytes
notes
on representation
length of string
‘Smith’
length of string
‘London’
unsigned long
11
12. Indication of Java serialized form
The true serialized form contains additional type markers; h0 and h1 are handles
Serialized values
Person
3
1984
8-byte version number
int year
5 Smith
java.lang.String
name:
6 London
h0
java.lang.String
place:
h1
Explanation
class name, version number
number, type and name of
instance variables
values of instance variables
12
13. XML definition of the Person structure
<person id="123456789">
<name>Smith</name>
<place>London</place>
<year>1984</year>
<!-- a comment -->
</person >
use of namespace in the Person structure
<person pers:id="123456789" xmlns:pers="http://www.cdk5.net/person">
<pers:name> Smith </pers:name>
<pers:place> London </pers:place >
<pers:year> 1984 </pers:year>
</person>
13
14. An XML schema for the Person structure
<xsd:schema xmlns:xsd = URL of XML schema definitions >
<xsd:element name= "person" type ="personType" />
<xsd:complexType name="personType">
<xsd:sequence>
<xsd:element name = "name" type="xs:string"/>
<xsd:element name = "place" type="xs:string"/>
<xsd:element name = "year" type="xs:positiveInteger"/>
</xsd:sequence>
<xsd:attribute name= "id" type = "xs:positiveInteger"/>
</xsd:complexType>
</xsd:schema>
Representation of a remote object reference
Internet addressport number time object number
interface of
remote object
32 bits 32 bits 32 bits 32 bits
14
15. Multicast peer joins a group and sends
and receives datagrams
import java.net.*;
import java.io.*;
public class MulticastPeer{
public static void main(String args[]){
// args give message contents & destination multicast group (e.g. "228.5.6.7")
MulticastSocket s =null;
try {
InetAddress group = InetAddress.getByName(args[1]);
s = new MulticastSocket(6789);
s.joinGroup(group);
byte [] m = args[0].getBytes();
DatagramPacket messageOut =
new DatagramPacket(m, m.length, group, 6789);
s.send(messageOut);
// this figure continued on the next slide
15
16. // get messages from others in group
byte[] buffer = new byte[1000];
for(int i=0; i< 3; i++) {
DatagramPacket messageIn =
new DatagramPacket(buffer, buffer.length);
s.receive(messageIn);
System.out.println("Received:" + new String(messageIn.getData()));
}
s.leaveGroup(group);
}catch (SocketException e){System.out.println("Socket: " + e.getMessage());
}catch (IOException e){System.out.println("IO: " + e.getMessage());}
}finally {if(s != null) s.close();}
}
}
16
24. Operations of the request-reply protocol
public byte[] doOperation(RemoteRef s,int operationId,byte[] arguments)
sends a request message to the remote server and returns the reply.
The arguments specify the remote server, the operation to be invoked
and the arguments of that operation.
public byte[] getRequest ();
acquires a client request via the server port.
public void sendReply(byte[] reply,InetAddress clientHost, int clientPort);
sends the reply message reply to the client at its Internet address and
port.
24
25. Fig 5.4:Request-reply message structure
messageType
requestId
remoteReference
operationId
arguments
int (0=Request, 1= Reply)
int
RemoteRef
int or Operation
array of bytes
25
26. Fig5.5:RPC exchange protocols
R Request
R R Reply
R R A Acknowledge reply
Request
Request Reply
Client Server Client
Name Messages sent by
Fig 5.6:HTTP request message
GET //www.dcs.qmw.ac.uk/index.html HTTP/ 1.1
URL or pathnamemethod HTTP version headers message body
Fig 5.7:HTTP Reply message
HTTP/1.1 200 OK resource data
HTTP version status code reason headers message body
26
27. Fig5.8:CORBA IDL
example
// In file Person.idl
struct Person {
string name;
string place;
long year;} ;
interface PersonList {
readonly attribute string listname;
void addPerson(in Person p) ;
void getPerson(in string name, out Person p);
long number();};
Fig5.9:Call semantics
Fault tolerance measures Call
semantics
Retransmit request
message
Duplicate
filtering
Re-execute procedure
or retransmit reply
No
Yes
Yes
Not applicable
No
Yes
Not applicable
Re-execute procedure
Retransmit reply At-most-once
At-least-once
Maybe
28. Fig5.10: Role of client and server stub
procedures in RPC
client
Request
Reply
CommunicationCommunication
modulemodule dispatcher
service
client stub server stub
procedure procedure
client process server process
procedureprogram
28
29. Fig5.11: Files interface in Sun XDR
const MAX = 1000;
typedef int FileIdentifier;
typedef int FilePointer;
typedef int Length;
struct Data {
int length;
char buffer[MAX];
};
struct writeargs {
FileIdentifier f;
FilePointer position;
Data data;
};
struct readargs {
FileIdentifier f;
FilePointer position;
Length length;
};
program FILEREADWRITE {
version VERSION {
void WRITE(writeargs)=1; 1
Data READ(readargs)=2; 2
}=2;
} = 9999;
29
30. Fig5.12: Remote and local method
invocations
invocation invocation
remote
invocation
remote
local
local
local
invocation
invocation
A
B
C
D
E
F
30
31. Fig5.13:A remote object and its remote
interface
interface
remote
m1
m2
m3
m4
m5
m6
Data
implementation
remoteobject
{ of methods
31
33. Fig5.15: The role of proxy and skeleton
in remote method invocation
33
34. Fig5.16:Java Remote interfaces Shape
and ShapeList
import java.rmi.*;
import java.util.Vector;
public interface Shape extends Remote {
int getVersion() throws RemoteException;
GraphicalObject getAllState() throws RemoteException; 1
}
public interface ShapeList extends Remote {
Shape newShape(GraphicalObject g) throws RemoteException; 2
Vector allShapes() throws RemoteException;
int getVersion() throws RemoteException;
}
34
35. The Naming class of Java RMIregistry
void rebind (String name, Remote obj)
This method is used by a server to register the identifier of a remote object by
name, as shown in Figure 15.18, line 3.
void bind (String name, Remote obj)
This method can alternatively be used by a server to register a remote object by
name, but if the name is already bound to a remote object reference an
exception is thrown.
void unbind (String name, Remote obj)
This method removes a binding.
Remote lookup(String name)
This method is used by clients to look up a remote object by name, as shown in
Figure 5.20 line 1. A remote object reference is returned.
String [] list()
This method returns an array of Strings containing the names bound in the registry.
35
36. Fig5.18:Java class ShapeListServer
with main method
import java.rmi.*;
public class ShapeListServer{
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
try{
ShapeList aShapeList = new ShapeListServant(); 1
Naming.rebind("Shape List", aShapeList ); 2
System.out.println("ShapeList server ready");
}catch(Exception e) {
System.out.println("ShapeList server main " + e.getMessage());}
}
}
36
37. Fig5.19:Java class ShapeListServant
implements interface ShapeList
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
public class ShapeListServant extends UnicastRemoteObject implements ShapeList {
private Vector theList; // contains the list of Shapes
private int version;
public ShapeListServant()throws RemoteException{...}
public Shape newShape(GraphicalObject g) throws RemoteException { 1
version++;
Shape s = new ShapeServant( g, version); 2
theList.addElement(s);
return s;
}
public Vector allShapes()throws RemoteException{...}
public int getVersion() throws RemoteException { ... }
}
37
42. Fig6.3: The role of group membership
management
Join
Group
address
expansion
Multicast
communication
Group
send
Fail
Group membership
management
Leave
Process group
42
50. Fig6.11:Filtering-based routing
upon receive publish(event e) from node x 1
matchlist := match(e, subscriptions) 2
send notify(e) to matchlist; 3
fwdlist := match(e, routing); 4
send publish(e) to fwdlist - x; 5
upon receive subscribe(subscription s) from node x 6
if x is client then 7
add x to subscriptions; 8
else add(x, s) to routing; 9
send subscribe(s) to neighbours - x; 10
50
51. Fig6.12:Rendezvous-based routing
upon receive publish(event e) from node x at node i
rvlist := EN(e);
if i in rvlist then begin
matchlist :=match(e, subscriptions);
send notify(e) to matchlist;
end
send publish(e) to rvlist - i;
upon receive subscribe(subscription s) from node x at node i
rvlist := SN(s);
if i in rvlist then
add s to subscriptions;
else
send subscribe(s) to rvlist - i;
51
60. Replication and the tuple space
operations
write
1. The requesting site multicasts the write request to all members of the view;
2. On receiving this request, members insert the tuple into their replica and
acknowledge this action;
3. Step 1 is repeated until all acknowledgements are received.
read
1. The requesting site multicasts the read request to all members of the view;
2. On receiving this request, a member returns a matching tuple to the requestor;
3. The requestor returns the first matching tuple received as the result of the operation
(ignoring others);
4. Step 1 is repeated until at least one response is received.
continued on next slide
60
61. take Phase 1: Selecting the tuple to be removed
Phase 2: Removing the selected tuple
1. The requesting site multicasts the take request to all members of the view;
2. On receiving this request, each replica acquires a lock on the associated tuple set and, if the
lock cannot
be acquired, the take request is rejected;
3. All accepting members reply with the set of all matching tuples;
4. Step 1 is repeated until all sites have accepted the request and responded with their set of
tuples and the
intersection is non-null;
5. A particular tuple is selected as the result of the operation (selected randomly from the
intersection of all
the replies);
6. If only a minority accept the request, this minority are asked to release their locks and
phase 1 repeats.
1. The requesting site multicasts a remove request to all members of the view citing the
tuple to be
removed;
2. On receiving this request, members remove the tuple from their replica, send an
acknowledgement
and release the lock;
3. Step 1 is repeated until all acknowledgements are received.
61
64. Fig6.24:Java class AlarmTupleJS
import net.jini.core.entry.*;
public class AlarmTupleJS implements Entry {
public String alarmType;
public AlarmTupleJS() { }
}
public AlarmTupleJS(String alarmType) {
this.alarmType = alarmType;}
}}
64
65. Fig6.25:Java class FireAlarmJS
import net.jini.space.JavaSpace;
public class FireAlarmJS {
public void raise() {
try {
JavaSpace space =
SpaceAccessor.findSpace("AlarmSpace");
AlarmTupleJS tuple = new AlarmTupleJS("Fire!");
space.write(tuple, null, 60*60*1000);
catch (Exception e) {
} } }
65
66. Fig16.26:Java class FireAlarmReceiverJS
import net.jini.space.JavaSpace;
public class FireAlarmConsumerJS {
public String await() {
try {
JavaSpace space = SpaceAccessor.findSpace();
AlarmTupleJS template = new AlarmTupleJS("Fire!");
AlarmTupleJS recvd = (AlarmTupleJS)
space.read(template, null,
Long.MAX_VALUE);
return recvd.alarmType;
}
catch (Exception e) {
return null;
} }}
66
71. Fig8.4:IDL constructed types – 1
Type Examples Use
sequence typedef sequence <Shape, 100> All;
typedef sequence <Shape> All
bounded and unbounded sequences
of Shapes
Defines a type for a variable-length
sequence of elements of a specified
IDL type. An upper bound on the
length may be specified.
string String name;
typedef string<8> SmallString;
unboundedand bounded
sequences of characters
Defines a sequences of characters,
terminated by the null character. An
upper bound on the length may be
specified.
array typedef octet uniqueId[12];
typedef GraphicalObject GO[10][8]
Defines a type for a multi-dimensional
fixed-length sequence of elements of a
specified IDL type.
this figure continues on the next slide
71
72. Fig8.4: IDL constructed types – 2
Type Examples Use
record struct GraphicalObject {
string type;
Rectangle enclosing;
boolean isFilled;
};
Defines a type for a record containing a
group of related entities. Structs are
passed by value in arguments and
results.
enumerated enum Rand
(Exp, Number, Name);
The enumerated type in IDL maps a
type name onto a small set of integer
values.
union union Exp switch (Rand) {
case Exp: string vote;
case Number: long n;
case Name: string s;
The IDL discriminated union allows
one of a given set of types to be passed
as an argument. The header is
parameterized by anenum, which
specifies which member is in use.};
72
73. Fig8.5:The main components of the
CORBA architecture
client server
proxy
or dynamic invocation
implementation
repository object
adapter
ORBORB
skeleton
or dynamic skeleton
client
program
interface
repository
Request
Reply
corecorefor A
Servant
A
73
76. Fig8.7:Java interfaces generated by idlj
from CORBA interface ShapeList
public interface ShapeListOperations {
Shape newShape(GraphicalObject g)
throws ShapeListPackage.FullException;
Shape[] allShapes();
int getVersion();
}
public interface ShapeList extends ShapeListOperations,
org.omg.CORBA.Object,
org.omg.CORBA.portable.IDLEntity { }
76
77. Fig8.8: ShapeListServant class of the Java
server program for CORBA interface ShapeList
import org.omg.CORBA.*;
import org.omg.PortableServer.POA;
class ShapeListServant extends ShapeListPOA {
private POA theRootpoa;
private Shape theList[];
private int version;
private static int n=0;
public ShapeListServant(POA rootpoa){
theRootpoa = rootpoa;
// initialize the other instance variables
}
// continued on the next slide
77
78. public Shape newShape(GraphicalObject g)
throws ShapeListPackage.FullException { 1
version++;
Shape s = null;
ShapeServant shapeRef = new ShapeServant( g, version);
try {
org.omg.CORBA.Object ref =
theRoopoa.servant_to_reference(shapeRef); 2
s = ShapeHelper.narrow(ref);
} catch (Exception e) {}
if(n >=100) throw new ShapeListPackage.FullException();
theList[n++] = s;
return s;
}
public Shape[] allShapes(){ ... }
public int getVersion() { ... }
}
78