1
Distributed Program
Generation (rpcgen Concept)
Chuan-Ming Liu
CSIE,NTUT
Spring ’04,TAIWAN
2
Introduction
Focuses on the structure of programs that
use RPC,and shows how program can be
divided along procedure boundaries
Introduces the stub procedure concept and
a program generator tool that automates
much of the code generation associated with
ONC RPC
It also discusses a library of procedures that
makes it easy to build servers that offer
remote procedures and clients that call them
3
Using RPCs
The RPC model is general
A programmer can choose to use the
remote procedure paradigm in any of the
following ways:
As a program specification techniques only
For both program specifications and as an
abstraction during program design
4
Using RPCs (cont.)
For the conceptual design and explicitly in the
implementation
For design and implementation,constructing
all software from scratch
For design and implementation,using
standard libraries
For an automated implementation
5
Programming Mechanisms
to Support RPC
Implementations of ONC RPC provide
significant help for those who wish to
avoid unnecessary programming
Assistance comes in four forms:
XDR library routines that convert individual
data items from internal form to the XDR
standard external representation
6
Programming Mechanisms
to Support RPC (cont.)
XDR library routines that format the complex
data aggregates (e.g.,arrays and structures)
used to define RPC messages
RPC run-time library routines that allow a
program to call a remote procedure,register a
service with the port mapper,or dispatch an
incoming call to the correct remote procedure
inside a remote program
A program generator tool that produces many
of the C source files needed to build a
distributed program that uses RPC
7
Programming Mechanisms
to Support RPC (cont.)
The RPC run-time library has procedures
that supply most of the functionality
needed for RPC
For example,procedure callrpc sends an
RPC message to a server
callrpc(host,prog,progver,procnum,inproc,
in,outproc,out);
8
Programming Mechanisms
to Support RPC (cont.)
A client calls function clnt_create to create
an integer identifier (called a handle) that
can be used to send RPC message:
handle = clnt_create(host,prog,vers,proto)
Several RPC library procedures take a
handle as one of their argument
9
Programming Mechanisms
to Support RPC (cont.)
The library library also contains routines that
create,store,and manipulate authentication
information
For example,procedure authunix creates an
authentication handle for a given use on a
given host computer
authunix_create(host,uid,gid,len,aup_gids)
Most programmers rely on the program
generator tool to generate a code that contains
calls to the library procedures
10
Dividing a Program into Local
and Remote Procedures
Fig,22.1 illustrates an example procedure
call that shows the procedural interface
used by a calling procedure and a called
procedure
The dashed lines denote a match (the total
number and types) between arguments in
the procedure call and formal parameters in
the called procedure
12
Adding Code for RPC
Moving one or more procedures to a remote
machine requires a programmer to add code
between the procedure call and the remote
procedure
On the client side,the new code must
marshal arguments and translate them to a
machine-independent representation,create
an RPC CALL message,send the message
to the remote program,wait for the results,
and translating the resulting values back to
the client’s native representation
13
Adding Code for RPC
(cont.)
On the server side,the new code must
accept an incoming RPC request,translate
arguments to the server’s native data
representation,dispatch the message to the
appropriate procedure,form a reply
message by translating values to the
machine-independent data representation,
and send the result back to the client
14
Adding Code for RPC
(cont.)
To keep the program structure (including
the interface between the original calling
and called procedures) intact and to isolate
the code from handles RPC from the code
that handles the application,the additional
code required for RPC can be added in the
form of two extra procedures (called stub
procedures) that completely hide the
communication details
15
Stub Procedures
Fig,22.2 illustrates the stub concept,
showing how stub procedures allow the
procedure call shown in Fig,22.1 to be
separated into local and remote parts
Because stubs use the same interface as the
original call,adding them does not require
a change to either the original called
procedure or the called procedure
17
Multiple Remote Procedures
and Dispatching
Fig,22.3 shows message dispatch in an
RPC server
Clients send RPC requests to a single
server program
The server (dispatcher) uses the remote
procedure number in a message to decide
which procedure should receive the call
19
Name of the Client-Side Stub
Procedure
As shown in Fig,22.3,if the programmer
names the client-side stub B1 and builds it
to have the exactly the same interface as
the original procedure B1,the calling
procedure (A1) does not need to change
That is,the addition of stub procedures
allows the original calling and called
procedures to remain unchanged as long as
the client-side stub has the same name as
the original called procedure
20
Using Rpcgen to Generate
Distributed Programs
To avoid unnecessary programming,ONC
RPC includes a tool,rpcgen,that reads a
specification file as input and generates
files of C source code as output
The specification file contains declarations
for constants,global data types,global data,
and remote procedures (including the
procedure argument and result types)
21
Using Rpcgen to Generate
Distributed Programs (cont.)
Rpcgen generates code for the client-side
and server-side stub procedures,including
the code to marshal arguments,to send an
RPC message,to dispatch an incoming call
to the correct procedure,to send a reply,
and to translate arguments and results
between the external representations and
native data representations
22
Rpcgen Output and Interface
Procedures
To maintain flexibility and to allow automatic
generation of significant portions of the stub
code,rpcgen separates each stub procedure
into two parts,
communication routine (common to almost
all applications that use RPC,provides basic
client-server communication)
interface routine (an interface to the
application procedure)
23
Rpcgen Output and Interface
Procedures (cont.)
The idea behind separating the stub into
communication and interface routines,as
shown in Fig,22.4,is,it allows rpcgen to
choose the calling conventions the
communication stubs use,while it allows
the programmer to choose the calling
convention the remote procedures use
25
Rpcgen Output and Interface
Procedures (cont.)
The programmer creates interface stubs to
map between the remote procedure calling
conventions and the conventions provided
by the communication stub procedures that
rpcgen generates
As Fig,22.4 illustrates,on the client side,
the interface procedure calls the
communication procedure,while on the
server side,the communication procedure
calls the interface procedure
26
Rpcgen Input and Output
Rpcgen reads an input file (e.g.,Q.x) that
contains of a specification of a remote
program
It produces 4 output files (Q.h,Q_xdr.c,
Q_clnt.c,Q_svc.c),as described in Fig,
22.5
28
Using Rpcgen to Build a
Client and Server
Fig,22.6 illustrates the files (the darkended
boxes) that a programmer must write to
build a client and server suing rpcgen
The programmer divides the application
into a driver program (the client) and a set
of procedures that comprise the remote
program (the server)
The programmer then writes a specification
for the remote program and uses rpcgen to
generate the remaining pieces
30
Summary
When using ONC RPC,the programmer can
choose to follow the specification while
building code from scratch,to use procedures
found in the RPC library,or to use an
automatic program generation tool called
rpcgen
RPC allows a programmer to construct a
conventional program and then to transform
it into a distributed program by moving some
procedures to a remote machine
31
Summary (cont.)
Using stub procedures allows the original
calling and called procedures to remain
unchanged
Rpcgen generates the client and server
programs,including procedures that register
the server with the port mapper,provide
communication between client and server,
and dispatch incoming calls to the correct
remote procedure