#include "CAVERN.h++"
#include <unistd.h>
 

void A_keyCB(CAVERN_irbKey_c::CAVERN_irbKeyEvent_t event,
      CAVERN_irbKey_c *thisKey,
      void* userData);

main(int argc, char** argv)
{

         /// Startup CAVERN.

         CAVERN_irb_c *personalIRB = CAVERNInit(&argc, &argv, NULL); 
         if (!personalIRB) exit(1);

         /// Define a local key called A_KEY.

         CAVERN_irbKeyId_c aKeyId;
         aKeyId.setName("A_KEY");

         CAVERN_irb_c::status_t irbStatus;
         CAVERN_irbKey_c *aKey = personalIRB->define(&aKeyId, NULL, &irbStatus); 

         /// Notify me when A_KEY gets changed by a remote client.

         aKey->trigger(A_keyCB,NULL); 

         /// Do nothing forever.

         while(1) {
              sleep(10);
              cvrnMesg(NULL,".");
         }

}

/// Callback that fires when A_KEY gets filled with new data.

void A_keyCB(CAVERN_irbKey_c::CAVERN_irbKeyEvent_t event,
      CAVERN_irbKey_c *thisKey,
      void* userData)
{

         char *data;
         int size;
         CAVERN_irbKey_c::status_t status;

         /// Grab the data and print it.

         thisKey->getAutoAlloc(data, &size, &status);
         if (size) {
                  cvrnPrintf("DATA: %s\n",data);
                  delete data;
             }
        else {
                  cvrnPrintf("SIZE 0\n");

             }

}