Assignment No 07
AIM:
Implement Chatting between two users using RMI. (RMI and AWT)
THEORY:
Need For Distributed Application
The traditional applications are monolithic in nature. If we need to make any extensions or enhancements in this application, we need to recompile and integrate the whole application because each function depends on another. In distributed application the application is divided into parts called components or objects. We can make changes to a particular object without affecting other objects. The IP Address and interface is used to identify the object. ( Fig 1.1)
The ORB ( Object request broker) is not required in RMI applications which reduces the cost. ( Fig 1.2)
Advantages:
- Component Reuse
- Less time & resources.
- Less Complexity
- Less Storage Space
- Replication of object is avoided
Ex- Spell Checker
Alternatives to RMI:
(only JVM to JVM Communication )
Sockets:
Socket is the channel through which application can connect & communicate with each other. ( Between 2 processes in the same machine). Developer have to write data to the socket and read from the socket.( socket programming). API is low level & not suitable for complex applications.
RPC ( Remote Procedure Call)
RPC is function-oriented interface & used along with socket.
CORBA ( Common Object Request Broker Architecture )
The corba uses IDL ( interface definition language) which specifies the interface between corba objects. Corba is language independent.
DCOM ( Distributed Component object model)
Whenever a component is invoked, its reference count is incremented by 1 and whenever the client has finished using the component, the reference count decrements by 1. If the reference count reaches 0, the component is discarded from memory and the memory is freed. A ping message is sent at regular intervals from the client to the server. If the client crashes the server will not receive the ping message. If 3 pings are not received continuously, the server assumes that the client has crashed and frees all the resources associated with it. It is language independent but works only in win environments.
About RMI
Remote Method Invocation (RMI) is a part of Java Development Kit. It allows us to develop distributed applications. The RMI is a platform independent because Java is platform independent. The RMI can communicate only from one java Virtual Machine to another. So the user need not learn any other language to develop the distributed applications by using RMI.
In RMI the application is divided into objects. The objects communicate with each other through an interface. This interface is used to access the remote object and its methods. To develop the distributed application using RMI the following steps are followed.
1) Define the interface. ( interface must extend java.rmi.remote )
2) Implementing the interface
3) Compile the interfaces and their implementation with the java compiler
4) Compile the server implementation with RMI compiler
5) Run the RMI registry
6) Run the application.
The server needs to create and register objects in the local RMI registry. The server should provide an implementation for each remote method defined in the interface. In RMI , the arguments are passed by reference if they are remote objects and passed by value if they are local. Each remote method must throw the RemoteException.
The client searches for the server name in the registry by using the naming.lookup( ) method. The important packages are java.rmi, java.rmi.registry, java.rmi.server .
There are many ways of developing the distributed application like Sockets, remote Procedural Call, Distributed Component Object Model (DCOM), Common Object Request Broker Architecture (CORBA) and RMI.
Advantages of distributed applications:
1) Component Reuse
2) Less time & resources
3) Less complexity
4) Less storage space
RMI architecture:
RMI architecture consists of four layers and each layer can perform specific functions
1) Application Layer: It has contained the actual object definition.
2) Proxy Layer: It consists of two parts namely Stub and Skeleton. These are used for marshalling and unmarshalling the data that is transferred through the network. Marshalling is the process by which we can convert the Java byte codes into a stream of bytes, and unmarshalling is the reverse process of it. Stub is a proxy for the server. It is placed on the client side of the application while the skeleton is placed on the server side.
3) Remote reference Layer: It gets the stream of bytes from the transport layer and sends it to the proxy layer.
4) Transport layer: This layer is responsible for handling the actual machine-to-machine communication.
RMI registry is a simple server, it keeps track of the remote object name that are exported by the servers. Registry should run as a background process when running the application. It can add/remove the object to and from the registry. In a distributed systems, it is desirable to atomically delete those remote objects that are no longer referenced by any client . this is called garbage collections.
Remote Objects:
Remote objects are passed by reference. A remote object reference is a stub, which is a client side proxy. Passing an object by reference mans that any changes made to the state of the object by remote method are reflected in the original remote object. Local Objects are passed by value using object serialization,.
rmic
option Meaning
-d Specifies the directory
-keep Do not delete the intermediate class files
-keepgenerated Do not delete the intermediate class files
-v1.1 Create stub/skeleton for jdk1.1 stub protocol version
-v1.2 Create stub/skeleton for jdk1.2 stub protocol version
-vcompat Create stub/skeleton for jdk1.2 & jdk1.2 version
-verbose shows the time required for rmi compilation
RMI Flow:
Server creates the remote object
- Server registers the remote object with rmi registry
- Client makes a request for the object to rmi registry
- Rmi registry returns the remote object to the stub
- Client invokes stub method
- Stub passes the request to the skeleton
- Skeleton invokes the remote objects methods.
CONCLUSION
Chatting using RMI is successfully implemented.
Leave a Reply