rpc and rmi - griffith university

58
RPC and RMI 2501ICT Nathan

Upload: others

Post on 21-Jan-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

RPC and RMI

2501ICTNathan

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

2

Contents

• Client/Server revisited• RPC

• Architecture• XDR

• RMI• Principles and Operation• Case Studies

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

3

Client/Server Revisited

• Server• Accepts commands over Network connections• Executes code according to commands• No direct control over this code

• Client• Communicates with server• Sends commands• Awaits results

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

4

Remote Methods/Procedures

• Extension to the Client/Server concept• “Client-Programmable” Interface• Similar to Function/Method calls

• Method Invocations over Network• RPC / RMI

• Remote Procedure Calls• Remote Method Invocations

• Implementation: Middleware

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

5

Middleware

• Programming Model above BasicNetworking Layer

Applications

RPC / RMI

Request/Reply Protocol

External Data Represent.

Operating System

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

6

Interfaces

• Modules run as separate Tasks• Similar to Process/Thread Model

• Data exchange via Messages• Carry Parameters and Return Values

• Integration• Direct

• Objective-C Distributed Objects, RMI for C++• Interface Definition Languages (IDLs)

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

7

Interface Definition Languages

• Uniform Interface• Across Programming Languages

• CORBA• CORBA IDL

• Java• Java RMI

• Sun XDR• IDL for RPC

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

8

RPC

• Originally designed for NFS• Networking File System

• Extension Function Call Interface• Clients can call Procedures across Network

Connections

• Service Interface• Provides Function and Data Prototypes

• Request/Response Protocol

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

9

RPC Architecture

client

Request

Reply

CommunicationCommunication module module dispatcher

service

client stub

server stubprocedure procedure

client process server process

procedureprogram

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

10

Stub Procedures

• Proxy between local and remoteprocedures

• Server• Skeletons that receive calls from dispatcher and

pass them on to the Server Application

• Client• Stubs that can be called using local (statically

compiled) code

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

11

Sun XDR Example

const MAXLEN = 80;typedef Person {

char name[MAXLEN];char address[MAXLEN];int year_of_birth;

};

program PERSONLIST {version VERSION {

void ADDPERSON(Person)=1;int GETYEAROFBIRTH(Person)=2;

}=2;}=9999;

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

12

XDR Properties

• No Symbolic Interface Names• Program, Version, and Procedure Numbers

• Only Single Input Parameter• Compound Structure to encapsulate multiple

parameters!

• Binding via Portmapper• Globally register RPCs• Unique signatures required!

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

13

Distributed Objects

• Extension of the local OO Model• Design Issues

• Suitable Semantics for Remote Invocations?• How can RMI semantics be made similar to local

method call semantics?

• Let’s Compare• Local Object Model vs.• Distributed Object Model

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

14

Local Object Access

• Access via Object References• Target: Object whose Method is invoked

• Interfaces• Signatures of a Set of Methods• Does not specify actual Implementation

• Actions and Exceptions• Normal and abnormal Method terminations

• Garbage Collection• Freeing unused Space

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

15

Distributed Object Access

• Remote Object References• Provide Access to Remote Objects

• Remote Interfaces• Set of Methods to be invoked

• Actions and Exceptions• Synchronous or Asynchronous termination

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

16

RMI Architecture

• Objects distributed over Network• Location is transparent• Client/Server applications represented

by objects• Peer-to-Peer architecture

• Roles of Client or Server only specific for aparticular method call

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

17

Remote Method Illustration

invocation invocation

remote

invocationremote

local

local

local

invocation

invocation

AB

C

D

E

F

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

18

Remote Object Interface

interfaceremote

m1m2m3

m4m5m6

Data

Impl.

remote object

