830 likes | 1.07k Views
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. Themes. 1st Part:
E N D
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
Themes • 1st Part: Means of Interprocess Communication (RPC, RMI, …) • 2nd Part: Types of Interprocess Communication (Client-Sever, Group) CSI408-Distributed Information Systems
Means of Interprocess Communication • Introduction • Layered Protocols • RMI (Remote Method Invocation) • Readings CSI408-Distributed Information Systems
RMI, RPC and events Middleware layers Applications, services UDP and TCP Request-Reply Protocol Marshalling and external data representation Operating System Introduction (1) CSI408-Distributed Information Systems
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
Introduction (3) RPC/IDL : program RAND_PROG { version RAND_VERS { void INITIALIZE_RANDOM(long) = 1; double GET_NEXT_RANDOM(void) = 2; } = 1; } = 0x31111111; RPC/IDL: program RAND_PROG { version RAND_VERS { void INITIALIZE_RANDOM(long) = 1; double GET_NEXT_RANDOM(void) = 2; } = 1; } = 0x31111111; 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); }; }; 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; }; Definition of remote procedure using different IDL CSI408-Distributed Information Systems
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-blockingor blockingoperation CSI408-Distributed Information Systems
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
216 possible ports 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 CSI408-Distributed Information Systems
Internetwork layers (Internet) Layered Protocols (1) • Multiple layers intervene during interprocess communication CSI408-Distributed Information Systems
Layered Protocols (2) • Types of communication: • UDP DatagramCommunication • TCP StreamCommunication CSI408-Distributed Information Systems
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
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
Message’s bytes Message’s length Internet Address Port Number Data packet Length of data packet Destination address UDP Port 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 Datagram packet • Constructors: • Receiver: DatagramPacket(byte buffer[], int length) buffer for holding the incoming datagram Number of bytes to read • Sender: DatagramPacket( byte buffer[], int length, InetAddress address, int port) CSI408-Distributed Information Systems
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
Datagram Communication: Java API (4) • Manipulating sockets: • ClassDatagramSocket: represents a socketfor 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
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
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
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
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
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
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
Server: accepts a connection A thread is created for the new client 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?) CSI408-Distributed Information Systems
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
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
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
Execution results: 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 Stream Communication: Java API (3) • Example 1: list all services of ports 0-1023 on a local machine 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
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 = "www.dmi.usherb.ca"; 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
arlequin{elhadef}120: java Ports2 Server attached to port 7 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 9 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 13 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 15 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 19 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 25 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 37 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 53 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 79 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 80 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 109 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 110 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 111 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 113 of www.dmi.usherb.ca/132.210.40.10 Server attached to port 143 of www.dmi.usherb.ca/132.210.40.10 Execution results Stream Communication: Java API (4) • Example 2: manipulating InetAddress addresses CSI408-Distributed Information Systems
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
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
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 Stream Communication: Java API (6) • Manipulating data streams: CSI408-Distributed Information Systems
Execution results: arlequin{elhadef}131: java DateAndTime.java Date and time on: arlequin.dmi.usherb.ca are Sun Sep 22 17:18:23 2002 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); } }} CSI408-Distributed Information Systems
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
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
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
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
C must have a reference to object E so that it can invoke one of its methods B and F: distant objects RMI (Remote Method Invocation)(1) • Distributed object model: allow objects at different processes to communicate with each other using calls to remote methods CSI408-Distributed Information Systems
Before Studying RMI • External Data Representation • CORBA’s CDR • JAVA’s Object Serialization • Remote Object Reference • Remote Interface • RMI (continued) CSI408-Distributed Information Systems
Sending process Information to be transmitted = data structures (set of interconnected objects) converted into sequences of bytes Receiving process Rebuild the data structures External data Representation External Data Representation and Marshalling (1) • Different representations: • Floating-point numbers: different ways • Different coding to represent characters • Different formats: Little-endian, big-endian CSI408-Distributed Information Systems
Marshalling Unmarshalling 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 • 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
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
unsigned long (32 bits) CORBA’s CDR (2) • Example: {‘Smith’, ‘London’, 1934} struct Person { string name; string place; long year;}; CSI408-Distributed Information Systems
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 CSI408-Distributed Information Systems
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); CSI408-Distributed Information Systems
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(); CSI408-Distributed Information Systems
Unicity: time Unique identifier of the process (local) incremented when the process creates an object Unicity: space 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 CSI408-Distributed Information Systems
Remote object: can invoke only the methods m1-m3 published in the remote interface Local object: can invokeall methods m1-m6 Remote Interface (1) • Specify which method can be remotely invoked CSI408-Distributed Information Systems
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(); }; CSI408-Distributed Information Systems
RMI (Remote Method Invocation)(2) • RMI invocation semantics: Fault tolerance measures Invocation semantics CSI408-Distributed Information Systems