Interprocess Communication This chapter Concerns with the characteristics of protocols for communication btw processes and distributed system Java API provides both datagram and stream communication Protocols for the representation of collections of data objects in messages Construction of protocols to support the two communication patterns Client-server communication Group communication (multicast) Middleware layers Applic ations , s ervic es RMI and RPC T his c hapter reques t-reply protocol marshall ing and external data repres entation UDP and T CP Middleware layers API for the Internet protocols Characteristics of inter-process communication Synchronous communication In synchronous form of communication, the sending and receiving process synchronize at every message. Asynchronous communication In asynchronous form of communication, sending operation in non-blocking means sending process is allowed to proceed as soon as the message has been copied to local buffer. Receive operation can have 2 variant, like: Blocking and Non-blocking API for the Internet protocols (cont.) Message destination: if the client uses a fixed Internet address to refer to a service, then that service must always run on the same machine for its address to remain valid. It could be avoided by using name a name servers which will translate name into server location Mach Operating System provides location independent identifiers for message destinations Reliability: messages must arrive uncorrupted and without duplication Ordering: some applications require that messages should be delivered in sender order Sockets Both forms of communication uses socket abstraction socket any port agreed port socket message client server other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 Java API for Internet addresses inetAddress acomputer = InetAddress.getByName(“www.cnn.com”); UDP Datagram Communication Some issues relating to datagram communication: Message size: receiving process specify an array of bytes to receive message. If size of the message is bigger than the array, then message is truncated . Blocking: UDP datagram communication uses non-blocking sends and blocking receives. Timeouts Failure model Omission failures Ordering Use of UDP Java API for UDP datagram UDP client sends a message and get a reply 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();} } } Java API for UDP datagram cont… Java API provides datagram communication by means of 2 classes: DatagramPacket and DatagramSocket DatagramPacket: It Provides 2 constructors Firstly: a constructor makes an instance out of an array of bytes comprising a message, the length of the message and the Internet address and local port number of the destination socket. Secondly: a constructor for use when receiving a message. Its argument specify an array of bytes in which to receive the message and the length of the array. DatagramPacket provides getData, getPort and getAddress methods Java API for UDP datagram cont… DatagramSocket : it supports sockets for sending and receiving UDP datagrams. It provides constructor that takes a port number as argument to use particular port. It also provides a non-argument constructor that allows the system to choose a free local port. DatagramSocket provides the following methods: Send and Receive: the argument of send is an instance of DatagramPacket containing a message and destination; the argument of receive is an empty DatagramPacket in which to put the message. setSoTimeout: allows to set the timeout connect: used for connecting it to a particular remote port and Internet address 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();} } } TCP stream communication Following characteristics of the network are hidden by the stream abstraction: Message size Lost message Flow control Message duplication and ordering Message destinations Some issues related to stream communication: Matching of data items Blocking Threads Java API for TCP streams 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());}} } } Java API for TCP streams cont… Java interface to TCP streams is provided in the classes ServerSocket and Socket ServerSocket: this class is intended for use by server to create a socket at a server port for listening for connect requests. Its accept method gets a connect request from the queue, if the queue is empty, it blocks until one arrives. Socket: this class is for use by a pair of processes with a connection. The client uses a constructor to create a socket, specifying the DNS hostname and port of a server. This constructor not only creates a socket associated with the local port but also connects it to the specified remote computer and port number. External data representation and marshalling The information stored in running programs is represented as data structures – for example by set of interconnected objects – whereas the information in messages consists of sequences of bytes. One of the following methods can be used to enable any two computers to exchange data values: The values are converted to an agreed external format before transmission and converted to local form on receipt. The values are transmitted in the sender’s format, together with an indication of the format used, and the recipient converts the value if necessary. External data representation and marshalling (cont.) Marshalling: is the process of taking collection of data items and assembling them into a form suitable for transmission in a message. Unmarshalling: is the process of disassembling data items on arrival to produce an equivalent collection of data items. Two alternative approaches to external data representation and marshalling are: CORBA’s (common object oriented broker architecture) common data representation (CDR); used in many programming languages Java’s object serialization, used in Java only. CORBA’s CDR (Common Data Representation) index in sequence of bytes 0–3 4–7 8–11 12–15 16–19 20-23 24–27 4 bytes 5 "Smit" "h___" 6 "Lond" "on__" 1934 notes on representation length of string ‘Smith’ length of string ‘London’ unsigned long The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1934} Java object serialization Explanation Serialized values Person 8-byte version number h0 class name, version number 3 int year java.lang.String java.lang.String number, type and name of name: place: instance variables 1934 5 Smith 6 London h0 and h1 are handles h1 values of instance variables