Week 1 and Week 2 Intro and RPC

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 95

Distributed Computer Systems

CT024-3-3-DCOMS and Version VC1

Introduction & RPC


Lecturer Introduction
• Name : Umapathy

• Email:
[email protected]

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


2 of 95
Prerequisites for this module

• CT043-3-1 Introduction to Networking and


CT038-3-2 and Object Oriented
Development with Java or equivalent

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


3 of 95
Aims of the module

• Remote Method Invocation (RMI),


Common Object Request Broker
Architecture (CORBA), and Component
Object Model (COM). Students will learn
how distributed systems functioning in
three tier network.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


4 of 95
Topic & Structure of The
Lesson

a) Introduction
b) Remote Procedure Call
c) Remote Method Invocation (RMI)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


5 of 95
Course Learning Outcomes

Evaluate various distributed technologies to improve the


quality of distributed enterprise applications based on the
given case study (C5, PLO1)

Identify the future developments in distributed systems


technology and standards with organizaton needs based on
research. (A4, PLO6)

Propose an appropriate programming language and


system facilities to develop distributed and fault-tolerant
applications (A5, PLO5)
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
6 of 95
Mapping of CLOs with MOEs
Domain

PLO1 – Knowledge
PLO5 – Social Skills, Teamwork and Responsibilities
PLO6 – Values, Attitudes and Professionalism

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


7 of 95
MQF and MOE Domains

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


8 of 95
Teaching Strategies

• Lecture
• Tutorial
• Case Study (Individual and Group)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


9 of 95
Assessment Methods

• Final Exam (40%) : CLO1

• Group Assignment (60%)


- Research & Implementation : CLO2, CLO3

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


10 of 95
Methods of Delivery

Hence,
• We are now moving from the traditional
topic based teaching to outcome-based
education

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


11 of 95
Outcomes Based Education (OBE)

• OBE is education based on producing


particular educational outcomes that:
Focus on what students can actually do after
they are taught
Expect all learners / students to successfully
achieve particular (sometimes minimum) level
of knowledge and abilities.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


12 of 95
So…What is OBE?

It’s
NOT
What we want to teach,

It’s
What You should learn

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


13 of 95
Student Learning Time (SLT)

• Course Credit Value: 3


• Total Learning Hours:
 Lecture: 18 hours per semester
 Tutorial / Case Study : 24 hours
per semester
 Independent Learning Time: 76
hours per semester

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


14 of 95
Learning Outcomes

• At the end of this topic, You


should be able to
• Understand the key characteristics, basic design
issues, user requirements
• Understand how Remote Procedure Call uses in DS
• Understand the concept of Remote Method Invocation,
stub/skeleton, IIOP in DS

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


15 of 95
Key Terms You Must Be
Able To Use
• If you have mastered this topic, you should be able
to use the following terms correctly in your
assignments and exams:

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


16 of 95
INTRODUCTION TO COMPUTER
NETWORKS

Computer network
connects two or more
autonomous computers.

The computers can be


geographically located
anywhere.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


17 of 95
Networking

Connecting computers to form a Local Area Network


(LAN)

Here devices sharing of

Hardware
Software and
Information

LAN is a collection of computers connected together in a


NW which less than 1000 computers

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


18 of 95
LAN, MAN and WAN

Network in small geographical Area (Room, Building


or a Campus) is called LAN (Local Area Network)

Network in a City is call MAN (Metropolitan Area


Network)

Network spread geographically (Country or across


Globe) is called WAN (Wide Area Network)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


19 of 95
Applications of Networks

Resource Sharing
Hardware (computing resources, disks, printers)
Software (application software)
Information Sharing
Easy accessibility from anywhere (files,
databases)
Search Capability (WWW)
Communication
Email
Message broadcast
Remote computing
Distributed processing (GRID Computing)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


20 of 95
Internetworks & Intranetworks

A network of networks called internetwork that is a


networked collection of LAN.

More than one floor in a business or multiple buildings,


campus environment

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


21 of 95
Network Topology

The network topology defines the way in which


computers, printers, and other devices are connected. A
network topology describes the layout of the wire and
devices as well as the paths used by data transmissions.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


22 of 95
WAN

Spans miles of distance

