Mercurial > hg > orthanc
comparison OrthancFramework/Sources/DicomNetworking/Internals/StoreScp.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/DicomNetworking/Internals/StoreScp.cpp@58f92b1c8061 |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
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-2020 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 | |
43 Copyright (C) 1994-2011, OFFIS e.V. | |
44 All rights reserved. | |
45 | |
46 This software and supporting documentation were developed by | |
47 | |
48 OFFIS e.V. | |
49 R&D Division Health | |
50 Escherweg 2 | |
51 26121 Oldenburg, Germany | |
52 | |
53 Redistribution and use in source and binary forms, with or without | |
54 modification, are permitted provided that the following conditions | |
55 are met: | |
56 | |
57 - Redistributions of source code must retain the above copyright | |
58 notice, this list of conditions and the following disclaimer. | |
59 | |
60 - Redistributions in binary form must reproduce the above copyright | |
61 notice, this list of conditions and the following disclaimer in the | |
62 documentation and/or other materials provided with the distribution. | |
63 | |
64 - Neither the name of OFFIS nor the names of its contributors may be | |
65 used to endorse or promote products derived from this software | |
66 without specific prior written permission. | |
67 | |
68 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
69 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
70 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
71 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
72 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
73 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
74 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
75 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
76 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
77 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
78 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
79 | |
80 =========================================================================*/ | |
81 | |
82 | |
83 #include "../../PrecompiledHeaders.h" | |
84 #include "StoreScp.h" | |
85 | |
86 #if !defined(DCMTK_VERSION_NUMBER) | |
87 # error The macro DCMTK_VERSION_NUMBER must be defined | |
88 #endif | |
89 | |
90 #include "../../DicomParsing/FromDcmtkBridge.h" | |
91 #include "../../DicomParsing/ToDcmtkBridge.h" | |
92 #include "../../OrthancException.h" | |
93 #include "../../Logging.h" | |
94 | |
95 #include <dcmtk/dcmdata/dcfilefo.h> | |
96 #include <dcmtk/dcmdata/dcmetinf.h> | |
97 #include <dcmtk/dcmdata/dcostrmb.h> | |
98 #include <dcmtk/dcmdata/dcdeftag.h> | |
99 #include <dcmtk/dcmnet/diutil.h> | |
100 | |
101 | |
102 namespace Orthanc | |
103 { | |
104 namespace | |
105 { | |
106 struct StoreCallbackData | |
107 { | |
108 IStoreRequestHandler* handler; | |
109 const std::string* remoteIp; | |
110 const char* remoteAET; | |
111 const char* calledAET; | |
112 const char* modality; | |
113 const char* affectedSOPInstanceUID; | |
114 uint32_t messageID; | |
115 }; | |
116 | |
117 | |
118 static void | |
119 storeScpCallback( | |
120 void *callbackData, | |
121 T_DIMSE_StoreProgress *progress, | |
122 T_DIMSE_C_StoreRQ *req, | |
123 char * /*imageFileName*/, DcmDataset **imageDataSet, | |
124 T_DIMSE_C_StoreRSP *rsp, | |
125 DcmDataset **statusDetail) | |
126 /* | |
127 * This function.is used to indicate progress when storescp receives instance data over the | |
128 * network. On the final call to this function (identified by progress->state == DIMSE_StoreEnd) | |
129 * this function will store the data set which was received over the network to a file. | |
130 * Earlier calls to this function will simply cause some information to be dumped to stdout. | |
131 * | |
132 * Parameters: | |
133 * callbackData - [in] data for this callback function | |
134 * progress - [in] The state of progress. (identifies if this is the initial or final call | |
135 * to this function, or a call in between these two calls. | |
136 * req - [in] The original store request message. | |
137 * imageFileName - [in] The path to and name of the file the information shall be written to. | |
138 * imageDataSet - [in] The data set which shall be stored in the image file | |
139 * rsp - [inout] the C-STORE-RSP message (will be sent after the call to this function) | |
140 * statusDetail - [inout] This variable can be used to capture detailed information with regard to | |
141 * the status information which is captured in the status element (0000,0900). Note | |
142 * that this function does specify any such information, the pointer will be set to NULL. | |
143 */ | |
144 { | |
145 StoreCallbackData *cbdata = OFstatic_cast(StoreCallbackData *, callbackData); | |
146 | |
147 DIC_UI sopClass; | |
148 DIC_UI sopInstance; | |
149 | |
150 // if this is the final call of this function, save the data which was received to a file | |
151 // (note that we could also save the image somewhere else, put it in database, etc.) | |
152 if (progress->state == DIMSE_StoreEnd) | |
153 { | |
154 OFString tmpStr; | |
155 | |
156 // do not send status detail information | |
157 *statusDetail = NULL; | |
158 | |
159 // Concerning the following line: an appropriate status code is already set in the resp structure, | |
160 // it need not be success. For example, if the caller has already detected an out of resources problem | |
161 // then the status will reflect this. The callback function is still called to allow cleanup. | |
162 //rsp->DimseStatus = STATUS_Success; | |
163 | |
164 // we want to write the received information to a file only if this information | |
165 // is present and the options opt_bitPreserving and opt_ignore are not set. | |
166 if ((imageDataSet != NULL) && (*imageDataSet != NULL)) | |
167 { | |
168 DicomMap summary; | |
169 Json::Value dicomJson; | |
170 std::string buffer; | |
171 | |
172 try | |
173 { | |
174 std::set<DicomTag> ignoreTagLength; | |
175 | |
176 FromDcmtkBridge::ExtractDicomSummary(summary, **imageDataSet); | |
177 FromDcmtkBridge::ExtractDicomAsJson(dicomJson, **imageDataSet, ignoreTagLength); | |
178 | |
179 if (!FromDcmtkBridge::SaveToMemoryBuffer(buffer, **imageDataSet)) | |
180 { | |
181 LOG(ERROR) << "cannot write DICOM file to memory"; | |
182 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
183 } | |
184 } | |
185 catch (...) | |
186 { | |
187 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
188 } | |
189 | |
190 // check the image to make sure it is consistent, i.e. that its sopClass and sopInstance correspond | |
191 // to those mentioned in the request. If not, set the status in the response message variable. | |
192 if (rsp->DimseStatus == STATUS_Success) | |
193 { | |
194 // which SOP class and SOP instance ? | |
195 | |
196 #if DCMTK_VERSION_NUMBER >= 364 | |
197 if (!DU_findSOPClassAndInstanceInDataSet(*imageDataSet, sopClass, sizeof(sopClass), | |
198 sopInstance, sizeof(sopInstance), /*opt_correctUIDPadding*/ OFFalse)) | |
199 #else | |
200 if (!DU_findSOPClassAndInstanceInDataSet(*imageDataSet, sopClass, sopInstance, /*opt_correctUIDPadding*/ OFFalse)) | |
201 #endif | |
202 { | |
203 //LOG4CPP_ERROR(Internals::GetLogger(), "bad DICOM file: " << fileName); | |
204 rsp->DimseStatus = STATUS_STORE_Error_CannotUnderstand; | |
205 } | |
206 else if (strcmp(sopClass, req->AffectedSOPClassUID) != 0) | |
207 { | |
208 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
209 } | |
210 else if (strcmp(sopInstance, req->AffectedSOPInstanceUID) != 0) | |
211 { | |
212 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
213 } | |
214 else | |
215 { | |
216 try | |
217 { | |
218 cbdata->handler->Handle(buffer, summary, dicomJson, *cbdata->remoteIp, cbdata->remoteAET, cbdata->calledAET); | |
219 } | |
220 catch (OrthancException& e) | |
221 { | |
222 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
223 | |
224 if (e.GetErrorCode() == ErrorCode_InexistentTag) | |
225 { | |
226 summary.LogMissingTagsForStore(); | |
227 } | |
228 else | |
229 { | |
230 LOG(ERROR) << "Exception while storing DICOM: " << e.What(); | |
231 } | |
232 } | |
233 } | |
234 } | |
235 } | |
236 } | |
237 } | |
238 } | |
239 | |
240 /* | |
241 * This function processes a DIMSE C-STORE-RQ commmand that was | |
242 * received over the network connection. | |
243 * | |
244 * Parameters: | |
245 * assoc - [in] The association (network connection to another DICOM application). | |
246 * msg - [in] The DIMSE C-STORE-RQ message that was received. | |
247 * presID - [in] The ID of the presentation context which was specified in the PDV which contained | |
248 * the DIMSE command. | |
249 */ | |
250 OFCondition Internals::storeScp(T_ASC_Association * assoc, | |
251 T_DIMSE_Message * msg, | |
252 T_ASC_PresentationContextID presID, | |
253 IStoreRequestHandler& handler, | |
254 const std::string& remoteIp, | |
255 int timeout) | |
256 { | |
257 OFCondition cond = EC_Normal; | |
258 T_DIMSE_C_StoreRQ *req; | |
259 | |
260 // assign the actual information of the C-STORE-RQ command to a local variable | |
261 req = &msg->msg.CStoreRQ; | |
262 | |
263 // intialize some variables | |
264 StoreCallbackData data; | |
265 data.handler = &handler; | |
266 data.remoteIp = &remoteIp; | |
267 data.modality = dcmSOPClassUIDToModality(req->AffectedSOPClassUID/*, "UNKNOWN"*/); | |
268 if (data.modality == NULL) | |
269 data.modality = "UNKNOWN"; | |
270 | |
271 data.affectedSOPInstanceUID = req->AffectedSOPInstanceUID; | |
272 data.messageID = req->MessageID; | |
273 if (assoc && assoc->params) | |
274 { | |
275 data.remoteAET = assoc->params->DULparams.callingAPTitle; | |
276 data.calledAET = assoc->params->DULparams.calledAPTitle; | |
277 } | |
278 else | |
279 { | |
280 data.remoteAET = ""; | |
281 data.calledAET = ""; | |
282 } | |
283 | |
284 DcmFileFormat dcmff; | |
285 | |
286 // store SourceApplicationEntityTitle in metaheader | |
287 if (assoc && assoc->params) | |
288 { | |
289 const char *aet = assoc->params->DULparams.callingAPTitle; | |
290 if (aet) dcmff.getMetaInfo()->putAndInsertString(DCM_SourceApplicationEntityTitle, aet); | |
291 } | |
292 | |
293 // define an address where the information which will be received over the network will be stored | |
294 DcmDataset *dset = dcmff.getDataset(); | |
295 | |
296 cond = DIMSE_storeProvider(assoc, presID, req, NULL, /*opt_useMetaheader*/OFFalse, &dset, | |
297 storeScpCallback, &data, | |
298 /*opt_blockMode*/ (timeout ? DIMSE_NONBLOCKING : DIMSE_BLOCKING), | |
299 /*opt_dimse_timeout*/ timeout); | |
300 | |
301 // if some error occured, dump corresponding information and remove the outfile if necessary | |
302 if (cond.bad()) | |
303 { | |
304 OFString temp_str; | |
305 LOG(ERROR) << "Store SCP Failed: " << cond.text(); | |
306 } | |
307 | |
308 // return return value | |
309 return cond; | |
310 } | |
311 } |