Download presentation
Presentation is loading. Please wait.
1
Chapters 4 & 5 Interprocess Communication
Presentation of the characteristics of protocols for communication between processes: Client-Server or group communication, RPC, RMI, … Communication between distributed objects and remote invocation
2
Themes 1st Part: Means of Interprocess Communication (RPC, RMI, …)
2nd Part: Types of Interprocess Communication (Client-Sever, Group) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
3
Means of Interprocess Communication
Introduction Layered Protocols RMI (Remote Method Invocation) Readings CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
4
Request-Reply Protocol Marshalling and external data representation
Introduction (1) Applications, services RMI, RPC and events Middleware layers Request-Reply Protocol Marshalling and external data representation UDP and TCP Operating System CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
5
Introduction (2) Characteristics of middleware layers:
Location transparency (definition) Independent: protocols that support the middleware abstractions are independent of the underlying transport protocols (example) Heterogeneous hardware: hide the difference due to hardware architectures (big-endian, little-endian, …) (mean) Use of several programming languages: allow distributed applications to use more than one programming language (example) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
6
Introduction (3) Definition of remote procedure using different IDL
RPC/IDL: program RAND_PROG { version RAND_VERS void INITIALIZE_RANDOM(long) = 1; double GET_NEXT_RANDOM(void) = 2; } = 1; } = 0x ; RPC/IDL : program RAND_PROG { version RAND_VERS void INITIALIZE_RANDOM(long) = 1; double GET_NEXT_RANDOM(void) = 2; } = 1; } = 0x ; CORBA/IDL: module EXAMPLES { interface RAND_PROG void INITIALIZE_RANDOM(in long arg); double GET_NEXT_RANDOM(void); }; CORBA/IDL : module EXAMPLES { interface RAND_PROG void INITIALIZE_RANDOM(in long arg); double GET_NEXT_RANDOM(void); }; Definition of remote procedure using different IDL RMI/INTERFACE: Import java.rmi.*; package EXAMPLES; public interface RAND_VERS extends Remote { public void INITIALIZE_RANDOM(long arg) throws RemoteExceptions; public double GET_NEXT_RANDOM(void) throws RemoteExceptions; }; CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
7
Introduction (4) Characteristics of interprocess communication:
Types of communication: Synchronous: the sending and receiving processes synchronize at every message Send and Receive: blocking operations Asynchronous: no synchronization between the sending and receiving processes Send: non-blocking operation Receive: non-blocking or blocking operation CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
8
Introduction (5) Characteristics (continued): Message destinations:
Messages sent to <Internet address, Local port > Processes may use multiple ports from which to receive messages Communication reliability: Integrity (definition) Validity (definition) Ordering: some applications require that messages be delivered in sender order CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
9
Introduction (6) Characteristics (continued) :
Sockets: used by UDP and TCP as endpoint for communication between processes Must be bound to a local port and one of the Internet addresses Each socket is associated to one protocol: UDP or TCP 216 possible ports CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
10
Layered Protocols (1) Multiple layers intervene during interprocess communication Internetwork layers (Internet) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
11
Layered Protocols (2) UDP Datagram Communication
Types of communication: UDP Datagram Communication TCP Stream Communication CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
12
UDP Datagram Communication
Datagrams transmitted by UDP: without acknowledgments or retries Blocking: UDP use a non-blocking send and a blocking receive Failure model: UDP suffers from Omission failures: messages dropped (causes) Ordering: messages can sometimes be delivered out of sender order UDP advantage: do not suffer from the overheads associated with guaranteed message delivery CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
13
Datagram Communication: Java API (1)
Manipulating Internet addresses: Class InetAddress: represents Internet addresses (example) InetAddress ( string address ): multiple forms of the constructor exist short int getPort () const bool setPort ( short int port ) string getAddress () const bool setAddress ( string address ) bool setAddress ( unsigned long int address ) string getHostName () string getHostByIP ( string ip ) string getIPByName ( string host ) … CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
14
Datagram Communication: Java API (2)
Manipulating datagrams: Class DatagramPacket : creates packets containing data to be transmitted or received and the address of destination or source of the datagrams Port Number Internet Address Message’s length Message’s bytes Datagram packet Constructors: Receiver: DatagramPacket(byte buffer[], int length) buffer for holding the incoming datagram Number of bytes to read UDP Port Data packet Length of data packet Destination address Sender: DatagramPacket( byte buffer[], int length, InetAddress address, int port) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
15
Datagram Communication: Java API (3)
Manipulating datagrams (continued): main methods of the class DatagramPacket InetAddress getAddress () int getPort () void setAddress(InetAddress iaddr) byte[] getData () void setData(byte ibuf[]) void setPort(int iport) int getLength () void setLength(int ilength) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
16
Datagram Communication: Java API (4)
Manipulating sockets: Class DatagramSocket: represents a socket for sending and receiving datagram packets Constructors: DatagramSocket () throws SocketException DatagramSocket (int port) throws SocketException DatagramSocket(int port, InetAddress laddr) throws SocketException CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
17
Datagram Communication: Java API (5)
Manipulating sockets (continued): main methods of the class DatagramSocket void send(DatagramPacket data) throws IOException void receive(DatagramPacket data) throws IOException void setSoTimeout(int timeout) throws SocketException Enable/disable SO_TIMEOUT with the specified timeout. 0 = no timeout void close () int getLocalPort () int getSoTimeout() throws SocketException void connect (InetAddress address, int port) void disconnect() CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
18
Datagram Communication: Java API (6)
Example: Client sends a request import java.net.*; import java.io.*; public class UDPClient{ public static void main(String args[]) { // args[0] = message to be sent to the server; args[1] = IP address of the server 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, args[0].length(), aHost, serverPort); aSocket.send(request); CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
19
Datagram Communication: Java API (7)
Example (continued): Client waits for a reply 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();}}} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
20
Datagram Communication: Java API (8)
Example (continued): Server waits continually for a request 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); CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
21
Datagram Communication: Java API (9)
Example (continued): Server sends the response to the client 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();}}} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
22
TCP Stream Communication (1)
Use of input or output streams Characteristics: Message sizes (size) Lost messages: use of an acknowledgment scheme Flow control: TCP protocol attempts to match the speeds of the processes that read and write to a stream (how?) Message duplication and ordering: (How?) Message destinations: (how?) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
23
TCP Stream Communication (2)
Characteristics (continued): Matching of data items: processes need to agree as to the contents of the data transmitted over a stream (example) Blocking: (When?) Use of threads: (How?) Server: accepts a connection A thread is created for the new client CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
24
TCP Stream Communication (3)
Failure model: must satisfy Integrity property: (How?) Validity property: (How?) TCP limits: does not guarantee to deliver messages in the face of all possible difficulties CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
25
Stream Communication: Java API (1)
Client: Class Socket: used by a client to establish a connection with the server Constructors: creates a TCP socket by specifying the DNS hostname and a port of a server public Socket (String Host, int Port) throws UnknownHost… public Socket (InetAddress Host, int Port) throws Unknown… public Socket (InetAddress Host, int Port, InetAddress LocalAddress, int LocalPort) throws UnknownHost… … CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
26
Stream Communication: Java API (2)
Client (continued): main methods of class Socket close() throws IOException InetAddress getInetAddress () int getPort () InetAddress getLocalAddress () int getLocalPort () int getSoTimeout() throws SocketException void setSoTimeout(int timeout) throws SocketException InputStream getInputStream() throws IOException OutputStream getOutputStream() throws IOException … CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
27
Stream Communication: Java API (3)
Example 1: list all services of ports on a local machine arlequin{elhadef}102: java Ports1 Server attached to port 7 Server attached to port 9 Server attached to port 13 Server attached to port 19 Server attached to port 22 Server attached to port 23 Server attached to port 37 Server attached to port 79 Server attached to port 111 Server attached to port 512 Server attached to port 513 Server attached to port 514 Server attached to port 515 Execution results: import java.net.*; import java.io.*; public class Ports1 { public static void main(String[] args) { Socket s; String host = "localhost"; if(args.length > 0) host=args[0] ; for(int i = 0 ; i < 1024; i++) { try { s=new Socket(host,i); System.out.println("Server " + " attached to port "+ i); } catch (ConnectException e) { ; } catch (UnknownHostException e) { System.err.println(e+" --> "+i) ; } catch (IOException e) { System.err.println(e); }}}} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
28
Stream Communication: Java API (4)
Example 2: manipulating InetAddress addresses import java.net.*; import java.io.*; public class Ports2 { public static void main(String[] args) { Socket s; String host = " if(args.length > 0) hote=args[0] ; try { InetAddress address = InetAddress.getByName(host); for(int i = 1 ; i < 1024; i++) { try { s=new Socket(address,i); System.out.println("Server attached to port "+ i + " of " + address); } catch (ConnectException e) { ; } catch (IOException e) { System.err.println(e); }} } catch (UnknownHostException e) { System.err.println(e) ; }}} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
29
Stream Communication: Java API (4)
Example 2: manipulating InetAddress addresses arlequin{elhadef}120: java Ports2 Server attached to port 7 of Server attached to port 9 of Server attached to port 13 of Server attached to port 15 of Server attached to port 19 of Server attached to port 25 of Server attached to port 37 of Server attached to port 53 of Server attached to port 79 of Server attached to port 80 of Server attached to port 109 of Server attached to port 110 of Server attached to port 111 of Server attached to port 113 of Server attached to port 143 of Execution results CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
30
Stream Communication: Java API (5)
Example 3: manipulating socket’s attributes import java.net.*; import java.io.*; public class Attributes { public static void main(String[] args) { Socket s; for(int i = 0 ; i < args.length ; i++) { try { s=new Socket(args[i],80); System.out.println("\nConnect to "+ s.getInetAddress() + " on port " + s.getPort() + "\nfrom port "+ s.getLocalPort() + " of "+ s.getLocalAddress()) ; } catch (ConnectException e) { ; } catch (IOException e) { System.err.println(e); } } }} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
31
Stream Communication: Java API (5)
Example 3: manipulating socket’s attributes import java.net.*; import java.io.*; public class Attributes { public static void main(String[] args) { Socket s; for(int i = 0 ; i < args.length ; i++) { try { s=new Socket(args[i],80); System.out.println("\nConnect to "+ s.getInetAddress() + " on port " + s.getPort() + "\nfrom port "+ s.getLocalPort() + " of "+ s.getLocalAddress()) ; } catch (ConnectException e) { ; } catch (IOException e) { System.err.println(e); } } }} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
32
Stream Communication: Java API (6)
Manipulating data streams: Get I/O stream: functions public InputStream getInputStream() throws IOException publi OutputStream getOutputStream() throws IOException Manipulate streams with the following classes that offer many functionalities DataInputStream DataOutputStream CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
33
Stream Communication: Java API (7)
Example 4: read the date and the time from the Unix’ daytime server (port 13) on the host arlequin.dmi.usherb.ca import java.net.*; import java.io.*; public class DateAndTime { public static void main(String[] args) { Socket s; String host; DataInputStream timeStream; host="arlequin.dmi.usherb.ca"; try { InetAddress address = InetAddress.getByName(host); try { s=new Socket(address,13); timeStream = new DataInputStream(s.getInputStream()); String DateTime= timeStream.readLine(); System.out.println("Date and time on: "+host+" are "+DateTime); } catch (ConnectException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } } catch (UnknownHostException e) { System.err.println(e); } }} arlequin{elhadef}131: java DateAndTime.java Date and time on: arlequin.dmi.usherb.ca are Sun Sep 22 17:18: Execution results: CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
34
Stream Communication: Java API (8)
Server: Class ServerSocket: used by a server to establish a connection with a client Constructors: create a TCP socket at a server port for listening for connect requests from clients public ServerSocket(int port) throws IOException, … public ServerSocket(int port, int backlog) throws IOExcept… public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException, … port: the specified port, or 0 to use any free port bindAddr: the local InetAddress the server will bind to backlog: the maximum length of the queue Method used to establish the communication: accept a connection socket accept() throws IOException CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
35
Stream Communication: Java API (9)
Example 5: TCP Client connects to a server, sends a request and waits for a response import java.net.*; import java.io.*; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname 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.4 String data = in.readUTF(); // read a line of data from the stream System.out.println("Received: "+ data) ; }catch (UnknownHostException e) {System.out.println("Socket:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("readline:"+e.getMessage()); }finally {if(s!=null) try {s.close(); }catch (IOException e) {System.out.println ("close:" e.getMessage());}}}} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
36
Stream Communication: Java API (10)
Example 5 (continued): TCP Server establishes a connection with each client and displays its request import java.net.*; import java.io.*; public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; // the server port ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());} }} class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
37
Stream Communication: Java API (11)
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(); // read a line of data from the stream out.writeUTF(data); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("readline:"+e.getMessage()); } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}} }} CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
38
RMI (Remote Method Invocation) (1)
Distributed object model: allow objects at different processes to communicate with each other using calls to remote methods C must have a reference to object E so that it can invoke one of its methods B and F: distant objects CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
39
Before Studying RMI External Data Representation CORBA’s CDR
JAVA’s Object Serialization Remote Object Reference Remote Interface RMI (continued) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
40
External Data Representation and Marshalling (1)
Different representations: Floating-point numbers: different ways Different coding to represent characters Different formats: Little-endian, big-endian converted into sequences of bytes Receiving process Rebuild the data structures Sending process Information to be transmitted = data structures (set of interconnected objects) External data Representation CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
41
External Data Representation and Marshalling (2)
Form suitable for transmission into a message (binary, ASCII, e.g., HTTP) Data collection Carried out by a middleware layer Unmarshalling Different tools: CORBA’s Common Data Representation (CDR) JAVA’s Object Serialization SUN’s XDR Standard: exchange of messages between clients et servers SUN NFS (Network File System) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
42
CORBA’s CDR (1) Defined with CORBA 2.0 [Object Management Group, 1998]
Binary format: arguments and results of the invocations Can represent all data types: short, long, unsigned short, unsigned long, float, double, char, boolean, octet, any (tout type simple or composed : string, array, struct, enumerated, union, …) CDR (CORBA) & XDR (SUN): types not included, suppose that the sender and the receiver both know the order and the field types CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
43
CORBA’s CDR (2) Example:
{‘Smith’, ‘London’, 1934} struct Person { string name; string place; long year;}; unsigned long (32 bits) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
44
JAVA’s Object Serialization (1)
Java RMI: objects are passed as parameters or results to method invocations Binary format: messages are build from an object or hierarchy of objects Class ObjectOutputStream: writing the contents of the instance variables Class ObjectInputStream: reconstructing the original message Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
45
JAVA’s Object Serialization (2)
Example: Class Person Public class Person implements Serializable { private String name; private String place; private int year; public Person (String aName, String a Place, int aYear) { name = aName; place = aPlace; year = aYear; } … } Person p= new Person("Smith", "London", 1934); Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
46
JAVA’s Object Serialization (3)
Class ObjectOutputStream ObjectOutputStream(OutputStream) ObjectOutputStream(void) write(byte[]) write(int) writeByte(int) writeChar(int) writeFloat(float) writeLong(long) writeShort(int) writeUTF(String) writeBytes(String) writeObject(Object) writeDouble(double) writeBoolean(boolean) … Person p= new Person("Smith", "London", 1934); // Serialization ObjectOutputStream SP = new ObjectOutputStream(); SP.writeObject(p); // Deserialization ObjectInputStream DSP = new ObjectInputStream(); Person aPerson = DSP.readObject(); Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
47
Remote Object Reference
Client Process Invokes one method of a remote object Invocation message Server Process Remote Object Reference: remote object identifier valid in a distributed system Unicity: time Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Unique identifier of the process (local) incremented when the process creates an object Unicity: space CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
48
Remote Interface (1) Specify which method can be remotely invoked
Local object: can invoke all methods m1-m6 Remote object: can invoke only the methods m1-m3 published in the remote interface Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
49
Remote Interface (2) Example: CORBA’s Interface Definition Language (IDL) // 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(); }; Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
50
RMI (Remote Method Invocation) (2)
RMI invocation semantics: Fault tolerance measures Retransmit request message Duplicate filtering Re-execute procedure or retransmit reply Invocation semantics No Not applicable Maybe CORBA Yes No Re-execute procedure At-least-one SUN/RPC Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Yes retransmit reply At-most-one Java RMI, CORBA CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
51
RMI (Remote Method Invocation) (3)
Architecture: RMI Client RMI Server Stub Skeleton Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Remote Reference Layer (RRL) Remote Reference Layer (RRL) Transport Layer Transport Layer CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
52
RMI (Remote Method Invocation) (4)
Programming with RMI: steps to follow Define the interfaces of all distant classes Create and compile the implementations of these classes Create stub and skeleton classes: RMIC Create and compile a server application Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Start RMIREGISTRY and execute the server application Create and compile a client program that invokes the distant methods Start client application CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
53
RMI (Remote Method Invocation) (5)
Case study: A bank’s application capable of managing multiples accounts It’ll be placed on a distant machine, interrogated and manipulated by clients Define the interfaces of all distant classes Class CreditManager : search or create a bank account Class CreditCard: make purchases, set up signature, or get the account’s state Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
54
RMI (Remote Method Invocation) (6)
Class CreditManager: package credit; import credit.*; import java.rmi.*; public interface CreditManager extends Remote { public CreditCard findCreditAccount(String Customer) throws UnknownAccountException, RemoteException; public CreditCard newCreditAccount(String newCustomer) throws DuplicateAccountException, RemoteException; } Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
55
RMI (Remote Method Invocation) (7)
Class CreditCard: package credit; import credit.*; import java.rmi.*; public interface CreditCard extends Remote { public float getCreditLine() throws RemoteException; public void makePurchase(float amount, int signature) throws InvalidSignatureException, RemoteException, CreditLineExceededException; public void setSignature(int pin) throws RemoteException; } Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
56
RMI (Remote Method Invocation) (8)
Create and compile the implementations of these classes package credit; import java.rmi.*; import java.rmi.server.*; import java.util.Hashtable; public class CreditManagerImpl extends UnicastRemoteObject implements CreditManager { private static transient Hashtable accounts = new Hashtable(); public CreditManagerImpl() throws RemoteException { } … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
57
RMI (Remote Method Invocation) (9)
Create and compile the implementations of these classes (continued) public CreditCard newCreditAccount(String customerName) throws DuplicateAccountException, RemoteException { CreditCardImpl newCard = null; if (accounts.get(customerName) == null) newCard = new CreditCardImpl(customerName); else throw new DuplicateAccountException(); accounts.put(customerName, newCard); return newCard; } … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
58
RMI (Remote Method Invocation) (9)
Create and compile the implementations of these classes (continued) public CreditCard findCreditAccount(String customer) throws UnknownAccountException, RemoteException { CreditCardImpl account = (CreditCardImpl)accounts.get(customer); if (account == null) throw new UnknownAccountException(); else return account; } Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
59
RMI (Remote Method Invocation) (10)
Create and compile the implementations of these classes (continued) package credit; import java.rmi.*; import java.rmi.server.*; import java.io.Serializable; public class CreditCardImpl extends UnicastRemoteObject implements CreditCard, Serializable { private float creditLine = 5000f; private int signature = 0; private String accountName; … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
60
RMI (Remote Method Invocation) (11)
Create and compile the implementations of these classes (continued) public CreditCardImpl(String customer) throws RemoteException { accountName = customer; } public float getCreditLine() throws RemoteException { return creditLine; public void setSignature(int pin) throws RemoteException { signature = pin; … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
61
RMI (Remote Method Invocation) (12)
Create and compile the implementations of these classes (continued) public void makePurchase(float amount, int signature) throws InvalidSignatureException, RemoteException, CreditLineExceededException { if (signature != this.signature) throw new InvalidSignatureException(); if (amount > creditLine) throw new CreditLineExceededException(); else creditLine -= amount; } Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
62
RMI (Remote Method Invocation) (13)
Create and compile a server application RMIregistry (binder): maintains a table containing mapping from textual names to remote object references class Naming void rebind (String name, Remote obj) void bind (String name, Remote obj) void unbind (String name, Remote obj) Remote lookup(String name) String [] list() Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur //computerName:port/objectName Where to find RMIregistry CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
63
RMI (Remote Method Invocation) (13)
Create and compile a server application package credit; import java.util.*; import java.rmi.*; public class ServerBank { public static void main (String args[]) { System.setSecurityManager(new RMISecurityManager()); try { CreditManagerImpl cmi = new CreditManagerImpl(); Naming.rebind("cardManager", cmi); } catch (Exception e) { System.out.println(e.getMessage()); }}} Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
64
RMI (Remote Method Invocation) (14)
Create and compile a client program that invokes the distant methods package credit; import java.rmi.*; public class ClientBank { public static void main(String args[]) { CreditManager cm = null; CreditCard account = null; if (args.length < 3) { System.err.println("Usage : java ClientBank" + " <server> <name1> <name2>"); System.exit (1); } … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
65
RMI (Remote Method Invocation) (15)
Create and compile a client program that invokes the distant methods (continued) System.setSecurityManager(new RMISecurityManager()); try { String url = new String ("rmi://" + args[0] + "/cardManager"); cm = (CreditManager)Naming.lookup(url); } catch (Exception e) { System.out.println(“ Error while accessing the bank manager" + e); } account = cm.newCreditAccount(args[1]); System.out.println(“ Error during creation " + args[1] + " : " + e); } … Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
66
RMI (Remote Method Invocation) (16)
Create and compile a client program that invokes the distant methods (continued) try { System.out.println(" Avail credit : " + account.getCreditLine()); account.setSignature(1234); account.makePurchase(100.00f, 1234); System.out.println(" Avail. credit : " + account.getCreditLine()); account.makePurchase(160.00f, 1234); System.out.println(" Avail. credit : "+ account.getCreditLine()); } catch (Exception e) { System.out.println("Error " + args[1]);} account = cm.findCreditAccount(args[2]); } catch (Exception e) { System.out.println(“ Error while seeking for" + args[2] + e);}} Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
67
RMI (Remote Method Invocation) (17)
Start client application javac -d . -classpath . *.java rmic -d . -classpath . credit.CreditCardImpl credit.CreditManagerImpl CreditCardImpl_Skel.class CreditCardImpl_Stub.class CreditManagerImpl_Skel.class CreditManagerImpl_Stub.class start rmiregistry start java -Djava.security.policy=c:\BankRMI\permit.policy -Djava.rmi.server.codebase=file:///c:/BankRMI/ - classpath . credit.ServerBank Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur java -Djava.security.policy=c:\BankRMI\permit.policy -Djava.rmi.server.codebase=file:///c:/BankRMI/ -classpath . credit.ClientBank Pierre Marie CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
68
RMI (Remote Method Invocation) (17)
Start client application Execution results: Avail. credit: Setting up signature First purchase of 100 $ Avail. credit: Second purchase of 160 $ Avail. credit: Error while seeking for Marie credit.UnknownAccountException Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
69
Readings Tutorial: Getting Started Using RMI, Jim Waldo, Remote procedure calls and Java Remote Method Invocation, IEEE Concurrency, 6(3), pages 5-7, Sept 1998 A.D. Birrell and B.J. Nelson, Implementing Remote Procedure Calls (RPC), ACM Trans. on Computer Systems, 1984, pp 39-54 CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
70
Types of Interprocess Communication
Client-Server Communication Group Communication Readings CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
71
Client-Server Communication(1)
Synchronous communication: Client process is blocked until the response is received from the server Reliable: Server’s reply = acknowledgment Client Server Request getRequest doOperation (Wait) . Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Select object Execute method Reply sendReply . (continuation) Request-Reply Communication CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
72
Client-Server Communication(2)
Request-Reply message structure: messageType Int (0=Request, 1=Reply) Int : generated by doOperation and copied by the server (message number + address + port) requestId RemoteObjectRef : a reference to a remote object encapsulated in a packet objectReference methodId Int or Method arguments Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Array of bytes Internet Address Port Number Time Object Number Interface of remote object 32 bits 32 bits 32 bits 32 bits CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
73
Client-Server Communication(3)
Primitives: public byte [] doOperation (RemoteObjectRef o, int methodId, byte[] arguments); public byte[] getRequest(); public void sendReply(byte[] reply, InetAddress clientHost, int clientPort); Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
74
Client-Server Communication(4)
Failure model for the RR protocol: primitives doOperation, getRequest and sendReply suffer from Omission Faults Message delivery not guaranteed Solutions: Timeouts Enable servers to recognize two identical messages Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Lost reply messages: the server re-execute only idempotent operations History: save the results of the last executed request of each client CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
75
Client-Server Communication(5)
RPC exchange Protocols: Messages sent by Client Server R Request RR Request Reply RRA Request Reply Acknowledgment reply Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Depends on the application, one of these protocols will be used CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
76
Group Communication (1)
Multicast communication: sends one message to each member of the group Applications types: (examples) Multicast messages provide a useful infrastructure for distributed systems with the following characteristics: Fault tolerance based on replicated services Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Replicated data: data are replicated to increase the performance of service Propagation of event notifications (example) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
77
Group Communication (2)
IP multicast: IP multicast UDP In practice only UDP is used for multicast IP Packet Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur Group (details) CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
78
Group Communication (3)
API Java : class MulticastSocket MulticastSocket() MulticastSocket(int port) void joinGroup(InetAddress groupe) void leaveGroup(InetAddress groupe) synchronized void send(DatagramPacket dp, byte TTL) (TTL : definition) synchronized void receive(DatagramPacket dp) Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur byte getTTL() void setTTL(byte TTL) InetAddress getInterface() Retrieve the address of the network interface used for multicast packets setInterface(InetAddress nt) Set the outgoing network interface for multicast packets on this socket, to other than the system default CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
79
Group Communication (4)
Example : a member joins the group, sends and receives datagrams import java.net.*; import java.io.*; public class MulticastPeer{ public static void main(String args[]){ // args[0] = message to multicast to the group // args[1] = IP address of the group (e.g. " ") MulticastSocket s =null; try { InetAddress group = InetAddress.getByName(args[1]); s = new MulticastSocket(6789); s.joinGroup(group); Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
80
Group Communication (5)
Example (continued): byte [] m = args[0].getBytes(); DatagramPacket msgOut = new DatagramPacket(m, m.length, group, 6789); s.send(msgOut); byte[] buffer = new byte[1000]; // get messages from others in group for(int i=0; i< 3;i++) { DatagramPacket msgIn = new DatagramPacket(buffer, buffer.length); s.receive(msgIn); System.out.println("Rec. :" + new String(msgIn.getData())); } Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
81
Group Communication (6)
Example (suite): 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();} }} Processus émetteur peut s’exécuter dès que le message est copié dans le buffer local. La transmission du message se fait en parallèle avec l’exécution du processus expéditeur CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
82
Readings MBone and Multicast Informations, Ian Brown et al., Internet Multicast Tomorrow, Internet Protocol Journal, Vol 5(4), Dec CSI408-Distributed Information Systems © Mourad Elhadef, Ph. D.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.