Lab 15 - Remote Method Invocation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Lab Manual for Advance Computer Programming

Lab-15
Remote Method Invocation
Lab 15: Remote Method Invocation

Table of Contents
1. Introduction 154

2. Activity Time boxing 155

3. Objective of the experiment 155

4. Concept Map 155


4.1 Distributed Computing and RMI 155

5. Homework before Lab 156


5.1 Problem Solution Modeling 156
5.1.1 Problem description: 156
5.2 Practices from home 156
5.2.1 Task-1 157
5.2.2 Task-2 157

6. Procedure& Tools 157


6.1 Tools 157
6.2 Setting-up JDK 1.7 [Expected time = 5mins] 157
6.2.1 Compile a Program 157
6.2.2 Run a Program 157
6.3 Walkthrough Task[Expected time = 30mins] 157
6.3.1 Writing Interface 158
6.3.2 Writing Server 158
6.3.3 Running RMI Registry 159
6.3.4 Exporting Server Object and registering it 159
6.3.5 Implementing Client side 159

7. Practice Tasks 162


7.1 Practice Task 1 [Expected time = 60mins] 162
7.2 Testing 162

8. Evaluation Task (Unseen) [Expected time = 60mins for two tasks] 162

9. Evaluation criteria 162

10. Further Reading 163


10.1 Books 163
10.2 Slides 163

Department of Computer Science, Page 153


C.U.S.T
Lab 15: Remote Method Invocation

Lab 15: Remote Method Invocation

1. Introduction

A distributed Java system is basically a grouping of cooperative distributed objects within a


network. The framework that is required to build distributed Java systems has been named as
Remote Method Invocation (RMI). RMI framework facilitates the invocation of a method
residing on one system with an object residing on another system.

Working of RMI
In Java, fundamentally objects can be categorized into two types i.e. local objects (objects
accessible within local host only) and remote objects (objects accessible from are remote hosts).
Essential components required to implement the functionality of RMI are given as under:

 For remote object invocation, it is required for that object to be defined in Java interface,
which must be accessible to server and client machines
 User interface must extend the marker interface known as java.rmi.Remote
 Server object interface, which is sub-interface of java.rmi.Remote to define method(s) for
server object
 A Server Class, which has implemented remote object interface and an instance of the server
class known as Server Object.
 To locate objects, a utility known as RMI registry is required to register remote objects for
the provisioning of naming services.
 Server stub: An object which resides on the client host and serves as a surrogate for remote
object
 Server Skeleton: an object residing on the server host and is responsible for the
communication with stub on client side and the actual server object

Figure 1 has shown the typical interaction between various components of RMI.

Client Server

Stub Skeleton

Network

Figure 1: RMI Components Interaction

The working sequence or flow to implement RMI has been described as under:

 A server object registration is required with RMI registry


 Client locate remote object through RMI registry

Department of Computer Science, Page 154


C.U.S.T
Lab 15: Remote Method Invocation

 Upon locating remote object its stub is returned to the client


 Stub and Skeleton are required to handle communication between client and server

The implementation of RMI architecture has been simplified by the automatic generation of stub
and skeleton in JDK version 1.5 and onwards.

Relevant Lecture Material

a) Revise Lecture No. 26, 27 and 28


b) http://docs.oracle.com/javase/tutorial/rmi/overview.html

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 20mins 20mins
6.2 Setting-upPath for JDK 5mins 5mins
6.3 Walkthrough Tasks 30mins 30mins
7 Practice task 60mins 60mins
8 Evaluation Task 60mins for all assigned task 55mins

3. Objective of the experiment


After completing this lab, the student should be able to

 explain the working of RMI


 develop applications using RMI
 differentiate between Java socket programming and Java RMI

4. Concept Map

4.1 Distributed Computing and RMI

The prime objective of RMI is to facilitate the communication of objects that are distributed in a
network. This distributed object model is an object-oriented counterpart of remote procedure call
(RPC) or simply it is Java version of RPC. Using RMI API, a programmer is able to write a code
in Java for the interaction of objects residing on different computers.

RMI is also considered as an advancement of client/server architecture in a sense that client is a


program that requests for some services and server on the other hand is a program that is
responsible to provide requested services. Similarly, RMI concept is based on client-server
conception but in a more flexible way because an RMI element can be a client or server.
Therefore, in RMI, functionality is interchangeable from a server to client and vice versa.

Department of Computer Science, Page 155


C.U.S.T
Lab 15: Remote Method Invocation

