comparison OrthancStone/Sources/Loaders/GenericLoadersContext.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Loaders/GenericLoadersContext.cpp@297e57d3508c
children 85e117739eca
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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-2020 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 if (!that_.scheduler_)
38 {
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
40 }
41 }
42
43 virtual ILoadersContext& GetContext() const ORTHANC_OVERRIDE
44 {
45 return that_;
46 };
47
48 virtual void AddLoader(boost::shared_ptr<IObserver> loader) ORTHANC_OVERRIDE
49 {
50 that_.loaders_.push_back(loader);
51 }
52
53 virtual IObservable& GetOracleObservable() const ORTHANC_OVERRIDE
54 {
55 return that_.oracleObservable_;
56 }
57
58 virtual void Schedule(boost::shared_ptr<IObserver> receiver,
59 int priority,
60 IOracleCommand* command /* Takes ownership */) ORTHANC_OVERRIDE
61 {
62 that_.scheduler_->Schedule(receiver, priority, command);
63 };
64
65 virtual void CancelRequests(boost::shared_ptr<IObserver> receiver) ORTHANC_OVERRIDE
66 {
67 that_.scheduler_->CancelRequests(receiver);
68 }
69
70 virtual void CancelAllRequests() ORTHANC_OVERRIDE
71 {
72 that_.scheduler_->CancelAllRequests();
73 }
74
75 virtual void GetStatistics(uint64_t& scheduledCommands,
76 uint64_t& processedCommands) ORTHANC_OVERRIDE
77 {
78 scheduledCommands = that_.scheduler_->GetTotalScheduled();
79 processedCommands = that_.scheduler_->GetTotalProcessed();
80 }
81 };
82
83
84 void GenericLoadersContext::EmitMessage(boost::weak_ptr<IObserver> observer,
85 const IMessage& message)
86 {
87 boost::recursive_mutex::scoped_lock lock(mutex_);
88 //LOG(INFO) << " inside emit lock: " << message.GetIdentifier().AsString();
89 oracleObservable_.EmitMessage(observer, message);
90 //LOG(INFO) << " outside emit lock";
91 }
92
93
94 GenericLoadersContext::GenericLoadersContext(unsigned int maxHighPriority,
95 unsigned int maxStandardPriority,
96 unsigned int maxLowPriority)
97 {
98 oracle_.reset(new ThreadedOracle(*this));
99 scheduler_ = OracleScheduler::Create(*oracle_, oracleObservable_, *this,
100 maxHighPriority, maxStandardPriority, maxLowPriority);
101
102 if (!scheduler_)
103 {
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
105 }
106 }
107
108
109 GenericLoadersContext::~GenericLoadersContext()
110 {
111 LOG(INFO) << "scheduled commands: " << scheduler_->GetTotalScheduled()
112 << ", processed commands: " << scheduler_->GetTotalProcessed();
113 scheduler_.reset();
114 //LOG(INFO) << "counter: " << scheduler_.use_count();
115 }
116
117
118 void GenericLoadersContext::SetOrthancParameters(const Orthanc::WebServiceParameters& parameters)
119 {
120 boost::recursive_mutex::scoped_lock lock(mutex_);
121 oracle_->SetOrthancParameters(parameters);
122 }
123
124
125 void GenericLoadersContext::SetRootDirectory(const std::string& root)
126 {
127 boost::recursive_mutex::scoped_lock lock(mutex_);
128 oracle_->SetRootDirectory(root);
129 }
130
131
132 void GenericLoadersContext::SetDicomCacheSize(size_t size)
133 {
134 boost::recursive_mutex::scoped_lock lock(mutex_);
135 oracle_->SetDicomCacheSize(size);
136 }
137
138
139 void GenericLoadersContext::StartOracle()
140 {
141 boost::recursive_mutex::scoped_lock lock(mutex_);
142 oracle_->Start();
143 //LOG(INFO) << "STARTED ORACLE";
144 }
145
146
147 void GenericLoadersContext::StopOracle()
148 {
149 /**
150 * DON'T lock "mutex_" here, otherwise Stone won't be able to
151 * stop if one command being executed by the oracle has to emit
152 * a message (method "EmitMessage()" would have to lock the
153 * mutex too).
154 **/
155
156 //LOG(INFO) << "STOPPING ORACLE";
157 oracle_->Stop();
158 //LOG(INFO) << "STOPPED ORACLE";
159 }
160
161
162 void GenericLoadersContext::WaitUntilComplete()
163 {
164 for (;;)
165 {
166 {
167 boost::recursive_mutex::scoped_lock lock(mutex_);
168 if (scheduler_ &&
169 scheduler_->GetTotalScheduled() == scheduler_->GetTotalProcessed())
170 {
171 return;
172 }
173 }
174
175 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
176 }
177 }
178
179 ILoadersContext::ILock* GenericLoadersContext::Lock()
180 {
181 return new Locker(*this);
182 }
183 }