Use C language API to talk to RimStone Server


RimStone application server can be accessed via C API. Most programming languages allow for C linkage, so this makes it easy to talk to RimStone server from anywhere. The Client-API is very simple with just a few functions and a single data type. It's also MT-safe (i.e. safe for multi-threaded applications).

In this example, a RimStone server will use a tree object to store key/value pairs, which can be added, queried and deleted for as long as the server is running (i.e. it's an in-memory database, or a cache server). Client will insert the key/value pairs, query and delete them.
Create a server and start it
To get started, create a directory for this example and position in it:
mkdir -p c-api
cd c-api

Create file "srv.rim":
cat << 'EOF' > srv.rim
begin-handler /srv public
    silent-header
    do-once
        new-tree ind process-scope
    end-do-once
    get-param op
    get-param key default-value ""
    get-param data default-value ""
    if-true op equal "add"
        write-tree ind key (key) value data status st
        if-true st equal RIM_ERR_EXIST
            @Key exists [<<print-out key>>]
        else-if
            @Added [<<print-out key>>]
        end-if
    else-if op equal "delete"
        delete-tree ind key (key) value val status st
        if-true st equal RIM_ERR_EXIST
            @Not found [<<print-out key>>]
        else-if
            @Deleted, old value was [<<print-out val>>]
        end-if
    else-if op equal "query"
        read-tree ind equal (key) value val status st
        if-true st equal RIM_ERR_EXIST
            @Not found, queried [<<print-out key>>]
        else-if
            @Value [<<print-out val>>]
        end-if
    end-if
end-handler
EOF

Create "index" application ("-k"):
rim -k index

Compile the server - this also demonstrates excluding directories from compilation (since RimStone will by default try to compile the RimStone and C code in all subdirectories). In this case, we're excluding subdirectory "client", which we will create in just a sec and place a C client program in it:
rim -q --exclude-dir=client

Start the server, with a single server process running:
mrim -w 1 index

Create a client in C
Create directory for a C API client, and switch to it:
mkdir client
cd client

Next is the C code for your client. It simply inserts key/value pair, queries it, and then deletes it. Nice little program as a way of demonstration. Create file "cli.c" with this:
cat << 'EOF' > cli.c
#include "rcli.h"

int rim_client (rim_cli *req, char *connection, char *method, char *app_path, char *request, char *url_params);

// Send request to RimStone server and receive reply
int rim_client (rim_cli *req, char *connection, char *method, char *app_path, char *request, char *url_params)
{
    memset ((char*)req, 0, sizeof(rim_cli));
    req->server = connection;
    req->req_method = method;
    req->app_path = app_path;
    req->req = request;
    req->url_params = url_params;
    return rim_cli_request (req);
}

void main ()
{
    int res;
    char *urlreq, *data;
    rim_cli req;

    char dir[RIM_MAX_OS_UDIR_LEN];
    rim_dir (RIM_DIR_SOCKFILE, dir, sizeof(dir), "index", NULL); // socket for application
                                                               //
    urlreq = "/op=add/key=some_key/data=some_data"; // Add data
    res = rim_client (&req, dir, "GET", "/index", "/srv", urlreq);
    if (res != RIM_OKAY) printf("Request failed [%d] [%s]\n", res, req.errm); else printf("%s", rim_cli_data(&req));
    rim_cli_delete(&req);

    urlreq = "/op=query/key=some_key"; // Query data
    res = rim_client (&req, dir, "GET", "/index", "/srv", urlreq);
    if (res != RIM_OKAY) printf("Request failed [%d] [%s]\n", res, req.errm); else printf("%s", data = rim_cli_data(&req));
    rim_cli_delete(&req);

    urlreq = "/op=delete/key=some_key"; // Delete data
    res = rim_client (&req, dir, "GET", "/index", "/srv", urlreq);
    if (res != RIM_OKAY) printf("Request failed [%d] [%s]\n", res, req.errm); else printf("%s", data=rim_cli_data(&req));
    rim_cli_delete(&req);
}
EOF

Compile C program:
gcc -o cli cli.c $(rim -i)

Run your C client
./cli

The result is:
Added [some_key]
Value [some_data]
Deleted, old value was [some_data]



Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.