RMI emancipates the programmer to write difficult and lengthy code with the provisioning of
two helper classes called as Stub and Skeleton. All communications between client and server
takes place through Stub and Skeleton. Client computer holds Stub that ultimately contains
reference information about the server object. The main tasks of the Stub include parameter
sending to server and returning of results (received from server) to the client. On the other hand,
Skeleton is responsible for receiving and passing of parameter to the server object and eventually
to return the results to the Stub.

Concerning parameter passing, three types of parameters i.e. parameters of primitive, local and
remote object type are allowed to pass if a client invokes a method with parameters on remote
machine. However, you have to consider the following for each type of parameter:

 Parameters of primitive data types (i.e. int, float, long, char, double, boolean) can be
passed by value like function call on a local computer

 Parameters of local object type i.e. instance of java.lang.String can also passed by value.
However, it is necessary for passing objects to belong to class(es) that has/have
implemented serializable interface. It is due to the reason that Stub is responsible to send
serialized object parameter in a stream across the network.

In case of method invocation on remote machine with parameter of remote object type, actually
you have to pass the Stub of the remote object.

5. Homework before Lab

You must solve the following problems at home before the lab.

5.1 Problem Solution Modeling

After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.

5.1.1 Problem description:

Concerning RMI application development, write down an example code to explain the following
four steps i.e.

• Defining Server Object Interface


• Defining Server Implementation Object
• Creation and Registration of Server Object
• Development of Client Program

5.2 Practices from home


Solve the following subtasks.

Department of Computer Science, Page 156


C.U.S.T
Lab 15: Remote Method Invocation

5.2.1 Task-1

 What is a Marker interface? Explain the purpose of java.rmi.Remote.


 State the advantages of RMI over socket programming.
 In your own words, clearly explain the role of Stub and Skeleton in RMI architecture.

5.2.2 Task-2

Answer the followings:

 Describe the purpose of RMI registry.


 Illustrate the difference between bind() and rebind() methods of RMI registry class.
 How to register remote object with RMI registry
 Which command is required to start RMI registry
 How a client program locate remote object stub through an RMI registry

6. Procedure& Tools

In this section you will study how to send and receive data between two systems using TCP
Sockets.

6.1 Tools

Java Development Kit (JDK) 1.7

6.2 Setting-up JDK 1.7 [Expected time = 5mins]

Refer to Lab 1 sec 6.2.

6.2.1 Compile a Program

Refer to Lab 1 sec 6.2 for details.

6.2.2 Run a Program

Refer to Lab 1 sec 6.2 for details.

6.3 Walkthrough Task[Expected time = 30mins]

This task is designed to guide you towards implementing a Simple RMI application. Following 5
steps must be followed properly to setup and run the RMI application. The service that we are
going to build is a simple calculator Service which gets numbers from the client and computes
results and sends the results back to the client.

Department of Computer Science, Page 157


C.U.S.T
Lab 15: Remote Method Invocation

6.3.1 Writing Interface

Write an interface as the following containing all the methods that you want to call remotely. All
these methods will throw a remote exception. The remote exception means that this exception
can be handled remotely as it will be handled by the client who has called the service.

import java.rmi.RemoteException;
public interface Interfacermi extends java.rmi.Remote
{
public int get_add(int a,int b) throws RemoteException;
public int get_sub(int a,int b)throws RemoteException;
public int get_mul(int a,int b) throws RemoteException;
public double get_div(double a,double b) throws RemoteException;
}

The purpose of this interface is to provide the basic features of a calculator can be called
remotely.

6.3.2 Writing Server

This step is just to define the class which will implement the interface defined in the previous
step. This class will also be used to generate the client side and server side agents which are Stub
and Skelton.

