ctkDcmSCU.cc 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  1. /*
  2. *
  3. * Copyright (C) 2008-2012, OFFIS e.V.
  4. * All rights reserved. See COPYRIGHT file for details.
  5. *
  6. * This software and supporting documentation were developed by
  7. *
  8. * OFFIS e.V.
  9. * R&D Division Health
  10. * Escherweg 2
  11. * D-26121 Oldenburg, Germany
  12. *
  13. *
  14. * Module: dcmnet
  15. *
  16. * Author: Michael Onken
  17. *
  18. * Purpose: Base class for Service Class Users (SCUs)
  19. *
  20. */
  21. // just a hack for debian, has to be sorted out
  22. // see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=637687
  23. //
  24. #define HAVE_CONFIG_H
  25. #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
  26. #include "ctkDcmSCU.h"
  27. #include "dcmtk/dcmnet/diutil.h" /* for dcmnet logger */
  28. #include "dcmtk/dcmdata/dcuid.h" /* for dcmFindUIDName() */
  29. #include "dcmtk/dcmdata/dcostrmf.h" /* for class DcmOutputFileStream */
  30. #ifdef WITH_ZLIB
  31. #include <zlib.h> /* for zlibVersion() */
  32. #endif
  33. DcmSCU::DcmSCU() :
  34. m_assoc(NULL),
  35. m_net(NULL),
  36. m_params(NULL),
  37. m_assocConfigFilename(),
  38. m_assocConfigProfile(),
  39. m_presContexts(),
  40. m_assocConfigFile(),
  41. m_openDIMSERequest(NULL),
  42. m_maxReceivePDULength(ASC_DEFAULTMAXPDU),
  43. m_blockMode(DIMSE_BLOCKING),
  44. m_ourAETitle("ANY-SCU"),
  45. m_peer(),
  46. m_peerAETitle("ANY-SCP"),
  47. m_peerPort(104),
  48. m_dimseTimeout(0),
  49. m_acseTimeout(30),
  50. m_storageDir(),
  51. m_storageMode(DCMSCU_STORAGE_DISK),
  52. m_verbosePCMode(OFFalse),
  53. m_datasetConversionMode(OFFalse),
  54. m_progressNotificationMode(OFTrue)
  55. {
  56. #ifdef HAVE_GUSI_H
  57. GUSISetup(GUSIwithSIOUXSockets);
  58. GUSISetup(GUSIwithInternetSockets);
  59. #endif
  60. #ifdef HAVE_WINSOCK_H
  61. WSAData winSockData;
  62. /* we need at least version 1.1 */
  63. WORD winSockVersionNeeded = MAKEWORD( 1, 1 );
  64. WSAStartup(winSockVersionNeeded, &winSockData); // TODO: check with multiple SCU instances whether this is harmful
  65. #endif
  66. }
  67. void DcmSCU::freeNetwork()
  68. {
  69. if ((m_assoc != NULL) || (m_net != NULL) || (m_params != NULL))
  70. DCMNET_DEBUG("Cleaning up internal association and network structures");
  71. /* destroy association parameters, i.e. free memory of T_ASC_Parameters.
  72. Usually this is done in ASC_destroyAssociation; however, if we already
  73. have association parameters but not yet an association (e.g. after calling
  74. initNetwork() and negotiateAssociation()), the latter approach may fail.
  75. */
  76. if (m_params)
  77. {
  78. ASC_destroyAssociationParameters(&m_params);
  79. m_params = NULL;
  80. // make sure destroyAssocation does not try to free params a second time
  81. // (happens in case we have already have an association structure)
  82. if (m_assoc)
  83. m_assoc->params = NULL;
  84. }
  85. // destroy the association, i.e. free memory of T_ASC_Association* structure.
  86. ASC_destroyAssociation(&m_assoc);
  87. // drop the network, i.e. free memory of T_ASC_Network* structure.
  88. ASC_dropNetwork(&m_net);
  89. // Cleanup old DIMSE request if any
  90. delete m_openDIMSERequest;
  91. m_openDIMSERequest = NULL;
  92. }
  93. DcmSCU::~DcmSCU()
  94. {
  95. // abort association (if any) and destroy dcmnet data structures
  96. if (isConnected())
  97. {
  98. closeAssociation(DCMSCU_ABORT_ASSOCIATION); // also frees network
  99. } else {
  100. freeNetwork();
  101. }
  102. #ifdef HAVE_WINSOCK_H
  103. WSACleanup(); // TODO: check with multiple SCU instances whether this is harmful
  104. #endif
  105. }
  106. OFCondition DcmSCU::initNetwork()
  107. {
  108. /* Return if SCU is already connected */
  109. if (isConnected())
  110. return NET_EC_AlreadyConnected;
  111. /* Be sure internal network structures are clean (delete old) */
  112. freeNetwork();
  113. OFString tempStr;
  114. /* initialize network, i.e. create an instance of T_ASC_Network*. */
  115. OFCondition cond = ASC_initializeNetwork(NET_REQUESTOR, 0, m_acseTimeout, &m_net);
  116. if (cond.bad())
  117. {
  118. DimseCondition::dump(tempStr, cond);
  119. DCMNET_ERROR(tempStr);
  120. return cond;
  121. }
  122. /* initialize asscociation parameters, i.e. create an instance of T_ASC_Parameters*. */
  123. cond = ASC_createAssociationParameters(&m_params, m_maxReceivePDULength);
  124. if (cond.bad())
  125. {
  126. DCMNET_ERROR(DimseCondition::dump(tempStr, cond));
  127. return cond;
  128. }
  129. /* sets this application's title and the called application's title in the params */
  130. /* structure. The default values are "ANY-SCU" and "ANY-SCP". */
  131. ASC_setAPTitles(m_params, m_ourAETitle.c_str(), m_peerAETitle.c_str(), NULL);
  132. /* Figure out the presentation addresses and copy the */
  133. /* corresponding values into the association parameters.*/
  134. DIC_NODENAME localHost;
  135. DIC_NODENAME peerHost;
  136. gethostname(localHost, sizeof(localHost) - 1);
  137. /* Since the underlying dcmnet structures reserve only 64 bytes for peer
  138. as well as local host name, we check here for buffer overflow.
  139. */
  140. if ((m_peer.length() + 5 /* max 65535 */) + 1 /* for ":" */ > 63)
  141. {
  142. DCMNET_ERROR("Maximum length of peer host name '" << m_peer << "' is longer than maximum of 57 characters");
  143. return EC_IllegalCall; // TODO: need to find better error code
  144. }
  145. if (strlen(localHost) + 1 > 63)
  146. {
  147. DCMNET_ERROR("Maximum length of local host name '" << localHost << "' is longer than maximum of 62 characters");
  148. return EC_IllegalCall; // TODO: need to find better error code
  149. }
  150. sprintf(peerHost, "%s:%d", m_peer.c_str(), OFstatic_cast(int, m_peerPort));
  151. ASC_setPresentationAddresses(m_params, localHost, peerHost);
  152. /* Add presentation contexts */
  153. // First, import from config file, if specified
  154. OFCondition result;
  155. if (!m_assocConfigFilename.empty())
  156. {
  157. DcmAssociationConfiguration assocConfig;
  158. result = DcmAssociationConfigurationFile::initialize(assocConfig, m_assocConfigFilename.c_str());
  159. if (result.bad())
  160. {
  161. DCMNET_WARN("Unable to parse association configuration file " << m_assocConfigFilename
  162. << " (ignored): " << result.text());
  163. return result;
  164. }
  165. else
  166. {
  167. /* perform name mangling for config file key */
  168. OFString profileName;
  169. const unsigned char *c = OFreinterpret_cast(const unsigned char *, m_assocConfigProfile.c_str());
  170. while (*c)
  171. {
  172. if (! isspace(*c)) profileName += OFstatic_cast(char, toupper(*c));
  173. ++c;
  174. }
  175. result = assocConfig.setAssociationParameters(profileName.c_str(), *m_params);
  176. if (result.bad())
  177. {
  178. DCMNET_WARN("Unable to apply association configuration file " << m_assocConfigFilename
  179. <<" (ignored): " << result.text());
  180. return result;
  181. }
  182. }
  183. }
  184. // Adapt presentation context ID to existing presentation contexts.
  185. // It's important that presentation context IDs are numerated 1,3,5,7...!
  186. Uint32 nextFreePresID = 257;
  187. Uint32 numContexts = ASC_countPresentationContexts(m_params);
  188. if (numContexts <= 127)
  189. {
  190. // Need Uint16 to avoid overflow in currPresID (unsigned char)
  191. nextFreePresID = 2 * numContexts + 1; /* add 1 to point to the next free ID*/
  192. }
  193. // Print warning if number of overall presentation contexts exceeds 128
  194. if ((numContexts + m_presContexts.size()) > 128)
  195. {
  196. DCMNET_WARN("Number of presentation contexts exceeds 128 (" << numContexts + m_presContexts.size()
  197. << "). Some contexts will not be negotiated");
  198. }
  199. else
  200. {
  201. DCMNET_TRACE("Configured " << numContexts << " presentation contexts from config file");
  202. if (m_presContexts.size() > 0)
  203. DCMNET_TRACE("Adding another " << m_presContexts.size() << " presentation contexts configured for SCU");
  204. }
  205. // Add presentation contexts not originating from config file
  206. OFListIterator(DcmSCUPresContext) contIt = m_presContexts.begin();
  207. OFListConstIterator(DcmSCUPresContext) endOfContList = m_presContexts.end();
  208. while ((contIt != endOfContList) && (nextFreePresID <= 255))
  209. {
  210. const Uint16 numTransferSyntaxes = OFstatic_cast(Uint16, (*contIt).transferSyntaxes.size());
  211. const char** transferSyntaxes = new const char*[numTransferSyntaxes];
  212. // Iterate over transfer syntaxes within one presentation context
  213. OFListIterator(OFString) syntaxIt = (*contIt).transferSyntaxes.begin();
  214. OFListIterator(OFString) endOfSyntaxList = (*contIt).transferSyntaxes.end();
  215. Uint16 sNum = 0;
  216. // copy all transfer syntaxes to array
  217. while (syntaxIt != endOfSyntaxList)
  218. {
  219. transferSyntaxes[sNum] = (*syntaxIt).c_str();
  220. ++syntaxIt;
  221. ++sNum;
  222. }
  223. // add the presentation context
  224. cond = ASC_addPresentationContext(m_params, OFstatic_cast(Uint8, nextFreePresID),
  225. (*contIt).abstractSyntaxName.c_str(), transferSyntaxes, numTransferSyntaxes,(*contIt).roleSelect);
  226. // if adding was successfull, prepare presentation context ID for next addition
  227. delete[] transferSyntaxes;
  228. transferSyntaxes = NULL;
  229. if (cond.bad())
  230. return cond;
  231. contIt++;
  232. // goto next free number, only odd presentation context IDs permitted
  233. nextFreePresID += 2;
  234. }
  235. numContexts = ASC_countPresentationContexts(m_params);
  236. if (numContexts == 0)
  237. {
  238. DCMNET_ERROR("Cannot initialize network: No presentation contexts defined");
  239. return NET_EC_NoPresentationContextsDefined;
  240. }
  241. DCMNET_DEBUG("Configured a total of " << numContexts << " presentation contexts for SCU");
  242. return cond;
  243. }
  244. OFCondition DcmSCU::negotiateAssociation()
  245. {
  246. /* Return error if SCU is already connected */
  247. if (isConnected())
  248. return NET_EC_AlreadyConnected;
  249. /* dump presentation contexts if required */
  250. OFString tempStr;
  251. if (m_verbosePCMode)
  252. DCMNET_INFO("Request Parameters:" << OFendl << ASC_dumpParameters(tempStr, m_params, ASC_ASSOC_RQ));
  253. else
  254. DCMNET_DEBUG("Request Parameters:" << OFendl << ASC_dumpParameters(tempStr, m_params, ASC_ASSOC_RQ));
  255. /* create association, i.e. try to establish a network connection to another */
  256. /* DICOM application. This call creates an instance of T_ASC_Association*. */
  257. DCMNET_INFO("Requesting Association");
  258. OFCondition cond = ASC_requestAssociation(m_net, m_params, &m_assoc);
  259. if (cond.bad())
  260. {
  261. if (cond == DUL_ASSOCIATIONREJECTED)
  262. {
  263. T_ASC_RejectParameters rej;
  264. ASC_getRejectParameters(m_params, &rej);
  265. DCMNET_DEBUG("Association Rejected:" << OFendl << ASC_printRejectParameters(tempStr, &rej));
  266. return cond;
  267. }
  268. else
  269. {
  270. DCMNET_DEBUG("Association Request Failed: " << DimseCondition::dump(tempStr, cond));
  271. return cond;
  272. }
  273. }
  274. /* dump the presentation contexts which have been accepted/refused */
  275. if (m_verbosePCMode)
  276. DCMNET_INFO("Association Parameters Negotiated:" << OFendl << ASC_dumpParameters(tempStr, m_params, ASC_ASSOC_AC));
  277. else
  278. DCMNET_DEBUG("Association Parameters Negotiated:" << OFendl << ASC_dumpParameters(tempStr, m_params, ASC_ASSOC_AC));
  279. /* count the presentation contexts which have been accepted by the SCP */
  280. /* If there are none, finish the execution */
  281. if (ASC_countAcceptedPresentationContexts(m_params) == 0)
  282. {
  283. DCMNET_ERROR("No Acceptable Presentation Contexts");
  284. return NET_EC_NoAcceptablePresentationContexts;
  285. }
  286. /* dump general information concerning the establishment of the network connection if required */
  287. DCMNET_INFO("Association Accepted (Max Send PDV: " << OFstatic_cast(unsigned long, m_assoc->sendPDVLength) << ")");
  288. return EC_Normal;
  289. }
  290. OFCondition DcmSCU::addPresentationContext(const OFString &abstractSyntax,
  291. const OFList<OFString> &xferSyntaxes,
  292. const T_ASC_SC_ROLE role)
  293. {
  294. DcmSCUPresContext presContext;
  295. presContext.abstractSyntaxName = abstractSyntax;
  296. OFListConstIterator(OFString) it = xferSyntaxes.begin();
  297. OFListConstIterator(OFString) endOfList = xferSyntaxes.end();
  298. while (it != endOfList)
  299. {
  300. presContext.transferSyntaxes.push_back(*it);
  301. it++;
  302. }
  303. presContext.roleSelect = role;
  304. m_presContexts.push_back(presContext);
  305. return EC_Normal;
  306. }
  307. OFCondition DcmSCU::useSecureConnection(DcmTransportLayer *tlayer)
  308. {
  309. OFCondition cond = ASC_setTransportLayer(m_net, tlayer, OFFalse /* do not take over ownership */);
  310. if (cond.good())
  311. cond = ASC_setTransportLayerType(m_params, OFTrue /* use TLS */);
  312. return cond;
  313. }
  314. void DcmSCU::clearPresentationContexts()
  315. {
  316. m_presContexts.clear();
  317. m_assocConfigFilename.clear();
  318. m_assocConfigProfile.clear();
  319. }
  320. // Returns usable presentation context ID for a given abstract syntax UID and
  321. // transfer syntax UID. 0 if none matches.
  322. T_ASC_PresentationContextID DcmSCU::findPresentationContextID(const OFString &abstractSyntax,
  323. const OFString &transferSyntax)
  324. {
  325. if (!isConnected())
  326. return 0;
  327. DUL_PRESENTATIONCONTEXT *pc;
  328. LST_HEAD **l;
  329. OFBool found = OFFalse;
  330. if (abstractSyntax.empty()) return 0;
  331. /* first of all we look for a presentation context
  332. * matching both abstract and transfer syntax
  333. */
  334. l = &m_assoc->params->DULparams.acceptedPresentationContext;
  335. pc = (DUL_PRESENTATIONCONTEXT*) LST_Head(l);
  336. (void)LST_Position(l, (LST_NODE*)pc);
  337. while (pc && !found)
  338. {
  339. found = (strcmp(pc->abstractSyntax, abstractSyntax.c_str()) == 0);
  340. found &= (pc->result == ASC_P_ACCEPTANCE);
  341. if (!transferSyntax.empty()) // ignore transfer syntax if not specified
  342. found &= (strcmp(pc->acceptedTransferSyntax, transferSyntax.c_str()) == 0);
  343. if (!found) pc = (DUL_PRESENTATIONCONTEXT*) LST_Next(l);
  344. }
  345. if (found)
  346. return pc->presentationContextID;
  347. return 0; /* not found */
  348. }
  349. // Returns the presentation context ID that best matches the given abstract syntax UID and
  350. // transfer syntax UID.
  351. T_ASC_PresentationContextID DcmSCU::findAnyPresentationContextID(const OFString &abstractSyntax,
  352. const OFString &transferSyntax)
  353. {
  354. if (m_assoc == NULL)
  355. return 0;
  356. DUL_PRESENTATIONCONTEXT *pc;
  357. LST_HEAD **l;
  358. OFBool found = OFFalse;
  359. if (abstractSyntax.empty()) return 0;
  360. /* first of all we look for a presentation context
  361. * matching both abstract and transfer syntax
  362. */
  363. l = &m_assoc->params->DULparams.acceptedPresentationContext;
  364. pc = (DUL_PRESENTATIONCONTEXT*) LST_Head(l);
  365. (void)LST_Position(l, (LST_NODE*)pc);
  366. while (pc && !found)
  367. {
  368. found = (strcmp(pc->abstractSyntax, abstractSyntax.c_str()) == 0);
  369. found &= (pc->result == ASC_P_ACCEPTANCE);
  370. if (!transferSyntax.empty()) // ignore transfer syntax if not specified
  371. found &= (strcmp(pc->acceptedTransferSyntax, transferSyntax.c_str()) == 0);
  372. if (!found) pc = (DUL_PRESENTATIONCONTEXT*) LST_Next(l);
  373. }
  374. if (found) return pc->presentationContextID;
  375. /* now we look for an explicit VR uncompressed PC. */
  376. l = &m_assoc->params->DULparams.acceptedPresentationContext;
  377. pc = (DUL_PRESENTATIONCONTEXT*) LST_Head(l);
  378. (void)LST_Position(l, (LST_NODE*)pc);
  379. while (pc && !found)
  380. {
  381. found = (strcmp(pc->abstractSyntax, abstractSyntax.c_str()) == 0)
  382. && (pc->result == ASC_P_ACCEPTANCE)
  383. && ((strcmp(pc->acceptedTransferSyntax, UID_LittleEndianExplicitTransferSyntax) == 0)
  384. || (strcmp(pc->acceptedTransferSyntax, UID_BigEndianExplicitTransferSyntax) == 0));
  385. if (!found) pc = (DUL_PRESENTATIONCONTEXT*) LST_Next(l);
  386. }
  387. if (found) return pc->presentationContextID;
  388. /* now we look for an implicit VR uncompressed PC. */
  389. l = &m_assoc->params->DULparams.acceptedPresentationContext;
  390. pc = (DUL_PRESENTATIONCONTEXT*) LST_Head(l);
  391. (void)LST_Position(l, (LST_NODE*)pc);
  392. while (pc && !found)
  393. {
  394. found = (strcmp(pc->abstractSyntax, abstractSyntax.c_str()) == 0)
  395. && (pc->result == ASC_P_ACCEPTANCE)
  396. && (strcmp(pc->acceptedTransferSyntax, UID_LittleEndianImplicitTransferSyntax) == 0);
  397. if (!found) pc = (DUL_PRESENTATIONCONTEXT*) LST_Next(l);
  398. }
  399. if (found) return pc->presentationContextID;
  400. /* finally we accept everything we get.
  401. returns 0 if abstract syntax is not supported
  402. */
  403. return ASC_findAcceptedPresentationContextID(m_assoc, abstractSyntax.c_str());
  404. }
  405. void DcmSCU::findPresentationContext(const T_ASC_PresentationContextID presID,
  406. OFString &abstractSyntax,
  407. OFString &transferSyntax)
  408. {
  409. transferSyntax.clear();
  410. abstractSyntax.clear();
  411. if (m_assoc == NULL)
  412. return;
  413. DUL_PRESENTATIONCONTEXT *pc;
  414. LST_HEAD **l;
  415. /* we look for a presentation context matching
  416. * both abstract and transfer syntax
  417. */
  418. l = &m_assoc->params->DULparams.acceptedPresentationContext;
  419. pc = (DUL_PRESENTATIONCONTEXT*) LST_Head(l);
  420. (void)LST_Position(l, (LST_NODE*)pc);
  421. while (pc)
  422. {
  423. if (presID == pc->presentationContextID)
  424. {
  425. if (pc->result == ASC_P_ACCEPTANCE)
  426. {
  427. // found a match
  428. transferSyntax = pc->acceptedTransferSyntax;
  429. abstractSyntax = pc->abstractSyntax;
  430. }
  431. break;
  432. }
  433. pc = (DUL_PRESENTATIONCONTEXT*) LST_Next(l);
  434. }
  435. }
  436. Uint16 DcmSCU::nextMessageID()
  437. {
  438. if (!isConnected())
  439. return 0;
  440. else
  441. return m_assoc->nextMsgID++;
  442. }
  443. void DcmSCU::closeAssociation(const DcmCloseAssociationType closeType)
  444. {
  445. if (!isConnected())
  446. {
  447. DCMNET_WARN("Closing of association request but no association active (ignored)");
  448. return;
  449. }
  450. OFCondition cond;
  451. OFString tempStr;
  452. /* tear down association, i.e. terminate network connection to SCP */
  453. switch (closeType)
  454. {
  455. case DCMSCU_RELEASE_ASSOCIATION:
  456. /* release association */
  457. DCMNET_INFO("Releasing Association");
  458. cond = ASC_releaseAssociation(m_assoc);
  459. if (cond.bad())
  460. {
  461. DCMNET_ERROR("Association Release Failed: " << DimseCondition::dump(tempStr, cond));
  462. return; // TODO: do we really need this?
  463. }
  464. break;
  465. case DCMSCU_ABORT_ASSOCIATION:
  466. /* abort association */
  467. DCMNET_INFO("Aborting Association");
  468. cond = ASC_abortAssociation(m_assoc);
  469. if (cond.bad())
  470. {
  471. DCMNET_ERROR("Association Abort Failed: " << DimseCondition::dump(tempStr, cond));
  472. }
  473. break;
  474. case DCMSCU_PEER_REQUESTED_RELEASE:
  475. /* peer requested release */
  476. DCMNET_ERROR("Protocol Error: Peer requested release (Aborting)");
  477. DCMNET_INFO("Aborting Association");
  478. cond = ASC_abortAssociation(m_assoc);
  479. if (cond.bad())
  480. {
  481. DCMNET_ERROR("Association Abort Failed: " << DimseCondition::dump(tempStr, cond));
  482. }
  483. break;
  484. case DCMSCU_PEER_ABORTED_ASSOCIATION:
  485. /* peer aborted association */
  486. DCMNET_INFO("Peer Aborted Association");
  487. break;
  488. }
  489. // destroy and free memory of internal association and network structures
  490. freeNetwork();
  491. }
  492. /* ************************************************************************* */
  493. /* C-ECHO functionality */
  494. /* ************************************************************************* */
  495. // Sends C-ECHO request to another DICOM application
  496. OFCondition DcmSCU::sendECHORequest(const T_ASC_PresentationContextID presID)
  497. {
  498. if (!isConnected())
  499. return DIMSE_ILLEGALASSOCIATION;
  500. OFCondition cond;
  501. T_ASC_PresentationContextID pcid = presID;
  502. /* If necessary, find appropriate presentation context */
  503. if (pcid == 0)
  504. pcid = findPresentationContextID(UID_VerificationSOPClass, UID_LittleEndianExplicitTransferSyntax);
  505. if (pcid == 0)
  506. pcid = findPresentationContextID(UID_VerificationSOPClass, UID_BigEndianExplicitTransferSyntax);
  507. if (pcid == 0)
  508. pcid = findPresentationContextID(UID_VerificationSOPClass, UID_LittleEndianImplicitTransferSyntax);
  509. if (pcid == 0)
  510. {
  511. DCMNET_ERROR("No presentation context found for sending C-ECHO with SOP Class / Transfer Syntax: "
  512. << dcmFindNameOfUID(UID_VerificationSOPClass, "") << " / "
  513. << DcmXfer(UID_LittleEndianImplicitTransferSyntax).getXferName());
  514. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  515. }
  516. /* Now, assemble DIMSE message */
  517. T_DIMSE_Message msg;
  518. T_DIMSE_C_EchoRQ* req = &(msg.msg.CEchoRQ);
  519. // Set type of message
  520. msg.CommandField = DIMSE_C_ECHO_RQ;
  521. // Set message ID
  522. req->MessageID = nextMessageID();
  523. // Announce no dataset
  524. req->DataSetType = DIMSE_DATASET_NULL;
  525. // Set affected SOP Class UID (always Verification SOP Class)
  526. OFStandard::strlcpy(req->AffectedSOPClassUID, UID_VerificationSOPClass, sizeof(req->AffectedSOPClassUID));
  527. /* Send request */
  528. OFString tempStr;
  529. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  530. {
  531. DCMNET_INFO("Sending C-ECHO Request");
  532. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, NULL, pcid));
  533. } else {
  534. DCMNET_INFO("Sending C-ECHO Request (MsgID " << req->MessageID << ")");
  535. }
  536. cond = sendDIMSEMessage(pcid, &msg, NULL /*dataObject*/);
  537. if (cond.bad())
  538. {
  539. DCMNET_ERROR("Failed sending C-ECHO request: " << DimseCondition::dump(tempStr, cond));
  540. return cond;
  541. }
  542. /* Receive response */
  543. T_DIMSE_Message rsp;
  544. DcmDataset* statusDetail = NULL;
  545. cond = receiveDIMSECommand(&pcid, &rsp, &statusDetail, NULL /* not interested in the command set */);
  546. if (cond.bad())
  547. {
  548. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  549. return cond;
  550. }
  551. /* Check whether we received C-ECHO response, otherwise print error */
  552. if (rsp.CommandField == DIMSE_C_ECHO_RSP)
  553. {
  554. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  555. {
  556. DCMNET_INFO("Received C-ECHO Response");
  557. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  558. } else {
  559. DCMNET_INFO("Received C-ECHO Response (" << rsp.msg.CEchoRSP.DimseStatus << ")");
  560. }
  561. } else {
  562. DCMNET_ERROR("Expected C-ECHO response but received DIMSE command 0x"
  563. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  564. << OFstatic_cast(unsigned int, rsp.CommandField));
  565. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  566. delete statusDetail;
  567. return DIMSE_BADCOMMANDTYPE;
  568. }
  569. /* Print status detail if it was received */
  570. if (statusDetail != NULL)
  571. {
  572. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  573. delete statusDetail;
  574. }
  575. return EC_Normal;
  576. }
  577. /* ************************************************************************* */
  578. /* C-STORE functionality */
  579. /* ************************************************************************* */
  580. // Sends C-STORE request to another DICOM application
  581. OFCondition DcmSCU::sendSTORERequest(const T_ASC_PresentationContextID presID,
  582. const OFString &dicomFile,
  583. DcmDataset *dataset,
  584. Uint16 &rspStatusCode)
  585. {
  586. // Do some basic validity checks
  587. if (!isConnected())
  588. return DIMSE_ILLEGALASSOCIATION;
  589. OFCondition cond;
  590. OFString tempStr;
  591. T_ASC_PresentationContextID pcid = presID;
  592. DcmDataset* statusDetail = NULL;
  593. T_DIMSE_Message msg;
  594. T_DIMSE_C_StoreRQ* req = &(msg.msg.CStoreRQ);
  595. // Set type of message
  596. msg.CommandField = DIMSE_C_STORE_RQ;
  597. /* Set message ID */
  598. req->MessageID = nextMessageID();
  599. /* Load file if necessary */
  600. DcmFileFormat *fileformat = NULL;
  601. if (!dicomFile.empty())
  602. {
  603. fileformat = new DcmFileFormat();
  604. if (fileformat == NULL)
  605. return EC_MemoryExhausted;
  606. cond = fileformat->loadFile(dicomFile.c_str());
  607. if (cond.bad())
  608. {
  609. delete fileformat;
  610. return cond;
  611. }
  612. dataset = fileformat->getDataset();
  613. }
  614. /* Fill message according to dataset to be sent */
  615. OFString sopClassUID;
  616. OFString sopInstanceUID;
  617. E_TransferSyntax xferSyntax = EXS_Unknown;
  618. cond = getDatasetInfo(dataset, sopClassUID, sopInstanceUID, xferSyntax);
  619. DcmXfer xfer(xferSyntax);
  620. /* Check whether the information is sufficient */
  621. if (sopClassUID.empty() || sopInstanceUID.empty() || ((pcid == 0) && (xferSyntax == EXS_Unknown)))
  622. {
  623. DCMNET_ERROR("Cannot send SOP instance, missing information:");
  624. if (!dicomFile.empty())
  625. DCMNET_ERROR(" DICOM Filename : " << dicomFile);
  626. DCMNET_ERROR(" SOP Class UID : " << sopClassUID);
  627. DCMNET_ERROR(" SOP Instance UID : " << sopInstanceUID);
  628. DCMNET_ERROR(" Transfer Syntax : " << xfer.getXferName());
  629. if (pcid == 0)
  630. DCMNET_ERROR(" Pres. Context ID : 0 (find via SOP Class and Transfer Syntax)");
  631. else
  632. DCMNET_ERROR(" Pres. Context ID : " << OFstatic_cast(unsigned int, pcid));
  633. delete fileformat;
  634. return cond;
  635. }
  636. OFStandard::strlcpy(req->AffectedSOPClassUID, sopClassUID.c_str(), sizeof(req->AffectedSOPClassUID));
  637. OFStandard::strlcpy(req->AffectedSOPInstanceUID, sopInstanceUID.c_str(), sizeof(req->AffectedSOPInstanceUID));
  638. req->DataSetType = DIMSE_DATASET_PRESENT;
  639. req->Priority = DIMSE_PRIORITY_LOW;
  640. /* If no presentation context is specified by the caller ... */
  641. if (pcid == 0)
  642. {
  643. /* ... try to find an appropriate presentation context automatically */
  644. pcid = findPresentationContextID(sopClassUID, xfer.getXferID());
  645. }
  646. else if (m_datasetConversionMode)
  647. {
  648. /* Convert dataset to network transfer syntax (if required) */
  649. OFString abstractSyntax, transferSyntax;
  650. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  651. /* Check whether given presentation context was accepted by the peer */
  652. if (abstractSyntax.empty() || transferSyntax.empty())
  653. {
  654. /* Mark presentation context as invalid */
  655. pcid = 0;
  656. } else {
  657. if (abstractSyntax != sopClassUID)
  658. {
  659. DCMNET_WARN("Inappropriate presentation context with ID " << OFstatic_cast(unsigned int, pcid)
  660. << ": abstract syntax does not match SOP class UID");
  661. }
  662. /* Try to convert to the negotiated transfer syntax */
  663. DcmXfer netXfer = DcmXfer(transferSyntax.c_str()).getXfer();
  664. if (netXfer.getXfer() != xferSyntax)
  665. {
  666. DCMNET_INFO("Converting transfer syntax: " << xfer.getXferName() << " -> "
  667. << netXfer.getXferName());
  668. dataset->chooseRepresentation(netXfer.getXfer(), NULL);
  669. }
  670. }
  671. }
  672. /* No appropriate presentation context for sending */
  673. if (pcid == 0)
  674. {
  675. OFString sopClassName = dcmFindNameOfUID(sopClassUID.c_str(), sopClassUID.c_str());
  676. OFString xferName = xfer.getXferName();
  677. DCMNET_ERROR("No presentation context found for sending C-STORE with SOP Class / Transfer Syntax: "
  678. << sopClassName << " / " << xferName);
  679. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  680. }
  681. /* Send request */
  682. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  683. {
  684. DCMNET_INFO("Sending C-STORE Request");
  685. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, NULL, pcid));
  686. } else {
  687. DCMNET_INFO("Sending C-STORE Request (MsgID " << req->MessageID << ", "
  688. << dcmSOPClassUIDToModality(sopClassUID.c_str(), "OT") << ")");
  689. }
  690. cond = sendDIMSEMessage(pcid, &msg, dataset);
  691. delete fileformat;
  692. fileformat = NULL;
  693. if (cond.bad())
  694. {
  695. DCMNET_ERROR("Failed sending C-STORE request: " << DimseCondition::dump(tempStr, cond));
  696. return cond;
  697. }
  698. /* Receive response */
  699. T_DIMSE_Message rsp;
  700. cond = receiveDIMSECommand(&pcid, &rsp, &statusDetail, NULL /* not interested in the command set */);
  701. if (cond.bad())
  702. {
  703. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  704. return cond;
  705. }
  706. if (rsp.CommandField == DIMSE_C_STORE_RSP)
  707. {
  708. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  709. {
  710. DCMNET_INFO("Received C-STORE Response");
  711. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  712. } else {
  713. DCMNET_INFO("Received C-STORE Response (" << DU_cstoreStatusString(rsp.msg.CStoreRSP.DimseStatus) << ")");
  714. }
  715. } else {
  716. DCMNET_ERROR("Expected C-STORE response but received DIMSE command 0x"
  717. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  718. << OFstatic_cast(unsigned int, rsp.CommandField));
  719. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  720. delete statusDetail;
  721. return DIMSE_BADCOMMANDTYPE;
  722. }
  723. T_DIMSE_C_StoreRSP storeRsp = rsp.msg.CStoreRSP;
  724. rspStatusCode = storeRsp.DimseStatus;
  725. if (statusDetail != NULL)
  726. {
  727. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  728. delete statusDetail;
  729. }
  730. return cond;
  731. }
  732. /* ************************************************************************* */
  733. /* C-MOVE functionality */
  734. /* ************************************************************************* */
  735. // Sends a C-MOVE Request on given presentation context
  736. OFCondition DcmSCU::sendMOVERequest(const T_ASC_PresentationContextID presID,
  737. const OFString &moveDestinationAETitle,
  738. DcmDataset *dataset,
  739. OFList<RetrieveResponse*> *responses)
  740. {
  741. // Do some basic validity checks
  742. if (!isConnected())
  743. return DIMSE_ILLEGALASSOCIATION;
  744. if (dataset == NULL)
  745. return DIMSE_NULLKEY;
  746. /* Prepare DIMSE data structures for issuing request */
  747. OFCondition cond;
  748. OFString tempStr;
  749. T_ASC_PresentationContextID pcid = presID;
  750. T_DIMSE_Message msg;
  751. DcmDataset* statusDetail = NULL;
  752. T_DIMSE_C_MoveRQ* req = &(msg.msg.CMoveRQ);
  753. // Set type of message
  754. msg.CommandField = DIMSE_C_MOVE_RQ;
  755. // Set message ID
  756. req->MessageID = nextMessageID();
  757. // Announce dataset
  758. req->DataSetType = DIMSE_DATASET_PRESENT;
  759. // Set target for embedded C-Store's
  760. OFStandard::strlcpy(req->MoveDestination, moveDestinationAETitle.c_str(), sizeof(req->MoveDestination));
  761. // Set priority (mandatory)
  762. req->Priority = DIMSE_PRIORITY_LOW;
  763. /* Determine SOP Class from presentation context */
  764. OFString abstractSyntax, transferSyntax;
  765. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  766. if (abstractSyntax.empty() || transferSyntax.empty())
  767. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  768. OFStandard::strlcpy(req->AffectedSOPClassUID, abstractSyntax.c_str(), sizeof(req->AffectedSOPClassUID));
  769. /* Send request */
  770. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  771. {
  772. DCMNET_INFO("Sending C-MOVE Request");
  773. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, dataset, pcid));
  774. } else {
  775. DCMNET_INFO("Sending C-MOVE Request (MsgID " << req->MessageID << ")");
  776. }
  777. cond = sendDIMSEMessage(pcid, &msg, dataset);
  778. if (cond.bad())
  779. {
  780. DCMNET_ERROR("Failed sending C-MOVE request: " << DimseCondition::dump(tempStr, cond));
  781. return cond;
  782. }
  783. /* Receive and handle C-MOVE response messages */
  784. OFBool waitForNextResponse = OFTrue;
  785. while (waitForNextResponse)
  786. {
  787. T_DIMSE_Message rsp;
  788. statusDetail = NULL;
  789. // Receive command set
  790. cond = receiveDIMSECommand(&pcid, &rsp, &statusDetail, NULL /* not interested in the command set */);
  791. if (cond.bad())
  792. {
  793. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  794. delete statusDetail;
  795. break;
  796. }
  797. if (rsp.CommandField == DIMSE_C_MOVE_RSP)
  798. {
  799. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  800. {
  801. DCMNET_INFO("Received C-MOVE Response");
  802. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  803. } else {
  804. DCMNET_INFO("Received C-MOVE Response (" << DU_cmoveStatusString(rsp.msg.CMoveRSP.DimseStatus) << ")");
  805. }
  806. } else {
  807. DCMNET_ERROR("Expected C-MOVE response but received DIMSE command 0x"
  808. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  809. << OFstatic_cast(unsigned int, rsp.CommandField));
  810. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  811. delete statusDetail;
  812. cond = DIMSE_BADCOMMANDTYPE;
  813. break;
  814. }
  815. // Prepare response package for response handler
  816. RetrieveResponse *moveRSP = new RetrieveResponse();
  817. moveRSP->m_affectedSOPClassUID = rsp.msg.CMoveRSP.AffectedSOPClassUID;
  818. moveRSP->m_messageIDRespondedTo = rsp.msg.CMoveRSP.MessageIDBeingRespondedTo;
  819. moveRSP->m_status = rsp.msg.CMoveRSP.DimseStatus;
  820. moveRSP->m_numberOfRemainingSubops = rsp.msg.CMoveRSP.NumberOfRemainingSubOperations;
  821. moveRSP->m_numberOfCompletedSubops = rsp.msg.CMoveRSP.NumberOfCompletedSubOperations;
  822. moveRSP->m_numberOfFailedSubops = rsp.msg.CMoveRSP.NumberOfFailedSubOperations;
  823. moveRSP->m_numberOfWarningSubops = rsp.msg.CMoveRSP.NumberOfWarningSubOperations;
  824. moveRSP->m_statusDetail = statusDetail;
  825. //DCMNET_DEBUG("C-MOVE response has status 0x"
  826. // << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  827. // << moveRSP->m_status);
  828. if (statusDetail != NULL)
  829. {
  830. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  831. }
  832. // Receive dataset if there is one (status PENDING)
  833. DcmDataset *rspDataset = NULL;
  834. // Check if dataset is announced correctly
  835. if (rsp.msg.CMoveRSP.DataSetType != DIMSE_DATASET_NULL) // Some of the sub operations have failed, thus a dataset with a list of them is attached
  836. {
  837. // Receive dataset
  838. cond = receiveDIMSEDataset(&pcid, &rspDataset);
  839. if (cond.bad())
  840. {
  841. DCMNET_ERROR("Unable to receive C-MOVE dataset on presentation context "
  842. << OFstatic_cast(unsigned int, pcid) << ": " << DimseCondition::dump(tempStr, cond));
  843. delete moveRSP; // includes statusDetail
  844. break;
  845. }
  846. moveRSP->m_dataset = rspDataset;
  847. }
  848. // Handle C-MOVE response (has to handle all possible status flags)
  849. cond = handleMOVEResponse(pcid, moveRSP, waitForNextResponse);
  850. if (cond.bad())
  851. {
  852. DCMNET_WARN("Unable to handle C-MOVE response correctly: " << cond.text() << " (ignored)");
  853. delete moveRSP; // includes statusDetail
  854. // don't return here but trust the "waitForNextResponse" variable
  855. }
  856. // if response could be handled successfully, add it to response list
  857. else
  858. {
  859. if (responses != NULL) // only add if desired by caller
  860. responses->push_back(moveRSP);
  861. else
  862. delete moveRSP; // includes statusDetail
  863. }
  864. }
  865. /* All responses received or break signal occured */
  866. return cond;
  867. }
  868. // Standard handler for C-MOVE message responses
  869. OFCondition DcmSCU::handleMOVEResponse( const T_ASC_PresentationContextID /* presID */,
  870. RetrieveResponse *response,
  871. OFBool &waitForNextResponse )
  872. {
  873. // Do some basic validity checks
  874. if (!isConnected())
  875. return DIMSE_ILLEGALASSOCIATION;
  876. if (response == NULL)
  877. return DIMSE_NULLKEY;
  878. DCMNET_DEBUG("Handling C-MOVE Response");
  879. switch (response->m_status) {
  880. case STATUS_MOVE_Failed_IdentifierDoesNotMatchSOPClass:
  881. waitForNextResponse = OFFalse;
  882. DCMNET_ERROR("Identifier does not match SOP class in C-MOVE response");
  883. break;
  884. case STATUS_MOVE_Failed_MoveDestinationUnknown:
  885. waitForNextResponse = OFFalse;
  886. DCMNET_ERROR("Move destination unknown");
  887. break;
  888. case STATUS_MOVE_Failed_UnableToProcess:
  889. waitForNextResponse = OFFalse;
  890. DCMNET_ERROR("Unable to process C-Move response");
  891. break;
  892. case STATUS_MOVE_Cancel_SubOperationsTerminatedDueToCancelIndication:
  893. waitForNextResponse = OFFalse;
  894. DCMNET_DEBUG("Suboperations canceled by server due to CANCEL indication");
  895. break;
  896. case STATUS_MOVE_Warning_SubOperationsCompleteOneOrMoreFailures:
  897. waitForNextResponse = OFFalse;
  898. DCMNET_WARN("Suboperations of C-MOVE completed with one or more failures");
  899. break;
  900. case STATUS_Pending:
  901. /* in this case the current C-MOVE-RSP indicates that */
  902. /* there will be some more results */
  903. waitForNextResponse = OFTrue;
  904. DCMNET_DEBUG("One or more pending C-MOVE responses");
  905. break;
  906. case STATUS_Success:
  907. /* in this case, we received the last C-MOVE-RSP so there */
  908. /* will be no other responses we have to wait for. */
  909. waitForNextResponse = OFFalse;
  910. DCMNET_DEBUG("Received final C-MOVE response, no more C-MOVE responses expected");
  911. break;
  912. default:
  913. /* in all other cases, don't expect further responses to come */
  914. waitForNextResponse = OFFalse;
  915. DCMNET_WARN("Status is 0x"
  916. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  917. << response->m_status << " (unknown)");
  918. DCMNET_WARN("Will not wait for further C-MOVE responses");
  919. break;
  920. } //switch
  921. return EC_Normal;
  922. }
  923. /* ************************************************************************* */
  924. /* C-GET and acommpanying C-STORE functionality */
  925. /* ************************************************************************* */
  926. // Sends a C-GET Request on given presentation context
  927. OFCondition DcmSCU::sendCGETRequest(const T_ASC_PresentationContextID presID,
  928. DcmDataset *dataset,
  929. OFList<RetrieveResponse*> *responses)
  930. {
  931. // Do some basic validity checks
  932. if (!isConnected())
  933. return DIMSE_ILLEGALASSOCIATION;
  934. if (dataset == NULL)
  935. return DIMSE_NULLKEY;
  936. /* Prepare DIMSE data structures for issuing request */
  937. OFCondition cond;
  938. OFString tempStr;
  939. T_ASC_PresentationContextID pcid = presID;
  940. T_DIMSE_Message msg;
  941. T_DIMSE_C_GetRQ* req = &(msg.msg.CGetRQ);
  942. // Set type of message
  943. msg.CommandField = DIMSE_C_GET_RQ;
  944. // Set message ID
  945. req->MessageID = nextMessageID();
  946. // Announce dataset
  947. req->DataSetType = DIMSE_DATASET_PRESENT;
  948. // Specify priority
  949. req->Priority = DIMSE_PRIORITY_LOW;
  950. // Determine SOP Class from presentation context
  951. OFString abstractSyntax, transferSyntax;
  952. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  953. if (abstractSyntax.empty() || transferSyntax.empty())
  954. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  955. OFStandard::strlcpy(req->AffectedSOPClassUID, abstractSyntax.c_str(), sizeof(req->AffectedSOPClassUID));
  956. /* Send request */
  957. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  958. {
  959. DCMNET_INFO("Sending C-GET Request");
  960. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, dataset, pcid));
  961. } else {
  962. DCMNET_INFO("Sending C-GET Request (MsgID " << req->MessageID << ")");
  963. }
  964. cond = sendDIMSEMessage(pcid, &msg, dataset);
  965. if (cond.bad())
  966. {
  967. DCMNET_ERROR("Failed sending C-GET request: " << DimseCondition::dump(tempStr, cond));
  968. return cond;
  969. }
  970. cond = handleCGETSession(pcid, dataset, responses);
  971. return cond;
  972. }
  973. // Does the logic for switching between C-GET Response and C-STORE Requests
  974. OFCondition DcmSCU::handleCGETSession(const T_ASC_PresentationContextID /* presID */,
  975. DcmDataset * /* dataset */,
  976. OFList<RetrieveResponse*> *responses)
  977. {
  978. OFCondition result;
  979. OFBool continueSession = OFTrue;
  980. OFString tempStr;
  981. // As long we want to continue (usually, as long as we receive more objects,
  982. // i.e. the final C-GET reponse has not arrived yet)
  983. while (continueSession)
  984. {
  985. T_DIMSE_Message rsp;
  986. DcmDataset *statusDetail = NULL;
  987. T_ASC_PresentationContextID pcid = 0;
  988. // Receive command set
  989. result = receiveDIMSECommand(&pcid, &rsp, &statusDetail, NULL /* not interested in the command set */);
  990. if (result.bad())
  991. {
  992. DCMNET_ERROR("Failed receiving DIMSE command: " << DimseCondition::dump(tempStr, result));
  993. delete statusDetail;
  994. break;
  995. }
  996. // Handle C-GET Response
  997. if (rsp.CommandField == DIMSE_C_GET_RSP)
  998. {
  999. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1000. {
  1001. DCMNET_INFO("Received C-GET Response");
  1002. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  1003. } else {
  1004. DCMNET_INFO("Received C-GET Response (" << DU_cgetStatusString(rsp.msg.CGetRSP.DimseStatus) << ")");
  1005. }
  1006. // Prepare response package for response handler
  1007. RetrieveResponse *getRSP = new RetrieveResponse();
  1008. getRSP->m_affectedSOPClassUID = rsp.msg.CGetRSP.AffectedSOPClassUID;
  1009. getRSP->m_messageIDRespondedTo = rsp.msg.CGetRSP.MessageIDBeingRespondedTo;
  1010. getRSP->m_status = rsp.msg.CGetRSP.DimseStatus;
  1011. getRSP->m_numberOfRemainingSubops = rsp.msg.CGetRSP.NumberOfRemainingSubOperations;
  1012. getRSP->m_numberOfCompletedSubops = rsp.msg.CGetRSP.NumberOfCompletedSubOperations;
  1013. getRSP->m_numberOfFailedSubops = rsp.msg.CGetRSP.NumberOfFailedSubOperations;
  1014. getRSP->m_numberOfWarningSubops = rsp.msg.CGetRSP.NumberOfWarningSubOperations;
  1015. getRSP->m_statusDetail = statusDetail;
  1016. if (statusDetail != NULL)
  1017. {
  1018. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  1019. statusDetail = NULL; // forget reference to status detail, will be deleted with getRSP
  1020. }
  1021. result = handleCGETResponse(pcid, getRSP, continueSession);
  1022. if (result.bad())
  1023. {
  1024. DCMNET_WARN("Unable to handle C-GET response correctly: " << result.text() << " (ignored)");
  1025. delete getRSP; // includes statusDetail
  1026. // don't return here but trust the "continueSession" variable
  1027. }
  1028. // if response could be handled successfully, add it to response list
  1029. else {
  1030. if (responses != NULL) // only add if desired by caller
  1031. responses->push_back(getRSP);
  1032. else
  1033. delete getRSP; // includes statusDetail
  1034. }
  1035. }
  1036. // Handle C-STORE Request
  1037. else if (rsp.CommandField == DIMSE_C_STORE_RQ)
  1038. {
  1039. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1040. {
  1041. DCMNET_INFO("Received C-STORE Request");
  1042. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  1043. } else {
  1044. DCMNET_INFO("Received C-STORE Request (MsgID " << rsp.msg.CStoreRQ.MessageID << ")");
  1045. }
  1046. // Receive dataset if there is one (status PENDING)
  1047. DcmDataset *rspDataset = NULL;
  1048. // Check if dataset is announced correctly
  1049. if (rsp.msg.CStoreRQ.DataSetType == DIMSE_DATASET_NULL)
  1050. {
  1051. DCMNET_WARN("Incoming C-STORE with no dataset, trying to receive one anyway");
  1052. }
  1053. Uint16 desiredCStoreReturnStatus = 0;
  1054. // handle normal storage mode, i.e. receive in memory and store to disk
  1055. if (m_storageMode == DCMSCU_STORAGE_DISK)
  1056. {
  1057. // Receive dataset
  1058. result = receiveDIMSEDataset(&pcid, &rspDataset);
  1059. if (result.bad())
  1060. {
  1061. result = DIMSE_NULLKEY;
  1062. desiredCStoreReturnStatus = STATUS_STORE_Error_CannotUnderstand;
  1063. } else {
  1064. result = handleSTORERequest(pcid, rspDataset, continueSession, desiredCStoreReturnStatus);
  1065. }
  1066. }
  1067. // handle bit preserving storage mode, i.e. receive directly to disk
  1068. else if (m_storageMode == DCMSCU_STORAGE_BIT_PRESERVING)
  1069. {
  1070. OFString storageFilename;
  1071. OFStandard::combineDirAndFilename(storageFilename, m_storageDir, rsp.msg.CStoreRQ.AffectedSOPInstanceUID, OFTrue);
  1072. result = handleSTORERequestFile(&pcid, storageFilename, &(rsp.msg.CStoreRQ));
  1073. if (result.good())
  1074. {
  1075. notifyInstanceStored(storageFilename, rsp.msg.CStoreRQ.AffectedSOPClassUID, rsp.msg.CStoreRQ.AffectedSOPInstanceUID);
  1076. }
  1077. }
  1078. // handle ignore storage mode, i.e. ignore received dataset and do not store at all
  1079. else
  1080. {
  1081. result = ignoreSTORERequest(pcid, rsp.msg.CStoreRQ);
  1082. }
  1083. // Evaluate result from C-STORE request handling and send response
  1084. if (result.bad())
  1085. {
  1086. desiredCStoreReturnStatus = STATUS_STORE_Error_CannotUnderstand;
  1087. continueSession = OFFalse;
  1088. }
  1089. result = sendSTOREResponse(pcid, desiredCStoreReturnStatus, rsp.msg.CStoreRQ);
  1090. if (result.bad())
  1091. {
  1092. continueSession = OFFalse;
  1093. }
  1094. delete rspDataset; // should be NULL if not existing
  1095. }
  1096. // Handle other DIMSE command (error since other command than GET/STORE not expected)
  1097. else
  1098. {
  1099. DCMNET_ERROR("Expected C-GET response or C-STORE request but received DIMSE command 0x"
  1100. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1101. << OFstatic_cast(unsigned int, rsp.CommandField));
  1102. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  1103. result = DIMSE_BADCOMMANDTYPE;
  1104. continueSession = OFFalse;
  1105. }
  1106. delete statusDetail; // should be NULL if not existing or added to response list
  1107. statusDetail = NULL;
  1108. }
  1109. /* All responses received or break signal occured */
  1110. return result;
  1111. }
  1112. // Handles single C-GET Response
  1113. OFCondition DcmSCU::handleCGETResponse(const T_ASC_PresentationContextID /* presID */,
  1114. RetrieveResponse* response,
  1115. OFBool& continueCGETSession)
  1116. {
  1117. // Do some basic validity checks
  1118. if (!isConnected())
  1119. return DIMSE_ILLEGALASSOCIATION;
  1120. if (response == NULL)
  1121. return DIMSE_NULLKEY;
  1122. DCMNET_DEBUG("Handling C-GET Response");
  1123. /* First, perform separate check for 0xCxxx error codes */
  1124. Uint16 highNibble = response->m_status & 0xf000;
  1125. if (highNibble == STATUS_GET_Failed_UnableToProcess)
  1126. {
  1127. continueCGETSession = OFFalse;
  1128. DCMNET_ERROR("Unable to Process");
  1129. DCMNET_WARN("Full status is 0x"
  1130. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1131. << response->m_status);
  1132. return EC_Normal;
  1133. }
  1134. /* Check for other error codes */
  1135. switch (response->m_status) {
  1136. case STATUS_GET_Refused_OutOfResourcesNumberOfMatches:
  1137. continueCGETSession = OFFalse;
  1138. DCMNET_ERROR("Out of Resouces - Unable to calculate number of matches");
  1139. break;
  1140. case STATUS_GET_Refused_OutOfResourcesSubOperations:
  1141. continueCGETSession = OFFalse;
  1142. DCMNET_ERROR("Out of Resouces - Unable to perform sub-operations");
  1143. break;
  1144. case STATUS_GET_Failed_IdentifierDoesNotMatchSOPClass:
  1145. continueCGETSession = OFFalse;
  1146. DCMNET_ERROR("Identifier does not match SOP class");
  1147. break;
  1148. case STATUS_GET_Cancel_SubOperationsTerminatedDueToCancelIndication:
  1149. continueCGETSession = OFFalse;
  1150. DCMNET_DEBUG("Suboperations canceled by server due to CANCEL indication");
  1151. break;
  1152. case STATUS_GET_Warning_SubOperationsCompleteOneOrMoreFailures:
  1153. continueCGETSession = OFFalse;
  1154. DCMNET_WARN("Suboperations of C-GET completed with one or more failures");
  1155. break;
  1156. case STATUS_Pending:
  1157. /* in this case the current C-MOVE-RSP indicates that */
  1158. /* there will be some more results */
  1159. continueCGETSession = OFTrue;
  1160. DCMNET_DEBUG("One or more pending C-GET responses");
  1161. break;
  1162. case STATUS_Success:
  1163. /* in this case, we received the last C-MOVE-RSP so there */
  1164. /* will be no other responses we have to wait for. */
  1165. continueCGETSession = OFFalse;
  1166. DCMNET_DEBUG("Received final C-GET response, no more C-GET responses expected");
  1167. break;
  1168. default:
  1169. /* in all other cases, don't expect further responses to come */
  1170. continueCGETSession = OFFalse;
  1171. DCMNET_WARN("Status is 0x"
  1172. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1173. << response->m_status << " (unknown)");
  1174. DCMNET_WARN("Will not wait for further C-GET responses");
  1175. break;
  1176. } //switch
  1177. return EC_Normal;
  1178. }
  1179. // Handles single C-STORE Request received during C-GET session
  1180. OFCondition DcmSCU::handleSTORERequest(const T_ASC_PresentationContextID /* presID */,
  1181. DcmDataset *incomingObject,
  1182. OFBool& /* continueCGETSession */,
  1183. Uint16& cStoreReturnStatus)
  1184. {
  1185. if (incomingObject == NULL)
  1186. return DIMSE_NULLKEY;
  1187. OFString sopClassUID;
  1188. OFString sopInstanceUID;
  1189. OFCondition result = incomingObject->findAndGetOFString(DCM_SOPClassUID, sopClassUID);
  1190. if (result.good())
  1191. result = incomingObject->findAndGetOFString(DCM_SOPInstanceUID, sopInstanceUID);
  1192. if (result.bad())
  1193. {
  1194. DCMNET_ERROR("Cannot store received object: either SOP Instance or SOP Class UID not present");
  1195. cStoreReturnStatus = STATUS_STORE_Error_DataSetDoesNotMatchSOPClass;
  1196. return EC_TagNotFound;
  1197. }
  1198. OFString filename = createStorageFilename(incomingObject);
  1199. result = incomingObject->saveFile(filename.c_str());
  1200. if (result.good())
  1201. {
  1202. E_TransferSyntax xferSyntax;
  1203. getDatasetInfo(incomingObject, sopClassUID, sopInstanceUID, xferSyntax);
  1204. notifyInstanceStored(filename, sopClassUID, sopInstanceUID);
  1205. cStoreReturnStatus = STATUS_Success;
  1206. }
  1207. else
  1208. {
  1209. cStoreReturnStatus = STATUS_STORE_Refused_OutOfResources;
  1210. }
  1211. return result;
  1212. }
  1213. OFCondition DcmSCU::handleSTORERequestFile(T_ASC_PresentationContextID *presID,
  1214. const OFString& filename,
  1215. T_DIMSE_C_StoreRQ* request)
  1216. {
  1217. if (filename.empty())
  1218. return EC_IllegalParameter;
  1219. /* in the following, we want to receive data over the network and write it to a file */
  1220. /* exactly the way it was received over the network. Hence, a filestream will be created and the data */
  1221. /* set will be received and written to the file through the call to DIMSE_receiveDataSetInFile(...).*/
  1222. /* create filestream */
  1223. DcmOutputFileStream *filestream = NULL;
  1224. OFCondition cond = DIMSE_createFilestream(filename.c_str(), request, m_assoc, *presID, OFTrue, &filestream);
  1225. if (cond.good())
  1226. {
  1227. if (m_progressNotificationMode)
  1228. {
  1229. cond = DIMSE_receiveDataSetInFile(m_assoc, m_blockMode, m_dimseTimeout, presID, filestream,
  1230. callbackRECEIVEProgress, this /*callbackData*/);
  1231. } else {
  1232. cond = DIMSE_receiveDataSetInFile(m_assoc, m_blockMode, m_dimseTimeout, presID, filestream,
  1233. NULL /*callback*/, NULL /*callbackData*/);
  1234. }
  1235. delete filestream;
  1236. if (cond != EC_Normal)
  1237. {
  1238. unlink(filename.c_str());
  1239. }
  1240. DCMNET_DEBUG("Received dataset on presentation context " << OFstatic_cast(unsigned int, *presID));
  1241. }
  1242. else
  1243. {
  1244. OFString tempStr;
  1245. DCMNET_ERROR("Unable to receive and store dataset on presentation context "
  1246. << OFstatic_cast(unsigned int, *presID) << ": " << DimseCondition::dump(tempStr, cond));
  1247. }
  1248. return cond;
  1249. }
  1250. OFCondition DcmSCU::sendSTOREResponse(T_ASC_PresentationContextID presID,
  1251. Uint16 status,
  1252. const T_DIMSE_C_StoreRQ& request)
  1253. {
  1254. // Send back response
  1255. T_DIMSE_Message response;
  1256. T_DIMSE_C_StoreRSP &storeRsp = response.msg.CStoreRSP;
  1257. response.CommandField = DIMSE_C_STORE_RSP;
  1258. storeRsp.MessageIDBeingRespondedTo = request.MessageID;
  1259. storeRsp.DimseStatus = status;
  1260. storeRsp.DataSetType = DIMSE_DATASET_NULL;
  1261. storeRsp.opts = 0;
  1262. /* Following information is optional and normally not sent by the underlying
  1263. * dcmnet routines. However, maybe this could be changed later, so insert it.
  1264. */
  1265. OFStandard::strlcpy(storeRsp.AffectedSOPClassUID, request.AffectedSOPClassUID, sizeof(storeRsp.AffectedSOPClassUID));
  1266. OFStandard::strlcpy(storeRsp.AffectedSOPInstanceUID, request.AffectedSOPInstanceUID, sizeof(storeRsp.AffectedSOPInstanceUID));
  1267. OFString tempStr;
  1268. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1269. {
  1270. DCMNET_INFO("Sending C-STORE Response");
  1271. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_OUTGOING, NULL, presID));
  1272. } else {
  1273. DCMNET_INFO("Sending C-STORE Response (" << DU_cstoreStatusString(status) << ")");
  1274. }
  1275. OFCondition cond = sendDIMSEMessage(presID, &response, NULL /*dataObject*/);
  1276. if (cond.bad())
  1277. {
  1278. DCMNET_ERROR("Failed sending C-STORE response: " << DimseCondition::dump(tempStr, cond));
  1279. }
  1280. return cond;
  1281. }
  1282. OFString DcmSCU::createStorageFilename(DcmDataset *dataset)
  1283. {
  1284. OFString sopClassUID, sopInstanceUID;
  1285. E_TransferSyntax dummy;
  1286. getDatasetInfo(dataset, sopClassUID, sopInstanceUID, dummy);
  1287. // Create unique filename
  1288. if (sopClassUID.empty() || sopInstanceUID.empty())
  1289. return "";
  1290. OFString name = dcmSOPClassUIDToModality(sopClassUID.c_str(), "UNKNOWN");
  1291. name += ".";
  1292. name += sopInstanceUID;
  1293. OFString returnStr;
  1294. OFStandard::combineDirAndFilename(returnStr, m_storageDir, name, OFTrue);
  1295. return returnStr;
  1296. }
  1297. OFCondition DcmSCU::ignoreSTORERequest(T_ASC_PresentationContextID presID,
  1298. const T_DIMSE_C_StoreRQ& request)
  1299. {
  1300. /* We cannot create the filestream, so ignore the incoming dataset and return an out-of-resources error to the SCU */
  1301. DIC_UL bytesRead = 0;
  1302. DIC_UL pdvCount=0;
  1303. DCMNET_DEBUG("Ignoring incoming C-STORE dataset on presentation context "
  1304. << OFstatic_cast(unsigned int, presID)
  1305. << " with Affected SOP Instance UID: " << request.AffectedSOPInstanceUID );
  1306. OFCondition result = DIMSE_ignoreDataSet(m_assoc, m_blockMode, m_dimseTimeout, &bytesRead, &pdvCount);
  1307. if (result.good())
  1308. {
  1309. DCMNET_TRACE("Successfully skipped " << bytesRead << " bytes in " << pdvCount << " PDVs");
  1310. }
  1311. return result;
  1312. }
  1313. void DcmSCU::notifyInstanceStored(const OFString& filename,
  1314. const OFString& sopClassUID,
  1315. const OFString& sopInstanceUID) const
  1316. {
  1317. DCMNET_DEBUG("Stored instance to disk:");
  1318. DCMNET_DEBUG(" Filename: " << filename);
  1319. DCMNET_DEBUG(" SOP Class UID: " << sopClassUID);
  1320. DCMNET_DEBUG(" SOP Instance UID: " << sopInstanceUID);
  1321. }
  1322. /* ************************************************************************* */
  1323. /* C-FIND functionality */
  1324. /* ************************************************************************* */
  1325. // Sends a C-FIND Request on given presentation context
  1326. OFCondition DcmSCU::sendFINDRequest(const T_ASC_PresentationContextID presID,
  1327. DcmDataset *queryKeys,
  1328. OFList<QRResponse*> *responses)
  1329. {
  1330. // Do some basic validity checks
  1331. if (!isConnected())
  1332. return DIMSE_ILLEGALASSOCIATION;
  1333. if (queryKeys == NULL)
  1334. return DIMSE_NULLKEY;
  1335. /* Prepare DIMSE data structures for issuing request */
  1336. OFCondition cond;
  1337. OFString tempStr;
  1338. T_ASC_PresentationContextID pcid = presID;
  1339. T_DIMSE_Message msg;
  1340. DcmDataset* statusDetail = NULL;
  1341. T_DIMSE_C_FindRQ* req = &(msg.msg.CFindRQ);
  1342. // Set type of message
  1343. msg.CommandField = DIMSE_C_FIND_RQ;
  1344. // Set message ID
  1345. req->MessageID = nextMessageID();
  1346. // Announce dataset
  1347. req->DataSetType = DIMSE_DATASET_PRESENT;
  1348. // Specify priority
  1349. req->Priority = DIMSE_PRIORITY_LOW;
  1350. // Determine SOP Class from presentation context
  1351. OFString abstractSyntax, transferSyntax;
  1352. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  1353. if (abstractSyntax.empty() || transferSyntax.empty())
  1354. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  1355. OFStandard::strlcpy(req->AffectedSOPClassUID, abstractSyntax.c_str(), sizeof(req->AffectedSOPClassUID));
  1356. /* Send request */
  1357. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1358. {
  1359. DCMNET_INFO("Sending C-FIND Request");
  1360. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, queryKeys, pcid));
  1361. } else {
  1362. DCMNET_INFO("Sending C-FIND Request (MsgID " << req->MessageID << ")");
  1363. }
  1364. cond = sendDIMSEMessage(pcid, &msg, queryKeys);
  1365. if (cond.bad())
  1366. {
  1367. DCMNET_ERROR("Failed sending C-FIND request: " << DimseCondition::dump(tempStr, cond));
  1368. return cond;
  1369. }
  1370. /* Receive and handle response */
  1371. OFBool waitForNextResponse = OFTrue;
  1372. while (waitForNextResponse)
  1373. {
  1374. T_DIMSE_Message rsp;
  1375. statusDetail = NULL;
  1376. // Receive command set
  1377. cond = receiveDIMSECommand(&pcid, &rsp, &statusDetail, NULL /* not interested in the command set */);
  1378. if (cond.bad())
  1379. {
  1380. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  1381. return cond;
  1382. }
  1383. if (rsp.CommandField == DIMSE_C_FIND_RSP)
  1384. {
  1385. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1386. {
  1387. DCMNET_INFO("Received C-FIND Response");
  1388. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  1389. } else {
  1390. DCMNET_INFO("Received C-FIND Response (" << DU_cfindStatusString(rsp.msg.CFindRSP.DimseStatus) << ")");
  1391. }
  1392. } else {
  1393. DCMNET_ERROR("Expected C-FIND response but received DIMSE command 0x"
  1394. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1395. << OFstatic_cast(unsigned int, rsp.CommandField));
  1396. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, rsp, DIMSE_INCOMING, NULL, pcid));
  1397. delete statusDetail;
  1398. return DIMSE_BADCOMMANDTYPE;
  1399. }
  1400. // Prepare response package for response handler
  1401. QRResponse *findRSP = new QRResponse();
  1402. findRSP->m_affectedSOPClassUID = rsp.msg.CFindRSP.AffectedSOPClassUID;
  1403. findRSP->m_messageIDRespondedTo = rsp.msg.CFindRSP.MessageIDBeingRespondedTo;
  1404. findRSP->m_status = rsp.msg.CFindRSP.DimseStatus;
  1405. findRSP->m_statusDetail = statusDetail;
  1406. // Receive dataset if there is one (status PENDING)
  1407. DcmDataset *rspDataset = NULL;
  1408. if (DICOM_PENDING_STATUS(findRSP->m_status))
  1409. {
  1410. // Check if dataset is announced correctly
  1411. if (rsp.msg.CFindRSP.DataSetType == DIMSE_DATASET_NULL)
  1412. {
  1413. DCMNET_ERROR("Received C-FIND response with PENDING status but no dataset announced, aborting");
  1414. delete findRSP; // includes statusDetail
  1415. return DIMSE_BADMESSAGE;
  1416. }
  1417. // Receive dataset
  1418. cond = receiveDIMSEDataset(&pcid, &rspDataset);
  1419. if (cond.bad())
  1420. {
  1421. delete findRSP; // includes statusDetail
  1422. return DIMSE_BADDATA;
  1423. }
  1424. findRSP->m_dataset = rspDataset;
  1425. }
  1426. // Handle C-FIND response (has to handle all possible status flags)
  1427. cond = handleFINDResponse(pcid, findRSP, waitForNextResponse);
  1428. if (cond.bad())
  1429. {
  1430. DCMNET_WARN("Unable to handle C-FIND response correctly: " << cond.text() << " (ignored)");
  1431. delete findRSP; // includes statusDetail and rspDataset
  1432. // don't return here but trust the "waitForNextResponse" variable
  1433. }
  1434. // if response could be handled successfully, add it to response list
  1435. else
  1436. {
  1437. if (responses != NULL) // only add if desired by caller
  1438. responses->push_back(findRSP);
  1439. else
  1440. delete findRSP; // includes statusDetail and rspDataset
  1441. }
  1442. }
  1443. /* All responses received or break signal occured */
  1444. return EC_Normal;
  1445. }
  1446. // Standard handler for C-FIND message responses
  1447. OFCondition DcmSCU::handleFINDResponse(const T_ASC_PresentationContextID /* presID */,
  1448. QRResponse *response,
  1449. OFBool &waitForNextResponse)
  1450. {
  1451. if (!isConnected())
  1452. return DIMSE_ILLEGALASSOCIATION;
  1453. if (response == NULL)
  1454. return DIMSE_NULLKEY;
  1455. DCMNET_DEBUG("Handling C-FIND Response");
  1456. switch (response->m_status) {
  1457. case STATUS_Pending:
  1458. case STATUS_FIND_Pending_WarningUnsupportedOptionalKeys:
  1459. /* in this case the current C-FIND-RSP indicates that */
  1460. /* there will be some more results */
  1461. waitForNextResponse = OFTrue;
  1462. DCMNET_DEBUG("One or more pending C-FIND responses");
  1463. break;
  1464. case STATUS_Success:
  1465. /* in this case the current C-FIND-RSP indicates that */
  1466. /* there are no more records that match the search mask */
  1467. waitForNextResponse = OFFalse;
  1468. DCMNET_DEBUG("Received final C-FIND response, no more C-FIND responses expected");
  1469. break;
  1470. default:
  1471. /* in all other cases, don't expect further responses to come */
  1472. waitForNextResponse = OFFalse;
  1473. DCMNET_DEBUG("Status tells not to wait for further C-FIND responses");
  1474. break;
  1475. } //switch
  1476. return EC_Normal;
  1477. }
  1478. /* ************************************************************************* */
  1479. /* C-CANCEL functionality */
  1480. /* ************************************************************************* */
  1481. // Send C-CANCEL-REQ and, therefore, ends current C-FIND, -MOVE or -GET session
  1482. OFCondition DcmSCU::sendCANCELRequest(const T_ASC_PresentationContextID presID)
  1483. {
  1484. if (!isConnected())
  1485. return DIMSE_ILLEGALASSOCIATION;
  1486. /* Prepare DIMSE data structures for issuing request */
  1487. OFCondition cond;
  1488. OFString tempStr;
  1489. T_ASC_PresentationContextID pcid = presID;
  1490. T_DIMSE_Message msg;
  1491. T_DIMSE_C_CancelRQ* req = &(msg.msg.CCancelRQ);
  1492. // Set type of message
  1493. msg.CommandField = DIMSE_C_CANCEL_RQ;
  1494. /* Set message ID responded to. A new message ID is _not_ needed so
  1495. we do not increment the message ID here but instead have to give the
  1496. message ID that was used last.
  1497. Note that that it is required to actually use the message ID of the last
  1498. C-FIND/GET/MOVE that was issued on this presentation context channel.
  1499. However, since we only support synchronous association mode so far,
  1500. it is enough to take the last message ID used at all.
  1501. For asynchronous operation, we would have to lookup the message ID
  1502. of the last C-FIND/GET/MOVE request issued and thus, store this
  1503. information after sending it.
  1504. */
  1505. req->MessageIDBeingRespondedTo = m_assoc->nextMsgID - 1;
  1506. // Announce dataset
  1507. req->DataSetType = DIMSE_DATASET_NULL;
  1508. /* We do not care about the transfer syntax since no
  1509. dataset is transported at all, i.e. we trust that the user provided
  1510. the correct presentation context ID (could be private one).
  1511. */
  1512. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1513. {
  1514. DCMNET_INFO("Sending C-CANCEL Request");
  1515. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, msg, DIMSE_OUTGOING, NULL, pcid));
  1516. } else {
  1517. DCMNET_INFO("Sending C-CANCEL Request (MsgID " << req->MessageIDBeingRespondedTo
  1518. << ", PresID " << OFstatic_cast(unsigned int, pcid) << ")");
  1519. }
  1520. cond = sendDIMSEMessage(pcid, &msg, NULL /*dataObject*/);
  1521. if (cond.bad())
  1522. {
  1523. DCMNET_ERROR("Failed sending C-CANCEL request: " << DimseCondition::dump(tempStr, cond));
  1524. }
  1525. DCMNET_TRACE("There is no C-CANCEL response in DICOM, so none expected");
  1526. return cond;
  1527. }
  1528. /* ************************************************************************* */
  1529. /* N-ACTION functionality */
  1530. /* ************************************************************************* */
  1531. // Sends N-ACTION request to another DICOM application
  1532. OFCondition DcmSCU::sendACTIONRequest(const T_ASC_PresentationContextID presID,
  1533. const OFString &sopInstanceUID,
  1534. const Uint16 actionTypeID,
  1535. DcmDataset *reqDataset,
  1536. Uint16 &rspStatusCode)
  1537. {
  1538. // Do some basic validity checks
  1539. if (!isConnected())
  1540. return DIMSE_ILLEGALASSOCIATION;
  1541. if (sopInstanceUID.empty() || (reqDataset == NULL))
  1542. return DIMSE_NULLKEY;
  1543. // Prepare DIMSE data structures for issuing request
  1544. OFCondition cond;
  1545. OFString tempStr;
  1546. T_ASC_PresentationContextID pcid = presID;
  1547. T_DIMSE_Message request;
  1548. T_DIMSE_N_ActionRQ &actionReq = request.msg.NActionRQ;
  1549. DcmDataset *statusDetail = NULL;
  1550. request.CommandField = DIMSE_N_ACTION_RQ;
  1551. actionReq.MessageID = nextMessageID();
  1552. actionReq.DataSetType = DIMSE_DATASET_PRESENT;
  1553. actionReq.ActionTypeID = actionTypeID;
  1554. // Determine SOP Class from presentation context
  1555. OFString abstractSyntax, transferSyntax;
  1556. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  1557. if (abstractSyntax.empty() || transferSyntax.empty())
  1558. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  1559. OFStandard::strlcpy(actionReq.RequestedSOPClassUID, abstractSyntax.c_str(), sizeof(actionReq.RequestedSOPClassUID));
  1560. OFStandard::strlcpy(actionReq.RequestedSOPInstanceUID, sopInstanceUID.c_str(), sizeof(actionReq.RequestedSOPInstanceUID));
  1561. // Send request
  1562. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1563. {
  1564. DCMNET_INFO("Sending N-ACTION Request");
  1565. // Output dataset only if trace level is enabled
  1566. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::TRACE_LOG_LEVEL))
  1567. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_OUTGOING, reqDataset, pcid));
  1568. else
  1569. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_OUTGOING, NULL, pcid));
  1570. } else {
  1571. DCMNET_INFO("Sending N-ACTION Request (MsgID " << actionReq.MessageID << ")");
  1572. }
  1573. cond = sendDIMSEMessage(pcid, &request, reqDataset);
  1574. if (cond.bad())
  1575. {
  1576. DCMNET_ERROR("Failed sending N-ACTION request: " << DimseCondition::dump(tempStr, cond));
  1577. return cond;
  1578. }
  1579. // Receive response
  1580. T_DIMSE_Message response;
  1581. cond = receiveDIMSECommand(&pcid, &response, &statusDetail, NULL /* commandSet */);
  1582. if (cond.bad())
  1583. {
  1584. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  1585. return cond;
  1586. }
  1587. // Check command set
  1588. if (response.CommandField == DIMSE_N_ACTION_RSP)
  1589. {
  1590. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1591. {
  1592. DCMNET_INFO("Received N-ACTION Response");
  1593. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_INCOMING, NULL, pcid));
  1594. } else {
  1595. DCMNET_INFO("Received N-ACTION Response (" << DU_nactionStatusString(response.msg.NActionRSP.DimseStatus) << ")");
  1596. }
  1597. } else {
  1598. DCMNET_ERROR("Expected N-ACTION response but received DIMSE command 0x"
  1599. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1600. << OFstatic_cast(unsigned int, response.CommandField));
  1601. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_INCOMING, NULL, pcid));
  1602. delete statusDetail;
  1603. return DIMSE_BADCOMMANDTYPE;
  1604. }
  1605. if (statusDetail != NULL)
  1606. {
  1607. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  1608. delete statusDetail;
  1609. }
  1610. // Set return value
  1611. T_DIMSE_N_ActionRSP &actionRsp = response.msg.NActionRSP;
  1612. rspStatusCode = actionRsp.DimseStatus;
  1613. // Check whether there is a dataset to be received
  1614. if (actionRsp.DataSetType == DIMSE_DATASET_PRESENT)
  1615. {
  1616. // this should never happen
  1617. DcmDataset *tempDataset = NULL;
  1618. T_ASC_PresentationContextID tempID;
  1619. DCMNET_WARN("Trying to retrieve unexpected dataset in N-ACTION response");
  1620. cond = receiveDIMSEDataset(&tempID, &tempDataset);
  1621. if (cond.good())
  1622. {
  1623. DCMNET_WARN("Received unexpected dataset after N-ACTION response, ignoring");
  1624. delete tempDataset;
  1625. } else {
  1626. return DIMSE_BADDATA;
  1627. }
  1628. }
  1629. if (actionRsp.MessageIDBeingRespondedTo != actionReq.MessageID)
  1630. {
  1631. // since we only support synchronous communication, the message ID in the response
  1632. // should be identical to the one in the request
  1633. DCMNET_ERROR("Received response with wrong message ID (" << actionRsp.MessageIDBeingRespondedTo
  1634. << " instead of " << actionReq.MessageID << ")");
  1635. return DIMSE_BADMESSAGE;
  1636. }
  1637. return cond;
  1638. }
  1639. /* ************************************************************************* */
  1640. /* N-EVENT REPORT functionality */
  1641. /* ************************************************************************* */
  1642. // Sends N-EVENT-REPORT request and receives N-EVENT-REPORT response
  1643. OFCondition DcmSCU::sendEVENTREPORTRequest(const T_ASC_PresentationContextID presID,
  1644. const OFString &sopInstanceUID,
  1645. const Uint16 eventTypeID,
  1646. DcmDataset *reqDataset,
  1647. Uint16 &rspStatusCode)
  1648. {
  1649. // Do some basic validity checks
  1650. if (!isConnected())
  1651. return DIMSE_ILLEGALASSOCIATION;
  1652. if (sopInstanceUID.empty() || (reqDataset == NULL))
  1653. return DIMSE_NULLKEY;
  1654. // Prepare DIMSE data structures for issuing request
  1655. OFCondition cond;
  1656. OFString tempStr;
  1657. T_ASC_PresentationContextID pcid = presID;
  1658. T_DIMSE_Message request;
  1659. T_DIMSE_N_EventReportRQ &eventReportReq = request.msg.NEventReportRQ;
  1660. DcmDataset *statusDetail = NULL;
  1661. request.CommandField = DIMSE_N_EVENT_REPORT_RQ;
  1662. // Generate a new message ID
  1663. eventReportReq.MessageID = nextMessageID();
  1664. eventReportReq.DataSetType = DIMSE_DATASET_PRESENT;
  1665. eventReportReq.EventTypeID = eventTypeID;
  1666. // Determine SOP Class from presentation context
  1667. OFString abstractSyntax, transferSyntax;
  1668. findPresentationContext(pcid, abstractSyntax, transferSyntax);
  1669. if (abstractSyntax.empty() || transferSyntax.empty())
  1670. return DIMSE_NOVALIDPRESENTATIONCONTEXTID;
  1671. OFStandard::strlcpy(eventReportReq.AffectedSOPClassUID, abstractSyntax.c_str(), sizeof(eventReportReq.AffectedSOPClassUID));
  1672. OFStandard::strlcpy(eventReportReq.AffectedSOPInstanceUID, sopInstanceUID.c_str(), sizeof(eventReportReq.AffectedSOPInstanceUID));
  1673. // Send request
  1674. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1675. {
  1676. DCMNET_INFO("Sending N-EVENT-REPORT Request");
  1677. // Output dataset only if trace level is enabled
  1678. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::TRACE_LOG_LEVEL))
  1679. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_OUTGOING, reqDataset, pcid));
  1680. else
  1681. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_OUTGOING, NULL, pcid));
  1682. } else {
  1683. DCMNET_INFO("Sending N-EVENT-REPORT Request (MsgID " << eventReportReq.MessageID << ")");
  1684. }
  1685. cond = sendDIMSEMessage(pcid, &request, reqDataset);
  1686. if (cond.bad())
  1687. {
  1688. DCMNET_ERROR("Failed sending N-EVENT-REPORT request: " << DimseCondition::dump(tempStr, cond));
  1689. return cond;
  1690. }
  1691. // Receive response
  1692. T_DIMSE_Message response;
  1693. cond = receiveDIMSECommand(&pcid, &response, &statusDetail, NULL /* commandSet */);
  1694. if (cond.bad())
  1695. {
  1696. DCMNET_ERROR("Failed receiving DIMSE response: " << DimseCondition::dump(tempStr, cond));
  1697. return cond;
  1698. }
  1699. // Check command set
  1700. if (response.CommandField == DIMSE_N_EVENT_REPORT_RSP)
  1701. {
  1702. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1703. {
  1704. DCMNET_INFO("Received N-EVENT-REPORT Response");
  1705. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_INCOMING, NULL, pcid));
  1706. } else {
  1707. DCMNET_INFO("Received N-EVENT-REPORT Response (" << DU_neventReportStatusString(response.msg.NEventReportRSP.DimseStatus) << ")");
  1708. }
  1709. } else {
  1710. DCMNET_ERROR("Expected N-EVENT-REPORT response but received DIMSE command 0x"
  1711. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1712. << OFstatic_cast(unsigned int, response.CommandField));
  1713. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_INCOMING, NULL, pcid));
  1714. delete statusDetail;
  1715. return DIMSE_BADCOMMANDTYPE;
  1716. }
  1717. if (statusDetail != NULL)
  1718. {
  1719. DCMNET_DEBUG("Response has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  1720. delete statusDetail;
  1721. }
  1722. // Set return value
  1723. T_DIMSE_N_EventReportRSP &eventReportRsp = response.msg.NEventReportRSP;
  1724. rspStatusCode = eventReportRsp.DimseStatus;
  1725. // Check whether there is a dataset to be received
  1726. if (eventReportRsp.DataSetType == DIMSE_DATASET_PRESENT)
  1727. {
  1728. // this should never happen
  1729. DcmDataset *tempDataset = NULL;
  1730. T_ASC_PresentationContextID tempID;
  1731. cond = receiveDIMSEDataset(&tempID, &tempDataset);
  1732. if (cond.good())
  1733. {
  1734. DCMNET_WARN("Received unexpected dataset after N-EVENT-REPORT response, ignoring");
  1735. delete tempDataset;
  1736. } else {
  1737. DCMNET_ERROR("Failed receiving unexpected dataset after N-EVENT-REPORT response: "
  1738. << DimseCondition::dump(tempStr, cond));
  1739. return DIMSE_BADDATA;
  1740. }
  1741. }
  1742. // Check whether the message ID being responded to is equal to the message ID of the request
  1743. if (eventReportRsp.MessageIDBeingRespondedTo != eventReportReq.MessageID)
  1744. {
  1745. DCMNET_ERROR("Received response with wrong message ID (" << eventReportRsp.MessageIDBeingRespondedTo
  1746. << " instead of " << eventReportReq.MessageID << ")");
  1747. return DIMSE_BADMESSAGE;
  1748. }
  1749. return cond;
  1750. }
  1751. // Receives N-EVENT-REPORT request
  1752. OFCondition DcmSCU::handleEVENTREPORTRequest(DcmDataset *&reqDataset,
  1753. Uint16 &eventTypeID,
  1754. const int timeout)
  1755. {
  1756. // Do some basic validity checks
  1757. if (!isConnected())
  1758. return DIMSE_ILLEGALASSOCIATION;
  1759. OFCondition cond;
  1760. OFString tempStr;
  1761. T_ASC_PresentationContextID presID;
  1762. T_ASC_PresentationContextID presIDdset;
  1763. T_DIMSE_Message request;
  1764. T_DIMSE_N_EventReportRQ &eventReportReq = request.msg.NEventReportRQ;
  1765. DcmDataset *dataset = NULL;
  1766. DcmDataset *statusDetail = NULL;
  1767. Uint16 statusCode = 0;
  1768. if (timeout > 0)
  1769. DCMNET_DEBUG("Handle N-EVENT-REPORT request, waiting up to " << timeout << " seconds (only for N-EVENT-REPORT message)");
  1770. else if ((m_dimseTimeout > 0) && (m_blockMode == DIMSE_NONBLOCKING))
  1771. DCMNET_DEBUG("Handle N-EVENT-REPORT request, waiting up to " << m_dimseTimeout << " seconds (default for all DIMSE messages)");
  1772. else
  1773. DCMNET_DEBUG("Handle N-EVENT-REPORT request, waiting an unlimited period of time");
  1774. // Receive request, use specific timeout (if defined)
  1775. cond = receiveDIMSECommand(&presID, &request, &statusDetail, NULL /* commandSet */, timeout);
  1776. if (cond.bad())
  1777. {
  1778. if (cond != DIMSE_NODATAAVAILABLE)
  1779. DCMNET_ERROR("Failed receiving DIMSE request: " << DimseCondition::dump(tempStr, cond));
  1780. return cond;
  1781. }
  1782. // Check command set
  1783. if (request.CommandField == DIMSE_N_EVENT_REPORT_RQ)
  1784. {
  1785. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1786. DCMNET_INFO("Received N-EVENT-REPORT Request");
  1787. else
  1788. DCMNET_INFO("Received N-EVENT-REPORT Request (MsgID " << eventReportReq.MessageID << ")");
  1789. } else {
  1790. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_INCOMING, NULL, presID));
  1791. DCMNET_ERROR("Expected N-EVENT-REPORT request but received DIMSE command 0x"
  1792. << STD_NAMESPACE hex << STD_NAMESPACE setfill('0') << STD_NAMESPACE setw(4)
  1793. << OFstatic_cast(unsigned int, request.CommandField));
  1794. delete statusDetail;
  1795. return DIMSE_BADCOMMANDTYPE;
  1796. }
  1797. if (statusDetail != NULL)
  1798. {
  1799. DCMNET_DEBUG("Request has status detail:" << OFendl << DcmObject::PrintHelper(*statusDetail));
  1800. delete statusDetail;
  1801. }
  1802. // Check if dataset is announced correctly
  1803. if (eventReportReq.DataSetType == DIMSE_DATASET_NULL)
  1804. {
  1805. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_INCOMING, NULL, presID));
  1806. DCMNET_ERROR("Received N-EVENT-REPORT request but no dataset announced, aborting");
  1807. return DIMSE_BADMESSAGE;
  1808. }
  1809. // Receive dataset
  1810. cond = receiveDIMSEDataset(&presIDdset, &dataset);
  1811. if (cond.bad())
  1812. {
  1813. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_INCOMING, NULL, presID));
  1814. return DIMSE_BADDATA;
  1815. }
  1816. // Output dataset only if trace level is enabled
  1817. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::TRACE_LOG_LEVEL))
  1818. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_INCOMING, dataset, presID));
  1819. else
  1820. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, request, DIMSE_INCOMING, NULL, presID));
  1821. // Compare presentation context ID of command and data set
  1822. if (presIDdset != presID)
  1823. {
  1824. DCMNET_ERROR("Presentation Context ID of command (" << OFstatic_cast(unsigned int, presID)
  1825. << ") and data set (" << OFstatic_cast(unsigned int, presIDdset) << ") differ");
  1826. delete dataset;
  1827. return makeDcmnetCondition(DIMSEC_INVALIDPRESENTATIONCONTEXTID, OF_error,
  1828. "DIMSE: Presentation Contexts of Command and Data Set differ");
  1829. }
  1830. // Check the request dataset and return the DIMSE status code to be used
  1831. statusCode = checkEVENTREPORTRequest(eventReportReq, dataset);
  1832. // Send back response
  1833. T_DIMSE_Message response;
  1834. T_DIMSE_N_EventReportRSP &eventReportRsp = response.msg.NEventReportRSP;
  1835. response.CommandField = DIMSE_N_EVENT_REPORT_RSP;
  1836. eventReportRsp.MessageIDBeingRespondedTo = eventReportReq.MessageID;
  1837. eventReportRsp.DimseStatus = statusCode;
  1838. eventReportRsp.DataSetType = DIMSE_DATASET_NULL;
  1839. eventReportRsp.opts = 0;
  1840. eventReportRsp.AffectedSOPClassUID[0] = 0;
  1841. eventReportRsp.AffectedSOPInstanceUID[0] = 0;
  1842. if (DCM_dcmnetGetLogger().isEnabledFor(OFLogger::DEBUG_LOG_LEVEL))
  1843. {
  1844. DCMNET_INFO("Sending N-EVENT-REPORT Response");
  1845. DCMNET_DEBUG(DIMSE_dumpMessage(tempStr, response, DIMSE_OUTGOING, NULL, presID));
  1846. } else {
  1847. DCMNET_INFO("Sending N-EVENT-REPORT Response (" << DU_neventReportStatusString(statusCode) << ")");
  1848. }
  1849. cond = sendDIMSEMessage(presID, &response, NULL /*dataObject*/);
  1850. if (cond.bad())
  1851. {
  1852. DCMNET_ERROR("Failed sending N-EVENT-REPORT response: " << DimseCondition::dump(tempStr, cond));
  1853. delete dataset;
  1854. return cond;
  1855. }
  1856. // Set return values
  1857. reqDataset = dataset;
  1858. eventTypeID = eventReportReq.EventTypeID;
  1859. return cond;
  1860. }
  1861. Uint16 DcmSCU::checkEVENTREPORTRequest(T_DIMSE_N_EventReportRQ & /*eventReportReq*/,
  1862. DcmDataset * /*reqDataset*/)
  1863. {
  1864. // we default to success
  1865. return STATUS_Success;
  1866. }
  1867. /* ************************************************************************* */
  1868. /* General message handling */
  1869. /* ************************************************************************* */
  1870. void DcmSCU::notifySENDProgress(const unsigned long byteCount)
  1871. {
  1872. DCMNET_TRACE("Bytes sent: " << byteCount);
  1873. }
  1874. void DcmSCU::notifyRECEIVEProgress(const unsigned long byteCount)
  1875. {
  1876. DCMNET_TRACE("Bytes received: " << byteCount);
  1877. }
  1878. /* ************************************************************************* */
  1879. /* Various helpers */
  1880. /* ************************************************************************* */
  1881. // Sends a DIMSE command and possibly also instance data to the configured peer DICOM application
  1882. OFCondition DcmSCU::sendDIMSEMessage(const T_ASC_PresentationContextID presID,
  1883. T_DIMSE_Message *msg,
  1884. DcmDataset *dataObject,
  1885. DcmDataset **commandSet)
  1886. {
  1887. if (!isConnected())
  1888. return DIMSE_ILLEGALASSOCIATION;
  1889. if (msg == NULL)
  1890. return DIMSE_NULLKEY;
  1891. OFCondition cond;
  1892. /* call the corresponding DIMSE function to send the message */
  1893. if (m_progressNotificationMode)
  1894. {
  1895. cond = DIMSE_sendMessageUsingMemoryData(m_assoc, presID, msg, NULL /*statusDetail*/, dataObject,
  1896. callbackSENDProgress, this /*callbackData*/, commandSet);
  1897. } else {
  1898. cond = DIMSE_sendMessageUsingMemoryData(m_assoc, presID, msg, NULL /*statusDetail*/, dataObject,
  1899. NULL /*callback*/, NULL /*callbackData*/, commandSet);
  1900. }
  1901. #if 0
  1902. // currently disabled because it is not (yet) needed
  1903. if (cond.good())
  1904. {
  1905. /* create a copy of the current DIMSE command message */
  1906. delete m_openDIMSERequest;
  1907. m_openDIMSERequest = new T_DIMSE_Message;
  1908. memcpy((char*)m_openDIMSERequest, msg, sizeof(*m_openDIMSERequest));
  1909. }
  1910. #endif
  1911. return cond;
  1912. }
  1913. // Receive DIMSE command (excluding dataset!) over the currently open association
  1914. OFCondition DcmSCU::receiveDIMSECommand(T_ASC_PresentationContextID *presID,
  1915. T_DIMSE_Message *msg,
  1916. DcmDataset **statusDetail,
  1917. DcmDataset **commandSet,
  1918. const Uint32 timeout)
  1919. {
  1920. if (!isConnected())
  1921. return DIMSE_ILLEGALASSOCIATION;
  1922. OFCondition cond;
  1923. if (timeout > 0)
  1924. {
  1925. /* call the corresponding DIMSE function to receive the command (use specified timeout)*/
  1926. cond = DIMSE_receiveCommand(m_assoc, DIMSE_NONBLOCKING, timeout, presID,
  1927. msg, statusDetail, commandSet);
  1928. } else {
  1929. /* call the corresponding DIMSE function to receive the command (use default timeout) */
  1930. cond = DIMSE_receiveCommand(m_assoc, m_blockMode, m_dimseTimeout, presID,
  1931. msg, statusDetail, commandSet);
  1932. }
  1933. return cond;
  1934. }
  1935. // Receives one dataset (of instance data) via network from another DICOM application
  1936. OFCondition DcmSCU::receiveDIMSEDataset(T_ASC_PresentationContextID *presID,
  1937. DcmDataset **dataObject)
  1938. {
  1939. if (!isConnected())
  1940. return DIMSE_ILLEGALASSOCIATION;
  1941. OFCondition cond;
  1942. /* call the corresponding DIMSE function to receive the dataset */
  1943. if (m_progressNotificationMode)
  1944. {
  1945. cond = DIMSE_receiveDataSetInMemory(m_assoc, m_blockMode, m_dimseTimeout, presID, dataObject,
  1946. callbackRECEIVEProgress, this /*callbackData*/);
  1947. } else {
  1948. cond = DIMSE_receiveDataSetInMemory(m_assoc, m_blockMode, m_dimseTimeout, presID, dataObject,
  1949. NULL /*callback*/, NULL /*callbackData*/);
  1950. }
  1951. if (cond.good())
  1952. {
  1953. DCMNET_DEBUG("Received dataset on presentation context " << OFstatic_cast(unsigned int, *presID));
  1954. }
  1955. else
  1956. {
  1957. OFString tempStr;
  1958. DCMNET_ERROR("Unable to receive dataset on presentation context "
  1959. << OFstatic_cast(unsigned int, *presID) << ": " << DimseCondition::dump(tempStr, cond));
  1960. }
  1961. return cond;
  1962. }
  1963. void DcmSCU::setMaxReceivePDULength(const unsigned long maxRecPDU)
  1964. {
  1965. m_maxReceivePDULength = maxRecPDU;
  1966. }
  1967. void DcmSCU::setDIMSEBlockingMode(const T_DIMSE_BlockingMode blockingMode)
  1968. {
  1969. m_blockMode = blockingMode;
  1970. }
  1971. void DcmSCU::setAETitle(const OFString &myAETtitle)
  1972. {
  1973. m_ourAETitle = myAETtitle;
  1974. }
  1975. void DcmSCU::setPeerHostName(const OFString &peerHostName)
  1976. {
  1977. m_peer = peerHostName;
  1978. }
  1979. void DcmSCU::setPeerAETitle(const OFString &peerAETitle)
  1980. {
  1981. m_peerAETitle = peerAETitle;
  1982. }
  1983. void DcmSCU::setPeerPort(const Uint16 peerPort)
  1984. {
  1985. m_peerPort = peerPort;
  1986. }
  1987. void DcmSCU::setDIMSETimeout(const Uint32 dimseTimeout)
  1988. {
  1989. m_dimseTimeout = dimseTimeout;
  1990. }
  1991. void DcmSCU::setACSETimeout(const Uint32 acseTimeout)
  1992. {
  1993. m_acseTimeout = acseTimeout;
  1994. }
  1995. void DcmSCU::setAssocConfigFileAndProfile(const OFString &filename,
  1996. const OFString &profile)
  1997. {
  1998. m_assocConfigFilename = filename;
  1999. m_assocConfigProfile = profile;
  2000. }
  2001. void DcmSCU::setStorageDir(const OFString& storeDir)
  2002. {
  2003. m_storageDir = storeDir;
  2004. }
  2005. void DcmSCU::setStorageMode(const DcmStorageMode storageMode)
  2006. {
  2007. m_storageMode = storageMode;
  2008. }
  2009. void DcmSCU::setVerbosePCMode(const OFBool mode)
  2010. {
  2011. m_verbosePCMode = mode;
  2012. }
  2013. void DcmSCU::setDatasetConversionMode(const OFBool mode)
  2014. {
  2015. m_datasetConversionMode = mode;
  2016. }
  2017. void DcmSCU::setProgressNotificationMode(const OFBool mode)
  2018. {
  2019. m_progressNotificationMode = mode;
  2020. }
  2021. /* Get methods */
  2022. OFBool DcmSCU::isConnected() const
  2023. {
  2024. return (m_assoc != NULL) && (m_assoc->DULassociation != NULL);
  2025. }
  2026. Uint32 DcmSCU::getMaxReceivePDULength() const
  2027. {
  2028. return m_maxReceivePDULength;
  2029. }
  2030. OFBool DcmSCU::getTLSEnabled() const
  2031. {
  2032. return OFFalse;
  2033. }
  2034. T_DIMSE_BlockingMode DcmSCU::getDIMSEBlockingMode() const
  2035. {
  2036. return m_blockMode;
  2037. }
  2038. const OFString &DcmSCU::getAETitle() const
  2039. {
  2040. return m_ourAETitle;
  2041. }
  2042. const OFString &DcmSCU::getPeerHostName() const
  2043. {
  2044. return m_peer;
  2045. }
  2046. const OFString &DcmSCU::getPeerAETitle() const
  2047. {
  2048. return m_peerAETitle;
  2049. }
  2050. Uint16 DcmSCU::getPeerPort() const
  2051. {
  2052. return m_peerPort;
  2053. }
  2054. Uint32 DcmSCU::getDIMSETimeout() const
  2055. {
  2056. return m_dimseTimeout;
  2057. }
  2058. Uint32 DcmSCU::getACSETimeout() const
  2059. {
  2060. return m_acseTimeout;
  2061. }
  2062. OFString DcmSCU::getStorageDir() const
  2063. {
  2064. return m_storageDir;
  2065. }
  2066. DcmStorageMode DcmSCU::getStorageMode() const
  2067. {
  2068. return m_storageMode;
  2069. }
  2070. OFBool DcmSCU::getVerbosePCMode() const
  2071. {
  2072. return m_verbosePCMode;
  2073. }
  2074. OFBool DcmSCU::getDatasetConversionMode() const
  2075. {
  2076. return m_datasetConversionMode;
  2077. }
  2078. OFBool DcmSCU::getProgressNotificationMode() const
  2079. {
  2080. return m_progressNotificationMode;
  2081. }
  2082. OFCondition DcmSCU::getDatasetInfo(DcmDataset *dataset,
  2083. OFString &sopClassUID,
  2084. OFString &sopInstanceUID,
  2085. E_TransferSyntax &transferSyntax)
  2086. {
  2087. OFCondition status = EC_IllegalParameter;
  2088. sopClassUID.clear();
  2089. sopInstanceUID.clear();
  2090. transferSyntax = EXS_Unknown;
  2091. if (dataset != NULL)
  2092. {
  2093. // ignore returned condition codes (e.g. EC_TagNotFound)
  2094. dataset->findAndGetOFString(DCM_SOPClassUID, sopClassUID);
  2095. dataset->findAndGetOFString(DCM_SOPInstanceUID, sopInstanceUID);
  2096. transferSyntax = dataset->getOriginalXfer();
  2097. // check return values for validity
  2098. if (sopClassUID.empty())
  2099. status = NET_EC_InvalidSOPClassUID;
  2100. else if (sopInstanceUID.empty())
  2101. status = NET_EC_InvalidSOPInstanceUID;
  2102. else if (transferSyntax == EXS_Unknown)
  2103. status = NET_EC_UnknownTransferSyntax;
  2104. else
  2105. status = EC_Normal;
  2106. }
  2107. return status;
  2108. }
  2109. /* ************************************************************************* */
  2110. /* Callback functions */
  2111. /* ************************************************************************* */
  2112. void DcmSCU::callbackSENDProgress(void *callbackContext,
  2113. const unsigned long byteCount)
  2114. {
  2115. if (callbackContext != NULL)
  2116. OFreinterpret_cast(DcmSCU *, callbackContext)->notifySENDProgress(byteCount);
  2117. }
  2118. void DcmSCU::callbackRECEIVEProgress(void *callbackContext,
  2119. const unsigned long byteCount)
  2120. {
  2121. if (callbackContext != NULL)
  2122. OFreinterpret_cast(DcmSCU *, callbackContext)->notifyRECEIVEProgress(byteCount);
  2123. }
  2124. /* ************************************************************************* */
  2125. /* class RetrieveResponse */
  2126. /* ************************************************************************* */
  2127. void RetrieveResponse::print()
  2128. {
  2129. DCMNET_INFO(" Number of Remaining Suboperations : " << m_numberOfRemainingSubops);
  2130. DCMNET_INFO(" Number of Completed Suboperations : " << m_numberOfCompletedSubops);
  2131. DCMNET_INFO(" Number of Failed Suboperations : " << m_numberOfFailedSubops);
  2132. DCMNET_INFO(" Number of Warning Suboperations : " << m_numberOfWarningSubops);
  2133. }