NETWORK PROGRAMMING CNET 441 CHAPTER 5 RMI and

  • Slides: 40
Download presentation
NETWORK PROGRAMMING CNET 441 CHAPTER - 5 RMI and IP Multicast 1

NETWORK PROGRAMMING CNET 441 CHAPTER - 5 RMI and IP Multicast 1

Chapter 5: Objectives After Completing the Chapter 5, the student can understand the following

Chapter 5: Objectives After Completing the Chapter 5, the student can understand the following concepts. • The Basic RMI (Remote Method Invocation) Process • RMI Implementation Details • RMI Compilation and Execution • RMI Security • IP Multicast • Multicast Addresses and Groups • Multicast Sockets • Communicating with a Multicast Group 2

RMI Introduction 1. In a distributed environment, it is often desirable to be able

RMI Introduction 1. In a distributed environment, it is often desirable to be able to invoke methods on remote objects (i. e. , on objects located on other systems). RMI ( Remote Method Invocation ) provides a platformindependent means of doing this. 2. Once a reference to the remote object has been obtained, the methods of that object may be invoked in exactly the same way as those of local objects. 3. RMI will be making use of byte streams to transfer data and method invocations. 3

Overview of an RMI Application 4

Overview of an RMI Application 4

RMI Client Server Model 5

RMI Client Server Model 5

RMI Process 6

RMI Process 6

RMI System 7

RMI System 7

Basic RMI Process 1. The server program that has control of the remote object

Basic RMI Process 1. The server program that has control of the remote object registers an interface with a naming service. 2. The interface contains the signatures for those methods of the object that the server wishes to make publicly available. 3. Stub. 4. Skeleton. 5. The client program invokes a method of the remote object. 6. An equivalent method is being called in the stub. 8

Basic RMI Process (cont. . ) 7. Marshalling. 8. Un. Marshalling. 9. Finally, the

Basic RMI Process (cont. . ) 7. Marshalling. 8. Un. Marshalling. 9. Finally, the skeleton calls the implementation of the method on the server. Note: Please refer the Text Book for further Details. 9

RMI Process 10

RMI Process 10

11

11

Implementation Details The packages used in the implementation of an RMI client– server application

Implementation Details The packages used in the implementation of an RMI client– server application are as follows. Ø java. rmi. server and Ø java. rmi. registry The basic steps are listed below. 1. Create the interface. 2. Define a class that implements this interface. 3. Create the server process. 4. Create the client process. NOTE: Please refer Text Book for further Details. 12

Compilation and Execution 1. Compile all files with javac. 2. Start the RMI registry.

Compilation and Execution 1. Compile all files with javac. 2. Start the RMI registry. 3. Open a new window and run the server 4. Open a third window and run the client. 13

Compilation and Execution (cont. . ) 1. Compile all files with javac. Ø javac

Compilation and Execution (cont. . ) 1. Compile all files with javac. Ø javac Hello. java Ø javac Hello. Impl. java Ø javac Hello. Server. java Ø javac Hello. Client. java 2. Start the RMI registry. Enter the following command: rmiregistry When this is executed, the only indication that anything has happened is a change in the command window’s title. 14

Compilation and Execution (cont. . ) 3. Open a new window and run the

Compilation and Execution (cont. . ) 3. Open a new window and run the server. From the new window, invoke the Java compiler: Ø java Hello. Server output is shown as below. 4. Open a third window and run the client. Again, invoke the Java compiler: java Hello. Client Output is as shown in the below figure. 15

Using RMI Meaningfully In a realistic RMI application, multiple methods and probably multiple objects

Using RMI Meaningfully In a realistic RMI application, multiple methods and probably multiple objects will be employed. With such real-world applications, there are two possible strategies that may be adopted, as described below. Ø Use a single instance of the implementation class to hold instance(s) of a class whose methods are to be called remotely. Pass instance(s) of the latter class as argument(s) of the constructor for the implementation class. Ø Use the implementation class directly for storing required data and methods, creating instances of this class, rather than using separate class(es). 16

RMI Security Ø The file java. policy define security restrictions. Ø The file java.

RMI Security Ø The file java. policy define security restrictions. Ø The file java. security defines the security properties. Ø Implementation of the security policy is controlled by an object of class RMISecurity. Manager (a subclass of Security. Manager ). Ø We must create our own security manager that extends RMISecurity. Manager. This security manager must provide a definition for method check. Permission , which takes a single argument of class Permission from package java. security. Example: Please refer the Text Book 17

