Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages | Examples

Client.cpp

The test client application used in Device Server Example.
#include <rda/RDAService.h>
#include <rda/Config.h>

//
// Global variables
//
static rdaRDAService* rda = 0;      // RDA service
static rdaDeviceHandle* device = 0; // selected device handle
static rdaRequest* request = 0;     // current subscription request

//
// Prints the value and timestamp
//
static void printValue(double value, double timestamp)
{
   time_t sec = (time_t)(timestamp/1000);
   printf("%4.1f %s", value, ctime(&sec));
}

//
// Prints the value Data
//
static void printValueData(const rdaData& data)
{
   try
   {
      double value = data.extractDouble("value");
      double timestamp = data.extractDouble("timestamp");
      printValue(value, timestamp);
   }
   catch(...)
   {
      data.print();
   }
}

//
// Prints the error description
//
static void printError(const rdaException& ex)
{
   printf("%s: %s\n", ex.getType(), ex.getMessage());
}

//
// Executes the get operation on the specified property and prints results
//
static void doGet(const char* prop)
{
   try
   {
      rdaData* value = device->get(prop);
      printValueData(*value);
      delete value;
   }
   catch(const rdaException& ex)
   {
      printError(ex);
   }
}

//
// Attempts to set the property value
//
static void doSet(const char* prop, double value)
{
   try
   {
      rdaData data;
      data.insert("value", value);
      device->set(prop, data);
   }
   catch(const rdaException& ex)
   {
      printError(ex);
   }
}

//
// The callback object that is used to handle subscription results
//
class SubscriptionReportHandler : public rdaReplyHandler
{
   public:

      virtual void handleReply(const rdaRequest& rq, const rdaData& value)
      {
         printf("%s/%s = ",
                 rq.getDeviceHandle()->getDeviceName(),
                 rq.getPropertyName());
         printValueData(value);
      }

      virtual void handleError(const rdaRequest& rq, const rdaException& ex)
      {
         printf("%s/%s - error reported\n",
                 rq.getDeviceHandle()->getDeviceName(),
                 rq.getPropertyName());
         printError(ex);
      }

      virtual void disconnected(const rdaRequest& rq)
      {
         printf("%s/%s - disconnected from data source\n",
                 rq.getDeviceHandle()->getDeviceName(),
                 rq.getPropertyName());
      }

      virtual void reconnected(const rdaRequest& rq)
      {
         printf("%s/%s - reconnected to data source\n",
                 rq.getDeviceHandle()->getDeviceName(),
                 rq.getPropertyName());
      }

      virtual void cancelled(const rdaRequest& rq)
      {
         printf("%s/%s - subscription cancelled\n",
                 rq.getDeviceHandle()->getDeviceName(),
                 rq.getPropertyName());
      }
};

static SubscriptionReportHandler replyHandler;

//
// Starts a subscription to the specified property
//
static void doMon(const char* prop)
{
   try
   {
      if (request != 0) device->monitorOff(request);
      request = device->monitorOn(prop, false, &replyHandler);
   }
   catch(const rdaException& ex)
   {
      printError(ex);
   }
}


int main(int argc, char** argv)
{
   //
   // Check if the server name is specified in the command line
   //
   if (argc < 2) 
   {
      printf("Usage: %s <server name>\n", argv[0]);
      return 1;
   }
   //
   // Forward all device calls to the specified server
   //
   rdaConfig::setProperty("cmw.rda.testServer", argv[1]);

   try
   {
      //
      // Initialize the RDA
      //
      rda = rdaRDAService::init();

      while (true)
      {
         char command[100];
         char ss1[21];
         char ss2[21];
         char ss3[21];

         //
         // Wait for the user input
         //
         printf("=> ");
         fgets(command, 100, stdin);
         
         //
         // Interpret the user command
         //
         if (*command == '\n')
         {
            if (request != 0) device->monitorOff(request);
            continue;
         }
         int count = sscanf(command, "%20s %20s %20s", ss1, ss2, ss3);
         if (count == 1 && !strcmp(ss1, "device"))
         {
            if (device == 0) printf("Device not defined\n");
            else printf("%s\n", device->getDeviceName());
         }
         else if (count == 2 && !strcmp(ss1, "device"))
         {
            try { device = rda->getDeviceHandle(ss2); }
            catch(...) { printf("Device not found\n"); }
         }
         else if (count == 2 && !strcmp(ss1, "get"))
         {
            doGet(ss2);
         }
         else if (count == 3 && !strcmp(ss1, "set"))
         {
            double value = 0;
            int n = sscanf(ss3, "%lf", &value);
            if (n == 0) printf("Invalid value\n");
            else doSet(ss2, value);
         }
         else if (count == 2 && !strcmp(ss1, "mon"))
         {
            doMon(ss2);
         }
         else if (count == 1 && !strcmp(ss1, "quit"))
         {
            return 0;
         }
         else printf("Invalid command\n");

      } // end while
   }
   catch(const rdaInternalException& ex)
   {
      printf("RDA internal exception: %s\n", ex.getMessage());
      return 1;
   }
   catch(const rdaInternalError& ex)
   {
      printf("RDA internal error [%s:%d]: %s\n",
             ex.getFile(), ex.getLine(), ex.getMessage());
      return 1;
   }
   catch(...)
   {
      printf("Cannot proceed: unknown exception\n");
      return 1;
   }
   return 0;
}

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