Mercurial > hg > orthanc-stone
annotate Platforms/Generic/Oracle.cpp @ 702:c0a5eb9a4290 refactor-viewport-controller
Merge from default
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Sun, 19 May 2019 16:29:50 +0200 |
parents | 0f43e479b49c |
children | 4f2416d519b4 |
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 | |
439 | 5 * Copyright (C) 2017-2019 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 |
32 namespace OrthancStone | |
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 | |
65 std::auto_ptr<Orthanc::IDynamicObject> item(that->queue_.Dequeue(100)); | |
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 { | |
110 std::auto_ptr<IOracleCommand> protection(command); | |
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 { | |
143 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
144 } | |
145 | |
146 for (size_t i = 0; i < threads_.size(); i++) | |
147 { | |
148 threads_[i] = new boost::thread(Worker, this); | |
149 } | |
150 | |
151 state_ = State_Started; | |
152 } | |
153 | |
154 void Stop() | |
155 { | |
156 { | |
157 boost::mutex::scoped_lock lock(oracleMutex_); | |
158 | |
159 if (state_ != State_Started) | |
160 { | |
161 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
162 } | |
163 | |
164 state_ = State_Stopped; | |
165 } | |
166 | |
167 for (size_t i = 0; i < threads_.size(); i++) | |
168 { | |
169 if (threads_[i] != NULL) | |
170 { | |
171 if (threads_[i]->joinable()) | |
172 { | |
173 threads_[i]->join(); | |
174 } | |
175 | |
176 delete threads_[i]; | |
177 } | |
178 } | |
179 } | |
180 }; | |
181 | |
182 | |
271
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
183 Oracle::Oracle(unsigned int threadCount) : |
46c5296d867e
OracleWebService and BasicSdlApplicationContext using the same global mutex
am@osimis.io
parents:
212
diff
changeset
|
184 pimpl_(new PImpl(threadCount)) |
80 | 185 { |
186 } | |
187 | |
188 void Oracle::Start() | |
189 { | |
190 pimpl_->Start(); | |
191 } | |
192 | |
193 | |
194 void Oracle::Submit(IOracleCommand* command) | |
195 { | |
196 pimpl_->Submit(command); | |
197 } | |
198 | |
199 | |
200 void Oracle::Stop() | |
201 { | |
202 pimpl_->Stop(); | |
203 } | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
204 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
205 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
206 void Oracle::WaitEmpty() |
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 pimpl_->GetQueue().WaitEmpty(50); |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
209 } |
80 | 210 } |