RAD Event Handling in C

An event is an asynchronous notification generated by RAD and consumed by clients. For more information, see RAD Events in Remote Administration Daemon Module Developer's Guide.

The following example shows how to subscribe to and handle events. The ZoneManager instance defines a StateChange event that clients can subscribe to information about the changes in the runtime state of a zone.

Example 2-13 C Language – Subscribing to and Handling RAD Events

#include <unistd.h>
#include <time.h>
#include <rad/radclient.h>
#include <rad/radclient_basetypes.h>
#include <rad/client/1/zonemgr.h>

void stateChange_handler(rc_instance_t *inst, zonemgr_StateChange_t *payload, struct timespec timestamp, void *arg)
{       
    printf("event: zone state change\n");
    printf("payload:\n zone: %s\n old state: %s\n new state: %s\n",
    payload->zsc_zone, payload->zsc_oldstate, payload->zsc_newstate);
       
    zonemgr_StateChange_free(payload);
}

rc_err_t status;
rc_instance_t *zm_inst;
int result_count;

rc_conn_t *conn = rc_connect_unix(NULL, B_TRUE, NULL);
if (conn != NULL) {
        status = zonemgr_ZoneManager__rad_lookup(conn, B_TRUE, &zm_inst, 0);
        if (status == RCE_OK) {
            status = zonemgr_ZoneManager_subscribe_stateChange(zm_inst, stateChange_handler, NULL);
            if (status == RCE_OK)
                printf("Successfully subscribed to statechange event!\n");
            rc_instance_rele(zm_inst);
        }
 }
 for (;;)
 sleep(1);

In this example, you have subscribed to a single event by passing a handler and a handle for the ZoneManager object. The handler is invoked asynchronously by the framework with various event details and user data. In this example, the user data is NULL.