Building a Quote Server with PowerRPC

This tutorial demonstrates how to create a stock quote server using PowerRPC. Instead of writing low-level socket code and designing custom protocols, you'll use PowerRPC's elegant interface-based approach to build a professional distributed application in just 10 lines of semi-C code.

📝 Step 1: Define the Interface

Create the interface definition file quote.idl:

typedef struct {
    char Ticker[8];
    double Low, High, Close;
} stkQuote;

interface quote {
    int getQuote(inout stkQuote* pQuote);
} 0x12345;

This defines a stock quote structure and an interface with a single method getQuote() that takes an inout parameter.

💻 Step 2: Implement the Server

Create the server implementation file quote_impl.c:

#include "quote.h"

int getQuote(stkQuote* pQuote) {
    /* Return sample stock data */
    printf("Client asked quote for %s\n", pQuote->Ticker);
    pQuote->Low = 45.2;
    pQuote->High = 55.5;
    pQuote->Close = 54.1;
    return 0;
}

The server implementation receives the request, processes it, and returns the stock quote data.

⚙️ Step 3: Compile with PowerRPC

Run the PowerRPC compiler to generate the necessary files:

powerRPC quote.idl

Generated Files:

quote.h

Common header file for client and server

quote_xdr.c

XDR encoding/decoding for data types

quote_svc.c

Server dispatch code

quote_cln.c

Client stub implementation

quote.mak

Makefile for building the application

👨‍💻 Step 4: Create the Client

Write the client code that calls the RPC service:

#include "quote.h"

int main(int argc, char** argv) {
    stkQuote quote;
    
    if(argc < 3) {
        printf("Usage: %s serverhost ticker\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: Compile and Run

Build and test your quote server application:

# Compile the application
make -f quote.mak

# Start the server
quoteserv &

# Run the client
quoteclnt  NTBL

The client will connect to the server, request a quote, and display the results seamlessly.

🎯 Summary

This example demonstrates how PowerRPC simplifies distributed application development. The client calls getQuote() exactly like a local function, while PowerRPC handles all the networking, data marshaling, and protocol details automatically.

For more advanced examples including file servers and complex data structures, explore our comprehensive online documentation.