remote call

33
Chapter 4 Remote Call Lecturer: Sivadon Chaisiri Mathematics, Statistics and Computer Department Faculty of Science, Ubon Rajathanee University

Upload: sivadon-chaisiri

Post on 26-Jun-2015

836 views

Category:

Technology


0 download

DESCRIPTION

Remote Call Chapter used in Distributed Systems Course (Lecturer: Me)

TRANSCRIPT

Page 1: Remote Call

Chapter 4Remote Call

Lecturer: Sivadon ChaisiriMathematics, Statistics and Computer Department

Faculty of Science, Ubon Rajathanee University

Page 2: Remote Call

Distributed System (1104451) 2

Agenda

□Background Knowledge□Remote Procedure Call (RPC)□Remote Method Invocation (RMI)

□RMI Concepts□RMI Application Development

□Other remote call techniques…□CORBA, Jini, .NET Remoting, Web

Services

Page 3: Remote Call

Distributed System (1104451) 3

Client/Server Architecture

Server

Client1

Client2

Request

Request

Reply

Reply

Page 4: Remote Call

Distributed System (1104451) 4

Why Use a Client/Server?

□Resource sharing□Accessing remote resources□Delegating responsibilities□Performance distribution□Security (For centralized server)

Page 5: Remote Call

Distributed System (1104451) 5

N-tier Systems

□Client/server architecture can be described as a two-tier system: one tier

is a client, another one is a server□We can extend these tiers to any depth,

which are called N-tiers to make a distributed client/server system

□Distribute each responsibility to each tier

□ Some tiers may be replicated

Page 6: Remote Call

Distributed System (1104451) 6

Example of N-tier

Page 7: Remote Call

Distributed System (1104451) 7

Marshalling

□Marshalling is a process that converts a complex object into a

byte stream□Convert a marshal to a byte stream

into the original object, called unmarshalling

SomeObjectCopy of

SomeObjectByte StreamMarshal Unmarshal

Page 8: Remote Call

Distributed System (1104451) 8

Proxies□A proxy is an object that implements

a given interface, but does not directly execute some code to

compute a result, rather it delegates to some other object that performs

the actual computation□A proxy performs some tasks

□Error handling□Network communication□Security management

Page 9: Remote Call

Distributed System (1104451) 9

Naming

□Naming is a way to retrieve something (objects) by using a name

□The primary goal is to simplify the task of acquiring an object by using its name

□For example,□File System□Domain Name Service

Page 10: Remote Call

Distributed System (1104451) 10

Remote Procedure Call

□Remote Procedure Call (RPC) is the technique of calling functions over the network

□RPC libraries are programmed using C□The proxies used to communicate with

RPC libraries are often generated by first defining the interface of the library using an Interface Definition Language (IDL), and then using some tool to generate the proxies (also called stubs)

Page 11: Remote Call

Distributed System (1104451) 11

Conventional Procedure Call

□ Parameter passing in a local procedure call: the stack before the call to read□ The stack while the called procedure is active

Page 12: Remote Call

Distributed System (1104451) 12

Client and Server Stubs

□ Principle of RPC between a client and server program.

Page 13: Remote Call

Distributed System (1104451) 13

Steps of a Remote Procedure Call

1. Client procedure calls client stub in normal way

2. Client stub builds message, calls local OS3. Client's OS sends message to remote OS4. Remote OS gives message to server stub5. Server stub unpacks parameters, calls server6. Server does work, returns result to the stub7. Server stub packs it in message, calls local OS8. Server's OS sends message to client's OS9. Client's OS gives message to client stub10.Stub unpacks result, returns to client

Page 14: Remote Call

Distributed System (1104451) 14

Passing Value Parameter

□ Steps involved in doing remote computation through RPC

Page 15: Remote Call

Distributed System (1104451) 15

Synchronous VS Asynchronous

Page 16: Remote Call

Distributed System (1104451) 16

Asynchronous RPC

Page 17: Remote Call

Distributed System (1104451) 17

Remote Method Invocation

□RMI is a specification for how Java objects can be accessed remotely

from one JVM to another JVM□In JDK, RMI is just only a specification

provided in java.rmi and its subpackages

□JDK provides the RMI implementation named Java Remote Method Protocol

(JRMP)

Page 18: Remote Call

Distributed System (1104451) 18

Remote- vs. Local Invocation

□Remote exceptions□Parameter Passing

□Pass by value: both primitive data types & java objects (Serializable objects)

□Pass by reference: remote object (object which implements the interface Remote)

□Call overhead□Security

