Mercurial > hg > orthanc
annotate Core/DicomNetworking/Internals/GetScp.cpp @ 3954:67b457283499 c-get
coding style
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 20 May 2020 09:52:20 +0200 |
parents | 620e87e9e816 |
children | 66879215cbf3 |
rev | line source |
---|---|
3818 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 | |
35 | |
36 /*========================================================================= | |
37 | |
38 This file is based on portions of the following project: | |
39 | |
40 Program: DCMTK 3.6.0 | |
41 Module: http://dicom.offis.de/dcmtk.php.en | |
42 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
43 Copyright (C) 1994-2011, OFFIS e.V. |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
44 All rights reserved. |
3818 | 45 |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
46 This software and supporting documentation were developed by |
3818 | 47 |
48 OFFIS e.V. | |
49 R&D Division Health | |
50 Escherweg 2 | |
51 26121 Oldenburg, Germany | |
52 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
53 Redistribution and use in source and binary forms, with or without |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
54 modification, are permitted provided that the following conditions |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
55 are met: |
3818 | 56 |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
57 - Redistributions of source code must retain the above copyright |
3818 | 58 notice, this list of conditions and the following disclaimer. |
59 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
60 - Redistributions in binary form must reproduce the above copyright |
3818 | 61 notice, this list of conditions and the following disclaimer in the |
62 documentation and/or other materials provided with the distribution. | |
63 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
64 - Neither the name of OFFIS nor the names of its contributors may be |
3818 | 65 used to endorse or promote products derived from this software |
66 without specific prior written permission. | |
67 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
68 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
69 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
70 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
71 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
72 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
73 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
74 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
75 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
76 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
77 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
78 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
3818 | 79 |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
80 =========================================================================*/ |
3818 | 81 |
82 | |
83 #include "../../PrecompiledHeaders.h" | |
84 #include <dcmtk/dcmnet/diutil.h> | |
85 #include <dcmtk/dcmdata/dcdeftag.h> | |
86 #include "GetScp.h" | |
87 | |
88 #include <memory> | |
89 | |
90 #include "../../DicomParsing/FromDcmtkBridge.h" | |
91 #include "../../DicomParsing/ToDcmtkBridge.h" | |
92 #include "../../Logging.h" | |
93 #include "../../OrthancException.h" | |
94 | |
95 #include <boost/lexical_cast.hpp> | |
96 | |
97 | |
98 namespace Orthanc | |
99 { | |
100 namespace | |
101 { | |
102 struct GetScpData | |
103 { | |
104 // Handle returns void. | |
105 IGetRequestHandler* handler_; | |
106 DcmDataset* lastRequest_; | |
107 T_ASC_Association * assoc_; | |
108 | |
109 std::string remoteIp_; | |
110 std::string remoteAet_; | |
111 std::string calledAet_; | |
112 | |
113 GetScpData() | |
114 { | |
115 handler_ = NULL; | |
116 lastRequest_ = NULL; | |
117 assoc_ = NULL; | |
118 }; | |
119 }; | |
120 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
121 static DcmDataset *BuildFailedInstanceList(const std::string& failedUIDs) |
3818 | 122 { |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
123 if (failedUIDs.empty()) |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
124 { |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
125 return NULL; |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
126 } |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
127 else |
3818 | 128 { |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
129 std::unique_ptr<DcmDataset> rspIds(new DcmDataset()); |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
130 |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
131 if (!DU_putStringDOElement(rspIds.get(), DCM_FailedSOPInstanceUIDList, failedUIDs.c_str())) |
3818 | 132 { |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
133 throw OrthancException(ErrorCode_InternalError, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
134 "getSCP: failed to build DCM_FailedSOPInstanceUIDList"); |
3818 | 135 } |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
136 |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
137 return rspIds.release(); |
3818 | 138 } |
139 } | |
140 | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
141 static void GetScpCallback( |
3818 | 142 /* in */ |
143 void *callbackData, | |
144 OFBool cancelled, | |
145 T_DIMSE_C_GetRQ *request, | |
146 DcmDataset *requestIdentifiers, | |
147 int responseCount, | |
148 /* out */ | |
149 T_DIMSE_C_GetRSP *response, | |
150 DcmDataset **responseIdentifiers, | |
151 DcmDataset **statusDetail) | |
152 { | |
153 bzero(response, sizeof(T_DIMSE_C_GetRSP)); | |
154 *statusDetail = NULL; | |
155 *responseIdentifiers = NULL; | |
156 | |
157 GetScpData& data = *reinterpret_cast<GetScpData*>(callbackData); | |
158 if (data.lastRequest_ == NULL) | |
159 { | |
160 DicomMap input; | |
161 FromDcmtkBridge::ExtractDicomSummary(input, *requestIdentifiers); | |
162 | |
163 try | |
164 { | |
165 if(!data.handler_->Handle(input, data.remoteIp_, data.remoteAet_, | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
166 data.calledAet_)) |
3818 | 167 { |
168 response->DimseStatus = STATUS_GET_Failed_UnableToProcess; | |
169 return; | |
170 } | |
171 } | |
172 catch (OrthancException& e) | |
173 { | |
174 // Internal error! | |
175 LOG(ERROR) << "IGetRequestHandler Failed: " << e.What(); | |
176 response->DimseStatus = STATUS_GET_Failed_UnableToProcess; | |
177 return; | |
178 } | |
179 | |
180 data.lastRequest_ = requestIdentifiers; | |
181 } | |
182 else if (data.lastRequest_ != requestIdentifiers) | |
183 { | |
184 // Internal error! | |
185 LOG(ERROR) << "IGetRequestHandler Failed: Internal error lastRequestIdentifier"; | |
186 response->DimseStatus = STATUS_GET_Failed_UnableToProcess; | |
187 return; | |
188 } | |
189 | |
3954 | 190 if (data.handler_->GetRemainingCount() == 0) |
3818 | 191 { |
192 response->DimseStatus = STATUS_Success; | |
193 } | |
194 else | |
195 { | |
196 IGetRequestHandler::Status status; | |
197 | |
198 try | |
199 { | |
200 status = data.handler_->DoNext(data.assoc_); | |
201 } | |
202 catch (OrthancException& e) | |
203 { | |
204 // Internal error! | |
205 LOG(ERROR) << "IGetRequestHandler Failed: " << e.What(); | |
206 response->DimseStatus = STATUS_GET_Failed_UnableToProcess; | |
207 return; | |
208 } | |
209 | |
210 if (status == STATUS_Success) | |
211 { | |
3954 | 212 if (responseCount < static_cast<int>(data.handler_->GetRemainingCount())) |
3818 | 213 { |
214 response->DimseStatus = STATUS_Pending; | |
215 } | |
216 else | |
217 { | |
218 response->DimseStatus = STATUS_Success; | |
219 } | |
220 } | |
221 else | |
222 { | |
223 response->DimseStatus = STATUS_GET_Failed_UnableToProcess; | |
224 | |
3954 | 225 if (data.handler_->GetFailedCount() > 0 || |
226 data.handler_->GetWarningCount() > 0) | |
3818 | 227 { |
228 response->DimseStatus = STATUS_GET_Warning_SubOperationsCompleteOneOrMoreFailures; | |
229 } | |
3954 | 230 |
3818 | 231 /* |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
232 * if all the sub-operations failed then we need to generate |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
233 * a failed or refused status. cf. DICOM part 4, C.4.3.3.1 |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
234 * we choose to generate a "Refused - Out of Resources - |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
235 * Unable to perform suboperations" status. |
3818 | 236 */ |
3954 | 237 if ((data.handler_->GetFailedCount() > 0) && |
238 ((data.handler_->GetCompletedCount() + | |
239 data.handler_->GetWarningCount()) == 0)) | |
3818 | 240 { |
241 response->DimseStatus = STATUS_GET_Refused_OutOfResourcesSubOperations; | |
242 } | |
243 | |
3954 | 244 *responseIdentifiers = BuildFailedInstanceList(data.handler_->GetFailedUids()); |
3818 | 245 } |
246 } | |
247 | |
3954 | 248 response->NumberOfRemainingSubOperations = data.handler_->GetRemainingCount(); |
249 response->NumberOfCompletedSubOperations = data.handler_->GetCompletedCount(); | |
250 response->NumberOfFailedSubOperations = data.handler_->GetFailedCount(); | |
251 response->NumberOfWarningSubOperations = data.handler_->GetWarningCount(); | |
3818 | 252 } |
253 } | |
254 | |
255 OFCondition Internals::getScp(T_ASC_Association * assoc, | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
256 T_DIMSE_Message * msg, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
257 T_ASC_PresentationContextID presID, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
258 IGetRequestHandler& handler, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
259 std::string remoteIp, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
260 std::string remoteAet, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
261 std::string calledAet, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
262 int timeout) |
3818 | 263 { |
264 GetScpData data; | |
265 data.lastRequest_ = NULL; | |
266 data.handler_ = &handler; | |
267 data.assoc_ = assoc; | |
268 data.remoteIp_ = remoteIp; | |
269 data.remoteAet_ = remoteAet; | |
270 data.calledAet_ = calledAet; | |
271 | |
272 OFCondition cond = DIMSE_getProvider(assoc, presID, &msg->msg.CGetRQ, | |
3953
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
273 GetScpCallback, &data, |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
274 /*opt_blockMode*/ (timeout ? DIMSE_NONBLOCKING : DIMSE_BLOCKING), |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
275 /*opt_dimse_timeout*/ timeout); |
620e87e9e816
c-get: fixing memory with failedUIDs_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3818
diff
changeset
|
276 |
3818 | 277 // if some error occured, dump corresponding information and remove the outfile if necessary |
278 if (cond.bad()) | |
279 { | |
280 OFString temp_str; | |
281 LOG(ERROR) << "Get SCP Failed: " << cond.text(); | |
282 } | |
283 | |
284 return cond; | |
285 } | |
286 } |