comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:95226b754d9e
1 /**
2 * Transfers accelerator plugin for Orthanc
3 * Copyright (C) 2018 Osimis, Belgium
4 *
5 * This program is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU Affero General Public License
7 * as published by the Free Software Foundation, either version 3 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 **/
18
19
20 #include "HttpQueriesRunner.h"
21
22 #include <Core/OrthancException.h>
23
24 #include <boost/thread.hpp>
25
26
27 namespace OrthancPlugins
28 {
29 void HttpQueriesRunner::Worker(HttpQueriesRunner* that)
30 {
31 while (that->continue_)
32 {
33 size_t size;
34
35 if (that->queue_.ExecuteOneQuery(size))
36 {
37 boost::mutex::scoped_lock lock(that->mutex_);
38 that->totalTraffic_ += size;
39 that->lastUpdate_ = boost::posix_time::microsec_clock::local_time();
40 }
41 else
42 {
43 // We're done (either failure, or no more pending queries)
44 return;
45 }
46 }
47 }
48
49
50 HttpQueriesRunner::HttpQueriesRunner(HttpQueriesQueue& queue,
51 size_t threadsCount) :
52 queue_(queue),
53 continue_(true),
54 start_(boost::posix_time::microsec_clock::local_time()),
55 totalTraffic_(0),
56 lastUpdate_(start_)
57 {
58 if (threadsCount == 0)
59 {
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
61 }
62
63 workers_.resize(threadsCount);
64
65 for (size_t i = 0; i < threadsCount; i++)
66 {
67 workers_[i] = new boost::thread(Worker, this);
68 }
69 }
70
71
72 HttpQueriesRunner::~HttpQueriesRunner()
73 {
74 continue_ = false;
75
76 for (size_t i = 0; i < workers_.size(); i++)
77 {
78 if (workers_[i] != NULL)
79 {
80 if (workers_[i]->joinable())
81 {
82 workers_[i]->join();
83 }
84
85 delete workers_[i];
86 }
87 }
88 }
89
90
91 void HttpQueriesRunner::GetSpeed(float& kilobytesPerSecond)
92 {
93 boost::mutex::scoped_lock lock(mutex_);
94
95 double ms = static_cast<double>((lastUpdate_ - start_).total_milliseconds());
96
97 if (ms < 10.0)
98 {
99 // Prevents division by zero on very quick transfers
100 kilobytesPerSecond = 0;
101 }
102 else
103 {
104 kilobytesPerSecond = static_cast<float>(
105 static_cast<double>(totalTraffic_) * 1000.0 /*ms*/ / (1024.0 /*KB*/ * ms));
106 }
107 }
108 }