Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Rmi - RPC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

RMI - RPC

Remote and Local Method Invocations

Object Process
Process Process

Host A
remote local C
E
invocation invocation local
invocation remote
invocation F
B local
A
invocation D
Host B

Local invocation=between objects on same process.


Has exactly once semantics
Remote invocation=between objects on different processes.
Ideally also want exactly once semantics for remote invocations
But difficult (why?)
Local Objects
• Within one process address space
• Object
• consists of a set of data and a set of methods.
• E.g., C++ object, Java object.
• Object reference
• an identifier via which objects can be accessed.
• i.e., a pointer (e.g., virtual memory address within process)
• Interface
• provides a definition of the signatures of a set of methods (i.e., the
types of their arguments, return values, and exceptions) without
specifying their implementation.
Failure Modes of RMI/RPC
Request

correct lost
Execute request
function

Reply

Request Channel
Execute fails
during
Execute, crash reply
Crash before Reply
reply

Request Client
machine
Execute fails
crash before
Crash before
Reply receiving
execution reply

(and if request is received more than once?)


RMI/RPC - Motivation
v You write a program where objects call each other
v Works well if the program runs on one process
v What if you split your objects across multiple
processes?
v Solution
• RMIs: Remote Method Invocations (Object-based)
• RPCs: Remote Procedure Calls (non-Object-based)
Middleware Layers

Applications

RPCs and RMIs, e.g., CORBA


Middleware
Request reply protocol layers=
Provide
External data representation support to the
application
Operating System
Run at all servers
@user level
RPC = Remote Procedure Call
RMI=Remote Method Invocation
CORBA=Common Object Request Brokerage Architecture
Remote Method Invocation (RMI)
Client Proxy Proxy object is a hollow
Process Object container of Method
B names.
Object A Remote
Reference Remote Reference
Module Module translates
Comm. between local and
Module remote object
references.

Server Dispatcher sends the


Process Comm. Remote request to Skeleton
Module Reference
Module
Object
Skeleton unmarshals
Dispatcher parameters, sends it
Skeleton Object to the object, &
for B s B
marshals the results
Class for return

MIDDLEWARE
Remote Objects

• May cross multiple process address spaces


• Remote objects
• objects that can receive remote invocations.
• Remote object reference
• an identifier that can be used globally throughout a distributed
system to refer to a particular unique remote object.
• Remote interface
• Every remote object has a remote interface that specifies which of
its methods can be invoked remotely.
Proxy
• Is responsible for making RMI transparent to clients by
behaving like a local object to the invoker.
• The proxy implements (Java term, not literally) the methods in the
interface of the remote object that it represents. But,…
• Instead of executing an invocation, the proxy forwards it to a
remote object.
Remote Reference Module
• Is responsible for translating between local and remote object references and for
creating remote object references.
• Has a remote object table
• An entry for each remote object held by any process. E.g., B at P2.
• An entry for each local proxy. E.g., proxy-B at P1.
• When a new remote object is seen by the remote reference module, it creates a
remote object reference and adds it to the table.
• When a remote object reference arrives in a request or reply message, the remote
reference module is asked for the corresponding local object reference, which may
refer to either to a local proxy or a remote object.
• In case the remote object reference is not in the table, the RMI software creates a new
proxy and asks the remote reference module to add it to the table.
Remote Procedure Call (RPC)

v Similar to RMIs, but for non-OO/non-object-based


