Вы находитесь на странице: 1из 22

Slides for Chapter 4:

Interprocess Communication

From Coulouris, Dollimore, Kindberg and Blair

Distributed Systems:
Concepts and Design
Edition 5, Addison-Wesley 2012

Figure 4.1
Middleware layers

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.2
Sockets and ports

any port

socket

agreed port

socket

message
client

server
other ports

Internet address = 138.37.94.248

Internet address = 138.37.88.249

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.3
UDP client sends a message to the server and gets a reply
importjava.net.*;
importjava.io.*;
publicclassUDPClient{
publicstaticvoidmain(Stringargs[]){
//argsgivemessagecontentsandserverhostname
DatagramSocketaSocket=null;
try{
aSocket=newDatagramSocket();
byte[]m=args[0].getBytes();
InetAddressaHost=InetAddress.getByName(args[1]);
intserverPort=6789;

DatagramPacketrequest=newDatagramPacket(m,m.length(),aHost,serverPort);
aSocket.send(request);

byte[]buffer=newbyte[1000];
DatagramPacketreply=newDatagramPacket(buffer,buffer.length);
aSocket.receive(reply);
System.out.println("Reply:"+newString(reply.getData()));
}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(aSocket!=null)aSocket.close();}
}
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012
}

Figure 4.4
UDP server repeatedly receives a request and sends it back to the client
importjava.net.*;
importjava.io.*;
publicclassUDPServer{
publicstaticvoidmain(Stringargs[]){
DatagramSocketaSocket=null;
try{

aSocket=newDatagramSocket(6789);
byte[]buffer=newbyte[1000];

while(true){

DatagramPacketrequest=newDatagramPacket(buffer,buffer.length);

aSocket.receive(request);

DatagramPacketreply=newDatagramPacket(request.getData(),

request.getLength(),request.getAddress(),request.getPort());
aSocket.send(reply);
}
}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(aSocket!=null)aSocket.close();}
}
}
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.5
TCP client makes connection to server, sends request and receives reply
importjava.net.*;
importjava.io.*;
publicclassTCPClient{
publicstaticvoidmain(Stringargs[]){
//argumentssupplymessageandhostnameofdestination
Sockets=null;
try{

intserverPort=7896;

s=newSocket(args[1],serverPort);
DataInputStreamin=newDataInputStream(s.getInputStream());
DataOutputStreamout=
newDataOutputStream(s.getOutputStream());
out.writeUTF(args[0]);
//UTFisastringencodingseeSn4.3
Stringdata=in.readUTF();

System.out.println("Received:"+data);

}catch(UnknownHostExceptione){
System.out.println("Sock:"+e.getMessage());
}catch(EOFExceptione){System.out.println("EOF:"+e.getMessage());

}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(s!=null)try{s.close();}catch(IOExceptione){System.out.println("close:"+e.getMessage());}}

}
}
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.6
TCP server makes a connection for each client and then echoes the clients request
importjava.net.*;
importjava.io.*;
publicclassTCPServer{
publicstaticvoidmain(Stringargs[]){
try{
intserverPort=7896;
ServerSocketlistenSocket=newServerSocket(serverPort);
while(true){
SocketclientSocket=listenSocket.accept();
Connectionc=newConnection(clientSocket);
}
}catch(IOExceptione){System.out.println("Listen:"+e.getMessage());}
}
}
//thisfigurecontinuesonthenextslide

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.6 continued


