Upload
duongmien
View
230
Download
0
Embed Size (px)
Citation preview
Imed Bouchrika. Distributed Objects , Uni of Souk-Ahras 2014-2015 http://www.imed.ws
3. Remote Procedure Call
Master II – Software Engineering
Imed BouchrikaDept of Mathematics & Computer Science
University of Souk-Ahras
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Addition Server using Socketsimport java.io.*;
import java.net.*;
class Server implements Runnable
{
Socket connectionSocket;
public Server(Socket s){
try{
System.out.println("Client Got Connected " );
connectionSocket=s;
}catch(Exception e){e.printStackTrace();} }
public void run(){
try{
BufferedReader reader =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
BufferedWriter writer=
new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));
writer.write("*** Welcome to the Calculation Server (Addition Only) ***\r\n");
writer.write("*** Please type in the first number and press Enter : \r\n");
writer.flush();
String data1 = reader.readLine().trim();
writer.write("*** Please type in the second number and press Enter : \r\n");
writer.flush();
String data2 = reader.readLine().trim();
int num1=Integer.parseInt(data1);
int num2=Integer.parseInt(data2);
int result=num1+num2;
System.out.println("Addition operation done " );
writer.write("\r\n=== Result is : "+result);
writer.flush();
connectionSocket.close();
}catch(Exception e){e.printStackTrace();}
}
public static void main(String argv[]) throws Exception
{
System.out.println("Threaded Server is Running " );
ServerSocket mysocket = new ServerSocket(5555);
while(true) {
Socket sock = mysocket.accept();
Server server=new Server(sock);
Thread serverThread=new Thread(server);
serverThread.start(); } }
}
2
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Local Invocation of Methods ? A simple class Addition with a method to add two numbers.
Public class Addition{
public Addition(){ //Constructor }
public int add(int a, int b){
return a+b;
}
public static void main(String [] args){
Addition local = new Addition();
int result=local.add(9,10);
System.out.println(“The result is :”+result);
}
}
3
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Local Invocation of Methods ? To Invoke the Add method for the Addition Object
Public class Hello{
public static void main(String [] args){
Addition local = new Addition();
int result=local.add(9,10);
System.out.println(“The result is :”+result);
}
}
4
Where shall we put the Addition Class ?
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Local Invocation of Methods ? The method add is called locally :
Within the same machine that has the
implementation code.
Local Invocation involves:
Parameters passing
Local variables
Return Data.
5
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Local Invocation of Methods ? What if we want to call or invoke the method ADD
Remotely ( From a different machine ) ?
From a machine that does not have the implementation
code for the add operation.
Using Sockets ? With Proxies at the both ends of the
Server and Client ?
Would be extremely difficult to maintain for complex
and large systems.
The solution is to use: Remote Procedure Call (RPC)
6
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPCIn 1984, Birrell and Nelson devised a mechanism to allow
programs to call procedures on other machines.
Process on machine A can call a procedure on machine B.
The process on A is suspended and execution continues on
B. When B returns, the return value is passed to A and A
continues execution.
This mechanism is called Remote Procedure Call (RPC).
To the programmer, the goal is that it should appear as if a
normal procedure call is taking place.
7
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws RPC Slide 8
Blocking state
Process A Process B
request
reply
Executing state
Call procedure and wait for reply
Receive request and start process execution
Send reply and wait for next execution
Resume execution
Remote Procedure Call : RPC
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
Remote Invocation
Remote Procedure Call ( RPC )
Is a way of communication in which a computer program
can invoke a subroutine or function
To execute on a remote machine and returns the results
to the invoker if any.
The programmer usually write the same code as for the
local case
whilst all the complexity is hidden and managed by the
middleware
9
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPCThe RPC mechanism in short :
Create stub ( Proxy , Broker ....) functions to make it
appear to the user that the call is local
Stub function contains ONLY the function’s interface
The core of the functionalities or Business logic, still
resides on the server side.
10
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
MarshallingFor simple communication, we can send a string of
commands : plain formatted ASCII text.
Example : GET HTTP/1.0 /index.html
If the application is complex and object oriented ? Problem
with structures of data or objects
We need to push the objects through the connection pipe in
a format that the connection can handle.
The solution is called : Marshalling
11
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Marshalling of DataMarshalling is a process that converts a complex object
into a byte of stream and then converts it back into an
object using the reverse process.
In Java, This is implemented using Serialization
For RPC Marshalling is needed for passing methods
arguments as well as returned data through the network
stream and vice versa.
12
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
13
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
14
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
15
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
16
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
17
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
18
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
19
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
20
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
21
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPC
22
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
2-8
Addition Example via RPC
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Remote Procedure Call : RPCBenefits of RPC
Writing applications is simplified
RPC hides all network code into stub functions
Application programmers don’t have to worry
about details
Sockets, port numbers, byte ordering
24
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Programming with RPCLanguage support
Most programming languages (C, C++, Java, ...) have no
concept of remote procedure calls
Language compilers will not generate client and server
stubs
Common solution:
Use a separate compiler to generate stubs (pre-compiler)
In Java, we use rmic for generating stubs(Not true Now)
25
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Issues with RPCParameter passing
Pass by value
Easy: just copy data to network message
Pass by reference
Makes no sense without shared memory
Representing data
Remote machine may have:
Different byte ordering
Different sizes of integers and other types
Different floating point representations
Different character sets
....
26
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Issues with RPCDelivery guarantees
Retry request message:
whether to retransmit the request message until either a reply or the server
is assumed to have failed;
Duplicate filtering :
when retransmission are used, whether to filter out duplicates at the
server;
Retransmission of replies:
whether to keep a history of reply messages to enable lost replies to be
retransmitted without re-executing the server operations.
27
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
RPC Call SemanticsThis ensure the Remote Procedure is called exactly once if the
server does not crash during execution of the Remote Procedure.
When the server crashes during the Remote Procedure
execution, the partial execution may lead to erroneous results.
In this case, we want the effect that the Remote Procedure has
not been executed at all.
We consider usually the following errors:
Lost request message
Lost reply message
Server crash
Client crash28
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
RPC Call SemanticsMaybe Call Semantics
After a RPC time-out (or a client crashed and restarted), the
client is not sure if the Remote Procedure may or may not
have been called.
This is the case when no fault tolerance is built into RPC
mechanism.
maybe semantics is not desirable.
29
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
RPC Call Semanticsat-least-once Call Semantics
With this call semantics, the client can assume that the
Remote Procedure is executed at least once (on return from
the Remote Procedure).
Can be implemented by retransmission of the (call) request
message on time-out.
Acceptable only if the server’s operations are idempotent.
That is : f(x) = f(f(x)).
30
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
RPC Call Semanticsat-most-once Call Semantics
When a RPC returns, it can be assumed that the remote
procedure has been called exactly once or not at all.
Implemented by the server's filtering of duplicate requests and
caching of replies (in reply history).
31
Imed Bouchrika. Distributed Objects, Uni of Souk-Ahras 2014-2015 http://www.imed.ws
Examples for RPCJava RMI
CORBA
DCOM
32
Imed Bouchrika. Distributed Objects , Uni of Souk-Ahras 2014-2015 http://www.imed.ws
User Interface for Chat
Master II – Software Engineering
Imed BouchrikaDept of Mathematics & Computer Science
University of Souk-Ahras