{ of methods

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

19

Distributed Objects API

• Language Specific Framework forDistributed Objects

• Applications must• Locate remote Objects• Communicate with remote Objects

• Invoke remote methods• Handle remote exceptions

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

20

Distributed Objects API

• Messages• Transport Parameters and Results

• Interface• Specifies Services provided by an Object• Describes Messages an Object can Respond to

• Instance Variables• Only accessed and changed through invoking Methods

• Requirement for Access Methods!

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

21

Object Proxy

• Local Proxy for Remote Objects• Local object that represents an instance of a remote

object

• Transparent integration into Programs• Methods are invoked on the proxy “pretending” the

server object is a local object

• NSDistantObject class

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

22

RMI Process

• RMI In a Nutshell• Server makes available its remote objects• Client invokes methods on a local proxy• Request is marshalled and sent onto server• Request is unmarshalled on server• Remote object executes method for the request and

returns result to client

• Transparent to Program

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

23

RMI Schematic

object A object BskeletonRequest

proxy for B

Reply

CommunicationRemote Remote referenceCommunication module modulereference module module

for B’s class& dispatcher

remoteclient server

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

24

RMI Transport Layer

• Uses TCP (UDP also possible)• Transport Layer Functions

• Locate RMI server• Establish socket connection to server• Pass connection back to client Remote Reference

Layer• Add remote object to a table of ‘reachable’ objects• Monitor connection status

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

25

Parameter Marshalling

• Encoding Parameters• Required for Network Transport

• Remote Interface Parameter• Call By Reference: byref qualifier• Call By Copy: bycopy qualifier

• Efficiency• in: parameter is only sent to the server• out: parameter is returned from the server• inout: parameter is sent in both directions

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

26

Registration/Naming Service

• Allows Finding a Server by Name• Intermediary between Object Client and Server

• NSConnection class• Server Side Registration

• [NSConnection registerName: myName];

• Client Side Access• [NSConnectionrootProxyForConnectionWithRegisteredName:myName host: ”*”];

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

27

Distributed Object Server

…NSConnection *c = [NSConnection defaultConnection];

[c setRootObject: myObject];

if ([c registerName: @”Name] == NO){ /* Handle the error */ …}…

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

28

Distributed Object Client

…id proxy = [NSConnection

rootProxyForConnectionWithRegisteredName: @”Name”host: @”*”];

if (proxy == nil){ /* Handle the error */ …}

[proxy someMethod]; // invokes method on server…

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

29

Asynchronous Calls

• Client won’t wait for Server• for methods defined as oneway void

• More flexible• Server operates independently of client• Useful to support collaborative functionality

• e.g. chat programs, BBS’s, etc.

• What if return value is needed?• Explicit synchronisation required• Callbacks

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

30

Callbacks

• Allow Server to call Client method• Client acts as an RMI server

• Implicitly: registering an object name• Explicitly: object references (byref parameters)

• Peer-to-Peer Relationship• No dedicated clients and servers• Each peer is both a client and a server

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

31

CORBA

• Common Object Request BrokerArchitecture• Machine and Language-Independent Distributed

OO Architecture

• Supports RMI• CORBA IDL

• Compiled for different programming languages

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

32

CORBA IDL Example

struct Person {string name;string address;long year_of_birth;

};

interface PersonList {readonly attribute string listname;void addPerson(in Person p);void getPerson(in string name, out Person p);long getYearOfBirth();

}

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

33

The CORBA Architecture

• ORBs• Object Request Brokers• Help a Client to invoke a method on an object

• Locate/activate object, communicate client’s request

• GIOP• General Inter-Orb Protocol• Standardised communication Protocol

• IIOP• IP Implementation of GIOP

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

34

CORBA RMI

• Multi-Language RMI System• More Versatile Object Model

• Client’s don’t need to be objects

• CORBA Naming Service• Allows services to register (bind) objects

• Pseudo-Objects• init, connect, conversion objects

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

35

CORBA Components

client server

proxy

or dynamic invocation

implementation repository object

adapter

ORBORB

skeleton

or dynamic skeleton

client program

interface repository

Request

Replycorecore for A

Servant A

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

36

CORBA Language Support

• CORBA is cross-platform• No “native” implementation• Language bindings through toolkits

• Toolkit examples• AdORB: CORBA API for Objective-C• ORBit: CORBA API for C• ORBit C++: CORBA API for C++

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

37

References

• Objective-C Distributed Objects• http://www.gnustep.org/resources/documentation/Developer/Base/Pr

ogrammingManual/manual_7.html• http://www.devworld.apple.com/documentation/Cocoa/Conceptual/D

istrObjects/index.html

• ORBit Tutorial:• http://www.gnome.org/projects/ORBit2/orbit-docs/orbit/book1.html

• ORBit C++:• http://orbitcpp.sourceforge.net/

• C++ RMI• http://www.ddj.com/dept/cpp/184403949

Appendix

Java RMI

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

39

Java RMI

• Java-Specific Framework forDistributed Objects

• Applications must• Locate remote Objects• Communicate with remote Objects• Provide Mechanisms for loading/passing Bytecode

of an Object

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

40

Generating Proxies

• Involves rmic• Java RMI Compiler

• Procedure for generating proxies• Interface and implementation

• Compiled normally, using javac

• Generate stubs• Use rmic on implementation class file, e.g.• rmic myRemoteClassImpl will generate

myRemoteClassImpl_Stub.class myRemoteClassImp_Skel.class

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

41

Java RMI Programming

• The RMI Development Steps are:• Identify remote objects and their interfaces• Provide implementation for distributed objects• Compile sources• Use rmic to create stubs• Start a registry

• Web server with access to the server classes• Start the applications

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

42

Java Remote Interfaces

• Remote Objects• Any non-private object accepting method calls• Needs interface defining their exported behaviour

• Defining Remote Interfaces• Extends java.rmi.Remote• Each method throwsjava.rmi.RemoteException

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

43

Java Remote Interface Example

import java.rmi.Remote;import java.rmi.RemoteException;

public interface MyRemote extends Remote{

public Object getXXXX() throws RemoteException;

}

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

44

Java RMI Clients

• Are just like any other Java Programs!• Can have access to a running Server

• Using Naming• e.g.

testServer = (xmplServer)Naming.lookup(”rmi://somehost/xmplServer”);

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

45

Java RMI and Threading

• Server Processes• Threads are generated and controlled automatically• Multiple Threads for multiple connections

• Client Processes• Threads are created upon service requests

• Synchronisation• Not handled automatically!

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

46

Synchronising RMI Threads

• Critical Regions are• Global Server Methods• Global Server Data

• Need to be protected• To avoid inconsistencies• To prevent race conditions

• Use Synchronisations constructs• Semaphores, synchronized, …

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

47

Java RMI Programming

• The RMI Development Steps are:• Identify remote objects and their interfaces• Provide implementation for distributed objects• Compile sources• Use rmic to create stubs• Start a registry

• Web server with access to the server classes• Start the applications

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

48

Java Remote Interfaces

• Remote Objects• Any non-private object accepting method calls• Needs interface defining their exported behaviour

• Defining Remote Interfaces• Extends java.rmi.Remote• Each method throwsjava.rmi.RemoteException

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

49

Implementing Java RemoteInterfaces

• Classes need to• Declare the remote interface they implement• Provide a constructor• Provide implementations for all remote methods in

the interface

• Remote Object Implementations• Usually export themselves to RMI• Optionally register with the naming service

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

50

Remote Object Activation

• Problems Addressed by ROA:• What happens if remote object crashes?

• Need to be restarted from scratch!• All instance data lost

• Remote objects are resource intensive!• But usually only needed on occasionOnly activate as needed

• Lazy Activation• Addresses these efficiency issues

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

51

Java Lazy Activation

• New API in Java 2• Create remote objects that instantiate only when

needed (conserve resources)• No need for manual start:• Extend java.rmi.activation.Activatable:

public myclass(ActivationID id, MarshalledObjectdata) throws RemoteException

• Register actionable object w/ activation daemon• Start the RMI activation daemon rmid

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

52

Java Activatable Objects

• Requirements• An activation ID class variable

ActivationID id;

• A Serializable data variable of any type, e.g.Date myMarshalledObject;

• rmid can re-instantiate an object by its ID• Client cannot call default constructor

Has no idea about activation ID or marshalled object

Create another constructor for client

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

53

Java Object Activation Server

• Codebase URL:String codebase = ”http://localhost/”;

• Create activation group descriptor forserver:ActivationGroupDesc lAg = new

ActivationGroupDescriptor(null, null);

• Register activation group:ActivationGroupID lAgID =

ActivationGroup.getSystem().registerGroup(lAg);

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

54

Java Object Activation Server (2)

• Create actual activation group:ActivationGroup.createGroup(lAgID, lAg, 0);

• Create MarshalledObject usedto initialise the server object:Date lInitObject = new Date();MarshalledObject lMO = newMarshalledObject(lInitObject);

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

55

Java Object Activation Server (3)

• Construct Activation DescriptorActivationDesc lAD = new ActivationDesc(lAgID,

”ExampleDateServ”, codebase, lMO);

• Register with activation daemon:ExampleDateServ lDateServer =

(ExampleDateServ)Activatable.register(lAD);

• Bind instance to a name:• Naming.rebind(”DateServer”, lDateServer);

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

56

Java Object Activation Setup

• Compilejavac *.java

• Generate Stubs and Skeletonsrmic ExampleDateServ

• Start RMI registry and Daemonrmiregistryrmid

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

57

Running Java Server and Client

• Start Server Applicationjava -Djava.security.policy=all.policyExampleDateServ

• Start Client Applicationjava -Djava.security.policy=all.policyExampleDateClient

• Start Client Appletappletviewer Test.html

P3 Lecture #14 May2006

Copyright © 2002-2006 René Hexel.All rights reserved.

58

CORBA in Java

• RMI-IIOP• Access Java RMI interfaces via CORBA• Use -iiop option on rmic

• Java to IDL• Use -idl option on rmic

• IDL to Java• idlj compiler