XDR and RPC

eXternal Data Representation 


Each computer architecture provides its own definition for the representation of data Programmers who write programs for a single computer do not need to think about data representation 

Compiler uses the computer¶s native data representation when it allocates storage 

Programmers who create client and software must contend with data representation

The N-Squared Conversion Problem 

Programmer can choose to embed knowledge of the computer¶s architecture in each client-server pair so the client and server agree on which side will convert the data 


Called asymmetric data conversion because one side or the other performs conversion What is the main problem?

The N-Squared Conversion Problem 

Programmer can choose to embed knowledge of the computer¶s architecture in each client-server pair so the client and server agree on which side will convert the data 


Called asymmetric data conversion because one side or the other performs conversion Programmer must write a different version of the client-server pair for each pair of architectures on which they will be used

The N-Squared Conversion Problem    Consider a set of N computers Assume each computer uses a different data representation to store floating point numbers A programmer must construct _______ versions of any client-server program that exchanges floating point values  We called this n-squared conversion problem  Now add a new architecture to the set of N machines .

machine-independent representation Likewise convert from machine-independent representation to receivers native representation .Network Standard Byte Order  TCP/IP protocol employs symmetric data conversion  Both end performs the required conversion   Before sending across the network they convert data from the sending computer¶s native representation into a standard.

XDR  The standard representation used for data traversing the network is known as the eXternal Data Representation   Advantages? Disadvantages? .

XDR  The standard representation used for data traversing the network is known as the eXternal Data Representation   Advantages? Disadvantages?    Computational overhead ± same architectures Even for different architectures there is additional computation due to an intermediate form Conversion may result in a larger stream of bytes than necessary .

Cray* XDR fits into the ISO Presentation Layer Roughly analogous in purpose to X. ISO Abstract Syntax Notation . IBM-PC*.409.A De Facto Standard XDR     Sun¶s XDR Has been used to communicate data between diverse machines as Sun Workstation*. VAX*.

it is not a programming language Allows one to describe intricate data formats in a concise manner  The alternative of using graphical representations (itself an informal language) quickly becomes incomprehensible when faced with complexity   Similar to C language Protocols such as ONC RPC and the NFS* use XDR .Sun¶s XDR    XDR uses a language to describe data formats The language can be used only to describe data.

XDR  XDR makes the following assumptions:   Bytes (or octets) are portable where a byte is defined to be 8 bits of data A given hardware device should encode the bytes onto the various media in such a way that other hardware devices may decode the bytes without loss of meaning .

XDR: Basic Block Size     The representation of all items requires a multiple of four bytes (or 32 bits) of data The bytes are numbered 0 through n-1 The bytes are read or written to some byte stream such that byte m always precedes byte m+1. to make the total byte count a multiple of 4. If the n bytes needed to contain the data are not a multiple of four. r. then the n bytes are followed by enough (0 to 3) residual zero bytes. .

.

XDR Data Types Data Type int unsigned int bool enum hyper unsigned hyper float Size 32 bits 32 bits 32 bits arb 64 bits 64 bits 32 bits .

XDR Data Types double opaque string fixed array counted array structure discriminated union void symbolic constant optional data 64 bits arb -do-do-do-do-do0 arb arb .

respectively. . The integer is represented in two's complement notation.XDR integer  An XDR signed integer is a 32-bit datum that encodes an integer in the range [2147483648. The most and least significant bytes are 0 and 3.2147483647].

It is represented by an unsigned binary number whose most and least significant bytes are 0 and 3.XDR Unsigned Integer  An XDR unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0. . respectively.4294967295].

