html5-img
1 / 17

Interprocess Communication

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

mirit
Download Presentation

Interprocess Communication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. Middleware layers

  3. 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

  4. 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

  5. agreed port any port socket socket message client server other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 Sockets • Both forms of communication uses socket abstraction • Java API for Internet addresses inetAddress acomputer = InetAddress.getByName(“www.cnn.com”);

  6. 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

  7. Java API for UDP datagramUDP 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();} } }

  8. 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

  9. 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

  10. 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();} } }

  11. 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

  12. Java API for TCP streamsTCP 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());}} } }

  13. 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.

  14. 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.

  15. 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.

  16. notes index in on representation sequence of bytes 4 bytes length of string 5 0–3 "Smit" 4–7 ‘Smith’ "h___" 8–11 12–15 6 length of string "Lond" 16–19 ‘London’ "on__" 20-23 1934 24–27 unsigned long The flattened form represents a Person struct with value: {‘Smith’, ‘London’, 1934} CORBA’s CDR (Common Data Representation)

  17. Explanation Serialized values Person 8-byte version number h0 class name, version number java.lang.String java.lang.String number, type and name of int year 3 name: place: instance variables 1934 5 Smith 6 London h1 values of instance variables h0 and h1 are handles Java object serialization

More Related