0% found this document useful (0 votes)
5 views26 pages

Network Programming - Applications

The document provides an overview of Remote Method Invocation (RMI) in Java, explaining its role in enabling remote method calls across different address spaces. It details the architecture, key programming elements, and the process of building and running an RMI system, including the creation of remote interfaces, server and client implementations, and the use of stubs and skeletons. Additionally, it compares RMI with traditional socket programming and RPC, highlighting RMI's advantages in object-oriented programming and dynamic class loading.

Uploaded by

jwswafula
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

Network Programming - Applications

The document provides an overview of Remote Method Invocation (RMI) in Java, explaining its role in enabling remote method calls across different address spaces. It details the architecture, key programming elements, and the process of building and running an RMI system, including the creation of remote interfaces, server and client implementations, and the use of stubs and skeletons. Additionally, it compares RMI with traditional socket programming and RPC, highlighting RMI's advantages in object-oriented programming and dynamic class loading.

Uploaded by

jwswafula
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CN 622: NETWORK PROGRAMMING AND APPLICATIONS

Lecture 4: Overview of RMI

5/15/2023 CN 622 1
What is Remote Method Invocation (RMI)?
• A way to invoke methods from a different address space, typically
remotely but also locally.
• A true distributed computing application interface for Java, written
to provide easy access to objects existing on remote virtual
machines
• Helps provide access to objects existing on remote virtual machines

5/15/2023 CN 622 2
Remote Method Invocation
• Java RMI is a mechanism that allows a Java program running on one computer to
apply a method to an object living on a different computer.
• RMI is an implementation of the Distributed Object programming model—similar to
CORBA, but simpler and specialized to the Java language.
• The syntax of the remote method invocation looks like an ordinary Java method
invocation.
• The remote method call can be passed arguments computed in the context of the local
machine. It can return arbitrary values computed in the context of the remote machine.
The RMI runtime system transparently communicates all data required.
• In some ways Java RMI is more general than CORBA—it can exploit Java features like object
serialization and dynamic class loading to provide more complete object-oriented
semantics.

5/15/2023 CN 622 3
Two kinds of classes
• Two types of objects are useful when dealing with RMI.
• Remote object
• Serializable object

5/15/2023 CN 622 4
Remote object
• A remote object is one whose instances can be used remotely. A handle to a
remote object identifies where that object is located and how to contact it
remotely (via rmi).
• When used locally, it works like any other object.
• When it is passed as a parameter, its handle is passed (as with ordinary java
objects).

5/15/2023 CN 622 5
Serializable object
• A serializable object is one whose value can be marshaled.
• This means that the contents of the object can be represented in a pointerless
form as a bunch of bytes and then reconstructed into their respective values as
needed.
• This is useful, for example, in saving the contents of an object into a file and then
reading back that object.
• In the RMI case, it is useful if we want to pass an object as a parameter to a
remote method or receive a result from a remote method. In this case, we don’t
want to pass object handles, because the pointers will make no sense on a
different JVM. If an object is defined to implement [Link], however,
passing it as a parameter will allow us to pass the value of the object (marshaled)
rather than the handle.

5/15/2023 CN 622 6
Remote classes
• There are two parts to defining a remote class: its interface and the actual class itself.
• The Remote Interface represents the type of an object handle and tells clients how to invoke
remote methods.
• The remote class defines those objects.
• The interface definition:
- must be public
- must extend the interface [Link]
- every method in the interface must declare that it thows [Link] (but other exceptions may
be thrown as well)
• The Remote class:
- must implement a Remote interface
- should extend [Link] class
• Objects of this class exist in the address space of the server and can be invoked remotely. (There are other
ways of defining a remote class - this is the easiest)
• Objects in the remote class may have methods that are not in its remote interface.
• However, these can only be invoked locally.
5/15/2023 CN 622 7
Java RMI Architecture
• Basic components
• Client
– Invokes a remote method on a remote object
• Server
– Owns the remote objects and implements the remote methods
• Registry
– Relates remote objects with names in plaintext

5/15/2023 CN 622 8
Java RMI in the TCP/IP Stack
• Middleware between transport and application layer
• Seamless to the programmer (remote object is handled as local
object)
• Runs over TCP (reliable communication)
• Stub
– Pretends to be the remote object
• Skeleton
– Handles requests from stub / Talks to real remote object

5/15/2023 CN 622 9
Three-layer architecture

5/15/2023 CN 622 10
Three-layer architecture …
• Stub and Skeleton layer
✓Intercepts method calls made by the client to the interface reference variable and redirects
these calls to a remote RMI service
• Remote Reference Layer
✓Interpret and manage references made from clients to the remote service objects
• Transport layer
✓Is based on TCP/IP connections between machines in a network
✓Provides basic connectivity, as well as some firewall penetration strategies

5/15/2023 CN 622 11
Key Programming Elements
Classes, Interfaces & Methods
• [Link]
– Needs to be extended by the classes that contain RMI methods
• [Link]
– Associates a name to a remote object
– Key methods:
• bind(string,Remote)
• lookup(string)
• Static Methods
– [Link]([string],[int])
• Static method to get the registry
– [Link](Remote,[int])
• Exports the remote object to JRE to receive remote calls

5/15/2023 CN 622 12
Building a Java RMI system
An RMI system must be composed of the following parts:
1. An interface definition of the remote services;

2. The implementations of the remote services;


3. A server to host the remote services;
4. An RMI Naming service
5. A client program that uses the remote services.

