30
Remote Procedure Call and Remote Method Invocation INF 123 – Software architecture [email protected] 1

Remote Procedure Call and Remote Method Invocation

  • Upload
    duy

  • View
    69

  • Download
    0

Embed Size (px)

DESCRIPTION

Remote Procedure Call and Remote Method Invocation. INF 123 – Software architecture [email protected]. Outline. RPC theory Sun RPC XML RPC RMI. RPC: theory. The Problem. 1980s: Internet and web servers How can a client make a server do something? - PowerPoint PPT Presentation

Citation preview

Page 1: Remote Procedure Call and Remote Method Invocation

1

Remote Procedure Calland Remote Method Invocation

INF 123 – Software [email protected]

Page 2: Remote Procedure Call and Remote Method Invocation

2

Outline

• RPC theory• Sun RPC• XML RPC• RMI

Page 3: Remote Procedure Call and Remote Method Invocation

3

RPC: THEORY

Page 4: Remote Procedure Call and Remote Method Invocation

4

The Problem

• 1980s: Internet and web servers• How can a client make a server do something?

– Sockets are used to transmit data, not functions– Sending function code as text is a bad idea

• Client in Python, server in C#?• Server runs eval(evil_code)? Eval is evil …

Page 5: Remote Procedure Call and Remote Method Invocation

5

Dr. Eval

eval(readline()), eval(message), …

Page 6: Remote Procedure Call and Remote Method Invocation

6

Reminder: main and subroutines

• Main calls subroutines• Subroutines return values• Subroutines have no side-effects

• AKA “call and return”• “Not a real architecture” <- really? Why?

Page 7: Remote Procedure Call and Remote Method Invocation

7

RPC

• Transparently call a procedure located in another process or machine

• “make the invocation of a procedure on a remote machine appear as though it were local” http://people.sju.edu/~jhodgson/netw/rpc.html

Page 8: Remote Procedure Call and Remote Method Invocation

8

Solution: RPC

r = foo(a, b)

define foo(a, b) …end

caller

callee

program

host

Local Procedure Calls

r = foo(a, b)

define foo(a, b) …end

caller

callee

program

host 1

host 2program

Remote Procedure Calls

Caller stub

Callee stub

Page 9: Remote Procedure Call and Remote Method Invocation

9

RPC with words

• The local procedure calls a caller stub• The caller stub serializes the call (function name +

arguments) and sends it to the callee stub on a remote machine

• The callee stub calls the remote procedure• The remote procedure does its job, and gives the return

value to the callee stub• The callee stub sends the value back to the caller stub• The caller stub passes that value back to the local

procedure

Page 10: Remote Procedure Call and Remote Method Invocation

10

Stubs

• Caller = client (initiate, active end)• Callee = server (listen, passive end)• The stubs are coupled• Client stub must know server stub’s IP and port

– Sometimes, a port is reserved for RPC• Stubs are usually generated by tools

– Rpcgen for Sun RPC, wscompile for JAX-RPC, …• Other naming: server stub = skeleton

Page 11: Remote Procedure Call and Remote Method Invocation

11

RPC

• RPC belongs to the application layer

Procedure Interface Definition (in Interface Def Language)

Callee Program (in server language)

Caller program (in client language)

Client Stub(in client language)

Server Stub(in server language)

generate

Network OS Network OS

call

Usual code

RPC code

call

Serialize and send Deserialize and receive

Page 12: Remote Procedure Call and Remote Method Invocation

12

RPC as middleware

Application

RPC stubs

RPC Request-reply protocol, and RPC serialization format

OS

Middleware layers

Middleware: software between OS and application

Page 13: Remote Procedure Call and Remote Method Invocation

13

RPC is not P2P

• In both cases, nodes are clients and servers to each other

• But in RPC, the goal is to execute a function remotely– A client and a server could use RPC– Peers could use RPC with each other– Peers could simply exchange data, and not use RPC

• RPC stubs use a client-server paradigm– With a request-response messaging pattern

Page 14: Remote Procedure Call and Remote Method Invocation

14

SUN RPC: DATE/TIME SERVER EXAMPLE IN C

From Unix Network Programming, by Stevens et al.

Page 15: Remote Procedure Call and Remote Method Invocation

15

Sun RPC• AKA Open Network Computing• Created in the mid 80s by Sun• The developer writes:

– interface.x, the interface definition, in Interface Definition Language

– callee.c, the server with the remote procedure– caller.c, the client calling the remote procedure

• The rpcgen compiler generates:– interface.h, to be imported by caller.c and callee.c– callee_stub.c– caller_stub.c

Page 16: Remote Procedure Call and Remote Method Invocation

16

XDR

• eXternal Data Representation• Created in the mid 80s by Sun• Binary encoding• The data format exchanged between stubs• Serialization format used by:

– Network File System (created by Sun)– SpiderMonkey (Firefox JS engine, XDR used to

exchange compiled JS)– R (stats tool)

Page 17: Remote Procedure Call and Remote Method Invocation

17

Sun RPC

caller.c

interface.x

callee_stub.ccaller_stub.c

callee.c

Import function signature

Call Call

network

Send XDR Recv XDR

Generate

interface.h

Generate

generated

Hand-writtenImport function signature

Page 18: Remote Procedure Call and Remote Method Invocation

18

caller.c (I adapted it)server = ‘167.56.7.99’;

client = create_client(server, DATE_PROG, DATE_VERS, "udp");