scenarios
v Procedure call that crosses process boundary
v Client process calls for invocation of a procedure at the
server process.
q Semantics are similar to RMIs – at least once, at most once,
maybe
q Format of the message is standard (marshaled), uses request-
reply
RPC - Remote Procedure Call What is it?
• RPC is a technique for constructing distributed, client-server
based applications.
• It is based on extending the notion of conventional, or local
procedure calling.
• As “remote” suggests, the called procedure need not to exist in
the same address space as the calling procedure.
• The two processes may be on the same system, or they may be on
different systems with a network connecting them.
• By using RPC architectures, programmers of distributed
applications avoid the details of the interface with the network.
Finding RPCs
Finding An RPC:
CLIENT
Client
RPCs live on specific hosts
Stub at specific ports.
Client
Program Port mapper on the host
Comm.
Module maps from RPC name to
port#
When a server process is
initialized, it registers its
RPCs (handle) with the port
Comm. SERVER mapper on the server
Module
A client first connects to
port mapper (daemon on
Dispatcher standard port) to get this
Server
procedure handle
Server
Stub The call to RPC is then
made by connecting to the
corresponding port
Stubs
v Stubs are generated automatically from
interface specifications.
v Stubs hide details of (un)marshalling from
application programmer & library code
developer.
v Client Stubs perform marshalling into
request messages and unmarshalling from
reply messages
v Server Stubs perform unmarshalling from
request messages and marshalling into reply
messages
v Stubs also take care of invocation
XML - RPC
www.xmlrpc.com
What is XML-RPC?

It's a spec and a set of implementations that allow software running on


disparate operating systems, running in different environments to make
procedure calls over the Internet.

It's remote procedure calling using HTTP as the transport and XML as
the encoding. XML-RPC is designed to be as simple as possible, while
allowing complex data structures to be transmitted, processed and
returned.

See: http://xmlrpc.scripting.com/spec
XML-RPC and Java: The basic idea

XML
A Java package with A Java package with
specialist methods. specialist methods.
Main program Method: getGreeting()
(Client) (Server)

Specialist methods in the Java package org.apache.xmlrpc link


the local code to the remote code.
Local Method Call
(not distributed)

public class HelloWorldExample {


public static String getGreeting() {
return "Hello from this method.";
}
calls
public static void main (String [] args) {
String str = getGreeting();
System.out.println(str);
}
}
Local Method Call (not distributed)

public class HelloWorldExample {


public static String getGreeting() { Method
return "Hello from this method.";
}

public static void main (String [] args) {


String str = getGreeting();
System.out.println(str);
}
}
Caller of the Method
Remote Method Call
(distributed)
Server

public class HelloWorldExample {


public static String getGreeting() {
return "Hello from this method.";
}

public static void main (String [] args) {


String str = getGreeting();
System.out.println(str);
}
}

Client
Different Programming
Languages?
Java

public class HelloWorldExample {


public static String getGreeting() {
return "Hello from this method.";
}

void main () {
cout << getGreeting() << endl;
}
}

C++
Client and Server have to understand each other

Hello, what ?
time is it? Server

Client
Client and Server agree on a common
language.

Wieviel Uhr What’s the Quelle heure est-il?


ist es? 7pm!
time?

Client Server
The client encodes the information in the
“common language”, the server decodes the
information.
Decode
Encode
Wieviel Uhr What’s the Quelle heure est-il?
ist es? time?

Client Server
XML-RPC uses XML as a common language to
transmit data

Encode Decode

Java Java
C++ XML C++
Python Python
etc. etc.

Client Server

26
That explains the XML in XML-RPC
but what means RPC?

RPC means “Remote Procedure Call”,


that means you call a procedure (i.e. method,
function) on a different machine.
Typical flow of control in RPCs
o The client makes a procedure call that sends a request to the server and
waits.
o The thread is blocked from processing until either a reply is received, or
it times out.
o When the request arrives, the server calls a dispatch routine that
performs the requested service, and sends the reply to the client.
o After the RPC call is completed, the client program continues.
The specification, < 1800 words, is lightweight
and easy to learn.
Contains many examples.

• It's a spec and a set of implementations that allow


software running on disparate operating systems,
running in different environments to make
procedure calls over the Internet
• It's remote procedure calling using HTTP as the
transport and XML as the encoding. XML-RPC is
designed to be as simple as possible, while allowing
complex data structures to be transmitted,
processed and returned.

