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

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);

         if (argc > 1) {
                  if (strcmp(argv[1],"mcast") == 0) {
                           CAVERN_irbMcastChannel_c *mcastChannel = personalIRB->createMcastChannel();
                           CAVERN_irbMcastChannel_c::status_t mcastStatus;

                           mcastChannel->open("224.100.100.100",9123,64,NULL, &mcastStatus);
                           if (mcastStatus != CAVERN_irbMcastChannel_c::OK) exit(1);

                           CAVERN_irbMcastLink_c *mcastLink = mcastChannel->link(aKey);
                           cvrnPrintf("%s: Multicasting to 224.100.100.100:9123\n",argv[0]);
                      }
                else
                       cvrnPrintf("%s No multicasting\n",argv[0]);
             }
        else
                  cvrnPrintf("%s No multicasting\n",argv[0]);
 

         float movement = 0;
         float ping = 0.1;
         int size;
         CAVERN_irbKey_c::status_t keyStatus;

         while(1) {
                  size = sizeof(float);
                  aKey->put((char*) &movement,&size, &keyStatus);
                  movement+=ping;
                  if (movement > 3) ping = -ping;
                  if (movement < -3) ping = -ping;
                  sginap(10);

         }

}