Mercurial > hg > orthanc
annotate OrthancServer/DicomProtocol/DicomServer.cpp @ 1680:4113a9a668b1
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 Oct 2015 13:36:09 +0200 |
parents | d3ba98d6b6e9 |
children | ee4367497d0d |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1243
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1243
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
33 #include "../PrecompiledHeadersServer.h" |
0 | 34 #include "DicomServer.h" |
35 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1454
diff
changeset
|
36 #include "../../Core/Logging.h" |
62 | 37 #include "../../Core/OrthancException.h" |
0 | 38 #include "../../Core/Toolbox.h" |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
39 #include "../../Core/Uuid.h" |
0 | 40 #include "../Internals/CommandDispatcher.h" |
690
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
41 #include "../OrthancInitialization.h" |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
42 #include "EmbeddedResources.h" |
0 | 43 |
44 #include <boost/thread.hpp> | |
45 | |
376
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
46 #if defined(__linux) |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
47 #include <cstdlib> |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
48 #endif |
2cef9c2d4148
separate path for SQLite index, manual loading of external dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
49 |
0 | 50 |
62 | 51 namespace Orthanc |
0 | 52 { |
53 struct DicomServer::PImpl | |
54 { | |
55 boost::thread thread_; | |
56 | |
57 //std::set< | |
58 }; | |
59 | |
60 | |
1680 | 61 void DicomServer::ServerThread(DicomServer* server, |
62 T_ASC_Network *network) | |
685
b01cc78caba4
possibility to disable the DICOM/HTTP servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
660
diff
changeset
|
63 { |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
64 LOG(INFO) << "DICOM server started"; |
0 | 65 |
66 while (server->continue_) | |
67 { | |
68 /* receive an association and acknowledge or reject it. If the association was */ | |
69 /* acknowledged, offer corresponding services and invoke one or more if required. */ | |
1680 | 70 std::auto_ptr<Internals::CommandDispatcher> dispatcher(Internals::AcceptAssociation(*server, network)); |
0 | 71 |
1680 | 72 try |
0 | 73 { |
1680 | 74 if (dispatcher.get() != NULL) |
0 | 75 { |
1680 | 76 if (server->isThreaded_) |
77 { | |
78 server->bagOfDispatchers_.Add(dispatcher.release()); | |
79 } | |
80 else | |
81 { | |
82 IRunnableBySteps::RunUntilDone(*dispatcher); | |
83 } | |
0 | 84 } |
1680 | 85 } |
86 catch (OrthancException& e) | |
87 { | |
88 LOG(ERROR) << "Exception in the DICOM server thread: " << e.What(); | |
0 | 89 } |
90 } | |
91 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
92 LOG(INFO) << "DICOM server stopping"; |
0 | 93 |
1193
e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
94 if (server->isThreaded_) |
e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
95 { |
e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
96 server->bagOfDispatchers_.StopAll(); |
e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
97 } |
e95e7f9eba96
fix crash when stopping Orthanc during a DICOM connection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
890
diff
changeset
|
98 |
0 | 99 /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */ |
100 /* is the counterpart of ASC_initializeNetwork(...) which was called above. */ | |
1680 | 101 OFCondition cond = ASC_dropNetwork(&network); |
0 | 102 if (cond.bad()) |
103 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
104 LOG(ERROR) << "Error while dropping the network: " << cond.text(); |
0 | 105 } |
1680 | 106 } |
0 | 107 |
108 | |
656 | 109 DicomServer::DicomServer() : |
110 pimpl_(new PImpl), | |
111 aet_("ANY-SCP") | |
0 | 112 { |
113 port_ = 104; | |
114 findRequestHandlerFactory_ = NULL; | |
115 moveRequestHandlerFactory_ = NULL; | |
116 storeRequestHandlerFactory_ = NULL; | |
117 applicationEntityFilter_ = NULL; | |
118 checkCalledAet_ = true; | |
119 clientTimeout_ = 30; | |
120 isThreaded_ = true; | |
1680 | 121 continue_ = true; |
0 | 122 } |
123 | |
124 DicomServer::~DicomServer() | |
125 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
126 if (continue_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
127 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
128 LOG(ERROR) << "INTERNAL ERROR: DicomServer::Stop() should be invoked manually to avoid mess in the destruction order!"; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
129 Stop(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
130 } |
0 | 131 } |
132 | |
128 | 133 void DicomServer::SetPortNumber(uint16_t port) |
0 | 134 { |
135 Stop(); | |
136 port_ = port; | |
137 } | |
138 | |
128 | 139 uint16_t DicomServer::GetPortNumber() const |
0 | 140 { |
141 return port_; | |
142 } | |
143 | |
144 void DicomServer::SetThreaded(bool isThreaded) | |
145 { | |
146 Stop(); | |
147 isThreaded_ = isThreaded; | |
148 } | |
149 | |
150 bool DicomServer::IsThreaded() const | |
151 { | |
152 return isThreaded_; | |
153 } | |
154 | |
155 void DicomServer::SetClientTimeout(uint32_t timeout) | |
156 { | |
157 Stop(); | |
158 clientTimeout_ = timeout; | |
159 } | |
160 | |
161 uint32_t DicomServer::GetClientTimeout() const | |
162 { | |
163 return clientTimeout_; | |
164 } | |
165 | |
166 | |
167 void DicomServer::SetCalledApplicationEntityTitleCheck(bool check) | |
168 { | |
169 Stop(); | |
170 checkCalledAet_ = check; | |
171 } | |
172 | |
173 bool DicomServer::HasCalledApplicationEntityTitleCheck() const | |
174 { | |
175 return checkCalledAet_; | |
176 } | |
177 | |
178 void DicomServer::SetApplicationEntityTitle(const std::string& aet) | |
179 { | |
180 if (aet.size() == 0) | |
181 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
182 throw OrthancException(ErrorCode_BadApplicationEntityTitle); |
0 | 183 } |
184 | |
1234 | 185 if (aet.size() > 16) |
186 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
187 throw OrthancException(ErrorCode_BadApplicationEntityTitle); |
1234 | 188 } |
189 | |
0 | 190 for (size_t i = 0; i < aet.size(); i++) |
191 { | |
1243 | 192 if (!(aet[i] == '-' || |
193 aet[i] == '_' || | |
194 isdigit(aet[i]) || | |
195 (aet[i] >= 'A' && aet[i] <= 'Z'))) | |
0 | 196 { |
1234 | 197 LOG(WARNING) << "For best interoperability, only upper case, alphanumeric characters should be present in AET: \"" << aet << "\""; |
198 break; | |
0 | 199 } |
200 } | |
201 | |
202 Stop(); | |
203 aet_ = aet; | |
204 } | |
205 | |
206 const std::string& DicomServer::GetApplicationEntityTitle() const | |
207 { | |
208 return aet_; | |
209 } | |
210 | |
211 void DicomServer::SetFindRequestHandlerFactory(IFindRequestHandlerFactory& factory) | |
212 { | |
213 Stop(); | |
214 findRequestHandlerFactory_ = &factory; | |
215 } | |
216 | |
217 bool DicomServer::HasFindRequestHandlerFactory() const | |
218 { | |
219 return (findRequestHandlerFactory_ != NULL); | |
220 } | |
221 | |
222 IFindRequestHandlerFactory& DicomServer::GetFindRequestHandlerFactory() const | |
223 { | |
224 if (HasFindRequestHandlerFactory()) | |
225 { | |
226 return *findRequestHandlerFactory_; | |
227 } | |
228 else | |
229 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
230 throw OrthancException(ErrorCode_NoCFindHandler); |
0 | 231 } |
232 } | |
233 | |
234 void DicomServer::SetMoveRequestHandlerFactory(IMoveRequestHandlerFactory& factory) | |
235 { | |
236 Stop(); | |
237 moveRequestHandlerFactory_ = &factory; | |
238 } | |
239 | |
240 bool DicomServer::HasMoveRequestHandlerFactory() const | |
241 { | |
242 return (moveRequestHandlerFactory_ != NULL); | |
243 } | |
244 | |
245 IMoveRequestHandlerFactory& DicomServer::GetMoveRequestHandlerFactory() const | |
246 { | |
247 if (HasMoveRequestHandlerFactory()) | |
248 { | |
249 return *moveRequestHandlerFactory_; | |
250 } | |
251 else | |
252 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
253 throw OrthancException(ErrorCode_NoCMoveHandler); |
0 | 254 } |
255 } | |
256 | |
257 void DicomServer::SetStoreRequestHandlerFactory(IStoreRequestHandlerFactory& factory) | |
258 { | |
259 Stop(); | |
260 storeRequestHandlerFactory_ = &factory; | |
261 } | |
262 | |
263 bool DicomServer::HasStoreRequestHandlerFactory() const | |
264 { | |
265 return (storeRequestHandlerFactory_ != NULL); | |
266 } | |
267 | |
268 IStoreRequestHandlerFactory& DicomServer::GetStoreRequestHandlerFactory() const | |
269 { | |
270 if (HasStoreRequestHandlerFactory()) | |
271 { | |
272 return *storeRequestHandlerFactory_; | |
273 } | |
274 else | |
275 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
276 throw OrthancException(ErrorCode_NoCStoreHandler); |
0 | 277 } |
278 } | |
279 | |
280 void DicomServer::SetApplicationEntityFilter(IApplicationEntityFilter& factory) | |
281 { | |
282 Stop(); | |
283 applicationEntityFilter_ = &factory; | |
284 } | |
285 | |
286 bool DicomServer::HasApplicationEntityFilter() const | |
287 { | |
288 return (applicationEntityFilter_ != NULL); | |
289 } | |
290 | |
291 IApplicationEntityFilter& DicomServer::GetApplicationEntityFilter() const | |
292 { | |
293 if (HasApplicationEntityFilter()) | |
294 { | |
295 return *applicationEntityFilter_; | |
296 } | |
297 else | |
298 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
299 throw OrthancException(ErrorCode_NoApplicationEntityFilter); |
0 | 300 } |
301 } | |
302 | |
303 void DicomServer::Start() | |
304 { | |
305 Stop(); | |
306 | |
1680 | 307 /* initialize network, i.e. create an instance of T_ASC_Network*. */ |
308 T_ASC_Network *network; | |
309 OFCondition cond = ASC_initializeNetwork | |
310 (NET_ACCEPTOR, OFstatic_cast(int, port_), /*opt_acse_timeout*/ 30, &network); | |
311 if (cond.bad()) | |
0 | 312 { |
1680 | 313 LOG(ERROR) << "cannot create network: " << cond.text(); |
314 throw OrthancException(ErrorCode_DicomPortInUse); | |
0 | 315 } |
1680 | 316 |
317 continue_ = true; | |
318 pimpl_->thread_ = boost::thread(ServerThread, this, network); | |
0 | 319 } |
320 | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
321 |
0 | 322 void DicomServer::Stop() |
323 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
324 if (continue_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
325 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
326 continue_ = false; |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
327 |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
328 if (pimpl_->thread_.joinable()) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
329 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
330 pimpl_->thread_.join(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
331 } |
1454 | 332 |
333 bagOfDispatchers_.Finalize(); | |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
334 } |
0 | 335 } |
690
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
336 |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
337 |
690
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
338 bool DicomServer::IsMyAETitle(const std::string& aet) const |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
339 { |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
340 if (!HasCalledApplicationEntityTitleCheck()) |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
341 { |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
342 // OK, no check on the AET. |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
343 return true; |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
344 } |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
345 |
810
401a9633e492
configuration into a namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
690
diff
changeset
|
346 return Configuration::IsSameAETitle(aet, GetApplicationEntityTitle()); |
690
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
347 } |
2e67366aab83
case-insensitive matching of Application Entity Titles
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
348 |
0 | 349 } |