Page 19: Remote Call

Distributed System (1104451) 19

RMI Layer ModelServer Program

Skeleton

Remote Reference Layer

Transport Layer

Client Program

Stub

Remote Reference Layer

Transport LayerNetwork

Server Program

Skeleton

JRMP Engine

Transport Layer

Client Program

Stub

JRMP Engine

Transport LayerNetwork

Page 20: Remote Call

Distributed System (1104451) 20

RMI Components

ServerClient

Registry

(3) Invoke

(2) Looku

p

(4) Return Result

(1) Bind

Page 21: Remote Call

Distributed System (1104451) 21

RMI as Distributed System

Registry

Server

Client

Web Server

Lookup

Call

Load class

Bind

Web Server

Load class

Page 22: Remote Call

Distributed System (1104451) 22

Basic RMI Concepts□ Stubs

□Stubs are proxies that implement the interface Remote—also implemented by a remote object

□ Before creating a stub, you must create your remote interface, then this interface will be

implemented as a remote object, finally using rmic the tool to generate the stub code

□Must export stub to be remotely accessible (by UnicastRemoteObject.exportObject(Remote rem)

□ Marshalling□Transform objects in memory into a stream of

bytes

Page 23: Remote Call

Distributed System (1104451) 23

Basic RMI Concepts (Cont…)

□Dynamic classloading□The client can access the classes that are existed on the server by a technique called

dynamic classloading□Specify the property java.rmi.server.codebase

to refer the HTTP URL which keep server’s classes

□Security issues□RMI is secured by the Java sandbox

□RMI Threading□Distributed Garbage Collection

□Naming

Page 24: Remote Call

Distributed System (1104451) 24

Creating App using RMI/JRMP

1. Design the remote interface2. Implement the remote object

(Server)3. Generate the stub4. Implement the client5. Prepare the policy files6. Start the registry7. Run the server and the client

Page 25: Remote Call

Distributed System (1104451) 25

1. Design the remote interface

import java.rmi.Remote;import java.rmi.RemoteException;public interface Hello extends Remote {

public String sayHello(String name) throws RemoteException;

}

Page 26: Remote Call

Distributed System (1104451) 26

2. Implement the remote object (Server)import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*;

public class HelloRMIServer extends UnicastRemoteObject implements Hello {public HelloServer() throws RemoteException {

super();}public String sayHello(String name) throws RemoteException {

return "Hello " + name;}public static void main(String[] args) throws Exception {

if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager());

}//LocateRegistry.createRegistry(1099);Naming.rebind("rmi://localhost/Hello", new HelloServer());System.out.println("Ctrl + C to terminate");

}}

Page 27: Remote Call

Distributed System (1104451) 27

3. Generate the stub

□Generate the stub by using the Java RMI compiler (rmic)rmic [option] remote_object class

□Recommended rmic options□-v1.2□-keep or –keepgenerated

□For example,$ rmic –v1.2 HelloRMIServer

Page 28: Remote Call

Distributed System (1104451) 28

4. Implement the clientimport java.rmi.Naming;import java.rmi.RMISecurityManager;public class HelloRMIClient {

public static void main(String[] args) throws Exception { if (System.getSecurityManager() == null) {

System.setSecurityManager(new RMISecurityManager()); }

Hello h = (Hello)Naming.lookup("rmi://localhost/Hello"); String name = "World"; if (args.length > 0) name = args[0]; String msg = h.sayHello(name); System.out.println(msg);

}}

Page 29: Remote Call

Distributed System (1104451) 29

5. Prepare the policy files□In Java2, there is a notion of permissions.

□ Java codes that want to protect themselves can check whether the caller of the codes has a particular permission

□Permissions can be specified by a policy file

□ An example of a policy file grant {

permission java.security.AllPermission;};

Page 30: Remote Call

Distributed System (1104451) 30

6. Start Registry

□Two ways to start RMI registry□Via a command line rmiregistry [port]

$ rmiregistry 1099

□Via a class java.rmi.registry.LocateRegistry LocateRegistry.createRegistry(1099)

Page 31: Remote Call

Distributed System (1104451) 31

7. Run the server and the client

$ java –Djava.security.policy=java.policy HelloRMIServer

$ java –Djava.security.policy=java.policy HelloRMIClient Paula

Run the server

Run the client

□Assume we use the same java.policy

Page 32: Remote Call

Distributed System (1104451) 32

Other Remote Calling Techniques

□CORBA□.NET Remoting□Jini□Web Services

Page 33: Remote Call

The End

Any Questions?