Mercurial > hg > orthanc
annotate OrthancServer/Internals/StoreScp.cpp @ 734:8c29c1056d45
automatic search of the dicom dictionaries in CMake
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 28 Feb 2014 16:55:09 +0100 |
parents | 309e686b41e7 |
children | 0a2f8c707c78 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 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" | |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
36 #include "../ServerToolbox.h" |
0 | 37 #include "../ToDcmtkBridge.h" |
62 | 38 #include "../../Core/OrthancException.h" |
0 | 39 |
40 #include <dcmtk/dcmdata/dcfilefo.h> | |
41 #include <dcmtk/dcmdata/dcmetinf.h> | |
42 #include <dcmtk/dcmdata/dcostrmb.h> | |
43 #include <dcmtk/dcmdata/dcdeftag.h> | |
44 #include <dcmtk/dcmnet/diutil.h> | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
45 #include <glog/logging.h> |
0 | 46 |
47 | |
62 | 48 namespace Orthanc |
0 | 49 { |
50 namespace | |
51 { | |
52 struct StoreCallbackData | |
53 { | |
54 IStoreRequestHandler* handler; | |
55 const char* distantAET; | |
56 const char* modality; | |
57 const char* affectedSOPInstanceUID; | |
58 uint32_t messageID; | |
59 }; | |
60 | |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
61 |
0 | 62 static void |
63 storeScpCallback( | |
64 void *callbackData, | |
65 T_DIMSE_StoreProgress *progress, | |
66 T_DIMSE_C_StoreRQ *req, | |
67 char * /*imageFileName*/, DcmDataset **imageDataSet, | |
68 T_DIMSE_C_StoreRSP *rsp, | |
69 DcmDataset **statusDetail) | |
70 /* | |
71 * This function.is used to indicate progress when storescp receives instance data over the | |
72 * network. On the final call to this function (identified by progress->state == DIMSE_StoreEnd) | |
73 * this function will store the data set which was received over the network to a file. | |
74 * Earlier calls to this function will simply cause some information to be dumped to stdout. | |
75 * | |
76 * Parameters: | |
77 * callbackData - [in] data for this callback function | |
78 * progress - [in] The state of progress. (identifies if this is the initial or final call | |
79 * to this function, or a call in between these two calls. | |
80 * req - [in] The original store request message. | |
81 * imageFileName - [in] The path to and name of the file the information shall be written to. | |
82 * imageDataSet - [in] The data set which shall be stored in the image file | |
83 * rsp - [inout] the C-STORE-RSP message (will be sent after the call to this function) | |
84 * statusDetail - [inout] This variable can be used to capture detailed information with regard to | |
85 * the status information which is captured in the status element (0000,0900). Note | |
86 * that this function does specify any such information, the pointer will be set to NULL. | |
87 */ | |
88 { | |
89 StoreCallbackData *cbdata = OFstatic_cast(StoreCallbackData *, callbackData); | |
90 | |
91 DIC_UI sopClass; | |
92 DIC_UI sopInstance; | |
93 | |
94 // if this is the final call of this function, save the data which was received to a file | |
95 // (note that we could also save the image somewhere else, put it in database, etc.) | |
96 if (progress->state == DIMSE_StoreEnd) | |
97 { | |
98 OFString tmpStr; | |
99 | |
100 // do not send status detail information | |
101 *statusDetail = NULL; | |
102 | |
103 // Concerning the following line: an appropriate status code is already set in the resp structure, | |
104 // it need not be success. For example, if the caller has already detected an out of resources problem | |
105 // then the status will reflect this. The callback function is still called to allow cleanup. | |
106 //rsp->DimseStatus = STATUS_Success; | |
107 | |
108 // we want to write the received information to a file only if this information | |
109 // is present and the options opt_bitPreserving and opt_ignore are not set. | |
110 if ((imageDataSet != NULL) && (*imageDataSet != NULL)) | |
111 { | |
112 DicomMap summary; | |
113 Json::Value dicomJson; | |
291
4d7469f72a0b
embedding of dicom dictionaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
194
diff
changeset
|
114 std::string buffer; |
0 | 115 |
116 try | |
117 { | |
118 FromDcmtkBridge::Convert(summary, **imageDataSet); | |
119 FromDcmtkBridge::ToJson(dicomJson, **imageDataSet); | |
120 | |
524 | 121 if (!FromDcmtkBridge::SaveToMemoryBuffer(buffer, *imageDataSet)) |
0 | 122 { |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
123 LOG(ERROR) << "cannot write DICOM file to memory"; |
0 | 124 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; |
125 } | |
126 } | |
127 catch (...) | |
128 { | |
129 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
130 } | |
131 | |
132 // check the image to make sure it is consistent, i.e. that its sopClass and sopInstance correspond | |
133 // to those mentioned in the request. If not, set the status in the response message variable. | |
134 if ((rsp->DimseStatus == STATUS_Success)) | |
135 { | |
136 // which SOP class and SOP instance ? | |
137 if (!DU_findSOPClassAndInstanceInDataSet(*imageDataSet, sopClass, sopInstance, /*opt_correctUIDPadding*/ OFFalse)) | |
138 { | |
101 | 139 //LOG4CPP_ERROR(Internals::GetLogger(), "bad DICOM file: " << fileName); |
0 | 140 rsp->DimseStatus = STATUS_STORE_Error_CannotUnderstand; |
141 } | |
142 else if (strcmp(sopClass, req->AffectedSOPClassUID) != 0) | |
143 { | |
144 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
145 } | |
146 else if (strcmp(sopInstance, req->AffectedSOPInstanceUID) != 0) | |
147 { | |
148 rsp->DimseStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass; | |
149 } | |
150 else | |
151 { | |
152 try | |
153 { | |
154 cbdata->handler->Handle(buffer, summary, dicomJson, cbdata->distantAET); | |
155 } | |
62 | 156 catch (OrthancException& e) |
0 | 157 { |
158 rsp->DimseStatus = STATUS_STORE_Refused_OutOfResources; | |
730
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
159 |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
160 if (e.GetErrorCode() == ErrorCode_InexistentTag) |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
161 { |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
162 LogMissingRequiredTag(summary); |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
163 } |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
164 else |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
165 { |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
166 LOG(ERROR) << "Exception while storing DICOM: " << e.What(); |
309e686b41e7
better logging about nonexistent tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
167 } |
0 | 168 } |
169 } | |
170 } | |
171 } | |
172 } | |
173 } | |
174 } | |
175 | |
176 /* | |
177 * This function processes a DIMSE C-STORE-RQ commmand that was | |
178 * received over the network connection. | |
179 * | |
180 * Parameters: | |
181 * assoc - [in] The association (network connection to another DICOM application). | |
182 * msg - [in] The DIMSE C-STORE-RQ message that was received. | |
183 * presID - [in] The ID of the presentation context which was specified in the PDV which contained | |
184 * the DIMSE command. | |
185 */ | |
186 OFCondition Internals::storeScp(T_ASC_Association * assoc, | |
187 T_DIMSE_Message * msg, | |
188 T_ASC_PresentationContextID presID, | |
189 IStoreRequestHandler& handler) | |
190 { | |
191 OFCondition cond = EC_Normal; | |
192 T_DIMSE_C_StoreRQ *req; | |
193 | |
194 // assign the actual information of the C-STORE-RQ command to a local variable | |
195 req = &msg->msg.CStoreRQ; | |
196 | |
197 // intialize some variables | |
198 StoreCallbackData callbackData; | |
199 callbackData.handler = &handler; | |
101 | 200 callbackData.modality = dcmSOPClassUIDToModality(req->AffectedSOPClassUID/*, "UNKNOWN"*/); |
201 if (callbackData.modality == NULL) | |
202 callbackData.modality = "UNKNOWN"; | |
203 | |
0 | 204 callbackData.affectedSOPInstanceUID = req->AffectedSOPInstanceUID; |
205 callbackData.messageID = req->MessageID; | |
206 if (assoc && assoc->params) | |
207 { | |
208 callbackData.distantAET = assoc->params->DULparams.callingAPTitle; | |
209 } | |
210 else | |
211 { | |
212 callbackData.distantAET = ""; | |
213 } | |
214 | |
215 DcmFileFormat dcmff; | |
216 | |
217 // store SourceApplicationEntityTitle in metaheader | |
218 if (assoc && assoc->params) | |
219 { | |
220 const char *aet = assoc->params->DULparams.callingAPTitle; | |
221 if (aet) dcmff.getMetaInfo()->putAndInsertString(DCM_SourceApplicationEntityTitle, aet); | |
222 } | |
223 | |
224 // define an address where the information which will be received over the network will be stored | |
225 DcmDataset *dset = dcmff.getDataset(); | |
226 | |
227 cond = DIMSE_storeProvider(assoc, presID, req, NULL, /*opt_useMetaheader*/OFFalse, &dset, | |
228 storeScpCallback, &callbackData, | |
229 /*opt_blockMode*/ DIMSE_BLOCKING, | |
230 /*opt_dimse_timeout*/ 0); | |
231 | |
232 // if some error occured, dump corresponding information and remove the outfile if necessary | |
233 if (cond.bad()) | |
234 { | |
235 OFString temp_str; | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
101
diff
changeset
|
236 LOG(ERROR) << "Store SCP Failed: " << cond.text(); |
0 | 237 } |
238 | |
239 // return return value | |
240 return cond; | |
241 } | |
242 } |