1 / 102

Client/Server Distributed Systems

Client/Server Distributed Systems. 240-322, Semester 1, 2005-2006. Objectives look at how to program with SunOS RPCs use XDR and rpcgen briefly look at authentication. 10. Remote Procedure Calls (RPCs). Overview. 1. What is a RPC? 2. XDR (eXternal Data Representation)

Download Presentation

Client/Server Distributed Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Client/Server Distributed Systems 240-322, Semester 1, 2005-2006 • Objectives • look at how to program with SunOS RPCs • use XDR and rpcgen • briefly look at authentication 10. Remote Procedure Calls (RPCs)

  2. Overview 1. What is a RPC? 2. XDR (eXternal Data Representation) 3. primes.c Example 4. Networking primes.c 5. RPC Authentication 6. Other RPC Features 7. RPC Benefits 8. More Information

  3. 1. What is a RPC? • A remote procedure call (RPC) is a call to a procedure/function located on another machine. • 1.1. Background1.2. Ordinary Procedure Calls1.3. Parts of a RPC1.4. The Good News1.5. What kind of RPC?1.6. Finding a Remote Procedure

  4. 1.1. Background • Client-server code using sockets is complicated to write and understand • must deal with networking and application coding • Is there an easier networking model? • i.e. one that hides or reduces the networking coding

  5. 1.2. Ordinary Procedure Calls • We can view a procedure/function call as a client-server communication on the same machine. client main() call return function server

  6. 1.3. Parts of a RPC • This communication can be spread across two machines. client server main() call return server wrapper client stub XDR filters XDR filters serverfunction network interface network interface The Network

  7. The Client Stub • The client stub is the client’s interface to the networking code. • It is called like the original function. • It converts the input arguments into network form, and sends them to the server. • It receives the server’s answer in network form, converts it to ordinary data, and returns it to main().

  8. XDR Filters • A set of functions for converting data into network form, and back again. • XDR = eXternal Data Representation

  9. Server Wrapper • The server wrapper receives data in network form, calls its XDR filters to extract the original data, and calls the server function with it. • When the function returns, the wrapper converts the result into network form, and sends it back to the client.

  10. 1.4. The Good News • A RPC tool, rpcgen, will generate the: • client stub, XDR filters, server wrapper • It will even suggest possible code for: • client main(), the server function • The programmer must supply rpcgen with the (XDR) data structures passed between main() and the server function.

  11. 1.5. What kind of RPC? • The most popular one is probably Sun RPC • Sun Microsystems invented it • also called ONC RPC (Open Network Computing) • NIS, NFS built using Sun RPC • more information in sections 3.3, 3.4 of Brown continued

  12. Other RPC implementations: • Courier RPC (from Xerox) • NCS RPC (Network Computing System • developed by Apollo • used in DCE (Distributed Computing Environment)

  13. 1.6. Finding a Remote Procedure • How does a client find the right server over the network? • In ordinary client-server code, the user must supply a host name and a port number. • In RPC, the user only supplies a host name. continued

  14. The client asks a portmapper on the host for the port of the server. • the portmapper holds a database of all RPC services on its machine • On SunOS the portmapper is called rpcbind • (sometimes) see man rpcbind continued

  15. This approach requires that each server registers itself with the portmapper when it first starts. • The server registers: • { program number, version number, procedure/function number } continued

  16. Steps in RPC Communication 4: send data port client server 2: request server details port111 3: Sendserver details,includingport. 1: register details portmapper(rpcbind)

  17. Using rpcinfo often only rootcan use rpcinfo • Look at the portmapper using rpcinfo: $ rpcinfo -p takasila program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 100024 1 udp 918 status 100024 1 tcp 921 status 100003 2 udp 2049 nfs 100003 3 udp 2049 nfs : : called from fivedots

  18. Points to Note • The port numbers may change when the system is rebooted. • A server can be registered with the portmapper for more than one protocol (UDP and TCP). • Some servers may have many versions • allows new code to be developed alongside old code

  19. Program Names • A program name is associated with a program number in the host’s /etc/rpc file: portmapper 100000 portmap sunrpcrstatd 100001 rstat rstat_svc rup rusersd 100002 rusersnfs 100003 nfsprog : • The super-user can add new server details.

  20. Choosing a Program Number • A server must have a program number in the range 0x20000000 - 0x3fffffff • Other ranges: 0 - 1fffffff for Sun Microsystems20000000 - 3fffffff user-defined40000000 - 5fffffff transient60000000 - ffffffff reserved for future use

  21. 2. XDR (eXternal Data Representation) • XDR is used to encode data in a network form for communication between the client and server. Why? • Answer: data is represented differently on different machines. • e.g. sending an integer (or array, or struct, etc.) directly from machine A to machine B is not usually possible

  22. 2.1. Machine Archtecture Problems a) Different byte orders • big-endian: 68000 family, SPARC • little-endian: Intel, VAX b) Alignment rules • e.g. all 32-bit integers must start at an address which is a multiple of 4 so they align with the processor’s 32-bit words • the compiler will generate ‘holes’ in the data structures to get the correct alignment continued

  23. Structure Alignment Example struct demo { char c; int i; long x;} 2-byte int, no align rule c i x 4-byte int,2-byte align rule c i x 4-byte int,4-bytealign rule c i x 0 1 2 3 4 5 6 7 8 9 10 11 12 continued

  24. c) Programming Language Differences • e.g. a Pascal string is stored with its length • e.g. a C 'string' is stored with a '\0' d) Pointers • we cannot directly pass pointers between machines since an address on one machine means nothing on another • how do we transfer linked-lists, trees, stacks, etc.?

  25. 2.2. The XDR Data Description Lang. • XDR data types are very similar to C types, but there are some differences. • Some XDR types: • int, float (like C) • arrays (fixed and variable length) • strings • structs and unions (like C)

  26. XDR Type Examples • int page_number; same as C • const SIZE = 4; like C’s #define • int week[7] • a fixed length array of 7 integers • the array must contain 7 values when it is transferred over the network continued

  27. double density<50> • a variable length array of at most 50 doubles • the array can contain less than 50 values when it is transferred over the network • string name<50> • a variable length string of at most 50 chars • there is no char *in XDR continued

  28. XDR can represent pointer data structures, so long as they do not contain loops • ok: lists, stacks, trees, queues • not ok: doubly-linked lists, rings • For other types, see Brown p.304-311 • also man xdr

  29. 3. primes.c Example • This program prints out the primes in the range given by the user on the command line. $ ./primes 1 65 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61$ No networked RPC yet; first get the stand-alone application working.

  30. Function Calls Diagram All on one machine, in one program. main() range r calls pinfo pi find_primes() report_results() isprime()

  31. primes.c #include <stdio.h>#include <stdlib.h> /* for atoi() */#define MAXPRI 1000 /* max no of primes *//* I/O structures for find_primes() */struct range { /* range for search */ int min, max;};struct pinfo { /* collected primes */ int primes[MAXPRI]; /* at most MAXPRI primes */ int num_primes; /* total number found */};struct pinfo find_primes(struct range r);int isprime(int n); :

  32. int main(int argc, char *argv[]){ struct range r; struct pinfo pi; if (argc != 3) { fprintf(stderr, "Usage: primes <min> <max>\n"); exit(1); } r.min = atoi(argv[1]); /* no error checking */ r.max = atoi(argv[2]); pi = find_primes(r); report_results(pi); return 0;}

  33. struct pinfo find_primes(struct range r)/* Collect primes between r.min and r.max */{ struct pinfo pi; int i; if (r.min > r.max) pi.num_primes = -1; else { pi.num_primes = 0; for (i = r.min; i <= r.max; i++) if (isprime(i)) { if (pi.num_primes < MAXPRI) pi.primes[pi.num_primes] = i; pi.num_primes++; } } return pi;}

  34. int isprime(int n){ int i; for (i = 2; i*i <= n; i++) if ((n % i) == 0) return 0; return 1;}

  35. void report_results(struct pinfo pi){ int i; if (pi.num_primes == -1) fprintf(stderr, "range error\n"); else { if (pi.num_primes > MAXPRI) { fprintf(stderr, "Too many primes: %d\n", pi.num_primes); pi.num_primes = MAXPRI; } for (i = 0; i < pi.num_primes; i++) { printf("%5d", pi.primes[i]); if (((i+1)%10) == 0) putchar('\n'); } putchar('\n'); }}

  36. Comments • The complex data structures are to make the conversion from a standalone to networked RPC version easier. • Coding strategy: • get the application working first (generate primes); • then add in the network code (RPC)

  37. 4. Networking Primes • Convert primes.c into a network application using RPC. • find_primes() (and isprime()) will become the remote procedure. • Usually the choice depends on: • resource utilisation • balancing work against communication costs

  38. Function Calls Diagram client (fivedots) main() range r server(takasila) pinfo pi find_primes() report_results() isprime()

  39. RPC Communication client (fivedots) main() report_results() server (takasila) call return server wrapper client stub XDR filters XDR filters find_primes()and isprime() network interface network interface range r The Network pinfo pi

  40. 4.1. The ‘.x’ Files • The ‘.x’ file contains the XDR data types passed between main() and find_primes() $ rpcgen -C primes.x-->primes.h header file for C datatypes involved in network comms.primes_xdr.c XDR filtersprimes_clnt.c client stubprimes_svc.c server wrapper • No need to even look at these ‘.c’ files continued

  41. $ rpcgen -a -C primes.x • all of the above, plus:primes_client.c simple client main()primes_server.c simple server functionmakefile.primes a makefile • these files must not already exist • the C files show how to use the C datatypes in primes.h for network commication • the programmer must add the application code

  42. Relationships between the Files client (fivedots) server (takasila) primes_client.c primes_svc.c primes.h primes_clnt.c primes.h primes_xdr.c primes_xdr.c primes_ server.c network interface network interface The Network

  43. 4.2. primes.x • This file contains the XDR data types and the server information: { program number, version number, function number }

  44. const MAXPRI = 1000; /* max no. of primes */struct range { /* range for search */ int min; int max;};struct pinfo { /* collected primes */ int primes<MAXPRI>; /*at most MAXPRI primes */ int num_primes;};program PRIMEPROG { /* server info. */ version PRIMEVERS { pinfo FIND_PRIMES(range) = 1; } = 1; /* the version number */} = 0x2000009a; /* the program number */

  45. Notes • A restriction of standard RPC is that the remote procedure can only take one input, and return one output. pinfo FIND_PRIMES(range) • Program, version, and function names must be in uppercase. • primes<MAXPRI> is a variable length array.

  46. 4.3. primes.h • This header file contains the C datatypes generated from the XDR datatypes. • They are used in primes_client.c and primes_server.c for communication between main() and find_primes(). • primes.h will contain 3 versions of the datatypes: • K&R C, ANSI C, C++

  47. ANSI C headers in primes.h For these slides, I've deleted the K&R C and C++ code #include <rpc/rpc.h>#define MAXPRI 1000struct range { int min; int max;};typedef struct range range;extern bool_t xdr_range(XDR *, range*); :

  48. more complex version of pinfo structto do with variable length struct pinfo {struct { u_int primes_len; /* the array size */ int *primes_val; /* pointer to array */ } primes; int num_primes;};typedef struct pinfo pinfo;extern bool_t xdr_pinfo((XDR *, pinfo*); :

  49. #define PRIMEPROG ((unsigned long)(0x2000009a))#define PRIMEVERS ((unsigned long)(1))#define FIND_PRIMES ((unsigned long)(1))extern pinfo * find_primes_1(range *, CLIENT *);extern pinfo * find_primes_1_svc(range *, struct svc_req *);

  50. Notes • There are typedefs for range and pinfo. • struct pinfo is more complex: • primes_val is a pointer, so we must either malloc space for it or make it point to an array at run time • primes_len must be assigned the length of the array continued

More Related