classConnectionextendsThread{
DataInputStreamin;
DataOutputStreamout;
SocketclientSocket;
publicConnection(SocketaClientSocket){
try{
clientSocket=aClientSocket;
in=newDataInputStream(clientSocket.getInputStream());
out=newDataOutputStream(clientSocket.getOutputStream());
this.start();
}catch(IOExceptione){System.out.println("Connection:"+e.getMessage());}
}
publicvoidrun(){
try{
//anechoserver
Stringdata=in.readUTF();

out.writeUTF(data);
}catch(EOFExceptione){System.out.println("EOF:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{try{clientSocket.close();}catch(IOExceptione){/*closefailed*/}}
}
}
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.7
CORBA CDR for constructed types

Type
sequence
string
array
struct
enumerated
union

Representation
length(unsignedlong)followedbyelementsinorder
length(unsignedlong)followedbycharactersinorder(canalso
canhavewidecharacters)
arrayelementsinorder(nolengthspecifiedbecauseitisfixed)
intheorderofdeclarationofthecomponents
unsignedlong(thevaluesarespecifiedbytheorderdeclared)
typetagfollowedbytheselectedmember

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.8
CORBA CDR message

indexin
sequenceofbytes
03
47
811
1215
1619
2023
2427

4bytes
5
"Smit"
"h___"
6
"Lond"
"on__"
1984

notes
onrepresentation
lengthofstring
Smith
lengthofstring
London
unsignedlong

The flattened form represents a Person struct with value: {Smith, London, 1984}

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.9
Indication of Java serialized form

Explanation

Serializedvalues
Person

8byteversionnumber

h0

classname,versionnumber

intyear

java.lang.String java.lang.String number,typeandnameof


name:
place:
instancevariables

1984

5Smith

6London

h1

valuesofinstancevariables

The true serialized form contains additional type markers; h0 and h1 are handles

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.10 XML definition of the Person structure

<personid="123456789">
<name>Smith</name>
<place>London</place>
<year>1984</year>
<!acomment>
</person>

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.11 Illustration of the use of a namespace in the Person structure

<personpers: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>

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.12 An XML schema for the Person structure

<xsd:schemaxmlns:xsd=URLofXMLschemadefinitions
>
<xsd:elementname="person"type="personType"/>
<xsd:complexTypename="personType">
<xsd:sequence>
<xsd:elementname="name"type="xs:string"
<xsd:elementname="place"type="xs:string"
<xsd:elementname="year"type="xs:positive
</xsd:sequence>
<xsd:attributename="id"type="xs:positiveInteger
</xsd:complexType>
</xsd:schema>
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.13
Representation of a remote object reference

32bits

32bits

Internetaddress

portnumber

32bits
time

32bits
objectnumber

interfaceof
remoteobject

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.14
Multicast peer joins a group and sends and receives datagrams
importjava.net.*;
importjava.io.*;
publicclassMulticastPeer{
publicstaticvoidmain(Stringargs[]){

//argsgivemessagecontents&destinationmulticastgroup(e.g."228.5.6.7")
MulticastSockets=null;

try{

InetAddressgroup=InetAddress.getByName(args[1]);

s=newMulticastSocket(6789);

s.joinGroup(group);

byte[]m=args[0].getBytes();

DatagramPacketmessageOut=
newDatagramPacket(m,m.length,group,6789);

s.send(messageOut);

//thisfigurecontinuedonthenextslide
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.14
continued

}
}

//getmessagesfromothersingroup

byte[]buffer=newbyte[1000];

for(inti=0;i<3;i++){
DatagramPacketmessageIn=
newDatagramPacket(buffer,buffer.length);
s.receive(messageIn);
System.out.println("Received:"+newString(messageIn.getData()));

s.leaveGroup(group);
}catch(SocketExceptione){System.out.println("Socket:"+e.getMessage());
}catch(IOExceptione){System.out.println("IO:"+e.getMessage());}
}finally{if(s!=null)s.close();}

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

Figure 4.15
Types of overlay

tablecontinuesonthenextslide
InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

1
8

Figure 4.15 (continued)


Types of overlay

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

1
9

Figure 4.16
Skype overlay architecture

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

2
0

Figure 4.17
An overview of point-to-point communication in MPI

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

2
1

Figure 4.18
Selected send operations in MPI

InstructorsGuideforCoulouris,Dollimore,KindbergandBlair,DistributedSystems:ConceptsandDesignEdn.5
PearsonEducation2012

2
2

Вам также может понравиться