Mercurial > hg > orthanc
annotate OrthancServer/Internals/StoreScp.cpp @ 220:bb8c260c0092
fix for windows
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 29 Nov 2012 15:06:50 +0100 |
parents | 0186ac92810c |
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 "StoreScp.h" | |
34 | |
35 #include "../FromDcmtkBridge.h" | |
36 #include "../ToDcmtkBridge.h" | |
62 | 37 #include "../../Core/OrthancException.h" |
0 | 38 |
39 #include <dcmtk/dcmdata/dcfilefo.h> | |
40 #include <dcmtk/dcmdata/dcmetinf.h> | |
41 #include <dcmtk/dcmdata/dcostrmb.h> | |
42 #include <dcmtk/dcmdata/dcdeftag.h> | |
43 #include <dcmtk/dcmnet/diutil.h> | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
44 #include <glog/logging.h> |
0 | 45 |
46 | |
62 | 47 namespace Orthanc |
0 | 48 { |
49 namespace | |
50 { | |
51 struct StoreCallbackData | |
52 { | |
53 IStoreRequestHandler* handler; | |
54 const char* distantAET; | |
55 const char* modality; | |
56 const char* affectedSOPInstanceUID; | |
57 uint32_t messageID; | |
58 }; | |
59 | |
60 | |
61 static int SaveToMemoryBuffer(DcmDataset* dataSet, | |
62 std::vector<uint8_t>& buffer) | |
63 { | |
64 // Determine the transfer syntax which shall be used to write the | |
65 // information to the file. We always switch to the Little Endian | |
66 // syntax, with explicit length. | |
67 | |
68 // http://support.dcmtk.org/docs/dcxfer_8h-source.html | |
69 E_TransferSyntax xfer = EXS_LittleEndianExplicit; | |
70 E_EncodingType encodingType = /*opt_sequenceType*/ EET_ExplicitLength; | |
71 | |
72 uint32_t s = dataSet->getLength(xfer, encodingType); | |
73 | |
74 buffer.resize(s); | |
75 DcmOutputBufferStream ob(&buffer[0], s); | |
76 | |
77 dataSet->transferInit(); | |
101 | 78 |
103
f2ecbe7d50b8
detection of dcmtk version
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
102
diff
changeset
|
79 #if DCMTK_VERSION_NUMBER >= 360 |
0 | 80 OFCondition c = dataSet->write(ob, xfer, encodingType, NULL, |
81 /*opt_groupLength*/ EGL_recalcGL, | |
82 /*opt_paddingType*/ EPD_withoutPadding); | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
83 #else |
194
0186ac92810c
fixes for Ubuntu 10.04
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
84 OFCondition c = dataSet->write(ob, xfer, encodingType, NULL); |
101 | 85 #endif |
86 | |
0 | 87 dataSet->transferEnd(); |
88 if (c.good()) | |
89 { | |
90 return 0; | |
91 } | |
92 else | |
93 { | |
94 buffer.clear(); | |
95 return -1; | |
96 } | |
97 | |
98 #if 0 | |
99 OFCondition cond = cbdata->dcmff->saveFile(fileName.c_str(), xfer, | |
100 encodingType, | |
101 /*opt_groupLength*/ EGL_recalcGL, | |
102 /*opt_paddingType*/ EPD_withoutPadding, | |
103 OFstatic_cast(Uint32, /*opt_filepad*/ 0), | |
104 OFstatic_cast(Uint32, /*opt_itempad*/ 0), | |
105 (opt_useMetaheader) ? EWM_fileformat : EWM_dataset); | |
106 #endif | |
107 } | |
108 | |
109 | |
110 static void | |
111 storeScpCallback( | |
112 void *callbackData, | |
113 T_DIMSE_StoreProgress *progress, | |
114 T_DIMSE_C_StoreRQ *req, | |
115 char * /*imageFileName*/, DcmDataset **imageDataSet, | |
116 T_DIMSE_C_StoreRSP *rsp, | |
117 DcmDataset **statusDetail) | |
118 /* | |
119 * This function.is used to indicate progress when storescp receives instance data over the | |
120 * network. On the final call to this function (identified by progress->state == DIMSE_StoreEnd) | |
121 * this function will store the data set which was received over the network to a file. | |
122 * Earlier calls to this function will simply cause some information to be dumped to stdout. | |
123 * | |
124 * Parameters: | |
125 * callbackData - [in] data for this callback function | |
126 * progress - [in] The state of progress. (identifies if this is the initial or final call | |
127 * to this function, or a call in between these two calls. | |
128 * req - [in] The original store request message. | |
129 * imageFileName - [in] The path to and name of the file the information shall be written to. | |
130 * imageDataSet - [in] The data set which shall be stored in the image file | |
131 * rsp - [inout] the C-STORE-RSP message (will be sent after the call to this function) | |
132 * statusDetail - [inout] This variable can be used to capture detailed information with regard to | |
133 * the status information which is captured in the status element (0000,0900). Note | |
134 * that this function does specify any such information, the pointer will be set to NULL. | |
135 */ | |
136 { | |
137 StoreCallbackData *cbdata = OFstatic_cast(StoreCallbackData *, callbackData); | |
138 | |
139 DIC_UI sopClass; | |
140 DIC_UI sopInstance; | |
141 | |
142 // if this is the final call of this function, save the data which was received to a file | |
143 // (note that we could also save the image somewhere else, put it in database, etc.) | |
144 if (progress->state == DIMSE_StoreEnd) | |
145 { | |
146 OFString tmpStr; | |
147 | |
148 // do not send status detail information | |
149 *statusDetail = NULL; | |
150 | |
151 // Concerning the following line: an appropriate status code is already set in the resp structure, | |
152 // it need not be success. For example, if the caller has already detected an out of resources problem | |
153 // then the status will reflect this. The callback function is still called to allow cleanup. | |
154 //rsp->DimseStatus = STATUS_Success; | |
155 | |
156 // we want to write the received information to a file only if this information | |
157 // is present and the options opt_bitPreserving and opt_ignore are not set. | |
158 if ((imageDataSet != NULL) && (*imageDataSet != NULL)) | |
159 { | |
160 DicomMap summary; | |
161 Json::Value dicomJson; | |
162 std::vector<uint8_t> buffer; | |
163 | |
164 try | |
165 { | |
166 FromDcmtkBridge::Convert(summary, **imageDataSet); | |
167 FromDcmtkBridge::ToJson(dicomJson, **imageDataSet); | |
168 | |
169 if (SaveToMemoryBuffer(*imageDataSet, buffer) < 0) | |
170 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
171 LOG(ERROR) << "cannot write DICOM file to memory"; |
0 | 172 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; |
173 } | |
174 } | |
175 catch (...) | |
176 { | |
177 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
178 } | |
179 | |
180 // check the image to make sure it is consistent, i.e. that its sopClass and sopInstance correspond | |
181 // to those mentioned in the request. If not, set the status in the response message variable. | |
182 if ((rsp->DimseStatus == STATUS_Success)) | |
183 { | |
184 // which SOP class and SOP instance ? | |
185 if (!DU_findSOPClassAndInstanceInDataSet(*imageDataSet, sopClass, sopInstance, /*opt_correctUIDPadding*/ OFFalse)) | |
186 { | |
101 | 187 //LOG4CPP_ERROR(Internals::GetLogger(), "bad DICOM file: " << fileName); |
0 | 188 rsp->DimseStatus = STATUS_STORE_Error_CannotUnderstand; |
189 } | |
190 else if (strcmp(sopClass, req->AffectedSOPClassUID) != 0) | |
191 { | |
192 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
193 } | |
194 else if (strcmp(sopInstance, req->AffectedSOPInstanceUID) != 0) | |
195 { | |
196 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
197 } | |
198 else | |
199 { | |
200 try | |
201 { | |
202 cbdata->handler->Handle(buffer, summary, dicomJson, cbdata->distantAET); | |
203 } | |
62 | 204 catch (OrthancException& e) |
0 | 205 { |
206 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
207 LOG(ERROR) << "Exception while storing DICOM: " << e.What(); |
0 | 208 } |
209 } | |
210 } | |
211 } | |
212 } | |
213 } | |
214 } | |
215 | |
216 /* | |
217 * This function processes a DIMSE C-STORE-RQ commmand that was | |
218 * received over the network connection. | |
219 * | |
220 * Parameters: | |
221 * assoc - [in] The association (network connection to another DICOM application). | |
222 * msg - [in] The DIMSE C-STORE-RQ message that was received. | |
223 * presID - [in] The ID of the presentation context which was specified in the PDV which contained | |
224 * the DIMSE command. | |
225 */ | |
226 OFCondition Internals::storeScp(T_ASC_Association * assoc, | |
227 T_DIMSE_Message * msg, | |
228 T_ASC_PresentationContextID presID, | |
229 IStoreRequestHandler& handler) | |
230 { | |
231 OFCondition cond = EC_Normal; | |
232 T_DIMSE_C_StoreRQ *req; | |
233 | |
234 // assign the actual information of the C-STORE-RQ command to a local variable | |
235 req = &msg->msg.CStoreRQ; | |
236 | |
237 // intialize some variables | |
238 StoreCallbackData callbackData; | |
239 callbackData.handler = &handler; | |
101 | 240 callbackData.modality = dcmSOPClassUIDToModality(req->AffectedSOPClassUID/*, "UNKNOWN"*/); |
241 if (callbackData.modality == NULL) | |
242 callbackData.modality = "UNKNOWN"; | |
243 | |
0 | 244 callbackData.affectedSOPInstanceUID = req->AffectedSOPInstanceUID; |
245 callbackData.messageID = req->MessageID; | |
246 if (assoc && assoc->params) | |
247 { | |
248 callbackData.distantAET = assoc->params->DULparams.callingAPTitle; | |
249 } | |
250 else | |
251 { | |
252 callbackData.distantAET = ""; | |
253 } | |
254 | |
255 DcmFileFormat dcmff; | |
256 | |
257 // store SourceApplicationEntityTitle in metaheader | |
258 if (assoc && assoc->params) | |
259 { | |
260 const char *aet = assoc->params->DULparams.callingAPTitle; | |
261 if (aet) dcmff.getMetaInfo()->putAndInsertString(DCM_SourceApplicationEntityTitle, aet); | |
262 } | |
263 | |
264 // define an address where the information which will be received over the network will be stored | |
265 DcmDataset *dset = dcmff.getDataset(); | |
266 | |
267 cond = DIMSE_storeProvider(assoc, presID, req, NULL, /*opt_useMetaheader*/OFFalse, &dset, | |
268 storeScpCallback, &callbackData, | |
269 /*opt_blockMode*/ DIMSE_BLOCKING, | |
270 /*opt_dimse_timeout*/ 0); | |
271 | |
272 // if some error occured, dump corresponding information and remove the outfile if necessary | |
273 if (cond.bad()) | |
274 { | |
275 OFString temp_str; | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
276 LOG(ERROR) << "Store SCP Failed: " << cond.text(); |
0 | 277 } |
278 | |
279 // return return value | |
280 return cond; | |
281 } | |
282 } |