Two or more separate locations

Down the road or across the world

Millions of machines in world wide web

Simple network needs

Medium to carry data, Adapter (NIC), Protocol, Client /


Server Software

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


23 of 95
Networking Terminology

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


24 of 95
Computers: Clients and
Servers
In a client/server network
arrangement, network
services are located in a
dedicated computer
whose only function is to
respond to the requests
of clients.

The server contains the


file, print, application,
security, and other
services in a central
computer that is
continuously available to
respond to client
requests.
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
25 of 95
Medium for data
communication
 Metallic cable coaxial
 Twisted pair cable
 Fiber optic cable
 Wireless

NIC (Network Interface Card)


Physical link between machine and the network
Connection between machine and the medium
Also knows as network adapter
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
26 of 95
Networking media can be defined simply as the means by which signals (data)
are sent from one computer to another (either by cable or wireless means).

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


27 of 95
Network Components

Physical Media
Interconnecting Devices
Computers
Networking Software
Applications

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


28 of 95
Networking Devices

HUB, Switches, Routers, Wireless Access Points, Modems


etc.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


29 of 95
Protocols

 Common set of communication rules


 Identify oneself and others on network
 Interpret signals
 Begin and end network communication
 Manage network information exchange
Some of the protocols are
TCP/IP for the internet
IPX/SPX & NWLINK for novel
NetBEUI for Microsoft
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
30 of 95
Network protocol TCP/IP

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


31 of 95
Applications

E-mail
Searchable Data (Web Sites)
E-Commerce
News Groups
Internet Telephony (VoIP)
Video Conferencing
Chat Groups
Instant Messengers
Internet Radio
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
32 of 95
Distributed Systems

Topics
– What is a Distributed System?
– Why Distributed Systems?
– Examples of Distributed Systems
– Distributed System Requirements
– Transparency in Distributed Systems

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


33 of 95
Distributed Systems

• Can think of DS as:


– breaking down an application into individual
computing agents
– distributed over a network
– work together on a cooperative task
• Motivation for DC:
– Scalability: can solve larger problems without larger
computers
– Openness and heterogeneity: applications and data
may be difficult to relocate and reconfigure
– Fault-tolerance: redundant processing agents for
system availability
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
34 of 95
What Is a Distributed
Ingredients of a Distributed
System
System?
Component-1 … Component-n Component-1 … Component-m
Network OS Network OS
Hardware Hardware

Host-1 Host-n

Component-1 … Component-n
Middleware
Network OS
Hardware

Host-1

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


35 of 95
What Is a Distributed
System?

Component-1 … Component-n Component-1 … Component-n

Middleware Middleware

Network OS Network OS

Hardware Hardware

Host-1 Host-2
Network
Component-1 … Component-n Component-1 … Component-n

Middleware Middleware

Network OS Network OS

Hardware Hardware

Host-n Host-3

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


36 of 95
What is a Distributed System

• Distributed System Definition:


– A distributed system is a collection of autonomous
hosts that are connected through a computer
network.
– Each host executes components and operates a
distribution middleware.
– Middleware enables the components to coordinate
their activities.
– Users perceive the system as a single, integrated
computing facility.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


37 of 95
Why Distributed Systems?
• Every application is part of your business model
– must make them work together!

Payables/
Receivables

Sales Accounting

Manufacturing

Inventory
Shipping/
Receiving
Engineering

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


38 of 95
Why Distributed Systems?
• Application Integration and Distributed Processing are
the same thing
– Constructing information-sharing distributed systems
from diverse sources:
• heterogeneous
• networked
• physically disparate
• multi-vendor

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


39 of 95
Why Distributed System?
• Application Requirements
– Functional
– Non-Functional
• Non-functional requirements drive distribution of a
system
– Scalability
– Concurrency
– Openness
– Heterogeneity
– Resource sharing
– Fault-tolerance

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


40 of 95
Centralized vs Distributed
• Centralized Systems
Systems
– Centralized systems have non-autonomous components
– Centralized systems are often build using homogeneous
technology
– Multiple users share the resources of a centralized system at all
times
– Centralized systems have a single point of control and of failure
• Distributed Systems
– Distributed systems have autonomous components
– Distributed systems may be built using heterogeneous
technology
– Distributed system components may be used exclusively
– Distributed systems are executed in concurrent processes
– Distributed systems have multiple points of failure

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


