Distributed System and Cloud Computing Lab Manual
ISBN 9788119221868

Highlights

Notes

  

Chapter 3: Remote Method Invocation

Description:

Remote Method Invocation (RMI) is an API which allows an object to invoke a method on an object that exists in another address space, which could be on the same machine or on a remote machine. Through RMI, object running in a JVM present on a computer (Client side) can invoke methods on an object present in another JVM (Server side). RMI creates a public remote server object that enables client and server side communications through simple method calls on the server object. RMI is used for building distributed application.

The RMI provides remote communication between the applications using two objects Stub and Skeleton.

Stubs:

The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object

Skeleton:

The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it.

RMI Working:

Two Intermediate objects are used for the communication between client and server [Stubs and Skeleton].

Steps to Implement RMI:

  • Step 1) Defining a Remote Interface.
  • Step 2) Implementing the Remote Interface.
  • Step 3) Creating Stub and Skeleton objects from the implementation class using rmic (rmi compiler).
  • Step 4) Start the rmiregistry using the command start rmiregistry.
  • Step 5) Create and execute the server application program.
  • Step 6) Create and execute the client application program.

Program 3.1: Retrieve time and date function from server to client. This program should display server date and time by implementing RMI.

Source Code:

Filename: MyInterface.java

  • import java.rmi.*;
  • public interface MyInterface extends Remote
  • {
  • public String getDate() throws RemoteException; public String getTime() throws RemoteException;
  • }

Filename: MyServer.java

import java.rmi.*;

import java.util.*;

import java.text.*;

import java.rmi.server.*;

public class MyServer extends UnicastRemoteObject implements MyInterface

{

  • MyServer() throws RemoteException
  • {
    • super();
  • }
  • public String getDate() throws RemoteException
  • {
    • return new SimpleDateFormat(“dd/mm/yyyy”).format(new Date()).toString();
  • }
  • public String getTime() throws RemoteException
  • {
    • return new SimpleDateFormat(“hh:mm:ss”).format(new Date()).toString();
  • }

}

Filename: Register.java

import java.rmi.*;

import java.rmi.registry.*; public class Register