Multicasting sends data from one host to many different hosts, but not to everyone;

Multicasting sends data from one host to many different hosts, but not to everyone; the data only goes to clients that have expressed an interest by joining a particular multicast group. 18

Use of Multicasting is Used in the following areas. 1. Audio and Video Applications.

Use of Multicasting is Used in the following areas. 1. Audio and Video Applications. 2. Multiplayer Games. 3. Distributed File systems. 4. Massively Parallel Computing. 5. Multi person Conferencing. 6. Database Replication. 7. Content Delivery Networks and more. 8. Multicasting can be used to implement name services and directory services that don’t require the client to know a server’s address in advance; 9. To look up a name, a host could multicast its request to some well -known address and wait until a response is received from the 19 nearest server.

How Multicast Works 20

How Multicast Works 20

How Multicasting Works? 1. Multicasting has been designed to fit into the Internet as

How Multicasting Works? 1. Multicasting has been designed to fit into the Internet as seamlessly as possible. 2. Most of the work is done by routers and should be transparent to application programmers. 3. An application simply sends datagram packets to a multicast address. 4. The routers make sure the packet is delivered to all the hosts in the multicast group. Problem with Multicasting: The biggest problem is that multicast routers are not yet ubiquitous; therefore, you need to know enough about them to find out whether multicasting is supported on your network. 21

Multicast Addresses 1. A multicast address is the shared address of a group of

Multicast Addresses 1. A multicast address is the shared address of a group of hosts called a multicast group. 2. IPv 4 multicast addresses are IP addresses in the CIDR (Classless inter-domain routing) group 224. 0. 0. 0/4 (i. e. , they range from 224. 0. 0. 0 to 239. 255). 3. IPv 6 multicast addresses are in the CIDR group ff 00: : /8 (i. e. , they all start with the byte 0 x. FF, or 1111 in binary). 4. Like any IP address, a multicast address can have a hostname. 22

Multicast Groups 1. A multicast group is a set of Internet hosts that share

Multicast Groups 1. A multicast group is a set of Internet hosts that share a multicast address. Any data sent to the multicast address is relayed to all the members of the group. 2. Membership in a multicast group is open; hosts can enter or leave the group at any time. 3. Groups can be either permanent or transient. 4. Permanent groups have assigned addresses that remain constant, whether or not there any members in the group. 23

Multicast Groups (cont. . ) 5. Most multicast groups are transient and exist only

Multicast Groups (cont. . ) 5. Most multicast groups are transient and exist only as long as they have members. 6. All you have to do to create a new multicast group is pick a random address from 225. 0. 0. 0 to 238. 255, construct an Inet. Address object for that address, and start sending it data. 24

Multicast Address Example 25

Multicast Address Example 25

Joining Multicast Groups 26

Joining Multicast Groups 26

Link Local Multicast Addresses 1. The IANA is responsible for handing out permanent multicast

Link Local Multicast Addresses 1. The IANA is responsible for handing out permanent multicast addresses as needed. 2. Link-local multicast addresses begin with 224. 0. 0 (i. e. , addresses from 224. 0. 0. 0 to 224. 0. 0. 255) and are reserved for routing protocols and other low-level activities, such as gateway discovery and group membership reporting. 3. For example, all-systems. mcast. net, 224. 0. 0. 1, is a multicast group that includes all systems on the local subnet. Multicast routers never forward datagrams with destinations in this range. 27

Some of the Link Local Multicast Addresses 28

Some of the Link Local Multicast Addresses 28

Some of the Common Permanent Multicast Addresses 29

Some of the Common Permanent Multicast Addresses 29

Multicast Sockets 30

Multicast Sockets 30

Multicast Sockets Ø The Fig 13 -3 shows one of the simplest possible multicast

Multicast Sockets Ø The Fig 13 -3 shows one of the simplest possible multicast configurations: a single server sending the same data to four clients served by the same router. Ø A multicast socket sends one stream of data over the Internet to the clients’ router; the router duplicates the stream and sends it to each of the clients. Ø Without multicast sockets, the server would have to send four separate but identical streams of data to the router, which would route each stream to a client. 31

Working with Multicast Sockets Ø In Java, you multicast data using the java. net.

