0
|
1 /**
|
|
2 * Palantir - A Lightweight, RESTful DICOM Store
|
|
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.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "DicomServer.h"
|
|
22
|
|
23 #include "../../Core/PalantirException.h"
|
|
24 #include "../../Core/Toolbox.h"
|
|
25 #include "../Internals/CommandDispatcher.h"
|
|
26
|
|
27 #include <boost/thread.hpp>
|
|
28 #include <dcmtk/dcmdata/dcdict.h>
|
|
29
|
|
30
|
|
31 namespace Palantir
|
|
32 {
|
|
33 struct DicomServer::PImpl
|
|
34 {
|
|
35 boost::thread thread_;
|
|
36
|
|
37 //std::set<
|
|
38 };
|
|
39
|
|
40
|
|
41 namespace Internals
|
|
42 {
|
|
43 OFLogger Logger = OFLog::getLogger("dcmtk.apps.storescp");
|
|
44 }
|
|
45
|
|
46
|
|
47 void DicomServer::ServerThread(DicomServer* server)
|
|
48 {
|
|
49 /* Disable "gethostbyaddr" (which results in memory leaks) and use raw IP addresses */
|
|
50 dcmDisableGethostbyaddr.set(OFTrue);
|
|
51
|
|
52 /* make sure data dictionary is loaded */
|
|
53 if (!dcmDataDict.isDictionaryLoaded())
|
|
54 {
|
|
55 OFLOG_WARN(Internals::Logger, "no data dictionary loaded, check environment variable: "
|
|
56 << DCM_DICT_ENVIRONMENT_VARIABLE);
|
|
57 }
|
|
58
|
|
59 /* initialize network, i.e. create an instance of T_ASC_Network*. */
|
|
60 T_ASC_Network *net;
|
|
61 OFCondition cond = ASC_initializeNetwork
|
|
62 (NET_ACCEPTOR, OFstatic_cast(int, server->port_), /*opt_acse_timeout*/ 30, &net);
|
|
63 if (cond.bad())
|
|
64 {
|
|
65 OFString temp_str;
|
|
66 OFLOG_ERROR(Internals::Logger, "cannot create network: " << DimseCondition::dump(temp_str, cond));
|
|
67 throw PalantirException("Cannot create network");
|
|
68 }
|
|
69
|
|
70 OFLOG_WARN(Internals::Logger, "DICOM server started");
|
|
71
|
|
72 server->started_ = true;
|
|
73
|
|
74 while (server->continue_)
|
|
75 {
|
|
76 /* receive an association and acknowledge or reject it. If the association was */
|
|
77 /* acknowledged, offer corresponding services and invoke one or more if required. */
|
|
78 std::auto_ptr<Internals::CommandDispatcher> dispatcher(Internals::AcceptAssociation(*server, net));
|
|
79
|
|
80 if (dispatcher.get() != NULL)
|
|
81 {
|
|
82 if (server->isThreaded_)
|
|
83 {
|
|
84 server->bagOfDispatchers_.Add(dispatcher.release());
|
|
85 }
|
|
86 else
|
|
87 {
|
|
88 IRunnableBySteps::RunUntilDone(*dispatcher);
|
|
89 }
|
|
90 }
|
|
91 }
|
|
92
|
|
93 OFLOG_WARN(Internals::Logger, "DICOM server stopping");
|
|
94
|
|
95 /* drop the network, i.e. free memory of T_ASC_Network* structure. This call */
|
|
96 /* is the counterpart of ASC_initializeNetwork(...) which was called above. */
|
|
97 cond = ASC_dropNetwork(&net);
|
|
98 if (cond.bad())
|
|
99 {
|
|
100 OFString temp_str;
|
|
101 OFLOG_ERROR(Internals::Logger, DimseCondition::dump(temp_str, cond));
|
|
102 }
|
|
103 }
|
|
104
|
|
105
|
|
106 DicomServer::DicomServer() : pimpl_(new PImpl)
|
|
107 {
|
|
108 aet_ = "ANY-SCP";
|
|
109 port_ = 104;
|
|
110 findRequestHandlerFactory_ = NULL;
|
|
111 moveRequestHandlerFactory_ = NULL;
|
|
112 storeRequestHandlerFactory_ = NULL;
|
|
113 applicationEntityFilter_ = NULL;
|
|
114 checkCalledAet_ = true;
|
|
115 clientTimeout_ = 30;
|
|
116 isThreaded_ = true;
|
|
117 }
|
|
118
|
|
119 DicomServer::~DicomServer()
|
|
120 {
|
|
121 Stop();
|
|
122 }
|
|
123
|
2
|
124 void DicomServer::SetPortNumber(uint16_t port)
|
0
|
125 {
|
|
126 Stop();
|
|
127 port_ = port;
|
|
128 }
|
|
129
|
2
|
130 uint16_t DicomServer::GetPortNumber() const
|
0
|
131 {
|
|
132 return port_;
|
|
133 }
|
|
134
|
|
135 void DicomServer::SetThreaded(bool isThreaded)
|
|
136 {
|
|
137 Stop();
|
|
138 isThreaded_ = isThreaded;
|
|
139 }
|
|
140
|
|
141 bool DicomServer::IsThreaded() const
|
|
142 {
|
|
143 return isThreaded_;
|
|
144 }
|
|
145
|
|
146 void DicomServer::SetClientTimeout(uint32_t timeout)
|
|
147 {
|
|
148 Stop();
|
|
149 clientTimeout_ = timeout;
|
|
150 }
|
|
151
|
|
152 uint32_t DicomServer::GetClientTimeout() const
|
|
153 {
|
|
154 return clientTimeout_;
|
|
155 }
|
|
156
|
|
157
|
|
158 void DicomServer::SetCalledApplicationEntityTitleCheck(bool check)
|
|
159 {
|
|
160 Stop();
|
|
161 checkCalledAet_ = check;
|
|
162 }
|
|
163
|
|
164 bool DicomServer::HasCalledApplicationEntityTitleCheck() const
|
|
165 {
|
|
166 return checkCalledAet_;
|
|
167 }
|
|
168
|
|
169 void DicomServer::SetApplicationEntityTitle(const std::string& aet)
|
|
170 {
|
|
171 if (aet.size() == 0)
|
|
172 {
|
|
173 throw PalantirException("Too short AET");
|
|
174 }
|
|
175
|
|
176 for (size_t i = 0; i < aet.size(); i++)
|
|
177 {
|
|
178 if (!isalnum(aet[i]) && aet[i] != '-')
|
|
179 {
|
|
180 throw PalantirException("Only alphanumeric characters are allowed in AET");
|
|
181 }
|
|
182 }
|
|
183
|
|
184 Stop();
|
|
185 aet_ = aet;
|
|
186 }
|
|
187
|
|
188 const std::string& DicomServer::GetApplicationEntityTitle() const
|
|
189 {
|
|
190 return aet_;
|
|
191 }
|
|
192
|
|
193 void DicomServer::SetFindRequestHandlerFactory(IFindRequestHandlerFactory& factory)
|
|
194 {
|
|
195 Stop();
|
|
196 findRequestHandlerFactory_ = &factory;
|
|
197 }
|
|
198
|
|
199 bool DicomServer::HasFindRequestHandlerFactory() const
|
|
200 {
|
|
201 return (findRequestHandlerFactory_ != NULL);
|
|
202 }
|
|
203
|
|
204 IFindRequestHandlerFactory& DicomServer::GetFindRequestHandlerFactory() const
|
|
205 {
|
|
206 if (HasFindRequestHandlerFactory())
|
|
207 {
|
|
208 return *findRequestHandlerFactory_;
|
|
209 }
|
|
210 else
|
|
211 {
|
|
212 throw PalantirException("No C-FIND request handler factory");
|
|
213 }
|
|
214 }
|
|
215
|
|
216 void DicomServer::SetMoveRequestHandlerFactory(IMoveRequestHandlerFactory& factory)
|
|
217 {
|
|
218 Stop();
|
|
219 moveRequestHandlerFactory_ = &factory;
|
|
220 }
|
|
221
|
|
222 bool DicomServer::HasMoveRequestHandlerFactory() const
|
|
223 {
|
|
224 return (moveRequestHandlerFactory_ != NULL);
|
|
225 }
|
|
226
|
|
227 IMoveRequestHandlerFactory& DicomServer::GetMoveRequestHandlerFactory() const
|
|
228 {
|
|
229 if (HasMoveRequestHandlerFactory())
|
|
230 {
|
|
231 return *moveRequestHandlerFactory_;
|
|
232 }
|
|
233 else
|
|
234 {
|
|
235 throw PalantirException("No C-MOVE request handler factory");
|
|
236 }
|
|
237 }
|
|
238
|
|
239 void DicomServer::SetStoreRequestHandlerFactory(IStoreRequestHandlerFactory& factory)
|
|
240 {
|
|
241 Stop();
|
|
242 storeRequestHandlerFactory_ = &factory;
|
|
243 }
|
|
244
|
|
245 bool DicomServer::HasStoreRequestHandlerFactory() const
|
|
246 {
|
|
247 return (storeRequestHandlerFactory_ != NULL);
|
|
248 }
|
|
249
|
|
250 IStoreRequestHandlerFactory& DicomServer::GetStoreRequestHandlerFactory() const
|
|
251 {
|
|
252 if (HasStoreRequestHandlerFactory())
|
|
253 {
|
|
254 return *storeRequestHandlerFactory_;
|
|
255 }
|
|
256 else
|
|
257 {
|
|
258 throw PalantirException("No C-STORE request handler factory");
|
|
259 }
|
|
260 }
|
|
261
|
|
262 void DicomServer::SetApplicationEntityFilter(IApplicationEntityFilter& factory)
|
|
263 {
|
|
264 Stop();
|
|
265 applicationEntityFilter_ = &factory;
|
|
266 }
|
|
267
|
|
268 bool DicomServer::HasApplicationEntityFilter() const
|
|
269 {
|
|
270 return (applicationEntityFilter_ != NULL);
|
|
271 }
|
|
272
|
|
273 IApplicationEntityFilter& DicomServer::GetApplicationEntityFilter() const
|
|
274 {
|
|
275 if (HasApplicationEntityFilter())
|
|
276 {
|
|
277 return *applicationEntityFilter_;
|
|
278 }
|
|
279 else
|
|
280 {
|
|
281 throw PalantirException("No application entity filter");
|
|
282 }
|
|
283 }
|
|
284
|
|
285 void DicomServer::Start()
|
|
286 {
|
|
287 Stop();
|
|
288 continue_ = true;
|
|
289 started_ = false;
|
|
290 pimpl_->thread_ = boost::thread(ServerThread, this);
|
|
291
|
|
292 while (!started_)
|
|
293 {
|
|
294 Toolbox::USleep(50000); // Wait 50ms
|
|
295 }
|
|
296 }
|
|
297
|
|
298 void DicomServer::Stop()
|
|
299 {
|
|
300 continue_ = false;
|
|
301 pimpl_->thread_.join();
|
|
302
|
|
303 bagOfDispatchers_.StopAll();
|
|
304 }
|
|
305 }
|