{

  • public static void main(String[] args)
  • {
    • try {

Registry reg=LocateRegistry.createRegistry(2099); MyServer obj=new MyServer(); Naming.rebind(“rmi://localhost:2099/dt”,obj);

}

  • catch(Exception e) { }
  • }

}

Filename: MyClient.java

import java.rmi.*;

public class MyClient

{

public static void main(String[] args)

{

  • try
  • {
  • MyInterface obj=(MyInterface)Naming.lookup(“rmi://localhost:2099/dt”); System.out.println(“Date is:” +obj.getDate());
  • System.out.println(“Time is:” +obj.getTime());
  • }
  • catch(Exception e) {}
  • }

}

OUTPUT:

  • 1> Compile all 4 programs.
  • 2> Create stubs and skeleton object using following command and start rmiregistry
  • 3> Run Server Program.
  • 4> Run Client Program.

Program 3.2: The client should provide the values of a, b. The server will solve the equation c = (a+b)2 & c= (a+b)3 and will give back the value of c. If a = 2, b = 3 and then return value will be c = 25 and c = 125.

Source Code:

Filename: MyInterface.java

import java.rmi.*;

public interface MyInterface extends Remote

{

public int solveEq1(int a,int b) throws RemoteException; public int solveEq2(int a,int b) throws RemoteException;

}

Filename: MyServer.java

import java.rmi.*;

import java.rmi.server.*;

import java.math.*;

public class MyServer extends UnicastRemoteObject implements MyInterface

{

public MyServer()throws RemoteException

{

  • super();

}

public int solveEq1(int a,int b) throws RemoteException

{

  • int ans= (a*a)+(b*b)+(2*a*b); return ans;

}

public int solveEq2(int a,int b) throws RemoteException

{

  • int ans= (a*a*a)+(3*a*a*b)+(3*a*b*b)+(b*b*b); return ans;

}

}

Filename: Register.java

import java.rmi.*;

import java.rmi.registry.*; public class Register

{

public static void main(String[] args)

{

try {

Registry reg=LocateRegistry.createRegistry(2099); MyServer obj=new MyServer(); Naming.rebind(“rmi://localhost:2099/equ”,obj);

}

catch(Exception e) { }

}}

Filename: MyClient.java

import java.rmi.*;

import java.util.*;

public class MyClient

{

public static void main(String[] args)

{

try {

int num1,num2,num3,res1,res2;

MyInterface object=(MyInterface)Naming.lookup(“rmi://localhost:2099/eq”); Scanner in= new Scanner(System.in);

System.out.println(“Enter 1st number”); num1=in.nextInt(); System.out.println(“Enter 2st number”); num2=in.nextInt();

System.out.println(“(a+b)2 =” + object.solveEq1(num1,num2);); System.out.println(“(a+b)3 =” + object.solveEq2(num1,num2););

}

catch(Exception e) {}

}

}

OUTPUT:

  • 1> Compile all 4 programs.
  • 2> Create stubs and skeleton object using following command and start rmiregistry
  • 3> Run Server Program.
  • 4> Run Client Program

Program 3.3: Design a Graphical User Interface to find greatest of two numbers. Implement using RMI.

Source Code:

Filename: MyInterface.java

import java.rmi.*;

public interface MyInterface extends Remote

{

public int find(int a,int b) throws RemoteException;

}

Filename: MyServer.java

import java.rmi.*;

import java.rmi.server.*;

public class MyServer extends UnicastRemoteObject implements MyInterface

  • {
    • MyServer() throws RemoteException
    • {
      • super();
    • }
    • public int find(int a, int b) throws RemoteException
    • {
    • if(a>b)
    • return a;
    • else
    • return b;
    • }
  • }

Filename: Register.java

import java.rmi.*;

import java.rmi.registry.*; public class Register

{

  • public static void main(String[] args)
  • {
    • try
      • {
  • Registry reg=LocateRegistry.createRegistry(2099); MyServer obj=new MyServer(); Naming.rebind(“rmi://localhost:2099/g”,obj);
    • }
    • catch(Exception e)
    • {
      • System.out.println(e);
    • }

Filename: MyClient.java

import java.rmi.*;

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class MyClient extends JFrame implements ActionListener

{

JTextField tf1,tf2;

JButton btn;

JLabel lb,lb1,lb2;

MyClient()

{

  • tf1=new JTextField(10); tf2=new JTextField(10); lb=new JLabel(“”);
  • lb1=new JLabel(“Enter First Number”); lb2=new JLabel(“Enter Second Number”); btn=new JButton(“Submit”);
  • add(lb1);
  • add(tf1);
  • add(lb2);
  • add(tf2);
  • add(btn);
  • add(lb); btn.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

  • try
  • {
    • MyInterface obj=(MyInterface)Naming.lookup(“rmi://localhost:2099/g”); int a=Integer.parseInt(tf1.getText());
    • int b=Integer.parseInt(tf2.getText()); lb.setText(obj.find(a,b)+ “is greatest”);
    • }
    • catch(Exception e){}
  • }
  • public static void main(String args[])
  • {
    • MyClient c=new MyClient(); c.setLayout(new GridLayout(6,1)); c.setVisible(true); c.setSize(300,300);

OUTPUT:

  • 1> Compile all 4 programs.
  • 2> Create stubs and skeleton object using following command and start rmiregistry
  • 3> Run Server Program.
  • 4> Run Client Program

Exercise 1: The client should provide the values of a, b & c. The server will solve the equation (ax2 + bx + c = 0) and will give back the value of x. If a = 1, b = 5 and c = 6 then return value will be x = -2 or x = -3.

Exercise 2: Design a Graphical User Interface (GUI) based Basic calculator by