comparison Framework/Loaders/GenericLoadersContext.cpp @ 1228:c471a0aa137b broker

adding the next generation of loaders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Dec 2019 13:58:37 +0100
parents
children b9b5d4378874
comparison
equal deleted inserted replaced
1227:a1c0c9c9f9af 1228:c471a0aa137b
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "GenericLoadersContext.h"
23
24 namespace OrthancStone
25 {
26 class GenericLoadersContext::Locker : public ILoadersContext::ILock
27 {
28 private:
29 GenericLoadersContext& that_;
30 boost::recursive_mutex::scoped_lock lock_;
31
32 public:
33 Locker(GenericLoadersContext& that) :
34 that_(that),
35 lock_(that.mutex_)
36 {
37 }
38
39 virtual ILoadersContext& GetContext() const ORTHANC_OVERRIDE
40 {
41 return that_;
42 };
43
44 virtual void AddLoader(boost::shared_ptr<IObserver> loader) ORTHANC_OVERRIDE
45 {
46 that_.loaders_.push_back(loader);
47 }
48
49 virtual IObservable& GetOracleObservable() const ORTHANC_OVERRIDE
50 {
51 return that_.oracleObservable_;
52 }
53
54 virtual void Schedule(boost::shared_ptr<IObserver> receiver,
55 int priority,
56 IOracleCommand* command /* Takes ownership */) ORTHANC_OVERRIDE
57 {
58 that_.scheduler_->Schedule(receiver, priority, command);
59 };
60
61 virtual void CancelRequests(boost::shared_ptr<IObserver> receiver) ORTHANC_OVERRIDE
62 {
63 that_.scheduler_->CancelRequests(receiver);
64 }
65
66 virtual void CancelAllRequests() ORTHANC_OVERRIDE
67 {
68 that_.scheduler_->CancelAllRequests();
69 }
70 };
71
72
73 void GenericLoadersContext::EmitMessage(boost::weak_ptr<IObserver> observer,
74 const IMessage& message)
75 {
76 boost::recursive_mutex::scoped_lock lock(mutex_);
77 //LOG(INFO) << " inside emit lock: " << message.GetIdentifier().AsString();
78 oracleObservable_.EmitMessage(observer, message);
79 //LOG(INFO) << " outside emit lock";
80 }
81
82
83 GenericLoadersContext::GenericLoadersContext(unsigned int maxHighPriority,
84 unsigned int maxStandardPriority,
85 unsigned int maxLowPriority)
86 {
87 oracle_.reset(new ThreadedOracle(*this));
88 scheduler_ = OracleScheduler::Create(*oracle_, oracleObservable_, *this,
89 maxHighPriority, maxStandardPriority, maxLowPriority);
90 }
91
92
93 GenericLoadersContext::~GenericLoadersContext()
94 {
95 LOG(WARNING) << "scheduled commands: " << scheduler_->GetTotalScheduled()
96 << ", processed commands: " << scheduler_->GetTotalProcessed();
97 scheduler_.reset();
98 //LOG(INFO) << "counter: " << scheduler_.use_count();
99 }
100
101
102 void GenericLoadersContext::SetOrthancParameters(const Orthanc::WebServiceParameters& parameters)
103 {
104 boost::recursive_mutex::scoped_lock lock(mutex_);
105 oracle_->SetOrthancParameters(parameters);
106 }
107
108
109 void GenericLoadersContext::SetRootDirectory(const std::string& root)
110 {
111 boost::recursive_mutex::scoped_lock lock(mutex_);
112 oracle_->SetRootDirectory(root);
113 }
114
115
116 void GenericLoadersContext::SetDicomCacheSize(size_t size)
117 {
118 boost::recursive_mutex::scoped_lock lock(mutex_);
119 oracle_->SetDicomCacheSize(size);
120 }
121
122
123 void GenericLoadersContext::StartOracle()
124 {
125 boost::recursive_mutex::scoped_lock lock(mutex_);
126 oracle_->Start();
127 //LOG(INFO) << "STARTED ORACLE";
128 }
129
130
131 void GenericLoadersContext::StopOracle()
132 {
133 /**
134 * DON'T lock "mutex_" here, otherwise Stone won't be able to
135 * stop if one command being executed by the oracle has to emit
136 * a message (method "EmitMessage()" would have to lock the
137 * mutex too).
138 **/
139
140 //LOG(INFO) << "STOPPING ORACLE";
141 oracle_->Stop();
142 //LOG(INFO) << "STOPPED ORACLE";
143 }
144
145
146 void GenericLoadersContext::WaitUntilComplete()
147 {
148 for (;;)
149 {
150 {
151 boost::recursive_mutex::scoped_lock lock(mutex_);
152 if (scheduler_ &&
153 scheduler_->GetTotalScheduled() == scheduler_->GetTotalProcessed())
154 {
155 return;
156 }
157 }
158
159 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
160 }
161 }
162
163
164 ILoadersContext::ILock* GenericLoadersContext::Lock()
165 {
166 return new Locker(*this);
167 }
168
169
170 void GenericLoadersContext::GetStatistics(uint64_t& scheduledCommands,
171 uint64_t& processedCommands)
172 {
173 boost::recursive_mutex::scoped_lock lock(mutex_);
174 if (scheduler_)
175 {
176 scheduledCommands = scheduler_->GetTotalScheduled();
177 processedCommands = scheduler_->GetTotalProcessed();
178 }
179 else
180 {
181 scheduledCommands = 0;
182 processedCommands = 0;
183 }
184 }
185 }