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