Working with Multicast Sockets Ø In Java, you multicast data using the java. net. Multicast. Socket class, a subclass of java. net. Datagram. Socket. Ø public class Multicast. Socket extends Datagram. Socket implements Closeable, Auto. Closeable Ø You put your data in Datagram. Packet objects that you send and receive with the Multicast. Socket. Ø To receive data that is being multicast from a remote site, first create a Multicast. Socket with the Multicast. Socket() constructor. Example: Multicast. Socket ms = new Multicast. Socket(2300); 32

Working with Multicast Sockets (cont. . ) Ø Join a multicast group using the

Working with Multicast Sockets (cont. . ) Ø Join a multicast group using the Multicast. Socket’s join. Group() method. Example: Inet. Address group = Inet. Address. get. By. Name("224. 2. 2. 2"); ms. join. Group(group); Ø You create a Datagram. Packet with a byte array that serves as a buffer for data and enter a loop in which you receive the data by calling the receive() method inherited from the Datagram. Socket class. Example: byte[] buffer = new byte[8192]; Datagram. Packet dp = new Datagram. Packet(buffer, buffer. length); ms. receive(dp); Ø When you no longer want to receive data, leave the multicast group by invoking the socket’s leave. Group() method. 33

Ø You can then close the socket with the close() method inherited from Datagram.

Ø You can then close the socket with the close() method inherited from Datagram. Socket. Example: ms. leave. Group(group); ms. close(); Ø Sending data to a multicast address is similar to sending UDP data to a unicast address. You do not need to join a multicast group to send data to it. You create a new Datagram Packet, stuff the data and the address of the multicast group into the packet, and pass it to the send() method. Example: Inet. Address ia = Inet. Address. get. By. Name("experiment. mcast. net"); byte[] data = "Here's some multicast datarn". get. Bytes("UTF-8"); int port = 4000; Datagram. Packet dp = new Datagram. Packet(data, data. length, ia, port); Multicast. Socket ms = new Multicast. Socket(); ms. send(dp); 34

Constructors - Multicast Socket 1. public Multicast. Socket() throws Socket. Exception 2. public Multicast.

Constructors - Multicast Socket 1. public Multicast. Socket() throws Socket. Exception 2. public Multicast. Socket(int port) throws Socket. Exception 3. public Multicast. Socket(Socket. Address bind. Address) throws IOException Note: All three constructors throw a Socket. Exception if the Socket can’t be created. 35

Communication with a Multicast Group 36

Communication with a Multicast Group 36

Communicating with a Multicast Group Once a Multicast Socket has been created, it can

Communicating with a Multicast Group Once a Multicast Socket has been created, it can perform four key operations: 1. Join a multicast group. 2. Send data to the members of the group. 3. Receive data from the group. 4. Leave the multicast group. 37

Joining Groups 1. To join a group, pass an Inet. Address or a Socket.

Joining Groups 1. To join a group, pass an Inet. Address or a Socket. Address for the multicast group to the join. Group() method: Ø public void join. Group(Inet. Address address) throws IOException Ø public void join. Group(Socket. Address address, Network. Interface interface) throws IOException 2. A single Multicast Socket can join multiple multicast groups. Information about membership in multicast groups is stored in multicast routers, not in the object. 38

Leaving groups and Sending Multicast Data Call the leave. Group() method when you no

Leaving groups and Sending Multicast Data Call the leave. Group() method when you no longer want to receive datagrams from the specified multicast group, on either all or a specified network interface. Ø public void leave. Group(Inet. Address address) throws IOException Ø public void leave. Group(Socket. Address multicast. Address, Network. Interface interface) throws IOException Sending data with a Multicast. Socket is similar to sending data with a Datagram. Socket. Stuff your data into a Datagram. Packet object and send it off using the send() method. The data is sent to every host that belongs to the multicast group to which the packet is addressed. 39

Loopback mode Passing true to set. Loopback() indicates you don’t want to receive the

Loopback mode Passing true to set. Loopback() indicates you don’t want to receive the packets you send. Passing false indicates you do want to receive the packets you send. Ø public void set. Loopback. Mode(boolean disable) throws Socket. Exception Ø public boolean get. Loopback. Mode() throws Socket. Exception The get. Loopback Mode() method returns true if packets are not looped back and false if they are. 40