import java.io.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
class service implements Interfacermi
{
public service() throws RemoteException
{}
public int get_add(int a,int b) throws RemoteException
{
return a+b;
}
public int get_sub(int a,int b) throws RemoteException
{
return a-b;
}
public int get_mul(int a,int b)
{
return a*b;
}
public double get_div(double a,double b)
{
return a/b;
}

Department of Computer Science, Page 158


C.U.S.T
Lab 15: Remote Method Invocation

6.3.3 Running RMI Registry

Open separate command prompt console and type the following command.

Start RMIRegistry

The new window will open. This shows that RMI Registry has started. The default port on which
RMI registry runs is 1099. Leave the new opened window as it is and go back to the server side
programming and generate a stub and Skelton by compiling the service class by RMIC tool.

6.3.4 Exporting Server Object and registering it

After starting the RMI Registry now we have a service which can hold the registry of objects.
The only object that we can register in RMI Registry can be a live object. Java provides built-in
class that can assure the live reference of Objects. That class is known as UniCastRemoteObject.
Following class has the main function which will publish the object of above class in RMI
registry.

class RmiServer
{
public static void main(String []args)
{
try
{
service r=new service();
Interfacermi stub=(Interfacermi) UnicastRemoteObject.exportObject(r, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("Calculator", stub);
System.out.println("Calculator Server is ready");
}
catch(RemoteException e)
{}
catch(Exception e)
{
}

}
}

6.3.5 Implementing Client side

Before this step we have a server side service running. Now the last step is to write a client side
which tries to find the reference of object from RMI Registry and utilizes this service using the
interface that the server has provided. This client and server communication is based on agents
called Stub and Skelton. Stub is a client side agent and Skelton is a server side agent. Following

Department of Computer Science, Page 159


C.U.S.T
Lab 15: Remote Method Invocation

client implementation utilizes the above service that we have created. Before running this client
side ensure that the client has both the interface and stub in its classpath.

import java.rmi.*;
import java.io.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
class Client
{
public static void main(String []args) throws Exception
{
String host=null;
Registry registry = LocateRegistry.getRegistry(host);
Interfacermi stub = (Interfacermi) registry.lookup("Calculator");
int z=1;
while(z==1)
{
try
{
System.out.println("press 1 for addition:");
System.out.println("press 2 for subtraction");
System.out.println("press 3 for multiplication");
System.out.println("press 4 for division");
System.out.println("press 5 for exit");
BufferedReader b=new BufferedReader(new
InputStreamReader(System.in));
String ch=b.readLine();
int a=Integer.parseInt(ch);
if(a==1)
{ System.out.println("*****Addition*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_add(x,y);
System.out.println("Addition of :"+x+" and "+y+" :"+response);
System.out.println("************");
}//end of if
else if(a==2)
{
System.out.println("*****Subtraction*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");

Department of Computer Science, Page 160


C.U.S.T
Lab 15: Remote Method Invocation

ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_sub(x,y);
System.out.println("Subtraction of :"+x+" and "+y+" :"+response);
System.out.println("************");
}
else if(a==3)
{
System.out.println("*****Multplication*****");
System.out.println("Enter the first value:");
ch=b.readLine();
int x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
int y=Integer.parseInt(ch);
int response = stub.get_mul(x,y);
System.out.println("Multiplication of :"+x+" and
"+y+" :"+response);
System.out.println("************");
}//end of else if
else if(a==4)
{
System.out.println("*****Division*****");
System.out.println("Enter the first value:");
ch=b.readLine();
double x=Integer.parseInt(ch);
System.out.println("Enter the second value:");
ch=b.readLine();
double y=Integer.parseInt(ch);
double response = stub.get_div(x,y);
System.out.println("Division of :"+x+" and "+y+" :"+response);
System.out.println("************");
}//end of else if 4
else if(a==5)
{
System.out.println("Good Bye -:(");
z=2;
}
else
{
System.out.println("Sorry wrong choice:");
}
}//end of try
catch(Exception e)
{
System.out.println("Exception is :"+e.toString());

Department of Computer Science, Page 161


C.U.S.T
Lab 15: Remote Method Invocation

}
}//end of while
}//end of main
}

7. Practice Tasks

This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Advanced Computer Programming\Lab14

7.1 Practice Task 1 [Expected time = 60mins]

Develop an application for Air Ticket Reservation System using Java RMI. It is assumed that a
passenger can book a number of seats of a specific type. There are two types of tickets i.e.
Business class tickets (having cost Rs. 80,000) and economical class tickets (of cost Rs. 50,000).
It is required that a remote method returns the total cost to the passenger for the number of
reservation seats.
To implement this system, you are required to:

 Define a server object named as ReservationServer


 Implement server implementation object using UnicastRemoteObject
 Create a server object from the server implementation class and register it with an RMI
registry.
 Write client program that locates a remote object and invokes its method to return total
fare of reserved seats.

7.2 Testing

For Practice Task 1, it is required to confirm the implementation of server interface/class,


creation and registration of remote method, and implantation of client program.

8. Evaluation Task (Unseen) [Expected time = 60mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria

The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Department of Computer Science, Page 162


C.U.S.T
Lab 15: Remote Method Invocation

Table 3: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7 Practice tasks and Testing 35
4 8 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Reading

This section provides the references to further polish your skills.

10.1 Books

Text Book:
 Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
 Java Beginners Guide: http://www.oracle.com/events/global/en/java-outreach/resources/java-a-
beginners-guide-1720064.pdf
 http://exampledepot.8waytrips.com/ for the package by package examples

10.2 Slides

The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\

Department of Computer Science, Page 163


C.U.S.T

You might also like