41 of 95
Advantages and
Disadvantages of Distributed

Systems
Advantages
– Shareability
– Expandability
– Local autonomy
– Improved performance
– Improved reliability and availability
– Potential cost reductions
• Disadvantages
– Network reliance
– Complexities
– Security
– Multiple point of failure

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


42 of 95
Middleware

Software that acts as a bridge between an operating system or


database and applications, especially on a network.

Middleware often sits between the operating system and applications


on different servers and simplifies the development of applications that
leverage services from other applications.

Software that connects two otherwise separate applications. For


example, there are a number of middleware products that link a
database system to a Web server. This allows users to request data
from the database using forms displayed on a Web browser, and it
enables the Web server to return dynamic Web pages based on the
user's requests and profile.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


43 of 95
History Review of Distributed
Systems
• Late 70’s and early 80’s: synchronous with distributed
processing
• Mid 80’s: no point of central control
• Late 80’s: peer structure and inter-connection
configuration
• Later: more fine-grained distribution
– Software is decomposed into components
– Components can resides on different computers and
be implemented with different languages

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


44 of 95
Examples of Distributed
Systems
• Video-on-Demand
– Client components are used to display videos
– Clients are hosted in the homes of the customers of
the service
– Server components load and transmit videos
– Multiple servers are needed in order to scale

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


45 of 95
Examples of Distributed
Systems
• IT Service Architecture of a Swiss Bank
– Service architecture consists of heterogeneous new and legacy components
– Hardware platforms range from mainframes to NTs
– Programming languages including Assembler, Cobol, C, C++, Java, …
– Different types of middleware can be used to resolve distribution and
heterogeneity

Authorization
Customer Services
Information
Services

Trading
Services
ATM
Services

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


46 of 95
Transparency in Distributed
Systems
• Distributed System Definition: A distributed system is a collection of
autonomous hosts that are connected through a computer network
and coordinate with each other in such a way that users perceive
the system as a single and integrated computing facility.

• Dimensions of transparency in DS
– Access Transparency
– Location Transparency
– Migration Transparency
– Replication Transparency
– Concurrency Transparency
– Scalability Transparency
– Performance Transparency
– Failure Transparency

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


47 of 95
Location Transparency

Caller

Machine 1 Machine 2
local
call
remote Implementor
call
Proxy

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


48 of 95
Remote Procedure Call (RPC)

• The most common framework for newer protocols and


for middleware
• Used both by operating systems and by applications
– NFS is implemented as a set of RPCs
– DCOM, CORBA, Java RMI, etc., are just RPC systems
• Fundamental idea: –
– Server process exports an interface of procedures or functions
that can be called by client programs
• similar to library API, class definitions, etc.
• Clients make local procedure/function calls
– As if directly linked with the server process
– Under the covers, procedure/function call is converted into a
message exchange with remote server process

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


49 of 95
Ordinary procedure/function
call

count = read(fd, buf, nbytes)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


50 of 95
Would like to do the same if called procedure or function is
on a remote server

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


51 of 95
Solution – a pair of stubs

• Client-side stub
– Looks like local server function
– Same interface as local function
– Bundles arguments into message, sends to server-
side stub
– Waits for reply, un-bundles results
– returns

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


52 of 95
• Server-side stub
– Looks like local client function to server
– Listens on a socket for message from client stub
– Un-bundles arguments to local variables
– Makes a local function call to server
– Bundles result into reply message to client stub

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


53 of 95
Result

• The hard work of building messages,


formatting, uniform representation, etc., is
buried in the stubs
• Where it can be automated!
• Client and server designers can
concentrate on the semantics of
application
• Programs behave in familiar way

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


54 of 95
RPC Model

• A server defines the service interface using an


interface definition language (IDL)
– the IDL specifies the names, parameters, and types
for all client-callable server procedures

• A stub compiler reads the IDL declarations and


produces two stub functions for each server
function
– Server-side and client-side

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


55 of 95
RPC Model (continued)

• Linking:–
– Server programmer implements the service’s
functions and links with the server-side stubs
– Client programmer implements the client program and
links it with client-side stubs
• Operation:–
– Stubs manage all of the details of remote
communication between client and server

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