03/09/2018 06:17:54 29
Languages include:
C/C++, Java, Perl, Python, Frontier, Lisp, PHP,
Microsoft .NET, Rebol, Real Basic, Tcl, Delphi,
WebObjects and Zope

• It's a spec and a set of implementations that allow


software running on disparate operating systems,
running in different environments to make procedure
calls over the Internet
• It's remote procedure calling using HTTP as the
transport and XML as the encoding. XML-RPC is
designed to be as simple as possible, while allowing
complex data structures to be transmitted, processed
and returned.

03/09/2018 06:17:54 30
Uses existing protocols (HTTP) and a well
established framework (XML).

• It's a spec and a set of implementations that allow


software running on disparate operating systems,
running in different environments to make procedure
calls over the Internet
• It's remote procedure calling using HTTP as the
transport and XML as the encoding. XML-RPC is
designed to be as simple as possible, while allowing
complex data structures to be transmitted, processed
and returned.

03/09/2018 06:17:54 31
The following data structures are supported: integer,
boolean, string, double, date & time, base64 binaries,
structs, arrays.

• It's a spec and a set of implementations that allow


software running on disparate operating systems,
running in different environments to make procedure
calls over the Internet
• It's remote procedure calling using HTTP as the
transport and XML as the encoding. XML-RPC is
designed to be as simple as possible, while allowing
complex data structures to be transmitted, processed
and returned.

03/09/2018 06:17:54 32
1. Writing Server and Client

1. Write 2. Identify 3. Communicate

Client Client Client


kdfjshkdsahfkdashfk

Server
ajshfkdsjhkjsahfkds
hfkhasdfkashfkdjsah
fkshdfkhsafkjshfkds
100
101
kdfjshkdsahfkdashfk
fhakjdhfksjdhfakhsf 011
ajshfkdsjhkjsahfkds
kjsdfkdasjfkdasjksda 011
hfkhasdfkashfkdjsah
jfkdsafhksajhkdjfhk 101
fkshdfkhsafkjshfkds
asjkdsahksdjfksakdj
fhakjdhfksjdhfakhsf 01
sfksakshfdkjfdkdsfh
kjsdfkdasjfkdasjksda
jfkdsafhksajhk
assfksakshfdkjf
Server Server
Example: An XML-RPC client/server
application in Java.

• The Java package org.apache.xmlrpc provides


classes to implement an XML-RPC client and an
XML-RPC server. The package can be found at
http://ws.apache.org/xmlrpc/
• A copy of the package is under the name
cis69mc.jar on http://perisic.com/xmlrpc .
A Java “Hello World” Server
import org.apache.xmlrpc.*;

public class HelloWorldServer {


public String getGreeting() {
return "Hello from far away.";
}

public static void main (String [] args) {


try {
WebServer server = new WebServer(80);
server.addHandler("hello", new HelloWorldServer());
server.start();
}
catch (Exception ex) {
System.err.println("There is a problem:" + ex);
}
}
}
A Java “Hello World” Server
import org.apache.xmlrpc.*;

public class HelloWorldServer {


public String getGreeting() {
return "Hello from far away.";
}

public static void main (String [] args) {


try {
WebServer server = new WebServer(80);
server.addHandler("hello", new HelloWorldServer());
server.start();
}
catch (Exception ex) {
System.err.println("There is a problem:" + ex);
}
} n The package org.apache.xmlrpc
}
contains the class WebServer for a
XML-RPC Server implementation
03/09/2018 06:17:54 36
A Java “Hello World” Server
import org.apache.xmlrpc.*;

public class HelloWorldServer {


public String getGreeting() {
return "Hello from far away.";
}

public static void main (String [] args) {


try {
WebServer server = new WebServer(80);
server.addHandler("hello", new HelloWorldServer());
server.start();
}
catch (Exception ex) {
System.err.println("There is a problem:" + ex);
}
} • The procedure that is called remotely is implemented as a public
} method in a class.
• An instance of this class is then associated with a handler that is
accessible by the client.
A Java “Hello World” Server
import org.apache.xmlrpc.*;

