|

|
Writing a quote server using powerRPC
A
typical example used to demonstrate client/server programming is a quote
server, from which the client requests stock quotes across internet
using TCP/IP. One can use low level socket (or Winsock or TLI) calls
to transfer the data, and design some special protocol . However, such
a solution contains application-specific networking code.
With powerRPC,
you write 10 lines of semi-C
code to achieve the same goal in an elegant way. Basically, you
declare a C function getQuote() in an interface defintion file.
The server implements
this function, e.g., using an embeded database query. The powerRPC compiler
pasres the interface defined in the IDL file and generates a client
side function defintion (client stub) getQuote() with the exact
prototype as the server implementation. Internally, the client stub
sends the function argument(s) to the server and waits for the result.
The server receives the arguments, makes the call to the real
getQuote() on behalf of the client and returns the result back to
the client. From the client programmer's point of view, making a getQuote()
RPC call looks no different from a call to a local function.
OK, so much for
our RPC primer, let's embark on to the journey to powerRPC programming.
The procedure
Step 1
write the interface
definition file quote.idl.
%cat quote.idl
typedef struct {
char Ticker[8];
double Low, High, Close;
}stkQuote;
interface quote {
int getQuote( inout stkQuote* pQuote);
} 0x12345;
|
Step 2
write the server
implementation file quote_impl.c .
%cat quote_impl.c
#include "quote.h"
/* the header file to
be generated by powerRPC*/
int getQuote(stkQuote* pQuote) {
/* let's just return some good numbers */
printf("Client asked quote for %s\n",
pQuote->Ticker);
pQuote->Low = 45.2;
pQuote->High = 55.5;
pQuote->Close = 54.1;
return 0;
}
|
Step 3
Run powerRPC to
compile quote.idl
%powerRPC
quote.idl
Five files will
be generated after a successful compilation:
File name
|
purpose
|
used by
|
quote.h |
The common
header file for
both the
client and server
|
Included
by the generated C code and
the server
implementation and client
call files.
|
quote_xdr.c |
The code
to encode and decode
data (args
and return values) to
and from
the XDR format. This
allows almost
abitrary C data types
to be tranfered
correctly and
efficiently.
|
by both the
client and server
|
quote_svc.c |
The server
dispatch code. When
a call is
received from the client,
the server
unpacks the arguments
and call
the function implementation.
|
server only
|
quote_cln.c |
The client
stub. Defines the getQuote()
stub call.
Inside it, the args are sent to
the server
and the client is blocked
waiting for
the results.
|
client only
|
quote.mak |
The makefile.
Default target will
compile both the server and client.
May need user custmization. |
the lucky
programmer
|
Step
4
Code a client
that calls getQuote() RPC.
%cat quote_call.c
#include "quote.h"
int main(int argc, char** argv) {
stkQuote quote;
if(argc<3) {
printf("Usage: %s serverhost tikcer\n",
argv[0]);
exit(0);
}
if(quote_bind(argv[1],0,0,0) == NULL) {
printf("Fail connecting to server on %s\n",
argv[1]);
exit(1);
}
strcpy(quote.Ticker, argv[2]);
if(getQuote("e)) {
printf("Fail getting quote for %s\n",
quote.Ticker);
} else {
printf("Low = %f, High =%f, Close = %f",
quote.Low, quote.High, quote.Close);
}
quote_unbind(0);
return 0;
}
|
Step
5
The last step--compile
and run
% make -f
quote.mak
This should compile
the server quoteserv amd the client quoteclnt. Now
we can test our quote client/server.
% quoteserv&
% quoteclnt
<server_host> NTBL
Summary
Now, you see how
easy to use powerRPC to write cient/server applications
. You may started guessing about the meaning of words such as inout
used in the IDL, and generated functions such as quote_bind(),
quote_unbind(), etc. But you have only see a tiny demo of the power
of powerRPC. For more interesting examples, please take a look at the
online tutorial,
where you can find code for a file server.
|