core38Example_main.c
                                            
                                            Client/Server example in C language. This example needs a compiled schema, core38Example.sdl.
/*
                                                    
                                                     * Demonstrates client/server with writing/reading and snapshot reads
                                                    
                                                     */
                                                    
                                                    #include <stdio.h>
                                                    
                                                    #include <string.h>
                                                    
                                                    #include <stdlib.h>
                                                    
                                                    #include "rdm.h"
                                                    
                                                    #include "core38Example_structs.h"
                                                    
                                                    #include "core38Example_cat.h"
                                                    
                                                    #include "example_fcns.h"
                                                    
                                                    #include "rdmstartupapi.h"
                                                    
                                                    RDM_CMDLINE cmd;
                                                    const char* const description = "Demonstrates client/server with writing/reading and snapshot reads";
                                                    
                                                        {"", "writer", NULL, "Start writer process with tfs_emded"},
                                                        {"", "reader", "s=*", "Start reader process and connect to given URI"},
                                                        {"", "snapshot", "s=*", "Start snapshot reader process and connect to given URI"},
                                                        {"", "timeout", "i=#", "Set writer timeout in seconds"},
                                                        {"", "end", "i=#", "Stop after NN minutes"},
                                                        {NULL, NULL, NULL, NULL} };
                                                    static uint32_t timeout = 0;
                                                    static uint32_t minutes = 5;
                                                    {
                                                    char timeoutString[100];
                                                        printf("Timeout: %u Second\n", timeout);
                                                        sprintf(timeoutString, "%u", timeout);
                                                    
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                        rc = rdm_rdmAllocTFS(pTFS);
                                                    print_error(rc);
                                                    
                                                        {
                                                    
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = rdm_tfsInitialize(*pTFS);
                                                    print_error(rc);
                                                            }
                                                    /* If any setup options have occurred, free the handle */
                                                    
                                                    if (rc != sOKAY)
                                                    rdm_tfsFree(*pTFS);
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                        rc = rdm_rdmAllocTFS(pTFS);
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                        {
                                                            rc = rdm_tfsSetOptions(*pTFS, "tfstype=remote");
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = rdm_tfsInitialize(*pTFS);
                                                    print_error(rc);
                                                            }
                                                    /* If any setup options have occurred, free the handle */
                                                    
                                                    if (rc != sOKAY)
                                                    rdm_tfsFree(*pTFS);
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    /* shutdown ability for new clients to connect */
                                                    
                                                        puts("** Disable new connections.");
                                                        rc = rdm_tfsDisableListener(tfs);
                                                    print_error(rc);
                                                        puts("** Drop existing connections.");
                                                    /* drop all currently connected clients */
                                                    
                                                    
                                                    print_error(rc);
                                                        puts("** Remove database image.");
                                                    
                                                    print_error(rc);
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    perfTimer_t timer;
                                                        READINGS recordData;
                                                        uint32_t exitTime = minutes * 60 * 1000;  /* change time to millisecs */
                                                        uint32_t counter = 0;
                                                        puts("Starting Writer Process");
                                                    timeMeasureBegin(&timer);
                                                    
                                                        {
                                                    
                                                    
                                                            {
                                                                printf("Timeout after inserting %u rows\n", counter);
                                                                counter = 0;
                                                            }
                                                    
                                                            {
                                                    rdm_timestampNow(0, &recordData.r_time);
                                                                recordData.r_value = rand();
                                                                strcpy(recordData.r_desc, "Sample description");
                                                    
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                                {
                                                                    counter++;
                                                    rdm_dbEnd(db);
                                                                }
                                                    else
                                                    
                                                                {
                                                    rdm_dbEndRollback(db);
                                                                }
                                                            }
                                                    else
                                                    
                                                            {
                                                    print_error(rc);
                                                    break;
                                                            }
                                                    timeMeasureEnd(&timer);
                                                    
                                                            {
                                                                printf("Normal Shutdown after inserting %u rows\n", counter);
                                                    break;
                                                            }
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    perfTimer_t timer;
                                                        uint32_t exitTime = minutes * 60 * 1000;  /* change time to millisecs */
                                                        puts("Starting Reader Process");
                                                    timeMeasureBegin(&timer);
                                                    while (RDM_TRUE)
                                                        {
                                                    
                                                    print_error(rc);
                                                    if (rc == eUNAVAIL)
                                                            {
                                                                puts("Lock timeout");
                                                            }
                                                    
                                                            {
                                                    RDM_CURSOR cursor = NULL;
                                                                uint32_t counter = 0;
                                                                rc = rdm_dbGetRows(db, TABLE_READINGS, &cursor);
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                                {
                                                    
                                                                    {
                                                                        READINGS recordData;
                                                    /* All we'll do here is just read the row */
                                                    
                                                    if (rc == sOKAY)
                                                                            counter++;
                                                                    }
                                                    /* sENDOFCURSOR is the expected return code */
                                                    
                                                    
                                                                        rc = sOKAY;
                                                    print_error(rc);
                                                    rdm_dbEnd(db);
                                                                    printf("Read %u rows\n", counter);
                                                                }
                                                            }
                                                    else
                                                    
                                                            {
                                                    break;
                                                            }
                                                    /* See if it is time to exit this loop */
                                                    
                                                    timeMeasureEnd(&timer);
                                                    if (timeMeasureDiff(&timer) >= exitTime)
                                                            {
                                                                puts("Normal Shutdown");
                                                    break;
                                                            }
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    perfTimer_t timer;
                                                        uint32_t exitTime = minutes * 60 * 1000;  /* change time to millisecs */
                                                        puts("Starting Reader Process");
                                                    timeMeasureBegin(&timer);
                                                    while (RDM_TRUE)
                                                        {
                                                    
                                                    print_error(rc);
                                                    if (rc == eUNAVAIL)
                                                            {
                                                                puts("Lock timeout");
                                                            }
                                                    
                                                            {
                                                    RDM_CURSOR cursor = NULL;
                                                                uint32_t counter = 0;
                                                                rc = rdm_dbGetRows(db, TABLE_READINGS, &cursor);
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                                {
                                                    
                                                                    {
                                                                        READINGS recordData;
                                                                        rc = rdm_cursorReadRow(cursor, &recordData, sizeof(recordData), NULL);
                                                    /* All we'll do here is just read the row */
                                                    
                                                    if (rc == sOKAY)
                                                                            counter++;
                                                                    }
                                                    /* sENDOFCURSOR is the expected return code */
                                                    
                                                    if (rc == sENDOFCURSOR)
                                                                        rc = sOKAY;
                                                    print_error(rc);
                                                    rdm_dbEnd(db);
                                                                    printf("Read %u rows\n", counter);
                                                                }
                                                            }
                                                    else
                                                    
                                                            {
                                                    break;
                                                            }
                                                    /* See if it is time to exit this loop */
                                                    
                                                    timeMeasureEnd(&timer);
                                                    if (timeMeasureDiff(&timer) >= exitTime)
                                                            {
                                                                puts("Normal Shutdown");
                                                    break;
                                                            }
                                                        }
                                                    return rc;
                                                    }
                                                    /* Start Embed TFS and being inserting rows into the database.
                                                    
                                                    */
                                                    
                                                    
                                                    {
                                                    RDM_RETCODE rc;
                                                    RDM_TFS tfs;
                                                    RDM_DB db;
                                                        printf("Writer with embedded TFS\n\nShutdown in %u minutes\n", minutes);
                                                        rc = startEmbedTFS(&tfs);
                                                    if (rc == sOKAY)
                                                        {
                                                    
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = setTimeout(db);
                                                    print_error(rc);
                                                            }
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = writerProcess(db);
                                                    rdm_dbClose(db);
                                                                shutdownEmbedTFS(tfs);
                                                            }
                                                    rdm_tfsFree(tfs);
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    RDM_TFS tfs;
                                                    RDM_DB db;
                                                        printf("Reader connecting to TFS at %s\n\nShutdown in %u minutes\n", uri, minutes);
                                                        rc = startRemoteTFS(&tfs);
                                                    if (rc == sOKAY)
                                                        {
                                                    char dbname[1000];
                                                            sprintf(dbname, "%s/core38", uri);
                                                            printf("Attempting to open: %s", dbname);
                                                            rc = exampleOpenNextDatabase(tfs, &db, dbname, NULL);
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = setTimeout(db);
                                                    print_error(rc);
                                                            }
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = readerProcess(db);
                                                    rdm_dbClose(db);
                                                            }
                                                    rdm_tfsFree(tfs);
                                                        }
                                                    return rc;
                                                    }
                                                    {
                                                    RDM_RETCODE rc;
                                                    RDM_TFS tfs;
                                                    RDM_DB db;
                                                        printf("Snapshot Reader connecting to TFS at %s\n\nShutdown in %u minutes\n", uri, minutes);
                                                        rc = startRemoteTFS(&tfs);
                                                    if (rc == sOKAY)
                                                        {
                                                    char dbname[1000];
                                                            sprintf(dbname, "%s/core38", uri);
                                                            printf("Attempting to open: %s", dbname);
                                                            rc = exampleOpenNextDatabase(tfs, &db, dbname, NULL);
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = setTimeout(db);
                                                    print_error(rc);
                                                            }
                                                    if (rc == sOKAY)
                                                            {
                                                                rc = snapshotProcess(db);
                                                    rdm_dbClose(db);
                                                            }
                                                    rdm_tfsFree(tfs);
                                                        }
                                                    return rc;
                                                    }
                                                    int main_core38(int argc, const char* const* argv)
                                                    {
                                                    RDM_RETCODE rc;
                                                    const char* opt;
                                                    const char* optarg;
                                                    char cMode = 0;
                                                        rc = rdm_cmdlineInit(&cmd, argc, argv, description, opts);
                                                    
                                                    print_error(rc);
                                                    if (rc == sOKAY)
                                                        {
                                                    
                                                            {
                                                    switch (*opt)
                                                                {
                                                    case 'w':
                                                    if (cMode)
                                                                    {
                                                                        puts("TFS mode already set.");
                                                                        rc = sCMD_USAGE;
                                                                    }
                                                    else
                                                    
                                                                    {
                                                                        cMode = *opt;
                                                                    }
                                                    break;
                                                    case 'r':
                                                    if (cMode)
                                                                    {
                                                                        puts("TFS mode already set.");
                                                                        rc = sCMD_USAGE;
                                                                    }
                                                    else
                                                    
                                                                    {
                                                                        cMode = *opt;
                                                                    }
                                                    break;
                                                    case 's':
                                                    if (cMode)
                                                                    {
                                                                        puts("TFS mode already set.");
                                                                        rc = sCMD_USAGE;
                                                                    }
                                                    else
                                                    
                                                                    {
                                                                        cMode = *opt;
                                                                    }
                                                    break;
                                                    case 't':
                                                                    timeout = atoi(optarg);
                                                    break;
                                                    case 'e':
                                                                    minutes = atoi(optarg);
                                                    break;
                                                    default:
                                                    break;
                                                                }
                                                            }
                                                    if (rc == sOKAY)
                                                            {
                                                    switch (cMode)
                                                                {
                                                    case 'w':
                                                                    rc = startWriterWithEmbeddedServer();
                                                    break;
                                                    case 'r':
                                                                    rc = startReader(optarg);
                                                    break;
                                                    case 's':
                                                                    rc = startSnapshotReader(optarg);
                                                    break;
                                                    default:
                                                                    puts("No Action Defined. Exiting.");
                                                    break;
                                                                }
                                                            }
                                                        }
                                                    return (int)rc;
                                                    }
                                                    RDM_STARTUP_EXAMPLE (core38)
                                                RDM_RETCODE rdm_tfsKillAllRemoteConnections(RDM_TFS tfs, const char *uri)
                                            Kill all the TFS connections.
                                        Header for the native RDM Runtime API.
                                        void timeMeasureBegin(perfTimer_t *timer)
                                        RDM_RETCODE rdm_timestampNow(int16_t time_zone, RDM_PACKED_TIMESTAMP_T *ts)
                                            Get the current local timestamp.
                                        RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
                                            Get read locks.
                                        RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
                                            Read all columns from a row.
                                        void timeMeasureEnd(perfTimer_t *timer)
                                        The buffer used by the command line parser to hold state information.
                                            Definition: rdmcmdlinetypes.h:85
                                        RDM_RETCODE rdm_dbClose(RDM_DB db)
                                            Close the database associated with a database handle.
                                        RDM_RETCODE rdm_dbSetOption(RDM_DB db, const char *keyword, const char *strValue)
                                            Set a single RDM option from a string.
                                        RDM_RETCODE rdm_dbStartSnapshot(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
                                            Start a snapshot.
                                        RDM_RETCODE rdm_dbGetRows(RDM_DB db, RDM_TABLE_ID tableId, RDM_CURSOR *pCursor)
                                            Associate an RDM_CURSOR with rows based on a table id.
                                        #define exampleOpenNextDatabase(tfs, db, name, catalog)
                                            Definition: example_fcns.h:26
                                        unsigned int timeMeasureDiff(perfTimer_t *timer)
                                        RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
                                            End and rollback a transactional operation.
                                        RDM_RETCODE rdm_tfsDropDatabase(RDM_TFS tfs, const char *dbNameSpec)
                                            Drop the specified database.
                                        RDM_RETCODE rdm_dbInsertRow(RDM_DB db, RDM_TABLE_ID tableId, const void *colValues, size_t bytesIn, RDM_CURSOR *pCursor)
                                            Insert a new row into a table at the specified rowId.
                                        RDM_RETCODE rdm_tfsDisableListener(RDM_TFS hTFS)
                                            Stop the listener process for the TFS.
                                        RDM_RETCODE rdm_cmdlineInit(RDM_CMDLINE *cmd, int32_t argc, const char *const argv[], const char *description, const RDM_CMDLINE_OPT *opts)
                                            Initialize an RDM_CMDLINE buffer and validate the command line.
                                        RDM_RETCODE rdm_dbStartUpdate(RDM_DB db, const RDM_TABLE_ID *writeTableIds, uint32_t numWriteTableIds, const RDM_TABLE_ID *readTableIds, uint32_t numReadTableIds, RDM_TRANS *pTrans)
                                            Get write locks.
                                        const char * rdm_cmdlineNextLongOption(RDM_CMDLINE *cmd, const char **arg)
                                            Get next option or argument.
                                        Definition: example_fcns.h:37
                                        RDM_RETCODE rdm_tfsSetOptions(RDM_TFS tfs, const char *optString)
                                            Set TFS options.
                                        Internal RDM Startup API used by startup macros.