Mercurial > hg > orthanc-stone
annotate Deprecated/Platforms/Generic/Oracle.cpp @ 1442:11f291ac3ef5 legacy-loader-refactoring
This branch is useless.
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 26 May 2020 10:11:49 +0200 |
parents | 419d0320c344 |
children |
rev | line source |
---|---|
80 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
956
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
80 | 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 "Oracle.h" | |
23 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
25 #include <Core/MultiThreading/SharedMessageQueue.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
26 #include <Core/OrthancException.h> |
80 | 27 |
28 #include <vector> | |
89
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
29 #include <stdio.h> |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
30 #include <boost/thread/mutex.hpp> |
80 | 31 |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
547
diff
changeset
|
32 namespace Deprecated |
80 | 33 { |
34 class Oracle::PImpl | |
35 { | |
36 private: | |
37 enum State | |
38 { | |
39 State_Init, | |
40 State_Started, | |
41 State_Stopped | |
42 }; | |
43 | |
44 boost::mutex oracleMutex_; | |
45 State state_; | |
46 std::vector<boost::thread*> threads_; | |
47 Orthanc::SharedMessageQueue queue_; | |
48 | |
49 static void Worker(PImpl* that) | |
50 { | |
51 for (;;) | |
52 { | |
53 State state; | |
54 | |
55 { | |
56 boost::mutex::scoped_lock lock(that->oracleMutex_); | |
57 state = that->state_; | |
58 } | |
59 | |
60 if (state == State_Stopped) | |
61 { | |
62 break; | |
63 } | |
64 | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
65 std::unique_ptr<Orthanc::IDynamicObject> item(that->queue_.Dequeue(100)); |
80 | 66 if (item.get() != NULL) |
67 { | |
68 IOracleCommand& command = dynamic_cast<IOracleCommand&>(*item); | |
309 | 69 try |
70 { | |
71 command.Execute(); | |
72 } | |
547
0f43e479b49c
Removed old rtstruct demo (correct one on its way) + Many warning fixes (mostly 64-bit ?) + reformat + small fixes + indent (breaking long lines)
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
73 catch (Orthanc::OrthancException& /*ex*/) |
309 | 74 { |
75 // this is probably a curl error that has been triggered. We may just ignore it. | |
76 // The command.success_ will stay at false and this will be handled in the command.Commit | |
77 } | |
80 | 78 |
89
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
79 // Random sleeping to test |
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
80 //boost::this_thread::sleep(boost::posix_time::milliseconds(50 * (1 + rand() % 10))); |
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
81 |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
82 command.Commit(); |
80 | 83 } |
84 } | |
85 } | |
86 | |
87 public: | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
88 PImpl(unsigned int threadCount) : |
80 | 89 state_(State_Init), |
90 threads_(threadCount) | |
91 { | |
92 } | |
93 | |
94 ~PImpl() | |
95 { | |
96 if (state_ == State_Started) | |
97 { | |
98 LOG(ERROR) << "You should have manually called Oracle::Stop()"; | |
99 Stop(); | |
100 } | |
101 } | |
102 | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
103 Orthanc::SharedMessageQueue& GetQueue() |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
104 { |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
105 return queue_; |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
106 } |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
107 |
80 | 108 void Submit(IOracleCommand* command) |
109 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
110 std::unique_ptr<IOracleCommand> protection(command); |
80 | 111 |
112 if (command == NULL) | |
113 { | |
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
115 } | |
116 | |
117 boost::mutex::scoped_lock lock(oracleMutex_); | |
118 | |
119 switch (state_) | |
120 { | |
121 case State_Init: | |
122 case State_Started: | |
123 queue_.Enqueue(protection.release()); | |
124 break; | |
125 | |
126 case State_Stopped: | |
127 LOG(ERROR) << "Cannot schedule a request to the Oracle after having " | |
128 << "called Oracle::Stop()"; | |
129 break; | |
130 | |
131 default: | |
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
133 } | |
134 | |
135 } | |
136 | |
137 void Start() | |
138 { | |
139 boost::mutex::scoped_lock lock(oracleMutex_); | |
140 | |
141 if (state_ != State_Init) | |
142 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
726
diff
changeset
|
143 LOG(ERROR) << "Oracle::PImpl::Start: (state_ != State_Init)"; |
80 | 144 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
145 } | |
146 | |
147 for (size_t i = 0; i < threads_.size(); i++) | |
148 { | |
149 threads_[i] = new boost::thread(Worker, this); | |
150 } | |
151 | |
152 state_ = State_Started; | |
153 } | |
154 | |
155 void Stop() | |
156 { | |
157 { | |
158 boost::mutex::scoped_lock lock(oracleMutex_); | |
159 | |
160 if (state_ != State_Started) | |
161 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
726
diff
changeset
|
162 LOG(ERROR) << "Oracle::PImpl::Stop(): (state_ != State_Started)"; |
80 | 163 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
164 } | |
165 | |
166 state_ = State_Stopped; | |
167 } | |
168 | |
169 for (size_t i = 0; i < threads_.size(); i++) | |
170 { | |
171 if (threads_[i] != NULL) | |
172 { | |
173 if (threads_[i]->joinable()) | |
174 { | |
175 threads_[i]->join(); | |
176 } | |
177 | |
178 delete threads_[i]; | |
179 } | |
180 } | |
181 } | |
182 }; | |
183 | |
184 | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
185 Oracle::Oracle(unsigned int threadCount) : |
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
186 pimpl_(new PImpl(threadCount)) |
80 | 187 { |
188 } | |
189 | |
190 void Oracle::Start() | |
191 { | |
192 pimpl_->Start(); | |
193 } | |
194 | |
195 | |
196 void Oracle::Submit(IOracleCommand* command) | |
197 { | |
198 pimpl_->Submit(command); | |
199 } | |
200 | |
201 | |
202 void Oracle::Stop() | |
203 { | |
204 pimpl_->Stop(); | |
205 } | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
206 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
207 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
208 void Oracle::WaitEmpty() |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
209 { |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
210 pimpl_->GetQueue().WaitEmpty(50); |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
211 } |
80 | 212 } |