Esempio n. 1
0
/**
 * The method passed to the thread constructor which transmits all
 * Posts in the posts queue to url with cURL in JSON format.
 * The thread is then put to sleep for 1000 milliseconds.
 */
tbb::task * UpdaterThread::execute()
{
  // Get the item id from the propertyMap
  std::string itemId = propertyMap.at(ITEM_ID);

  // Get the item id from the propertyMap
  std::string clientKey = propertyMap.at(CLIENT_KEY);

  // Get the url from the propertyMap
  std::string url = propertyMap.at(URL);

  // Create a new LibcurlUtils object to transmit posts
  MooseSharedPointer<LibcurlUtils> libcurlUtilsPtr(new LibcurlUtils());

  // Set the ignoreSslPeerVerification flag in libcurlUtilsPtr
  libcurlUtilsPtr->setIgnoreSslPeerVerification(ignoreSslPeerVerification);
  libcurlUtilsPtr->setNoProxyFlag(noProxyFlag);

  // Start an infinite loop
  while (!stop)
  {
    // If we have posts to transmit
    if (!empty())
    {
      // Lock the thread using a lock_guard object
      // lock_guard<std::mutex> lock(mutex);
      tbb::mutex::scoped_lock lock(mutex);

      // Create the transmission string and add initial json string to it
      std::string transmission = "{\"item_id\":\"" + itemId
        + "\", \"client_key\":\"" + clientKey + "\", \"posts\":[";

      // Cycle over all posts in posts queue
      while (!empty())
      {
        // Get the first post
        PostPtr postPtr;

        if (this->try_pop(postPtr))
        {
          // Get the JSON from the post
          std::string json = postPtr->getJSON();

          // Add the json to the posts array in the transmission
          transmission += json;

          // Remove the post from the queue
          // pop();

          // If posts is still non empty, add a comma separator
          if (!empty())
            transmission += ",";
        }
      }

      // Add the final characters to the transmission
      transmission += "]}";

      // Transmit post to url and return an error if there is one
      std::string error = libcurlUtilsPtr->post(url,
                                                transmission,
                                                propertyMap[USERNAME],
                                                propertyMap[PASSWORD]);

      // Check to see if there was an error, and log it.
      if (error != "")
        errorLoggerPtr->logError(error);
    }

    // Pause this thread for sleepTime seconds
    tbb::this_tbb_thread::sleep(tbb::tick_count::interval_t(0.2));
  }

  return NULL;
}
Esempio n. 2
0
/**
 * The method passed to the thread constructor which transmits all
 * Posts in the posts queue to url with cURL in JSON format.
 * The thread is then put to sleep for 1000 milliseconds.
 */
void UpdaterThread::runThreaded()
{
  NetworkingToolFactory factory;

  // Get the item id from the propertyMap
  std::string itemId = propertyMap.at(ITEM_ID);

  // Get the item id from the propertyMap
  std::string clientKey = propertyMap.at(CLIENT_KEY);

  // Get the url from the propertyMap
  std::string url = propertyMap.at(URL);

  std::string tool = propertyMap.at(NETWORKING_TOOL);

  // Create a new INetworkingTool object to transmit posts
  networkingTool = factory.createNetworkingTool(tool);

  // Set the ignoreSslPeerVerification flag in networkTool
  networkingTool->setIgnoreSslPeerVerification(ignoreSslPeerVerification);
  networkingTool->setNoProxyFlag(noProxyFlag);

  // Start an infinite loop
  while (!stop)
  {
    // If we have posts to transmit
    if (!empty())
    {
      // Lock the thread using a lock_guard object
      std::mutex mutex;
      std::lock_guard<std::mutex> lock(mutex);

      // Create the transmission string and add initial json string to it
      std::string transmission = "{\"item_id\":\"" + itemId
        + "\", \"client_key\":\"" + clientKey + "\", \"posts\":[";

      // Get the first post
      PostPtr postPtr;

      if (this->pop(postPtr))
      {
        // Get the JSON from the post
        std::string json = postPtr->getJSON();

        // Add the json to the posts array in the transmission
        transmission += json;

        // If posts is still non empty, add a comma separator
        if (!empty())
          transmission += ",";

        // Add the final characters to the transmission
        transmission += "]}";

        // Transmit post to url and return an error if there is one
        std::string error = networkingTool->post(url,
                                              transmission,
                                              propertyMap[USERNAME],
                                              propertyMap[PASSWORD]);

        // Check to see if there was an error, and log it.
        if (error != "")
        {
           errorLoggerPtr->logError(error + "\nStopping ICE HTTP Update.");
           stop.store(true);
        }

      }
    }

    // Pause this thread for sleepTime seconds
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
  }

  return;
}