5/15/2023 CN 622 13
Create the Interface Definition
• First thing: define the interface
• Interface defines what remote methods and variables are going to be
exported from the remote object.
• Remote interface must adhere to certain limitations:
• must be public
• must import the [Link].* package
• must extend the [Link] interface
• all exported methods must throw an RMI remote exception to manage errors during
invocation
• all references to remote objects must be references to the interface (not to the
remote object itself)

5/15/2023 CN 622 14
Implement the Interface Definition
• Next, implement the remote interface
• Limitations:
• must implement at least one remote interface
• must import the [Link].* package
• must extend [Link]
• must install a security manager
• must create at least one instance of a remote object (for instance itself)
• must register at least one of the remote objects with the RMI remote object registry

5/15/2023 CN 622 15
Creating the Stubs/Skeletons
• Stubs and skeleton code generated by using the rmic compiler
• rmic compiler creates stub and skeleton classes
• *_Stub and *_Skel classes are where the references to the remote objects
will resolve to in the client's address space
• RRL will manage the mapping of these objects to the server's address space

5/15/2023 CN 622 16
Client
• Must import [Link] package and [Link]
• Client must register with a security manager
• RMI Security Manager
• Constitutes the “sandbox” where Java applets reside
• Loading of classes
• Classes loaded from the network cannot be trusted
• If no security manager exists, only classes from the local file system will be loaded
• After registering the security manager
• create a URL string that is comprised of the server name and remote object name you are requesting
• rmi://[Link]/myServer
• “myServer” is the remote object
• enables the client to look up the remote object on the server via the rmiregistry
• Once the remote reference is made
• Client can invoke remote methods on the remote object
• The remote object is then treated as if it were a local object

5/15/2023 CN 622 17
Server
• Has the same requirements as the client regarding the security manager
• Once the server has registered with the security manager, it must create an
instance of the remote object it wants to export
• RMIRegistry
• Must be running on the server
• Objects registered through this
• Clients are given access to remote objects through this
• Since the server uses the rmiregistry, you must bind (i.e., alias) an instance of the
object with the name that will be used to look up the object

5/15/2023 CN 622 18
RMI versus RPC
• It differs from RPC:
+ RMI is not language/machine independent
+ RMI supports the concept of classes with methods within a class
+ RMI supports polymorphism
• RPC’s Support Procedural Programming Style
• RMI Supports Object-Oriented Programming Style
• Parameters to RPCs are Ordinary Data Structures
• Parameters to RMI are Objects

5/15/2023 CN 622 19
Socket Versus RMI Programming
Socket Programming RMI programming

Socket programming provides communication mechanism RMI programming provides mechanism to invoke method from
between the client and the server. a JVM that is executed on a remote JVM.
Socket programming is categorized as connection oriented Java programs that provide the transport layer to establish and
socket programming and connection less socket programming maintain connection between the client and the server invoke
based on the transport protocols such as TCP/IP and UDP used RMI programming.
in programming.
The main method on the server class object opens a port to The method calls from various clients initiate the connection
establish the connection. between the client and the server.

It does not require rmiregistry on the system to execute. It requires rmiregistry on the system to execute.

Socket programming can be accomplished in two modes, client RMI invokes the method call on the remote JVM.
mode and the server mode.

Socket programming cannot dynamically load new classes. RMI programming can load new classes dynamically.

Socket programming does not allow sharing of resources and RMI can be used to share resources and distributed load
distributing loads across processors. among various processors.
5/15/2023 CN 622 20
RMI Example
• [Link]
Remote interface for adding two integers
• [Link]
Remote object class
• [Link]
Server hosting an Adder
• [Link]
Client accessing the adding function

5/15/2023 CN 622 21
[Link]
import [Link].*;
public interface AddServerIntf extends Remote {
int add(int x, int y) throws RemoteException;
}

5/15/2023 CN 622 22
[Link]
import [Link].*;
import [Link].*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{
public AddServerImpl() throws RemoteException {}
public int add(int x, int y) throws RemoteException {
return x+y;
}
}

5/15/2023 CN 622 23
[Link]
import [Link].*;
public class AddServer {
public static void main(String[] args) {
try{
AddServerImpl server = new AddServerImpl();
[Link]("registerme",server);
[Link]("Server is running...");
} catch (Exception e) {
[Link](e);
}
}
}

5/15/2023 CN 622 24
[Link]
import [Link].*;
public class AddClient {
public static void main(String[] args) {
try{
AddServerIntf client =
(AddServerIntf)[Link]("registerme");
[Link]("First number is :" + args[0]);
int x = [Link](args[0]);
[Link]("Second number is :" + args[1]);
int y = [Link](args[1]);
[Link]("Sum =" + [Link](x,y));
} catch (Exception e){
[Link](e);
}
}
5/15/2023 CN 622 25
}
Compiling and Running RMI
• Compile the program files
• Using *.java instructs the compiler (javac) to compile all files with the .java extension. Alternatively, may compile
the files individually by specifying the file name.
c:\Projects\Eclipse\Adder>javac *.java
• Create the Stub and Skeleton
• Next, you need to create the stubs and skeleton files for the compiled Adder implementation ([Link]).
This is done by using the rmic command
c:\Projects\Eclipse\Adder>rmic AdderImpl
• Start the RMI registry
• Now , start the RMI registry, like this:
c:\Projects\Eclipse\Adder>start rmiregistry
• Start the server
c:\Projects\Eclipse\Adder>java AdderServer
• Run the Client
c:\Projects\Eclipse\Adder>java AdderClient

5/15/2023 CN 622 26

You might also like