Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | Examples

Monitor.cpp

This is an example of subscription on a device property value. This program:
See also:
Example.h
#include "Example.h"

int replyCount = 10;     // used to count incoming subscription reports
rdaDeviceHandle* device; // device handle
rdaRequest* rq;          // subscription request

//
// Called each time a subscription report is received by the
// SubscriptionReportHandler callback methods.
//
void updateReplyCount()
{
   replyCount--;
   if (replyCount == 0) device->monitorOff(rq); // unsubscribe
   else cout << "*****\n"; // print report separator
}

//
// This class provides a simple implementation of the callback methods
// defined in the rdaReplyHandler class.
//
class SubscriptionReportHandler : public rdaReplyHandler
{
   public:

      virtual void handleReply(const rdaRequest&, const rdaData& value)
      {
         cout << value;
         updateReplyCount();
      }

      virtual void handleError(const rdaRequest&, const rdaException& ex)
      {
         cout << ex << endl;
         updateReplyCount();
      }

      virtual void disconnected(const rdaRequest&)
      {
         cout << "DISCONNECTED FROM DATA SOURCE\n";
      }

      virtual void reconnected(const rdaRequest&)
      {
         cout << "RECONNECTED TO DATA SOURCE\n";
      }

      virtual void cancelled(const rdaRequest&)
      {
         cout << "SUBSCRIPTION CANCELLED\n";
         exit(0);
      }
};

int main(int argc, char** argv)
{
   //
   // Set device call parameters (deviceName, property, cycle) from
   // the command line arguments (see Example.h).
   //
   setParameters(argc, argv);

   try
   {
      //
      // Initialize RDA and obtain the device handle
      //
      rdaRDAService* rda = rdaRDAService::init();
      device = rda->getDeviceHandle(deviceName);
      //
      // Create a callback object that will receive subscription reports
      //
      SubscriptionReportHandler rh;
      //
      // Subscribe to the property value
      //
      if (cycle == 0) rq = device->monitorOn(property, false, &rh);
      else rq = device->monitorOn(property, cycle, false, &rh);
      //
      // Wait for results
      //
      sleepForever(); // see Example.h
   }
   catch(const rdaException& ex)
   {
      cout << ex << endl;
   }
   catch(const rdaInternalError& ex)
   {
      cout << ex << endl;
   }

   return 0;
}

RDA-2.3 documentation - 27 Jun 2007 - N.Trofimov