56 of 95
RPC Stubs

• A client-side stub is a function that looks to the client as if


it were a callable server function
– I.e., same API as the server’s implementation of the function
• A server-side stub looks like a caller to the server
– I.e., like a hunk of code invoking the server function
• The client program thinks it’s invoking the server
– but it’s calling into the client-side stub
• The server program thinks it’s called by the client
– but it’s really called by the server-side stub
• The stubs send messages to each other to make the
RPC happen transparently (almost!)

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


57 of 95
Marshalling Arguments

• Marshalling is the packing of function


parameters into a message packet
– the RPC stubs call type-specific functions to marshal
or unmarshal the parameters of an RPC
• Client stub marshals the arguments into a message
• Server stub unmarshals the arguments and uses them to
invoke the service function
– on return:
• the server stub marshals return values
• the client stub unmarshals return values, and returns to the
client program

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


58 of 95
Java Remote Object Invocation
(RMI)
• Overview of RMI https://docs.oracle.com/javase/7/docs/platform/rmi/spec/rmiTOC.html

• Java RMI allowed programmer to execute remote


function class using the same semantics as local
functions calls.
Local Machine (Client) Remote Machine (Server)

SampleServer remoteObject;
int s;

s = remoteObject.sum(1,2);
1,2
public int sum(int a,int b)
{
3 }
return a + b;

System.out.println(s);

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


59 of 95
The General RMI Architecture
Remote Machine
• The server must first bind
bind
its name to the registry RMI Server
• The client lookup the Registry
server name in the skeleton

registry to establish
remote references. return call lookup

• The Stub serializing the


parameters to skeleton, stub
the skeleton invoking the
remote method and RMI Client
serializing the result back
to the stub. Local Machine

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


60 of 95
The Stub and Skeleton
call

skeleton
Stub
RMI Client RMI Server
return

• A client invokes a remote method, the call is first


forwarded to stub.
• The stub is responsible for sending the remote call
over to the server-side skeleton
• The stub opening a socket to the remote server,
marshaling the object parameters and forwarding the
data stream to the skeleton.
• A skeleton contains a method that receives the
remote calls, unmarshals the parameters, and
invokes the actual remote object implementation.
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
61 of 95
Remote Method Invocation

• Remote method invocation(RMI) allow a java object to


invoke method on an object running on another machine.
RMI provide remote communication between java
program. RMI is used for building distributed application.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


62 of 95
Concept of RMI

• A RMI application can be divided into two part, Client


program and Server program. A Server program creates
some remote object, make their references available for
the client to invoke method on it. A Client program make
request for remote objects on server and invoke method
on them. Stub and Skeleton are two important object
used for communication with remote object.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


63 of 95
Stub and Skeleton

• Stub act as a gateway for Client program. It resides on


Client side and communicate with Skeleton object. It
establish the connection between remote object and
transmit request to it.
• Skeleton object resides on server program. It is
responsible for passing request from Stub to remote
object.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


64 of 95
Stub and Skeleton

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


65 of 95
Creating a simple RMI
application
• Define a remote interface.
• Implementing remote interface.
• create and start remote application
• create and start client application

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


66 of 95
Define a remote interface

• A remote interface specifies the methods that can be


invoked remotely by a client. Clients program
communicate to remote interfaces, not to classes
implementing it. To be a remote interface, a interface
must extend the Remote interface of java.rmi package.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


67 of 95
Remote Interface