public class HelloWorldServer {


public String getGreeting() {
return "Hello from far away.";
}

public static void main (String [] args) {


try {
WebServer server = new WebServer(80);
server.addHandler("hello", new HelloWorldServer());
server.start();
}
catch (Exception ex) {
System.err.println("There is a problem:" + ex);
}
• }The server is initialised by the port number (here: 80).
}
• The server starts to listen at port 80.
A Java “Hello World” Server
import org.apache.xmlrpc.*;

public class HelloWorldServer {


public String getGreeting() {
return "Hello from far away.";
}

public static void main (String [] args) {


try {
WebServer server = new WebServer(80);
server.addHandler("hello", new HelloWorldServer());
server.start();
}
catch (Exception ex) {
System.err.println("There is a problem:" + ex);
}
} • When problems occur (ie. a different process already listening on
} port 80) an Exception is thrown and has to be caught.
About Port 80

• An advantage of using port 80 is that it is usually not


blocked by firewalls. Port 80 is the default port used for the
World Wide Web. Using port 80 justifies the word “Web”
for this kind of Service: Web Service.
• Note that you cannot have two programs using the same
port on the same computer. That means, for instance, that
you cannot run two XML-RPC server at the same time
(however you may use different ports, e.g. 81 or 8080)
A Java XML-RPC Client for the “HelloWorld” Web Service

import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
Object result =
server.execute("hello.getGreeting", params);
System.out.println("Result from Server: "+
result.toString());
} catch (Exception ex) {
System.err.println("HelloClient: " + ex);
}
}
} 03/09/2018 06:17:55 41
A Java XML-RPC Client for the “HelloWorld” Service
import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
Object result =
server.execute("hello.getGreeting", params);
System.out.println("Result from Server: "+
result.toString());
}
catch (Exception ex) {
System.err.println("HelloClient: " + ex);
• The Java
} package org.apache.xmlrpc contains classes for XML-RPC Java
} clients and XML-RPC server. ie. XmlRpcClient.
} • The package java.util is necessary for the Vector class.
A Java XML-RPC Client for the “HelloWorld” Server
import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
Object result =
server.execute("hello.getGreeting", params);
System.out.println("Result from Server: "+
result.toString());
}
catch
• This line (Exception
sends ex) { to the server. The procedure getGreeting()
the request
is called on theSystem.err.println("HelloClient: " + ex); The return value
server as if it were a local procedure.
}
of a procedure call is always an Object.
}
} • “hello” denotes a handler that is defined in the server.
A Java XML-RPC Client for the “HelloWorld” Server
import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
Object result =
server.execute("hello.getGreeting", params);
System.out.println("Result from Server: "+
result.toString());
}
catch (Exception ex) {
• The parameters of System.err.println("HelloClient:
the procedure call are always" collected
+ ex); in a Vector.
}
• } In this example the procedure has no parameters.
}
A Java XML-RPC Client for the “HelloWorld” Server
import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
Object result =
server.execute("hello.getGreeting", params);
System.out.println("Result from Server: "+
result.toString());
}
catch (Exception ex) {
System.err.println("HelloClient: " + ex);
}
}
}
A Java XML-RPC Client for the “HelloWorld” Server
import org.apache.xmlrpc.*;
import java.util.Vector;

public class HelloWorldClient {


public static void main (String [] args) {
try {
XmlRpcClient server = new
XmlRpcClient("http://localhost/RPC2");
Vector params = new Vector();
• The XmlRpcClient Object
classresult =
is constructed by specifying the “web address”
server.execute("hello.getGreeting",
of the server machine followed by /RPC2. E.g. params);
System.out.println("Result from Server: "+
• localhost - means the local machine.
result.toString());
• An} IP number, e.g. 194.80.215.219
• A catch
name,(Exception ex) {
e.g. cis69.dyndns.org
System.err.println("HelloClient:
• All of the above, followed by a port number, " + e.g.
ex);
}
cis69.dyndns.org:8080. The default port is 80.
}
}
2. Identification of the Remote Service