. BLUE = 5 } colors..XDR Enumeration    Enumerations have the same representation as signed integers Enumerations are handy for describing subsets of the integers Enumerated data is declared as enum { name-identifier = constant.  It is an error to encode as an enum any other integer than those that have been given assignments in the enum declarations .. enum { RED = 2. } identifier. YELLOW = 3.

Booleans are declared as follows: bool identifier. This is equivalent to: enum { FALSE = 0. . TRUE = 1 } identifier.XDR Boolean    Booleans are important enough and occur frequently enough to warrant their own explicit type in the standard.

XDR Hyper Integer and Unsigned Hyper Integer     XDR standard also defines 64 bit (8 byte) numbers called hyper integer and unsigned hyper integer Their representations are the obvious extensions of integer and unsigned integer defined before Also represented in 2¶s complement notation MSB and LSB are 0 and 7 respectively .

XDR Floating Point   32 bits or 4 bytes Encoding used is the IEEE standard for normalized single-precision floating point numbers .

base 2.F . 23 bits are devoted to this field. One bit. respectively.XDR Floating Point     S: The sign of the number. Values 0 and 1 represent positive and negative. base 2. E: The exponent of the number. 8 bits are devoted to this field. F: The fractional part of the number's mantissa. Therefore the floating point is described by: (-1)**S * 2**(E-Bias) * 1. The exponent is biased by 127.

then the n bytes are followed by enough (0 to 3) residual zero bytes. . fixed-length uninterpreted data needs to be passed among machines. This data is called "opaque" and is declared as follows:    opaque identifier [n]. If n is not a multiple of four. to make the total byte count of the opaque object a multiple of four.XDR Fixed Length Opaque Data   At times. where the constant n is the (static) number of bytes necessary to contain the opaque data. r.

.XDR String    The standard defines a string of n (numbered 0 through n-1) ASCII bytes to be the number n encoded as an unsigned integer (as described before). and followed by the n bytes of the string. to make the total byte count a multiple of four. then the n bytes are followed by enough (0 to 3) residual zero bytes. Byte m of the string always precedes byte m+1 of the string. r. If n is not a multiple of four. and byte 0 of the string always follows the string's length.

.   Fixed-length arrays of elements numbered 0 through n-1 are encoded by individually encoding the elements of the array in their natural order. 0 through n-1. Each element's size is a multiple of four bytes.XDR Fixed Length Array  Declarations for fixed-length arrays of homogeneous elements are in the following form:  type-name identifier [n].

XDR Fixed Length Array   Though all elements are of the same type. . For example. in a fixed-length array of strings. yet each element will vary in its length. all elements are of type "string". the elements may have different sizes.

. as in the second declaration.XDR Variable Length Array   The array is encoded as the element count n (an unsigned integer) followed by the encoding of each of the array's elements. or type-name identifier <>.1. starting with element 0 and progressing through element n. it is assumed to be (2**32) .1. The declaration for variable-length arrays follows this form:    type-name identifier <m>. The constant m specifies the maximum acceptable element count of an array. if m is not specified.

XDR Structure 

Structures are defined as follows:
struct { component-declaration-A; component-declaration-B; ... } identifier;  

The components of the structure are encoded in the order of their declaration in the structure Each component's size is a multiple of four bytes, though the components may be different sizes.

XDR Constant 

The data declaration for a constant follows this form: 

const name-identifier = n; 

 

"const" is used to define a symbolic name for a constant; it does not declare any data. The symbolic constant may be used anywhere a regular constant may be used. For example, the following defines a symbolic constant DOZEN, equal to 12. 

const DOZEN = 12;

Software Support For Using XDR 


Most programmers could write the required XDR conversion functions with little effort However, some conversions require considerable expertise 

Converting from a computer¶s native floating point to XDR standard without losing precision may require an understanding of basic numerical analysis

XDR Library Routines 
 

To convert between machine representation and XDR standard (and vice versa) Most implementations use a buffer paradigm Buffer paradigm XDR requires a program to allocate a buffer large enough to hold the external representation of a message and to add items (i.e., fields) one at a time

XDR Library Routines in SunOS  A program first invokes a procedure xdrmem_create    It allocates a buffer in memory and inform XDR that it intends to compose an external representation in it It initializes the XDR stream to be empty It returns an pointer to the stream which must be uses in successive calls to XDR routines  Once a program has created an XDR stream it can call individual conversion routines to convert .

ptrint xdrs. ptrchar xdrs. ptrdouble xdrs. ip . maxsize xdrs. ptrbool xdrs. strsize.XDR Library Routines in SunOS Procedure xdr_bool xdr_bytes xdr_char xdr_double xdr_enum xdr_float xdr_int Arguments xdrs. ptrstr. ptrfloat xdrs.

XDR Streams. I/O and TCP  After items have been converted the application must call an I/O function like write to send it across a TCP connection .

Example of XDR Data Description  Write an XDR data description of a ³file´ which might be used to transfer files from one machine to another .

References   Chapter 19 of Douglas Comer book RFC 1832 .

Remote Procedure Call   Conceptual framework for building distributed programs Uses familiar concepts from conventional programs .

Two paradigms for building distributed programs .

Communication Oriented Design   Programmer may miss important subtleties in the application and may find that the protocol does not provide all the needed functionality Because few programmers have expertise and experience with protocol design they often produce awkward protocols  Small oversights in protocol design can lead to fundamental errors that remain hidden until the program run under stress  Programmer concentrates on communication. it usually becomes the centerpiece .

Application Oriented Design   Emphasizes the problem to be solved instead of the communication needed A programmer first designs a conventional program to solve the problem. and then divides the program into pieces that run on two or more computers .

Remote Procedure Call .

Remote Subroutine .

Execution of Conventional Procedure Call and Return  In the procedural execution model. a single thread of execution flows through all procedures .

RPC .

the format of arguments. format of results that the called procedure returns to the caller ONC RPC permits the calling program to use either UDP or TCP to carry messages and uses XDR to represent procedure arguments as well as other items in an RPC message header ONC RPC includes a compiler system that help programmers build distributed programs automatically . Open Network Computing (ONC) or simply RPC ONC RPC defines the format of message that the caller (client) sends to invoke a remote procedure on a server.Sun¶s RPC     Known as Sun RPC.

Sun RPC organization    ONC RPC defines a remote execution environment It defines a remote program as the basic unit of software that executes on a remote machine Each remote program contains a set of one or more remote procedures plus global data .

Sun RPC organization .

Procedure Arguments .

Identifying Remote Programs and Procedures .

2.Identifying Remote Programs and Procedures  The procedures are numbered sequentially: 1. ONC RPC has divided the set of programs into eight groups .«N  By convention the number 0 is always reserved for an echo procedure  To help ensure that program numbers defined by separate organizations do not conflict.

Identifying Remote Programs and Procedures .

showmount rusersd 1000002 nfs ypserv 1000003 1000004 mountd 1000005 .Example RPC program numbers currently assigned portmap 1000000 rstatd 1000001 port mapper rstat. rup and perfmeter remote users network file system NIS mount.

Iterative Server .

Iterative can be good .

At Least One and Zero or More Semantics    ONC RPC defines the semantics of a remote procedure call by specifying that a program should draw the weakest possible conclusion from any interaction ONC RPC uses the term at least once semantics to describe RPC execution when the caller receives a reply ONC RPC uses the term at zero or more semantics to describe RPC execution when the caller does not receive a reply .

zero-or-more semantics usually means that a programmer makes each remote procedure call idempotent .At Least One and Zero or More Semantics  In practice.

Dynamic Port Mapping .

Port Lookup Service .

The portmapper .

More on the portmapper .

register a service with the port mapper. or dispatch an incoming call to the correct remote procedure A program generator tool that produces many of the C source files needed to build a distributed program that uses RPC .Programming Mechanisms to support RPC     XDR library routines to convert from internal form to standard external representation XDR library routines that format the complex data aggregates used to define RPC messages RPC run time library routines that allow a program to call a remote procedure.

RPC run-time library .

High-level client library .

Adding code for RPC   Stub (and skeleton) procedures Dispatcher .

Rpcgen .

Rpcgen .

Rpcgen output files .

An example to illustrate rpcgen      I i d l q -<none><word> <word> <word> -<none>- Initialize the database Insert word Delete word Lookup word Quit .

and so on.Terminal I/O    Handling of terminal I/O is a messy area regardless of the operating system Part of complexity of terminal I/O occurs because people use terminal I/O for so many different things: terminals. read(). The functions we have already covered (scanf(). hardwired lines between computers. modems. printf(). printers. write() and so on) are used by most applications to do I/O to a terminal .

Terminal Concepts   A classic terminal is a keyboard and a display (or printer) that is connected to the computer using an asynchronous communications port From the perspective of the OS. the important characteristic of a terminal is the communications port that connects it to the host and not the device sitting at the end of the wire .

Terminal Concepts .

Terminal Concepts .

Terminal Concepts   An asynchronous communications port can be used to talk to other computers. plotters and special I/O equipments Serial Device     Sends a character one bit at a time Each character starts with a leading zero called the start bit The data bits are sent one at a time. printers. beginning with LSB and ending with the MSB The last trailing bit is called the stop bit .

.

a character is sent from the terminal to the computer When it receives the character the computer sends the character back to the terminal where it is displayed   This is called µechoing¶ A program can echo a special sequence .Terminal Concepts    Most terminals operate in full-duplex When you press a key.

Setting Terminal Parameters    Terminal parameters are all manipulated through a data structure known as struct termios tcgetattr() copies the parameters from the OS into a termios structure tcsetattr() copies the parameters from a termios structure into the OS .

Terminal I/O modes   Canonical Mode  Terminal input is processed as lines Non Canonical Mode .

/* local flags */ cc_t c_cc[NCCS]. /* input flags */ tcflag_t c_oflag.struct termios #include <termios.h> struct termios { tcflag_t c_iflag. . /* control flags */ tcflag_t c_lflag. /* control characters */ }. /* output flags */ tcflag_t c_cflag.

struct termios  c_iflag   Controls the processing of input data ISTRIP ± mask characters to be masked to 7 bits Controls processing of output data OPOST ± system specific output processing Controls information related to hardware  c_oflag    c_cflag  .

struct termios  c_lflag      Controls echoing and character processing ECHO: turns on echoing ICANON: Turns on input processing ISIG: Enables signals TOSTOP: Stops background processes if they write to the controlling terminal An array of control characters  c_cc  .

^\ generates a SIGQUIT .struct termios  c_cc         Control characters Meaning of the members depends on the ICANON flag VEOF . ^j VERASE ± Typically backspace or delete erases the preceding character VINTR ± The interrupt character (usually ^c or DEL) generates a SIGINT VKILL ± The kill character (^U) deletes the entire line from the terminal VQUIT .^D may be used to generate EOF from terminal VEOL ± EOL character.

Further when change takes place all input data that has not been read is discarded    .Getting and Setting terminal attributes #include <termios.h> int tcgetattr(int filedes. const struct termios *termptr). int opt. struct termios *termptr). int tcsetattr(int filedes. TCSANOW ± change occurs immediately TCSADRAIN ± change occurs after all output has been transmitted TCSAFLUSH ± change occurs after all output as been transmitted.

it is our responsibility to see whether all the requested actions were performed. If the function returns OK. This function returns OK if it was able to perform any of the requested actions. .Getting and Setting terminal attributes  The return status of tcsetattr confuses the programming. This means that after we call tcsetattr to set the desired attributes. we need to call tcgetattr and compare the actual terminal's attributes to the desired attributes to detect any differences. even if it couldn't perform all the requested actions.

Sign up to vote on this title
UsefulNot useful