import java.rmi.*;
public interface AddServerInterface extends Remote
{
public int sum(int a,int b);
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


68 of 95
Implementation of remote
interface
• For implementation of remote interface, a class must
either extend UnicastRemoteObject or use
exportObject() method of UnicastRemoteObject class.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


69 of 95
import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject
implements AddServerInterface
{
Adder()throws RemoteException
{
super();
}
public int sum(int a,int b)
{
return a+b;
}
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


70 of 95
Create AddServer and host
rmi service

You need to create a server application and host rmi


service Adder in it.
This is done using rebind() method of java.rmi.Naming
class.
rebind() method take two arguments, first represent the
name of the object reference and second argument is
reference to instance of Adder

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


71 of 95
import java.rmi.*;
import java.rmi.registry.*;
public class AddServer
{
public static void main(String args[])
{ try
{
AddServerInterface addService=new Adder();
Naming.rebind("AddService",addService); //addService object
is hosted with name AddService.
}

catch(Exception e)
{
System.out.println(e);
}
}
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
72 of 95
Create Client Application

Client application contains a java program that invokes the


lookup() method of the Naming class.
This method accepts one argument, the rmi URL and
returns a reference to an object of type
AddServerInterface. All remote method invocation is done
on this object.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


73 of 95
import java.rmi.*;
public class Client
{
public static void main(String args[])
{
try
{
AddServerInterface
st=(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/AddService");
System.out.println(st.sum(25,8));
}
catch(Exception e)
{
System.out.println(e);
}
}
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
74 of 95
Execution

Compile all the files using javac


Start rmi registry
Run server
Run client
Output is : 33

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


75 of 95
Write a RMI application to
reverse the given
string .Write all interfaces
and required classes
import java.rmi.*;
public interface StrRev extends Remote
{
public String reverse(String str) throws
RemoteException;
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


76 of 95
• import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class StrRev_Impli extends UnicastRemoteObject
implements StrRev
{
public StrRev_Impli() throws RemoteException
{}

public String reverse(String str) throws RemoteException


{
String str1="";
for(int i=str.length()-1;i>=0;i--)
str1=str1+str.charAt(i);
return (str1);
}
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


77 of 95
• import java.rmi.*;
import java.net.*;
public class StrRev_Server
{
public static void main(String args[])
{
try
{
StrRev_Impli strimpli=new StrRev_Impli();
Naming.rebind("RmiReverse",strimpli);
}
catch(Exception ex){}
}
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


78 of 95
• import java.io.*;
import java.rmi.*;
import java.net.*;
public class StrRev_Client
{
public static void main(String args[])
{
try
{
String url="rmi://127.0.0.1/RmiReverse";
StrRev strf=(StrRev)Naming.lookup(url);

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));
System.out.println("Enter String to reverse : ");
String str=br.readLine();
System.out.println("Reverse of entered string is "+strf.reverse(str));
}
catch(Exception ex){}
}
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
79 of 95
Write a RMI application to
check whether the given
number is Prime or not .
Write all interfaces and
required classes.
• import java.rmi.*;
public interface Prime extends Remote
{
public int check_prime(int no) throws
RemoteException;
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


80 of 95
• import java.rmi.*;
import java.net.*;
public class Prime_server
{
public static void main(String args[])
{
try
{
Prime_Impli primpli=new Prime_Impli();
Naming.rebind("RmiPrime",primpli);
}
catch(Exception ex){}
}
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


81 of 95
import java.io.*;
import java.rmi.*;
import java.net.*;
public class Prime_client
{
public static void main(String args[])
{
try
{
String url="rmi://127.0.0.1/RmiPrime";
Prime intf=(Prime)Naming.lookup(url);

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter No : ");
int no=Integer.parseInt(br.readLine());
int x=intf.check_prime(no);
if(x==0)
System.out.println(no+" is prime number");
else
System.out.println(no+" is not prime number");
}
catch(Exception ex){}
}
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
82 of 95
Steps for Developing an RMI
System
1. Define the remote interface
2. Develop the remote object by implementing the
remote interface.
3. Develop the client program.
4. Compile the Java source files.
5. Generate the client stubs and server skeletons.
6. Start the RMI registry.
7. Start the remote server objects.
8. Run the client

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


83 of 95
Step 1: Defining the Remote
Interface
• To create an RMI application, the first step is
the defining of a remote interface between
the client and server objects.

/* SampleServer.java */
import java.rmi.*;

public interface SampleServer extends Remote


{
public int sum(int a,int b) throws RemoteException;
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


84 of 95
Step 2: Develop the remote
object and its interface
• The server is a simple unicast remote server.
• Create server by extending
java.rmi.server.UnicastRemoteObject.
• The server uses the RMISecurityManager to protect its
resources while engaging in remote communication.

/* SampleServerImpl.java */
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

public class SampleServerImpl extends UnicastRemoteObject


implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
85 of 95
Step 2: Develop the remote
object and its interface
• Implement the remote methods
/* SampleServerImpl.java */
public int sum(int a,int b) throws RemoteException
{
return a + b;
}
}
• The server must bind its name to the registry, the client will look
up the server name.
• Use java.rmi.Naming class to bind the server name to registry.
In this example the name call “SAMPLE-SERVER”.
• In the main method of your server object, the RMI security
manager is created and installed.

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


86 of 95
Step 2: Develop the remote
object and its interface
/* SampleServerImpl.java */
public static void main(String args[])
{
try
{
System.setSecurityManager(new RMISecurityManager());
//set the security manager

//create a local instance of the object


SampleServerImpl Server = new SampleServerImpl();

//put the local instance in the registry


Naming.rebind("SAMPLE-SERVER" , Server);

System.out.println("Server waiting.....");
}
catch (java.net.MalformedURLException me) {
System.out.println("Malformed URL: " + me.toString()); }
catch (RemoteException re) {
System.out.println("Remote exception: " + re.toString()); }
}

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


87 of 95
Step 3: Develop the client
program
• In order for the client object to invoke methods on the
server, it must first look up the name of server in the
registry. You use the java.rmi.Naming class to lookup the
server name.
• The server name is specified as URL in the from
( rmi://host:port/name )
• Default RMI port is 1099.
• The name specified in the URL must exactly match the
name that the server has bound to the registry. In this
example, the name is “SAMPLE-SERVER”
• The remote method invocation is programmed using the
remote interface name (remoteObject) as prefix and the
remote method name (sum) as suffix.
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
88 of 95
Step 3: Develop the client
program
import java.rmi.*;
import java.rmi.server.*;
public class SampleClient
{
public static void main(String[] args)
{
// set the security manager for the client
System.setSecurityManager(new RMISecurityManager());
//get the remote object from the registry
try
{
System.out.println("Security Manager loaded");
String url = "//localhost/SAMPLE-SERVER";
SampleServer remoteObject = (SampleServer)Naming.lookup(url);
System.out.println("Got remote object");
System.out.println(" 1 + 2 = " + remoteObject.sum(1,2) );
}
catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString()); }
catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString()); }
catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}
CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
89 of 95
Step 4 & 5: Compile the Java source
files & Generate the client stubs and
server skeletons
• Assume the program compile and executing at elpis
on ~/rmi
• Once the interface is completed, you need to
generate stubs and skeleton code. The RMI system
provides an RMI compiler (rmic) that takes your
generated interface class and procedures stub code
on its self.

elpis:~/rmi> set CLASSPATH=”~/rmi”


elpis:~/rmi> javac SampleServer.java
elpis:~/rmi> javac SampleServerImpl.java
elpis:~/rmi> rmic SampleServerImpl

elpis:~/rmi> javac SampleClient.java


CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC
90 of 95
Step 6: Start the RMI registry
• The RMI applications need install to Registry. And the
Registry must start manual by call rmiregisty.
• The rmiregistry us uses port 1099 by default. You
can also bind rmiregistry to a different port by
indicating the new port number as : rmiregistry
<new port>

elpis:~/rmi> rmiregistry

• Remark: On Windows, you have to type in from the


command line:
> start rmiregistry

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


91 of 95
Steps 7 & 8: Start the remote server
objects & Run the client
• Once the Registry is started, the server can be
started and will be able to store itself in the Registry.
• Because of the grained security model in Java 2.0,
you must setup a security policy for RMI by set
java.security.policy to the file policy.all

elpis:~/rmi> java –Djava.security.policy=policy.all


SampleServerImpl

elpis:~/rmi> java –Djava.security.policy=policy.all


SampleClient

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


92 of 95
Summary

• Distributed systems
• Centralized systems vs distributed systems
• The need for distributed systems --- application’s non-functional
requirements
• Transparencies
• Dependencies among different dimensions of transparencies
• RPC Model
• RPC Stubs
• Marshalling Arguments
• RMI

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


93 of 95
Question and answer session

Q&A

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


94 of 95
What we will cover next

• Processes and Threads

CT024-3-3 DCOMS-Distributed Computer Systems Introduction & RPC


95 of 95

You might also like