diff Framework/HttpQueries/HttpQueriesRunner.cpp @ 0:95226b754d9e

initial release
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 17 Sep 2018 11:34:55 +0200
parents
children 7e207ade2f1a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Framework/HttpQueries/HttpQueriesRunner.cpp	Mon Sep 17 11:34:55 2018 +0200
@@ -0,0 +1,108 @@
+/**
+ * Transfers accelerator plugin for Orthanc
+ * Copyright (C) 2018 Osimis, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "HttpQueriesRunner.h"
+
+#include <Core/OrthancException.h>
+
+#include <boost/thread.hpp>
+
+
+namespace OrthancPlugins
+{
+  void HttpQueriesRunner::Worker(HttpQueriesRunner* that)
+  {
+    while (that->continue_)
+    {
+      size_t size;
+        
+      if (that->queue_.ExecuteOneQuery(size))
+      {
+        boost::mutex::scoped_lock lock(that->mutex_);
+        that->totalTraffic_ += size;
+        that->lastUpdate_ = boost::posix_time::microsec_clock::local_time();
+      }
+      else
+      {
+        // We're done (either failure, or no more pending queries)
+        return;
+      }
+    }
+  }
+
+
+  HttpQueriesRunner::HttpQueriesRunner(HttpQueriesQueue& queue,
+                                       size_t threadsCount) :
+    queue_(queue),
+    continue_(true),
+    start_(boost::posix_time::microsec_clock::local_time()),
+    totalTraffic_(0),
+    lastUpdate_(start_)
+  {
+    if (threadsCount == 0)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+      
+    workers_.resize(threadsCount);
+
+    for (size_t i = 0; i < threadsCount; i++)
+    {
+      workers_[i] = new boost::thread(Worker, this);
+    }
+  }
+
+
+  HttpQueriesRunner::~HttpQueriesRunner()
+  {
+    continue_ = false;
+
+    for (size_t i = 0; i < workers_.size(); i++)
+    {
+      if (workers_[i] != NULL)
+      {
+        if (workers_[i]->joinable())
+        {
+          workers_[i]->join();
+        }
+
+        delete workers_[i];
+      }
+    }
+  }
+
+    
+  void HttpQueriesRunner::GetSpeed(float& kilobytesPerSecond)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+      
+    double ms = static_cast<double>((lastUpdate_ - start_).total_milliseconds());
+
+    if (ms < 10.0)
+    {
+      // Prevents division by zero on very quick transfers
+      kilobytesPerSecond = 0;
+    }
+    else
+    {
+      kilobytesPerSecond = static_cast<float>(
+        static_cast<double>(totalTraffic_) * 1000.0 /*ms*/ / (1024.0 /*KB*/ * ms));
+    }
+  }
+}