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