#include "RtnetDataRequest.h"

#include <cstdio>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace com::gpssolutions::rtnet;

RtnetDataRequest::RtnetDataRequest(boost::shared_ptr<TProtocol>& protocol,
                                   boost::shared_ptr<ConnectionStatusMonitor>& connectionMonitor)
                : ConnectionRequiredRunnable(connectionMonitor,"RtnetDataRequestService"),
                  client_(new RtnetDataClient(protocol))
{
}

void
RtnetDataRequest::run() {
  printf("Starting RtnetDataRequest thread.\n");
  while (!stop_) {
    printf("waiting for connection\n");
    connectWait();
    printf("Connected to rtnet\n");
    if (stop_) {
      printf("Quitting RtnetDataRequest thread (stop is true)\n");
      return;
    }
    try {
      printf("sending request for rtnet data stream\n");
      client_->startDataStream();
      disconnectWait();
    } catch (TException& e) {
      fprintf(stderr,"Caught an exception generated by Thrift in RtnetDataRequest: %s\n",e.what());
    }
  }
  printf("Quitting RtnetDataRequest thread\n");
}