bin_date = get_binary_date(NULL, client);

str_date = bin2str_date(bin_date, client);

printf("time on server %s: %s", server, *str_date); http://stencel.mimuw.edu.pl/so/3_rpc/G_rpcgen/

http://web.cs.wpi.edu/~rek/DCS/D04/SunRPC.html

Page 19: Remote Procedure Call and Remote Method Invocation

19

caller_stub.c (generated and adapted)#include "date.h"

static struct timeval TIMEOUT = { 25, 0 };

long * get_binary_date(void *argp, CLIENT *clnt) {static long clnt_res; // return value is a long

memset((char *)&clnt_res, 0, sizeof (clnt_res)); // allocate memory cli_call = client_call(clnt, GET_BIN_DATE, // RPC function number(xdrproc_t) xdr_void, // argument type (caddr_t) argp, // argument value (xdrproc_t) xdr_long, // return value type(caddr_t) &clnt_res, // store in this memory area TIMEOUT);if (cli_call != RPC_SUCCESS) {return (NULL);}

return (&clnt_res); }

Page 20: Remote Procedure Call and Remote Method Invocation

20

Sun RPC API

• client_call • create_client

• And more …

Page 21: Remote Procedure Call and Remote Method Invocation

21

XML RPC

Page 22: Remote Procedure Call and Remote Method Invocation

22

XML RPC

• Over HTTP POST• 1998, many language bindings• http://xmlrpc.scripting.com/spec.html

Page 23: Remote Procedure Call and Remote Method Invocation

23

XML RPC - request

POST /RPC2 HTTP/1.1User-Agent: Frontier/5.1.2 (WinNT)Host: betty.userland.comContent-Type: text/xmlContent-length: 181

<?xml version="1.0"?> <methodCall>

<methodName>examples.getStateName</methodName> <params>

<param> <value><int>41</int></value>

</param> </params>

</methodCall>

Page 24: Remote Procedure Call and Remote Method Invocation

24

XML RPC – success responseHTTP/1.1 200 OK Connection: close Content-Length: 158 Content-Type: text/xml Date: Fri, 17 Jul 1998 19:55:08 GMT Server: UserLand Frontier/5.1.2-WinNT

<?xml version="1.0"?> <methodResponse>

<params> <param>

<value><string>South Dakota</string></value> </param>

</params> </methodResponse>

Page 25: Remote Procedure Call and Remote Method Invocation

25

XML RPC – failure responseHTTP/1.1 200 OK Connection: close Content-Length: 426 Content-Type: text/xml Date: Fri, 17 Jul 1998 19:55:02 GMT Server: UserLand Frontier/5.1.2-WinNT

<?xml version="1.0"?> <methodResponse>

<fault> <value> <struct>

<member> <name>faultCode</name>

<value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.

</string> </value></member>

</struct> </value> </fault>

</methodResponse>

Page 26: Remote Procedure Call and Remote Method Invocation

26

XML RPC vs XML: bandwidth <struct> <member> <name>ROOM_ID</name> <value> <int>1</int> </value> </member> <member> <name>CODE</name> <value> <string>MR-101</string> </value> </member> <member> <name>NAME</name> <value> <string>Math Room</string> </value> </member> <member> <name>CAPACITY</name> <value> <int>30</int> </value> </member> </struct>

<room> <ROOM_ID>1</ROOM_ID> <CODE>MR-101</CODE> <NAME>Math Room</NAME> <CAPACITY>30</CAPACITY></room>http://stackoverflow.com/questions/1376688/what-is-the-benefit-of-xml-rpc-over-plain-xml

class Room { int id=1; String code="MR-101"; String name="Maths room"; int capacity=30; };

Page 27: Remote Procedure Call and Remote Method Invocation

27

REMOTE METHOD INVOCATION

Page 28: Remote Procedure Call and Remote Method Invocation

28

Proxy objects

• Equivalent of stub, but for client objects• Goal: make RMI transparent to clients

– Proxy objects behave like a local object to the invoker– Instead of executing, proxy objects serialize arguments

and forward the call + arguments to the remote object• Human writes class interface with method names• RMI compiler generates proxy classes, skeleton

classes, and server dispatcher

Page 29: Remote Procedure Call and Remote Method Invocation

29

Remote Method Invocation

Object A

Object B

Comm. Module

Comm. Module

Skeletonfor B’s Class

Server Process

Client Process

ProxyObject

B Remote

Reference Module

Dispatcher

Remote Reference Module

MIDDLEWARE

Proxy object is a hollow container of Method names.

Remote Reference Module translates between local and remote object references.

Dispatcher sends the request to Skeleton Object

Skeleton deserializes parameters, sends it to the object, & marshals the results for return

http://courses.engr.illinois.edu/cs425/fa2012/L8.fa12.ppt

Page 30: Remote Procedure Call and Remote Method Invocation

30

More• www.cs.bham.ac.uk/~

bxb/courses/02_03_DS/Lecture05_Slides.pdf• http://courses.engr.illinois.edu/cs425/fa2012/L8.fa12.ppt• www2.hh.se/staff/vero/concurrent/lectures/lecture6.pdf• Implementing Remote Procedure Calls, Birrell et al. 1984• https://docs.python.org/2/library/xmlrpclib.html• http://

technet.microsoft.com/en-us/library/cc738291%28v=WS.10%29.aspx

• http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXRPC3.html