1. Write 2. Identify 3. Communicate

Client Client Client


kdfjshkdsahfkdashfk

Server
ajshfkdsjhkjsahfkds
hfkhasdfkashfkdjsah
fkshdfkhsafkjshfkds
100
101
kdfjshkdsahfkdashfk
fhakjdhfksjdhfakhsf 011
ajshfkdsjhkjsahfkds
kjsdfkdasjfkdasjksda 011
hfkhasdfkashfkdjsah
jfkdsafhksajhkdjfhk 101
fkshdfkhsafkjshfkds
asjkdsahksdjfksakdj
fhakjdhfksjdhfakhsf 01
sfksakshfdkjfdkdsfh
kjsdfkdasjfkdasjksda
jfkdsafhksajhk
assfksakshfdkjf
Server Server
Localhost and remote hosts
• In our example Server and Client are running on the same
Computer. Hence we can use the address
http://localhost/RPC2.
• If you are using the program on two machines that are
connected to the Internet, use the IP address of the Server
machine to make the connection (for instance
http://123.45.67.89/RPC2). You can find the IP address of a
computer with the program ipconfig.
• If the Server machine is registered with a name (as ie.
http://perisic.com ) You can use the name instead of the IP
address (ie. http://perisic.com/RPC2).
• The suffix “/RPC2” is used by some Web Servers to
discriminate between XML-RPC calls and other incoming calls
(for instance requests for HTML pages)
What do we need to tell the client so that it
?
can connect to the Server?

• For a remote procedure call via the XML-RPC


protocol we need the following information on the
client side:
• The location of the remote service.
• The name of the remote procedure.
• The parameters of the remote procedure.
• The return type of the remote procedure.
3. Communication between Client and
Server

1. Write 2. Identify 3. Communicate

Client Client Client


kdfjshkdsahfkdashfk

Server
ajshfkdsjhkjsahfkds
hfkhasdfkashfkdjsah
fkshdfkhsafkjshfkds
100
101
kdfjshkdsahfkdashfk
fhakjdhfksjdhfakhsf 011
ajshfkdsjhkjsahfkds
kjsdfkdasjfkdasjksda 011
hfkhasdfkashfkdjsah
jfkdsafhksajhkdjfhk 101
fkshdfkhsafkjshfkds
asjkdsahksdjfksakdj
fhakjdhfksjdhfakhsf 01
sfksakshfdkjfdkdsfh
kjsdfkdasjfkdasjksda
jfkdsafhksajhk
assfksakshfdkjf
Server Server
Client Server

<?xml version="1.0" encoding="ISO-8859-1"?>


<methodCall>
<methodName>hello.getGreeting</methodName>
<params></params>
</methodCall> The name of the method
that is to be called on the Server.

(empty) list of parameters.

Request from Client to Server


Client Server

<?xml version="1.0" encoding="ISO-8859-1"?>


<methodResponse>
<params>
<param>
<value>Hello from far away.</value>
</param>
</params>
</methodResponse> The String that is returned
to the Client from the Server.

Response from the Server to the Client


Simple Object Access Protocol

• SOAP is another protocol for Client/Server applications.


• The general principle is similar as XML-RPC by using
XML as common language.
• Also labelled as “lightweight”, but the specification is >
77000 words.
SOAP and XML-RPC

• Wikipedia (2005): “[XML-RPC] was first created by


Dave Winer of UserLand Software in 1995 with
Microsoft. However Microsoft considered it too
simple, and started adding functionality. After several
rounds of this, the standard was no longer so simple,
and became what is now SOAP.”
• So, XML-RPC is
• An excellent starting point for understanding the principles
behind SOAP.
• More lightweight and easier to learn.
Assignment

Develop a simple application of XML-RPC.


Assume you have Calculator App in your server. Your client
want to use the provided “multiply” function of your
Calculator App.
https://www.tutorialspoint.com/xml-rpc/

You might also like