Mercurial > hg > orthanc
annotate OrthancServer/DicomProtocol/DicomServer.cpp @ 334:ae0818659d50
cleaning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jan 2013 15:52:45 +0100 |
parents | 6c4f6229aad7 |
children | 8d3a0db39967 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * Belgium | |
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 | |
33 #include "DicomServer.h" | |
34 | |
62 | 35 #include "../../Core/OrthancException.h" |
0 | 36 #include "../../Core/Toolbox.h" |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
37 #include "../../Core/Uuid.h" |
0 | 38 #include "../Internals/CommandDispatcher.h" |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
39 #include "EmbeddedResources.h" |
0 | 40 |
41 #include <boost/thread.hpp> | |
42 #include <dcmtk/dcmdata/dcdict.h> | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
43 #include <glog/logging.h> |
0 | 44 |
45 | |
62 | 46 namespace Orthanc |
0 | 47 { |
48 struct DicomServer::PImpl | |
49 { | |
50 boost::thread thread_; | |
51 | |
52 //std::set< | |
53 }; | |
54 | |
55 | |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
56 static void LoadEmbeddedDictionary(DcmDataDictionary& dictionary, |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
57 EmbeddedResources::FileResourceId resource) |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
58 { |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
59 Toolbox::TemporaryFile tmp; |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
60 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
61 FILE* fp = fopen(tmp.GetPath().c_str(), "wb"); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
62 fwrite(EmbeddedResources::GetFileResourceBuffer(resource), |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
63 EmbeddedResources::GetFileResourceSize(resource), 1, fp); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
64 fclose(fp); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
65 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
66 if (!dictionary.loadDictionary(tmp.GetPath().c_str())) |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
67 { |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
68 throw OrthancException(ErrorCode_InternalError); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
69 } |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
70 } |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
71 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
72 |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
73 |
0 | 74 void DicomServer::ServerThread(DicomServer* server) |
75 { | |
76 /* Disable "gethostbyaddr" (which results in memory leaks) and use raw IP addresses */ | |
77 dcmDisableGethostbyaddr.set(OFTrue); | |
78 | |
298
6c4f6229aad7
fix in embedding dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
291
diff
changeset
|
79 #if DCMTK_USE_EMBEDDED_DICTIONARIES == 1 |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
80 LOG(WARNING) << "Loading the embedded dictionaries"; |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
81 dcmDataDict.clear(); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
82 DcmDataDictionary& d = dcmDataDict.wrlock(); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
83 LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_DICOM); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
84 LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_PRIVATE); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
85 LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_DICONDE); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
86 dcmDataDict.unlock(); |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
87 #endif |
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
88 |
0 | 89 /* make sure data dictionary is loaded */ |
90 if (!dcmDataDict.isDictionaryLoaded()) | |
91 { | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
92 LOG(ERROR) << "no data dictionary loaded, check environment variable: " << DCM_DICT_ENVIRONMENT_VARIABLE; |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
93 throw OrthancException(ErrorCode_InternalError); |
0 | 94 } |
95 | |
96 /* initialize network, i.e. create an instance of T_ASC_Network*. */ | |
97 T_ASC_Network *net; | |
98 OFCondition cond = ASC_initializeNetwork | |
99 (NET_ACCEPTOR, OFstatic_cast(int, server->port_), /*opt_acse_timeout*/ 30, &net); | |
100 if (cond.bad()) | |
101 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
102 LOG(ERROR) << "cannot create network: " << cond.text(); |
62 | 103 throw OrthancException("Cannot create network"); |
0 | 104 } |
105 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
106 LOG(INFO) << "DICOM server started"; |
0 | 107 |
108 server->started_ = true; | |
109 | |
110 while (server->continue_) | |
111 { | |
112 /* receive an association and acknowledge or reject it. If the association was */ | |
113 /* acknowledged, offer corresponding services and invoke one or more if required. */ | |
114 std::auto_ptr<Internals::CommandDispatcher> dispatcher(Internals::AcceptAssociation(*server, net)); | |
115 | |
116 if (dispatcher.get() != NULL) | |
117 { | |
118 if (server->isThreaded_) | |
119 { | |
120 server->bagOfDispatchers_.Add(dispatcher.release()); | |
121 } | |
122 else | |
123 { | |
124 IRunnableBySteps::RunUntilDone(*dispatcher); | |
125 } | |
126 } | |
127 } | |
128 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
129 LOG(INFO) << "DICOM server stopping"; |
0 | 130 |
131 /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */ | |
132 /* is the counterpart of ASC_initializeNetwork(...) which was called above. */ | |
133 cond = ASC_dropNetwork(&net); | |
134 if (cond.bad()) | |
135 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
136 LOG(ERROR) << "Error while dropping the network: " << cond.text(); |
0 | 137 } |
138 } | |
139 | |
140 | |
141 DicomServer::DicomServer() : pimpl_(new PImpl) | |
142 { | |
143 aet_ = "ANY-SCP"; | |
144 port_ = 104; | |
145 findRequestHandlerFactory_ = NULL; | |
146 moveRequestHandlerFactory_ = NULL; | |
147 storeRequestHandlerFactory_ = NULL; | |
148 applicationEntityFilter_ = NULL; | |
149 checkCalledAet_ = true; | |
150 clientTimeout_ = 30; | |
151 isThreaded_ = true; | |
152 } | |
153 | |
154 DicomServer::~DicomServer() | |
155 { | |
156 Stop(); | |
157 } | |
158 | |
128 | 159 void DicomServer::SetPortNumber(uint16_t port) |
0 | 160 { |
161 Stop(); | |
162 port_ = port; | |
163 } | |
164 | |
128 | 165 uint16_t DicomServer::GetPortNumber() const |
0 | 166 { |
167 return port_; | |
168 } | |
169 | |
170 void DicomServer::SetThreaded(bool isThreaded) | |
171 { | |
172 Stop(); | |
173 isThreaded_ = isThreaded; | |
174 } | |
175 | |
176 bool DicomServer::IsThreaded() const | |
177 { | |
178 return isThreaded_; | |
179 } | |
180 | |
181 void DicomServer::SetClientTimeout(uint32_t timeout) | |
182 { | |
183 Stop(); | |
184 clientTimeout_ = timeout; | |
185 } | |
186 | |
187 uint32_t DicomServer::GetClientTimeout() const | |
188 { | |
189 return clientTimeout_; | |
190 } | |
191 | |
192 | |
193 void DicomServer::SetCalledApplicationEntityTitleCheck(bool check) | |
194 { | |
195 Stop(); | |
196 checkCalledAet_ = check; | |
197 } | |
198 | |
199 bool DicomServer::HasCalledApplicationEntityTitleCheck() const | |
200 { | |
201 return checkCalledAet_; | |
202 } | |
203 | |
204 void DicomServer::SetApplicationEntityTitle(const std::string& aet) | |
205 { | |
206 if (aet.size() == 0) | |
207 { | |
62 | 208 throw OrthancException("Too short AET"); |
0 | 209 } |
210 | |
211 for (size_t i = 0; i < aet.size(); i++) | |
212 { | |
213 if (!isalnum(aet[i]) && aet[i] != '-') | |
214 { | |
62 | 215 throw OrthancException("Only alphanumeric characters are allowed in AET"); |
0 | 216 } |
217 } | |
218 | |
219 Stop(); | |
220 aet_ = aet; | |
221 } | |
222 | |
223 const std::string& DicomServer::GetApplicationEntityTitle() const | |
224 { | |
225 return aet_; | |
226 } | |
227 | |
228 void DicomServer::SetFindRequestHandlerFactory(IFindRequestHandlerFactory& factory) | |
229 { | |
230 Stop(); | |
231 findRequestHandlerFactory_ = &factory; | |
232 } | |
233 | |
234 bool DicomServer::HasFindRequestHandlerFactory() const | |
235 { | |
236 return (findRequestHandlerFactory_ != NULL); | |
237 } | |
238 | |
239 IFindRequestHandlerFactory& DicomServer::GetFindRequestHandlerFactory() const | |
240 { | |
241 if (HasFindRequestHandlerFactory()) | |
242 { | |
243 return *findRequestHandlerFactory_; | |
244 } | |
245 else | |
246 { | |
62 | 247 throw OrthancException("No C-FIND request handler factory"); |
0 | 248 } |
249 } | |
250 | |
251 void DicomServer::SetMoveRequestHandlerFactory(IMoveRequestHandlerFactory& factory) | |
252 { | |
253 Stop(); | |
254 moveRequestHandlerFactory_ = &factory; | |
255 } | |
256 | |
257 bool DicomServer::HasMoveRequestHandlerFactory() const | |
258 { | |
259 return (moveRequestHandlerFactory_ != NULL); | |
260 } | |
261 | |
262 IMoveRequestHandlerFactory& DicomServer::GetMoveRequestHandlerFactory() const | |
263 { | |
264 if (HasMoveRequestHandlerFactory()) | |
265 { | |
266 return *moveRequestHandlerFactory_; | |
267 } | |
268 else | |
269 { | |
62 | 270 throw OrthancException("No C-MOVE request handler factory"); |
0 | 271 } |
272 } | |
273 | |
274 void DicomServer::SetStoreRequestHandlerFactory(IStoreRequestHandlerFactory& factory) | |
275 { | |
276 Stop(); | |
277 storeRequestHandlerFactory_ = &factory; | |
278 } | |
279 | |
280 bool DicomServer::HasStoreRequestHandlerFactory() const | |
281 { | |
282 return (storeRequestHandlerFactory_ != NULL); | |
283 } | |
284 | |
285 IStoreRequestHandlerFactory& DicomServer::GetStoreRequestHandlerFactory() const | |
286 { | |
287 if (HasStoreRequestHandlerFactory()) | |
288 { | |
289 return *storeRequestHandlerFactory_; | |
290 } | |
291 else | |
292 { | |
62 | 293 throw OrthancException("No C-STORE request handler factory"); |
0 | 294 } |
295 } | |
296 | |
297 void DicomServer::SetApplicationEntityFilter(IApplicationEntityFilter& factory) | |
298 { | |
299 Stop(); | |
300 applicationEntityFilter_ = &factory; | |
301 } | |
302 | |
303 bool DicomServer::HasApplicationEntityFilter() const | |
304 { | |
305 return (applicationEntityFilter_ != NULL); | |
306 } | |
307 | |
308 IApplicationEntityFilter& DicomServer::GetApplicationEntityFilter() const | |
309 { | |
310 if (HasApplicationEntityFilter()) | |
311 { | |
312 return *applicationEntityFilter_; | |
313 } | |
314 else | |
315 { | |
62 | 316 throw OrthancException("No application entity filter"); |
0 | 317 } |
318 } | |
319 | |
320 void DicomServer::Start() | |
321 { | |
322 Stop(); | |
323 continue_ = true; | |
324 started_ = false; | |
325 pimpl_->thread_ = boost::thread(ServerThread, this); | |
326 | |
327 while (!started_) | |
328 { | |
329 Toolbox::USleep(50000); // Wait 50ms | |
330 } | |
331 } | |
332 | |
333 void DicomServer::Stop() | |
334 { | |
335 continue_ = false; | |
336 pimpl_->thread_.join(); | |
337 | |
338 bagOfDispatchers_.StopAll(); | |
339 } | |
340 } |