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 "DicomUserConnection.h"
|
|
34
|
62
|
35 #include "../../Core/OrthancException.h"
|
0
|
36 #include "../ToDcmtkBridge.h"
|
|
37 #include "../FromDcmtkBridge.h"
|
|
38
|
|
39 #include <dcmtk/dcmdata/dcistrmb.h>
|
|
40 #include <dcmtk/dcmdata/dcistrmf.h>
|
|
41 #include <dcmtk/dcmdata/dcfilefo.h>
|
|
42 #include <dcmtk/dcmnet/diutil.h>
|
|
43
|
|
44 #include <set>
|
|
45
|
|
46
|
|
47
|
|
48 #ifdef _WIN32
|
|
49 /**
|
|
50 * "The maximum length, in bytes, of the string returned in the buffer
|
|
51 * pointed to by the name parameter is dependent on the namespace provider,
|
|
52 * but this string must be 256 bytes or less.
|
|
53 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms738527(v=vs.85).aspx
|
|
54 **/
|
|
55 #define HOST_NAME_MAX 256
|
|
56 #endif
|
|
57
|
|
58
|
62
|
59 namespace Orthanc
|
0
|
60 {
|
|
61 struct DicomUserConnection::PImpl
|
|
62 {
|
|
63 // Connection state
|
|
64 T_ASC_Network* net_;
|
|
65 T_ASC_Parameters* params_;
|
|
66 T_ASC_Association* assoc_;
|
|
67
|
|
68 bool IsOpen() const
|
|
69 {
|
|
70 return assoc_ != NULL;
|
|
71 }
|
|
72
|
|
73 void CheckIsOpen() const;
|
|
74
|
|
75 void Store(DcmInputStream& is);
|
|
76 };
|
|
77
|
|
78
|
|
79 static void Check(const OFCondition& cond)
|
|
80 {
|
|
81 if (cond.bad())
|
|
82 {
|
62
|
83 throw OrthancException("DicomUserConnection: " + std::string(cond.text()));
|
0
|
84 }
|
|
85 }
|
|
86
|
|
87 void DicomUserConnection::PImpl::CheckIsOpen() const
|
|
88 {
|
|
89 if (!IsOpen())
|
|
90 {
|
62
|
91 throw OrthancException("DicomUserConnection: First open the connection");
|
0
|
92 }
|
|
93 }
|
|
94
|
|
95
|
|
96 void DicomUserConnection::CheckIsOpen() const
|
|
97 {
|
|
98 pimpl_->CheckIsOpen();
|
|
99 }
|
|
100
|
|
101
|
|
102 void DicomUserConnection::CopyParameters(const DicomUserConnection& other)
|
|
103 {
|
|
104 Close();
|
|
105 localAet_ = other.localAet_;
|
|
106 distantAet_ = other.distantAet_;
|
|
107 distantHost_ = other.distantHost_;
|
|
108 distantPort_ = other.distantPort_;
|
|
109 }
|
|
110
|
|
111
|
|
112 void DicomUserConnection::SetupPresentationContexts()
|
|
113 {
|
|
114 // The preferred abstract syntax
|
|
115 std::string preferredSyntax = UID_LittleEndianImplicitTransferSyntax;
|
|
116
|
|
117 // Fallback abstract syntaxes
|
|
118 std::set<std::string> abstractSyntaxes;
|
|
119 abstractSyntaxes.insert(UID_LittleEndianExplicitTransferSyntax);
|
|
120 abstractSyntaxes.insert(UID_BigEndianExplicitTransferSyntax);
|
|
121 abstractSyntaxes.insert(UID_LittleEndianImplicitTransferSyntax);
|
|
122 abstractSyntaxes.erase(preferredSyntax);
|
|
123 assert(abstractSyntaxes.size() == 2);
|
|
124
|
|
125 // Transfer syntaxes for C-ECHO, C-FIND and C-MOVE
|
|
126 std::vector<std::string> transferSyntaxes;
|
|
127 transferSyntaxes.push_back(UID_VerificationSOPClass);
|
|
128 transferSyntaxes.push_back(UID_FINDPatientRootQueryRetrieveInformationModel);
|
|
129 transferSyntaxes.push_back(UID_FINDStudyRootQueryRetrieveInformationModel);
|
|
130 transferSyntaxes.push_back(UID_MOVEStudyRootQueryRetrieveInformationModel);
|
|
131
|
|
132 // TODO: Allow the set below to be configured
|
|
133 std::set<std::string> uselessSyntaxes;
|
|
134 uselessSyntaxes.insert(UID_BlendingSoftcopyPresentationStateStorage);
|
|
135 uselessSyntaxes.insert(UID_GrayscaleSoftcopyPresentationStateStorage);
|
|
136 uselessSyntaxes.insert(UID_ColorSoftcopyPresentationStateStorage);
|
|
137 uselessSyntaxes.insert(UID_PseudoColorSoftcopyPresentationStateStorage);
|
|
138
|
|
139 // Add the transfer syntaxes for C-STORE
|
|
140 for (int i = 0; i < numberOfDcmShortSCUStorageSOPClassUIDs - 1; i++)
|
|
141 {
|
|
142 // Test to make some room to allow the ECHO and FIND requests
|
|
143 if (uselessSyntaxes.find(dcmShortSCUStorageSOPClassUIDs[i]) == uselessSyntaxes.end())
|
|
144 {
|
|
145 transferSyntaxes.push_back(dcmShortSCUStorageSOPClassUIDs[i]);
|
|
146 }
|
|
147 }
|
|
148
|
|
149 // Flatten the fallback abstract syntaxes array
|
|
150 const char* asPreferred[1] = { preferredSyntax.c_str() };
|
|
151 const char* asFallback[2];
|
|
152 std::set<std::string>::const_iterator it = abstractSyntaxes.begin();
|
|
153 asFallback[0] = it->c_str();
|
|
154 it++;
|
|
155 asFallback[1] = it->c_str();
|
|
156
|
|
157 unsigned int presentationContextId = 1;
|
|
158 for (size_t i = 0; i < transferSyntaxes.size(); i++)
|
|
159 {
|
|
160 Check(ASC_addPresentationContext(pimpl_->params_, presentationContextId,
|
|
161 transferSyntaxes[i].c_str(), asPreferred, 1));
|
|
162 presentationContextId += 2;
|
|
163
|
|
164 Check(ASC_addPresentationContext(pimpl_->params_, presentationContextId,
|
|
165 transferSyntaxes[i].c_str(), asFallback, 2));
|
|
166 presentationContextId += 2;
|
|
167 }
|
|
168 }
|
|
169
|
|
170
|
|
171 void DicomUserConnection::PImpl::Store(DcmInputStream& is)
|
|
172 {
|
|
173 CheckIsOpen();
|
|
174
|
|
175 DcmFileFormat dcmff;
|
|
176 Check(dcmff.read(is, EXS_Unknown, EGL_noChange, DCM_MaxReadLength));
|
|
177
|
|
178 // Figure out which SOP class and SOP instance is encapsulated in the file
|
|
179 DIC_UI sopClass;
|
|
180 DIC_UI sopInstance;
|
|
181 if (!DU_findSOPClassAndInstanceInDataSet(dcmff.getDataset(), sopClass, sopInstance))
|
|
182 {
|
62
|
183 throw OrthancException("DicomUserConnection: Unable to find the SOP class and instance");
|
0
|
184 }
|
|
185
|
|
186 // Figure out which of the accepted presentation contexts should be used
|
|
187 int presID = ASC_findAcceptedPresentationContextID(assoc_, sopClass);
|
|
188 if (presID == 0)
|
|
189 {
|
|
190 const char *modalityName = dcmSOPClassUIDToModality(sopClass);
|
|
191 if (!modalityName) modalityName = dcmFindNameOfUID(sopClass);
|
|
192 if (!modalityName) modalityName = "unknown SOP class";
|
62
|
193 throw OrthancException("DicomUserConnection: No presentation context for modality " +
|
0
|
194 std::string(modalityName));
|
|
195 }
|
|
196
|
|
197 // Prepare the transmission of data
|
|
198 T_DIMSE_C_StoreRQ req;
|
|
199 memset(&req, 0, sizeof(req));
|
|
200 req.MessageID = assoc_->nextMsgID++;
|
|
201 strcpy(req.AffectedSOPClassUID, sopClass);
|
|
202 strcpy(req.AffectedSOPInstanceUID, sopInstance);
|
|
203 req.DataSetType = DIMSE_DATASET_PRESENT;
|
|
204 req.Priority = DIMSE_PRIORITY_MEDIUM;
|
|
205
|
|
206 // Finally conduct transmission of data
|
|
207 T_DIMSE_C_StoreRSP rsp;
|
|
208 DcmDataset* statusDetail = NULL;
|
|
209 Check(DIMSE_storeUser(assoc_, presID, &req,
|
|
210 NULL, dcmff.getDataset(), /*progressCallback*/ NULL, NULL,
|
|
211 /*opt_blockMode*/ DIMSE_BLOCKING, /*opt_dimse_timeout*/ 0,
|
|
212 &rsp, &statusDetail, NULL));
|
|
213
|
|
214 if (statusDetail != NULL)
|
|
215 {
|
|
216 delete statusDetail;
|
|
217 }
|
|
218 }
|
|
219
|
|
220
|
|
221 static void FindCallback(
|
|
222 /* in */
|
|
223 void *callbackData,
|
|
224 T_DIMSE_C_FindRQ *request, /* original find request */
|
|
225 int responseCount,
|
|
226 T_DIMSE_C_FindRSP *response, /* pending response received */
|
|
227 DcmDataset *responseIdentifiers /* pending response identifiers */
|
|
228 )
|
|
229 {
|
|
230 DicomFindAnswers& answers = *(DicomFindAnswers*) callbackData;
|
|
231
|
|
232 if (responseIdentifiers != NULL)
|
|
233 {
|
|
234 DicomMap m;
|
|
235 FromDcmtkBridge::Convert(m, *responseIdentifiers);
|
|
236 answers.Add(m);
|
|
237 }
|
|
238 }
|
|
239
|
|
240 void DicomUserConnection::Find(DicomFindAnswers& result,
|
|
241 FindRootModel model,
|
|
242 const DicomMap& fields)
|
|
243 {
|
|
244 CheckIsOpen();
|
|
245
|
|
246 const char* sopClass;
|
|
247 std::auto_ptr<DcmDataset> dataset(ToDcmtkBridge::Convert(fields));
|
|
248 switch (model)
|
|
249 {
|
|
250 case FindRootModel_Patient:
|
|
251 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0052), "PATIENT");
|
|
252 sopClass = UID_FINDPatientRootQueryRetrieveInformationModel;
|
|
253
|
|
254 // Accession number
|
|
255 if (!fields.HasTag(0x0008, 0x0050))
|
|
256 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0050), "");
|
|
257
|
|
258 // Patient ID
|
|
259 if (!fields.HasTag(0x0010, 0x0020))
|
|
260 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0010, 0x0020), "");
|
|
261
|
|
262 break;
|
|
263
|
|
264 case FindRootModel_Study:
|
|
265 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0052), "STUDY");
|
|
266 sopClass = UID_FINDStudyRootQueryRetrieveInformationModel;
|
|
267
|
|
268 // Accession number
|
|
269 if (!fields.HasTag(0x0008, 0x0050))
|
|
270 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0050), "");
|
|
271
|
|
272 // Study instance UID
|
|
273 if (!fields.HasTag(0x0020, 0x000d))
|
|
274 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0020, 0x000d), "");
|
|
275
|
|
276 break;
|
|
277
|
|
278 case FindRootModel_Series:
|
|
279 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0052), "SERIES");
|
|
280 sopClass = UID_FINDStudyRootQueryRetrieveInformationModel;
|
|
281
|
|
282 // Accession number
|
|
283 if (!fields.HasTag(0x0008, 0x0050))
|
|
284 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0050), "");
|
|
285
|
|
286 // Study instance UID
|
|
287 if (!fields.HasTag(0x0020, 0x000d))
|
|
288 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0020, 0x000d), "");
|
|
289
|
|
290 // Series instance UID
|
|
291 if (!fields.HasTag(0x0020, 0x000e))
|
|
292 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0020, 0x000e), "");
|
|
293
|
|
294 break;
|
|
295
|
|
296 case FindRootModel_Instance:
|
|
297 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0052), "INSTANCE");
|
|
298 sopClass = UID_FINDStudyRootQueryRetrieveInformationModel;
|
|
299
|
|
300 // Accession number
|
|
301 if (!fields.HasTag(0x0008, 0x0050))
|
|
302 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0050), "");
|
|
303
|
|
304 // Study instance UID
|
|
305 if (!fields.HasTag(0x0020, 0x000d))
|
|
306 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0020, 0x000d), "");
|
|
307
|
|
308 // Series instance UID
|
|
309 if (!fields.HasTag(0x0020, 0x000e))
|
|
310 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0020, 0x000e), "");
|
|
311
|
|
312 // SOP Instance UID
|
|
313 if (!fields.HasTag(0x0008, 0x0018))
|
|
314 DU_putStringDOElement(dataset.get(), DcmTagKey(0x0008, 0x0018), "");
|
|
315
|
|
316 break;
|
|
317
|
|
318 default:
|
62
|
319 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
0
|
320 }
|
|
321
|
|
322 // Figure out which of the accepted presentation contexts should be used
|
|
323 int presID = ASC_findAcceptedPresentationContextID(pimpl_->assoc_, sopClass);
|
|
324 if (presID == 0)
|
|
325 {
|
62
|
326 throw OrthancException("DicomUserConnection: The C-FIND command is not supported by the distant AET");
|
0
|
327 }
|
|
328
|
|
329 T_DIMSE_C_FindRQ request;
|
|
330 memset(&request, 0, sizeof(request));
|
|
331 request.MessageID = pimpl_->assoc_->nextMsgID++;
|
|
332 strcpy(request.AffectedSOPClassUID, sopClass);
|
|
333 request.DataSetType = DIMSE_DATASET_PRESENT;
|
|
334 request.Priority = DIMSE_PRIORITY_MEDIUM;
|
|
335
|
|
336 T_DIMSE_C_FindRSP response;
|
|
337 DcmDataset* statusDetail = NULL;
|
|
338 OFCondition cond = DIMSE_findUser(pimpl_->assoc_, presID, &request, dataset.get(),
|
|
339 FindCallback, &result,
|
|
340 /*opt_blockMode*/ DIMSE_BLOCKING, /*opt_dimse_timeout*/ 0,
|
|
341 &response, &statusDetail);
|
|
342
|
|
343 if (statusDetail)
|
|
344 {
|
|
345 delete statusDetail;
|
|
346 }
|
|
347
|
|
348 Check(cond);
|
|
349 }
|
|
350
|
|
351
|
|
352 void DicomUserConnection::FindPatient(DicomFindAnswers& result,
|
|
353 const DicomMap& fields)
|
|
354 {
|
|
355 // Only keep the filters from "fields" that are related to the patient
|
|
356 DicomMap s;
|
|
357 fields.ExtractPatientInformation(s);
|
|
358 Find(result, FindRootModel_Patient, s);
|
|
359 }
|
|
360
|
|
361 void DicomUserConnection::FindStudy(DicomFindAnswers& result,
|
|
362 const DicomMap& fields)
|
|
363 {
|
|
364 // Only keep the filters from "fields" that are related to the study
|
|
365 DicomMap s;
|
|
366 fields.ExtractStudyInformation(s);
|
|
367
|
80
|
368 s.CopyTagIfExists(fields, DICOM_TAG_PATIENT_ID);
|
|
369 s.CopyTagIfExists(fields, DICOM_TAG_ACCESSION_NUMBER);
|
0
|
370
|
|
371 Find(result, FindRootModel_Study, s);
|
|
372 }
|
|
373
|
|
374 void DicomUserConnection::FindSeries(DicomFindAnswers& result,
|
|
375 const DicomMap& fields)
|
|
376 {
|
|
377 // Only keep the filters from "fields" that are related to the series
|
|
378 DicomMap s;
|
|
379 fields.ExtractSeriesInformation(s);
|
|
380
|
80
|
381 s.CopyTagIfExists(fields, DICOM_TAG_PATIENT_ID);
|
|
382 s.CopyTagIfExists(fields, DICOM_TAG_ACCESSION_NUMBER);
|
|
383 s.CopyTagIfExists(fields, DICOM_TAG_STUDY_INSTANCE_UID);
|
0
|
384
|
|
385 Find(result, FindRootModel_Series, s);
|
|
386 }
|
|
387
|
|
388 void DicomUserConnection::FindInstance(DicomFindAnswers& result,
|
|
389 const DicomMap& fields)
|
|
390 {
|
|
391 // Only keep the filters from "fields" that are related to the instance
|
|
392 DicomMap s;
|
|
393 fields.ExtractInstanceInformation(s);
|
|
394
|
80
|
395 s.CopyTagIfExists(fields, DICOM_TAG_PATIENT_ID);
|
|
396 s.CopyTagIfExists(fields, DICOM_TAG_ACCESSION_NUMBER);
|
|
397 s.CopyTagIfExists(fields, DICOM_TAG_STUDY_INSTANCE_UID);
|
|
398 s.CopyTagIfExists(fields, DICOM_TAG_SERIES_INSTANCE_UID);
|
0
|
399
|
|
400 Find(result, FindRootModel_Instance, s);
|
|
401 }
|
|
402
|
|
403
|
|
404 void DicomUserConnection::Move(const std::string& targetAet,
|
|
405 const DicomMap& fields)
|
|
406 {
|
|
407 CheckIsOpen();
|
|
408
|
|
409 const char* sopClass = UID_MOVEStudyRootQueryRetrieveInformationModel;
|
|
410 std::auto_ptr<DcmDataset> dataset(ToDcmtkBridge::Convert(fields));
|
|
411
|
|
412 // Figure out which of the accepted presentation contexts should be used
|
|
413 int presID = ASC_findAcceptedPresentationContextID(pimpl_->assoc_, sopClass);
|
|
414 if (presID == 0)
|
|
415 {
|
62
|
416 throw OrthancException("DicomUserConnection: The C-MOVE command is not supported by the distant AET");
|
0
|
417 }
|
|
418
|
|
419 T_DIMSE_C_MoveRQ request;
|
|
420 memset(&request, 0, sizeof(request));
|
|
421 request.MessageID = pimpl_->assoc_->nextMsgID++;
|
|
422 strcpy(request.AffectedSOPClassUID, sopClass);
|
|
423 request.DataSetType = DIMSE_DATASET_PRESENT;
|
|
424 request.Priority = DIMSE_PRIORITY_MEDIUM;
|
|
425 strncpy(request.MoveDestination, targetAet.c_str(), sizeof(DIC_AE) / sizeof(char));
|
|
426
|
|
427 T_DIMSE_C_MoveRSP response;
|
|
428 DcmDataset* statusDetail = NULL;
|
|
429 DcmDataset* responseIdentifiers = NULL;
|
|
430 OFCondition cond = DIMSE_moveUser(pimpl_->assoc_, presID, &request, dataset.get(),
|
|
431 NULL, NULL,
|
|
432 /*opt_blockMode*/ DIMSE_BLOCKING, /*opt_dimse_timeout*/ 0,
|
|
433 pimpl_->net_, NULL, NULL,
|
|
434 &response, &statusDetail, &responseIdentifiers);
|
|
435
|
|
436 if (statusDetail)
|
|
437 {
|
|
438 delete statusDetail;
|
|
439 }
|
|
440
|
|
441 if (responseIdentifiers)
|
|
442 {
|
|
443 delete responseIdentifiers;
|
|
444 }
|
|
445
|
|
446 Check(cond);
|
|
447 }
|
|
448
|
|
449
|
|
450 DicomUserConnection::DicomUserConnection() : pimpl_(new PImpl)
|
|
451 {
|
|
452 localAet_ = "STORESCU";
|
|
453 distantAet_ = "ANY-SCP";
|
|
454 distantPort_ = 104;
|
|
455 distantHost_ = "127.0.0.1";
|
|
456
|
|
457 pimpl_->net_ = NULL;
|
|
458 pimpl_->params_ = NULL;
|
|
459 pimpl_->assoc_ = NULL;
|
|
460 }
|
|
461
|
|
462 DicomUserConnection::~DicomUserConnection()
|
|
463 {
|
|
464 Close();
|
|
465 }
|
|
466
|
|
467 void DicomUserConnection::SetLocalApplicationEntityTitle(const std::string& aet)
|
|
468 {
|
|
469 Close();
|
|
470 localAet_ = aet;
|
|
471 }
|
|
472
|
|
473 void DicomUserConnection::SetDistantApplicationEntityTitle(const std::string& aet)
|
|
474 {
|
|
475 Close();
|
|
476 distantAet_ = aet;
|
|
477 }
|
|
478
|
|
479
|
|
480 void DicomUserConnection::SetDistantHost(const std::string& host)
|
|
481 {
|
|
482 if (host.size() > HOST_NAME_MAX - 10)
|
|
483 {
|
62
|
484 throw OrthancException("Distant host name is too long");
|
0
|
485 }
|
|
486
|
|
487 Close();
|
|
488 distantHost_ = host;
|
|
489 }
|
|
490
|
|
491 void DicomUserConnection::SetDistantPort(uint16_t port)
|
|
492 {
|
|
493 Close();
|
|
494 distantPort_ = port;
|
|
495 }
|
|
496
|
|
497 void DicomUserConnection::Open()
|
|
498 {
|
|
499 Close();
|
|
500
|
|
501 Check(ASC_initializeNetwork(NET_REQUESTOR, 0, /*opt_acse_timeout*/ 30, &pimpl_->net_));
|
|
502 Check(ASC_createAssociationParameters(&pimpl_->params_, /*opt_maxReceivePDULength*/ ASC_DEFAULTMAXPDU));
|
|
503
|
|
504 // Set this application's title and the called application's title in the params
|
|
505 Check(ASC_setAPTitles(pimpl_->params_, localAet_.c_str(), distantAet_.c_str(), NULL));
|
|
506
|
|
507 // Set the network addresses of the local and distant entities
|
|
508 char localHost[HOST_NAME_MAX];
|
|
509 gethostname(localHost, HOST_NAME_MAX - 1);
|
|
510
|
|
511 char distantHostAndPort[HOST_NAME_MAX];
|
2
|
512
|
|
513 #ifdef _MSC_VER
|
|
514 _snprintf
|
|
515 #else
|
|
516 snprintf
|
|
517 #endif
|
|
518 (distantHostAndPort, HOST_NAME_MAX - 1, "%s:%d", distantHost_.c_str(), distantPort_);
|
0
|
519
|
|
520 Check(ASC_setPresentationAddresses(pimpl_->params_, localHost, distantHostAndPort));
|
|
521
|
|
522 // Set various options
|
|
523 Check(ASC_setTransportLayerType(pimpl_->params_, /*opt_secureConnection*/ false));
|
|
524
|
|
525 SetupPresentationContexts();
|
|
526
|
|
527 // Do the association
|
|
528 Check(ASC_requestAssociation(pimpl_->net_, pimpl_->params_, &pimpl_->assoc_));
|
|
529
|
|
530 if (ASC_countAcceptedPresentationContexts(pimpl_->params_) == 0)
|
|
531 {
|
62
|
532 throw OrthancException("DicomUserConnection: No Acceptable Presentation Contexts");
|
0
|
533 }
|
|
534 }
|
|
535
|
|
536 void DicomUserConnection::Close()
|
|
537 {
|
|
538 if (pimpl_->assoc_ != NULL)
|
|
539 {
|
|
540 ASC_releaseAssociation(pimpl_->assoc_);
|
|
541 ASC_destroyAssociation(&pimpl_->assoc_);
|
|
542 pimpl_->assoc_ = NULL;
|
|
543 pimpl_->params_ = NULL;
|
|
544 }
|
|
545 else
|
|
546 {
|
|
547 if (pimpl_->params_ != NULL)
|
|
548 {
|
|
549 ASC_destroyAssociationParameters(&pimpl_->params_);
|
|
550 pimpl_->params_ = NULL;
|
|
551 }
|
|
552 }
|
|
553
|
|
554 if (pimpl_->net_ != NULL)
|
|
555 {
|
|
556 ASC_dropNetwork(&pimpl_->net_);
|
|
557 pimpl_->net_ = NULL;
|
|
558 }
|
|
559 }
|
|
560
|
|
561 bool DicomUserConnection::IsOpen() const
|
|
562 {
|
|
563 return pimpl_->IsOpen();
|
|
564 }
|
|
565
|
|
566 void DicomUserConnection::Store(const char* buffer, size_t size)
|
|
567 {
|
|
568 // Prepare an input stream for the memory buffer
|
|
569 DcmInputBufferStream is;
|
|
570 if (size > 0)
|
|
571 is.setBuffer(buffer, size);
|
|
572 is.setEos();
|
|
573
|
|
574 pimpl_->Store(is);
|
|
575 }
|
|
576
|
|
577 void DicomUserConnection::Store(const std::string& buffer)
|
|
578 {
|
|
579 if (buffer.size() > 0)
|
|
580 Store(reinterpret_cast<const char*>(&buffer[0]), buffer.size());
|
|
581 else
|
|
582 Store(NULL, 0);
|
|
583 }
|
|
584
|
|
585 void DicomUserConnection::StoreFile(const std::string& path)
|
|
586 {
|
|
587 // Prepare an input stream for the file
|
|
588 DcmInputFileStream is(path.c_str());
|
|
589 pimpl_->Store(is);
|
|
590 }
|
|
591
|
|
592 bool DicomUserConnection::Echo()
|
|
593 {
|
|
594 CheckIsOpen();
|
|
595 DIC_US status;
|
|
596 Check(DIMSE_echoUser(pimpl_->assoc_, pimpl_->assoc_->nextMsgID++,
|
|
597 /*opt_blockMode*/ DIMSE_BLOCKING, /*opt_dimse_timeout*/ 0,
|
|
598 &status, NULL));
|
|
599 return status == STATUS_Success;
|
|
600 }
|
|
601
|
|
602
|
|
603 void DicomUserConnection::MoveSeries(const std::string& targetAet,
|
|
604 const DicomMap& findResult)
|
|
605 {
|
|
606 DicomMap simplified;
|
80
|
607 simplified.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, findResult.GetValue(DICOM_TAG_STUDY_INSTANCE_UID));
|
|
608 simplified.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, findResult.GetValue(DICOM_TAG_SERIES_INSTANCE_UID));
|
0
|
609 Move(targetAet, simplified);
|
|
610 }
|
|
611
|
|
612 void DicomUserConnection::MoveSeries(const std::string& targetAet,
|
|
613 const std::string& studyUid,
|
|
614 const std::string& seriesUid)
|
|
615 {
|
|
616 DicomMap map;
|
80
|
617 map.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, studyUid);
|
|
618 map.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, seriesUid);
|
0
|
619 Move(targetAet, map);
|
|
620 }
|
|
621
|
|
622 void DicomUserConnection::MoveInstance(const std::string& targetAet,
|
|
623 const DicomMap& findResult)
|
|
624 {
|
|
625 DicomMap simplified;
|
80
|
626 simplified.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, findResult.GetValue(DICOM_TAG_STUDY_INSTANCE_UID));
|
|
627 simplified.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, findResult.GetValue(DICOM_TAG_SERIES_INSTANCE_UID));
|
|
628 simplified.SetValue(DICOM_TAG_SOP_INSTANCE_UID, findResult.GetValue(DICOM_TAG_SOP_INSTANCE_UID));
|
0
|
629 Move(targetAet, simplified);
|
|
630 }
|
|
631
|
|
632 void DicomUserConnection::MoveInstance(const std::string& targetAet,
|
|
633 const std::string& studyUid,
|
|
634 const std::string& seriesUid,
|
|
635 const std::string& instanceUid)
|
|
636 {
|
|
637 DicomMap map;
|
80
|
638 map.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, studyUid);
|
|
639 map.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, seriesUid);
|
|
640 map.SetValue(DICOM_TAG_SOP_INSTANCE_UID, instanceUid);
|
0
|
641 Move(targetAet, map);
|
|
642 }
|
|
643
|
|
644 void DicomUserConnection::SetConnectionTimeout(uint32_t seconds)
|
|
645 {
|
|
646 dcmConnectionTimeout.set(seconds);
|
|
647 }
|
|
648
|
|
649 }
|