ctkDcmSCU.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
  22. #if ! ( PACKAGE_VERSION_NUMBER == 360 )
  23. #include "dcmtk/dcmnet/scu.h"
  24. #else
  25. #ifndef SCU_H
  26. #define SCU_H
  27. #include "dcmtk/dcmdata/dctk.h" /* Covers most common dcmdata classes */
  28. #include "dcmtk/dcmnet/dcompat.h"
  29. #include "dcmtk/dcmnet/dimse.h" /* DIMSE network layer */
  30. #include "dcmtk/dcmnet/dcasccff.h" /* For reading a association config file */
  31. #include "dcmtk/dcmnet/dcasccfg.h" /* For holding association config file infos */
  32. #include "dcmtk/ofstd/oflist.h"
  33. /* Remove below if changing to more current DCMTK */
  34. const unsigned short ECC_AlreadyConnected = 0x901;
  35. const unsigned short ECC_NoAcceptablePresentationContexts = 0x902;
  36. const unsigned short ECC_NoPresentationContextsDefined = 0x903;
  37. const unsigned short ECC_InvalidSOPInstanceUID = 0x904;
  38. const unsigned short ECC_InvalidSOPClassUID = 0x905;
  39. const unsigned short ECC_UnknownTransferSyntax = 0x906;
  40. const OFConditionConst ECE_NoAcceptablePresentationContexts( OFM_dcmnet, ECC_NoAcceptablePresentationContexts, OF_error, "No Acceptable Presentation Contexts");
  41. const OFConditionConst ECE_NoPresentationContextsDefined( OFM_dcmnet, ECC_NoPresentationContextsDefined, OF_error, "No Presentation Contexts defined");
  42. const OFConditionConst ECE_InvalidSOPClassUID( OFM_dcmnet, ECC_InvalidSOPClassUID, OF_error, "Invalid SOP Class UID");
  43. const OFConditionConst ECE_InvalidSOPInstanceUID( OFM_dcmnet, ECC_InvalidSOPInstanceUID, OF_error, "Invalid SOP Instance UID");
  44. const OFConditionConst ECE_UnknownTransferSyntax( OFM_dcmnet, ECC_UnknownTransferSyntax, OF_error, "Unknown Transfer Syntax");
  45. const OFConditionConst ECE_AlreadyConnected( OFM_dcmnet, ECC_AlreadyConnected, OF_error, "Already Connected");
  46. const OFCondition NET_EC_AlreadyConnected( ECE_AlreadyConnected);
  47. const OFCondition NET_EC_NoAcceptablePresentationContexts( ECE_NoAcceptablePresentationContexts);
  48. const OFCondition NET_EC_NoPresentationContextsDefined( ECE_NoPresentationContextsDefined);
  49. const OFCondition NET_EC_UnknownTransferSyntax( ECE_UnknownTransferSyntax);
  50. const OFCondition NET_EC_InvalidSOPClassUID( ECE_InvalidSOPClassUID);
  51. const OFCondition NET_EC_InvalidSOPInstanceUID( ECE_InvalidSOPInstanceUID);
  52. /* Remove above if changing to more current DCMTK */
  53. /** Different types of closing an association
  54. */
  55. enum DcmCloseAssociationType
  56. {
  57. /// Release the current association
  58. DCMSCU_RELEASE_ASSOCIATION,
  59. /// Abort the current association
  60. DCMSCU_ABORT_ASSOCIATION,
  61. /// Peer requested release (Aborting)
  62. DCMSCU_PEER_REQUESTED_RELEASE,
  63. /// Peer aborted the association
  64. DCMSCU_PEER_ABORTED_ASSOCIATION
  65. };
  66. /** Storage mode used for DICOM objects received via C-STORE
  67. */
  68. enum DcmStorageMode
  69. {
  70. /// Ignore any objects received via C-STORE
  71. DCMSCU_STORAGE_IGNORE,
  72. /// Try to store the objects to disk
  73. DCMSCU_STORAGE_DISK,
  74. /// Try to store to disk in bit-preserving mode. This is especially useful
  75. /// for huge files that cannot fully be received in memory since the
  76. /// data is directly streamed to disk. Originally, this was introduced for
  77. /// DICOM signatures which can be kept valid this way.
  78. DCMSCU_STORAGE_BIT_PRESERVING
  79. };
  80. /** Base class for C-FIND, C-MOVE and C-GET responses
  81. */
  82. class QRResponse
  83. {
  84. public:
  85. /** Standard constructor.
  86. */
  87. QRResponse() :
  88. m_messageIDRespondedTo(0),
  89. m_affectedSOPClassUID(),
  90. m_dataset(NULL),
  91. m_status(0),
  92. m_statusDetail(NULL) {}
  93. /** Destructor, cleans up internal memory (dataset if present).
  94. */
  95. virtual ~QRResponse() { delete m_dataset; delete m_statusDetail; }
  96. /// The message ID responded to (mandatory response field,
  97. /// equals message ID from request)
  98. Uint16 m_messageIDRespondedTo;
  99. /// Optional response field according to part 7 of the standard
  100. /// If present, equals SOP Class UID from request.
  101. OFString m_affectedSOPClassUID;
  102. /// Conditional response field (NULL if absent). From the standard (2009,
  103. /// part 4, C.4.2.1.4.2), for C-MOVE: In Q/R if no C-STORE sub-operation
  104. /// failed, Failed SOP Instance UID List (0008,0058) is absent and
  105. /// therefore no Data Set shall be sent in the C-MOVE response. Further
  106. /// rules: Statuses of Canceled, Failure, Refused, or Warning shall
  107. /// contain the Failed SOP Instance UID List Attribute; status of
  108. /// Pending shall not.
  109. DcmDataset *m_dataset;
  110. /// The returned DIMSE status (mandatory Response Field)
  111. Uint16 m_status;
  112. /// Status detail (NULL if absent). For some DIMSE return status codes,
  113. /// an additional dataset is sent which gives further information (i.e.
  114. /// in case of warnings or errors).
  115. DcmDataset *m_statusDetail;
  116. private:
  117. Q_DISABLE_COPY(QRResponse);
  118. };
  119. /// Base class representing for single C-GET or C-MOVE response
  120. class RetrieveResponse : public QRResponse
  121. {
  122. public:
  123. /** Standard constructor
  124. */
  125. RetrieveResponse() :
  126. m_numberOfRemainingSubops(0),
  127. m_numberOfCompletedSubops(0),
  128. m_numberOfFailedSubops(0),
  129. m_numberOfWarningSubops(0) {}
  130. /** Destructor, cleans up internal memory
  131. */
  132. virtual ~RetrieveResponse() {}
  133. /** Prints response to INFO log level.
  134. */
  135. void print();
  136. /// Number of remaining sub operations (in Q/R: C-STORE calls).
  137. /// For Q/R MOVE and GET, for status of pending this field shall be filled.
  138. /// For others, the field may be filled.
  139. Uint16 m_numberOfRemainingSubops;
  140. /// Number of successfully completed sub operations (in Q/R: C-STORE calls).
  141. /// For Q/R MOVE and GET, for status of pending this field shall be filled.
  142. /// For others, the field may be filled.
  143. Uint16 m_numberOfCompletedSubops;
  144. /// Number of failed sub operations (in Q/R: C-STORE calls).
  145. /// For Q/R MOVE and GET, for status of pending this field shall be filled.
  146. /// For others, the field may be filled.
  147. Uint16 m_numberOfFailedSubops;
  148. /// Number generated warnings generated by sub operations (in Q/R: C-STORE calls).
  149. /// For Q/R MOVE and GET, for status of pending this field shall be filled.
  150. /// For others, the field may be filled.
  151. Uint16 m_numberOfWarningSubops;
  152. private:
  153. Q_DISABLE_COPY(RetrieveResponse);
  154. };
  155. /** Base class for implementing DICOM Service Class User functionality. The class offers
  156. * support for negotiating associations and sending and receiving arbitrary DIMSE messages
  157. * on that connection. DcmSCU has built-in C-ECHO support so derived classes do not have to
  158. * implement that capability on their own.
  159. * @warning This class is EXPERIMENTAL. Be careful to use it in production environment.
  160. */
  161. class DcmSCU
  162. {
  163. public:
  164. /** Constructor, just initializes internal class members
  165. */
  166. DcmSCU();
  167. /** Virtual destructor
  168. */
  169. virtual ~DcmSCU();
  170. /** Add presentation context to be used for association negotiation
  171. * @param abstractSyntax [in] Abstract syntax name in UID format
  172. * @param xferSyntaxes [in] List of transfer syntaxes to be added for the given abstract
  173. * syntax
  174. * @param role [in] The role to be negotiated
  175. * @return EC_Normal if adding was successful, otherwise error code
  176. */
  177. OFCondition addPresentationContext(const OFString &abstractSyntax,
  178. const OFList<OFString> &xferSyntaxes,
  179. const T_ASC_SC_ROLE role = ASC_SC_ROLE_DEFAULT);
  180. /** Initialize network, i.e.\ prepare for association negotiation. If the SCU is already
  181. * connected, the call will not be successful and the old connection keeps open.
  182. * @return EC_Normal if initialization was successful, otherwise error code.
  183. * NET_EC_AlreadyConnected if SCU is already connected.
  184. */
  185. virtual OFCondition initNetwork();
  186. /** Negotiate association by using presentation contexts and parameters as defined by
  187. * earlier function calls. If negotiation fails, there is no need to close the association
  188. * or to do anything else with this class.
  189. * @return EC_Normal if negotiation was successful, otherwise error code.
  190. * NET_EC_AlreadyConnected if SCU is already connected.
  191. */
  192. virtual OFCondition negotiateAssociation();
  193. /** After negotiation association, this call returns the first usable presentation context
  194. * given the desired abstract syntax and transfer syntax
  195. * @param abstractSyntax [in] The abstract syntax (UID) to look for
  196. * @param transferSyntax [in] The transfer syntax (UID) to look for. If empty, the transfer
  197. * syntax is not checked.
  198. * @return Adequate Presentation context ID that can be used. 0 if none found.
  199. */
  200. T_ASC_PresentationContextID findPresentationContextID(const OFString &abstractSyntax,
  201. const OFString &transferSyntax);
  202. /** After a successful association negotiation, this function is called to return the
  203. * presentation context ID that best matches the desired abstract syntax and transfer
  204. * syntax (TS). The function tries to do the following:
  205. * - If possible finds a presentation context with matching TS
  206. * - Else then tries to find an explicit VR uncompressed TS presentation context
  207. * - Else then tries to find an implicit VR uncompressed TS presentation context
  208. * - Else finally accepts each matching presentation ctx independent of TS.
  209. * @param abstractSyntax [in] The abstract syntax (UID) to look for
  210. * @param transferSyntax [in] The transfer syntax (UID) to look for. If empty, the transfer
  211. * syntax is not checked.
  212. * @return Adequate Presentation context ID that can be used. 0 if no appropriate
  213. * presentation context could be found at all.
  214. */
  215. T_ASC_PresentationContextID findAnyPresentationContextID(const OFString &abstractSyntax,
  216. const OFString &transferSyntax);
  217. /** This function sends a C-ECHO command via network to another DICOM application
  218. * @param presID [in] Presentation context ID to use. A value of 0 lets SCP class tries
  219. * to choose one on its own.
  220. * @return EC_Normal if echo was successful, an error code otherwise
  221. *
  222. */
  223. virtual OFCondition sendECHORequest(const T_ASC_PresentationContextID presID);
  224. /** This function sends a C-STORE request on the currently opened association and receives
  225. * the corresponding response then. If required and supported, the dataset of the SOP
  226. * instance can be converted automatically to the network transfer syntax that was
  227. * negotiated (and is specified by the parameter 'presID'). However, this feature is
  228. * disabled by default. See setDatasetConversionMode() on how to enable it.
  229. * @param presID [in] Contains in the end the ID of the presentation context which
  230. * was specified in the DIMSE command. If 0 is given, the
  231. * function tries to find an approriate presentation context
  232. * itself (based on SOP class and original transfer syntax of
  233. * the 'dicomFile' or 'dataset').
  234. * @param dicomFile [in] The filename of the DICOM file to be sent. Alternatively, a
  235. * dataset can be given in the next parameter. If both are given
  236. * the dataset from the file name is used.
  237. * @param dataset [in] The dataset to be sent. Alternatively, a filename can be
  238. * specified in the previous parameter. If both are given the
  239. * dataset from the filename is used.
  240. * @param rspStatusCode [out] The response status code received. 0 means success, others
  241. * can be found in the DICOM standard.
  242. * @return EC_Normal if request could be issued and response was received successfully,
  243. * error code otherwise. That means that if the receiver sends a response denoting
  244. * failure of the storage request, EC_Normal will be returned.
  245. */
  246. virtual OFCondition sendSTORERequest(const T_ASC_PresentationContextID presID,
  247. const OFString &dicomFile,
  248. DcmDataset *dataset,
  249. Uint16 &rspStatusCode);
  250. /** Sends a C-MOVE Request on given presentation context and receives list of responses.
  251. * The function receives the first response and then calls the function handleMOVEResponse()
  252. * which gets the relevant presentation context together with the response dataset and
  253. * status information. Then it waits again for the next response, if there are more to
  254. * come (i.e. response status is PENDING). In the end, after receiving all responses, the
  255. * full list of responses is returned to the caller. If he is not interested, he just sets
  256. * responses=NULL when calling the function.
  257. * This function can be overwritten by actual SCU implementations but just should work fine
  258. * for most people.
  259. * @param presID [in] The presentation context ID that should be used.
  260. * Must be an odd number.
  261. * @param moveDestinationAETitle [in] The move destination's AE title, i.e.\ the one that
  262. * is used for connection to the storage server.
  263. * @param dataset [in] The dataset containing the information about the
  264. * object(s) to be retrieved.
  265. * @param responses [out] The incoming C-MOVE responses for this request.
  266. * The caller is responsible for providing a non-NULL
  267. * pointer for this case. After receiving the results,
  268. * the caller is responsible for freeing the memory of
  269. * this variable. If NULL is specified, the responses
  270. * will not bereturned to the caller.
  271. * @return EC_Normal if everything went fine, i.e.\ if request could be send and responses
  272. * (with whatever status) could be received.
  273. */
  274. virtual OFCondition sendMOVERequest(const T_ASC_PresentationContextID presID,
  275. const OFString &moveDestinationAETitle,
  276. DcmDataset *dataset,
  277. OFList<RetrieveResponse*> *responses);
  278. /** This is the standard handler for C-MOVE message responses: It just adds up all responses
  279. * it receives and prints a DEBUG message. Therefore, it is called for each response
  280. * received in sendMOVERequest(). The idea is of course to overwrite this function in a
  281. * derived, actual SCU implementation if required. Thus, after each response, the caller of
  282. * sendMOVERequest() can decide on its own whether he wants to cancel the C-MOVE session,
  283. * terminate the association, do something useful or whatever. Thus this function is a more
  284. * object-oriented kind of callback.
  285. * @param presID [in] The presentation context ID where the response was
  286. * received on.
  287. * @param response [in] The C-MOVE response received.
  288. * @param waitForNextResponse [out] Denotes whether SCU should try to receive another
  289. * response. If set to OFTrue, then sendMOVERequest() will
  290. * continue waiting for responses. The current
  291. * implementation does that for all responses do not have
  292. * status Failed, Warning, Success or unknown. If set to
  293. * OFFalse, sendMOVERequest() will return control to the
  294. * caller.
  295. * @return EC_Normal, if response could be handled. Error code otherwise.
  296. * The current implementation always returns EC_Normal.
  297. */
  298. virtual OFCondition handleMOVEResponse(const T_ASC_PresentationContextID presID,
  299. RetrieveResponse *response,
  300. OFBool &waitForNextResponse);
  301. /** Sends a C-GET Request on given presentation context and receives list of responses. It
  302. * then switches control to the function handleCGETSession().
  303. * The full list of responses is returned to the caller. If he is not interested, he can
  304. * set responses=NULL when calling the function.
  305. * This function can be overwritten by actual SCU implementations but just should work fine
  306. * for most people.
  307. * @param presID [in] The presentation context ID that should be used. Must be an odd
  308. * number.
  309. * @param dataset [in] The dataset containing the information about the
  310. * object(s) to be retrieved
  311. * @param responses [out] The incoming C-GET responses for this request. If the caller
  312. * specifies NULL, no responses will be returned; otherwise there
  313. * should be at least one final C-GET response (mandatory). C-GET
  314. * responses after each DICOM object received are optional and may
  315. * have been ommitted by the server.
  316. * @return EC_Normal if everything went fine, i.e.\ if request could be sent and expected
  317. * responses (with whatever status) could be received.
  318. */
  319. virtual OFCondition sendCGETRequest(const T_ASC_PresentationContextID presID,
  320. DcmDataset *dataset,
  321. OFList<RetrieveResponse*> *responses);
  322. /** Does the logic for switching between C-GET Response and C-STORE Requests. Sends a C-GET
  323. * Request on given presentation context and receives list of responses. Ihe full list of
  324. * responses is returned to the caller. If he is not interested, he can set responses=NULL
  325. * when calling the function. After sending a C-GET Request, there might be two different
  326. * responses coming in: C-GET-RSP (optional after each received object and mandatory after
  327. * the last object) or a mandatory C-STORE for each incoming object that is received due to
  328. * the request. This function therefore either calls handleCGETResponse() or
  329. * handleSTORERequest() in order to deal with the incoming message. All other messages lead
  330. * to an error within this handler.
  331. * This function can be overwritten by actual SCU implementations but just should work fine
  332. * for most people.
  333. * @param presID [in] The presentation context ID that should be used. Must be an odd
  334. * number.
  335. * @param dataset [in] The dataset containing the information about the object(s) to be
  336. * retrieved
  337. * @param responses [out] The incoming C-GET responses for this request. If the caller
  338. * specifies NULL, no responses will be returned; otherwise there
  339. * should be at least one final C-GET response (mandatory). C-GET
  340. * responses after each DICOM object received are optional and may
  341. * have been ommitted by the server.
  342. * @return EC_Normal if everything went fine, i.e.\ if request could be send
  343. * and expected responses (with whatever status) could be received.
  344. */
  345. virtual OFCondition handleCGETSession(const T_ASC_PresentationContextID presID,
  346. DcmDataset *dataset,
  347. OFList<RetrieveResponse*> *responses);
  348. /** Function handling a single C-GET Response. This standard handler reads the status of the
  349. * response and decides whether to receive any further messages related to the original
  350. * C-GET Request or whether the last response was received or an error occured.
  351. * @param presID [in] The presentation context the C-GET Response was
  352. * received on.
  353. * @param response [in] The response received
  354. * @param continueCGETSession [out] Defines whether it is decided to wait for further C-GET
  355. * Responses/C-STORE Requests within this C-GET session
  356. * @return If no errors occur (dataset response NULL, SCU not connected), this method will
  357. * return EC_Normal, otherwise error code.
  358. */
  359. virtual OFCondition handleCGETResponse(const T_ASC_PresentationContextID presID,
  360. RetrieveResponse* response,
  361. OFBool &continueCGETSession);
  362. /** Function handling a single C-STORE Request. If storage mode is set to disk (default),
  363. * this function is called and the incoming object stored to disk.
  364. * @param presID [in] The presentation context the C-STORE Response was
  365. * received on.
  366. * @param incomingObject [in] The dataset (the object) received
  367. * @param continueCGETSession [out] Defines whether it is decided to wait for further
  368. * C-GET Responses/C-STORE requests within this C-GET
  369. * session.
  370. * @param cStoreReturnStatus [out] Denotes the desired C-STORE return status.
  371. * @return If errors occur (incomingObject NULL or SCU not connected or file could not be
  372. * stored), this method will return an error code otherwise EC_Normal.
  373. */
  374. virtual OFCondition handleSTORERequest(const T_ASC_PresentationContextID presID,
  375. DcmDataset *incomingObject,
  376. OFBool &continueCGETSession,
  377. Uint16 &cStoreReturnStatus);
  378. /** Function handling a single C-STORE Request. If storage mode is set to bit preserving,
  379. * this function is called and the incoming object stored directly to disk, i.e. not stored
  380. * fully in memory.
  381. * @param presID [in] The presentation context the C-STORE Response was received on.
  382. * @param filename [in] The filename to store to
  383. * @param request [in] The incoming C-STORE request command set
  384. * @return If errors occur (incomingObject NULL or SCU not connected filename not
  385. * specified), this method will return an error code otherwise EC_Normal.
  386. */
  387. virtual OFCondition handleSTORERequestFile(T_ASC_PresentationContextID *presID,
  388. const OFString &filename,
  389. T_DIMSE_C_StoreRQ *request);
  390. /** Sends a C-FIND Request on given presentation context and receives list of responses.
  391. * The function receives the first response and then calls the function handleFINDResponse
  392. * which gets the relevant presentation context together with the response dataset and
  393. * status information. Then it waits again for the next response, if there are more to
  394. * come (i.e. response status is PENDING). In the end, after receiving all responses, the
  395. * full list of responses is returned to the caller. If he is not interested, he just sets
  396. * responses=NULL when calling the function.
  397. * This function can be overwritten by actual SCU implementations but just should work fine
  398. * for most people.
  399. * @param presID [in] The presentation context ID that should be used. Must be an odd
  400. * number.
  401. * @param queryKeys [in] The dataset containing the query keys to be searched for on the
  402. * server (SCP).
  403. * @param responses [out] The incoming C-FIND responses for this request. The caller is
  404. * responsible for providing a non-NULL pointer for this case.
  405. * After receiving the results, the caller is responsible for
  406. * freeing the memory of this variable. If NULL is specified, the
  407. * responses will be not returned to the caller.
  408. * @return EC_Normal if everything went fine, i.e.\ if request could be send and responses
  409. * (with whatever status) could be received.
  410. */
  411. virtual OFCondition sendFINDRequest(const T_ASC_PresentationContextID presID,
  412. DcmDataset *queryKeys,
  413. OFList<QRResponse*> *responses);
  414. /** This is the standard handler for C-FIND message responses: It just adds up all responses
  415. * it receives and prints a DEBUG message. Therefore, it is called for each response
  416. * received in sendFINDRequest(). The idea is of course to overwrite this function in a
  417. * derived, actual SCU implementation if required. Thus, after each response, the caller of
  418. * sendFINDRequest() can decide on its own whether he wants to cancel the C-FIND session,
  419. * terminate the association, do something useful or whatever. That way this is a more
  420. * object-oriented kind of callback.
  421. * @param presID [in] The presentation context ID where the response was
  422. * received on.
  423. * @param response [in] The C-FIND response received.
  424. * @param waitForNextResponse [out] Denotes whether SCU should try to receive another
  425. * response. If set to OFTrue, then sendFINDRequest()
  426. * will continue waiting for responses. The current
  427. * implementation does that for all responses do not have
  428. * status SUCESSS. If set to OFFalse, sendFINDRequest()
  429. * will return control to the caller.
  430. * @return EC_Normal, if response could be handled. Error code otherwise.
  431. * The current implementation always returns EC_Normal.
  432. */
  433. virtual OFCondition handleFINDResponse(const T_ASC_PresentationContextID presID,
  434. QRResponse *response,
  435. OFBool &waitForNextResponse);
  436. /** Send C-CANCEL and, therefore, ends the C-FIND -GET or -MOVE session, i.e.\ no further
  437. * responses will be handled. A call to this function only makes sense if an association
  438. * is open, the given presentation context represents a valid C-FIND/GET/MOVE-enabled SOP
  439. * class and usually only, if the last command send on that presentation context was a
  440. * C-FIND message.
  441. * @param presID [in] The presentation context ID where the C-CANCEL should be sent on.
  442. * @return The current implementation always returns EC_Normal.
  443. */
  444. virtual OFCondition sendCANCELRequest(const T_ASC_PresentationContextID presID);
  445. /** This function sends a N-ACTION request on the currently opened association and receives
  446. * the corresponding response then
  447. * @param presID [in] The ID of the presentation context to be used for sending
  448. * the request message. Should not be 0.
  449. * @param sopInstanceUID [in] The requested SOP Instance UID
  450. * @param actionTypeID [in] The action type ID to be used
  451. * @param reqDataset [in] The dataset to be sent
  452. * @param rspStatusCode [out] The response status code received. 0 means success,
  453. * others can be found in the DICOM standard.
  454. * @return EC_Normal if request could be issued and response was received successfully,
  455. * an error code otherwise
  456. */
  457. virtual OFCondition sendACTIONRequest(const T_ASC_PresentationContextID presID,
  458. const OFString &sopInstanceUID,
  459. const Uint16 actionTypeID,
  460. DcmDataset *reqDataset,
  461. Uint16 &rspStatusCode);
  462. /** This function sends N-EVENT-REPORT request and receives the corresponding response
  463. * @param presID [in] The ID of the presentation context to be used for sending
  464. * the request message. Should not be 0.
  465. * @param sopInstanceUID [in] The requested SOP Instance UID
  466. * @param eventTypeID [in] The event type ID to be used
  467. * @param reqDataset [in] The request dataset to be sent
  468. * @param rspStatusCode [out] The response status code received. 0 means success,
  469. * others can be found in the DICOM standard.
  470. * @return EC_Normal if request could be issued and response was received successfully,
  471. * an error code otherwise
  472. */
  473. virtual OFCondition sendEVENTREPORTRequest(const T_ASC_PresentationContextID presID,
  474. const OFString &sopInstanceUID,
  475. const Uint16 eventTypeID,
  476. DcmDataset *reqDataset,
  477. Uint16 &rspStatusCode);
  478. /** Receives N-EVENT-REPORT request on the currently opened association and sends a
  479. * corresponding response. Calls checkEVENTREPORTRequest() in order to determine the
  480. * DIMSE status code to be used for the N-EVENT-REPORT response.
  481. * @param reqDataset [out] Pointer to the dataset received
  482. * @param eventTypeID [out] Event Type ID from the command set received
  483. * @param timeout [in] Optional timeout in seconds for receiving data. This value
  484. * (if not 0) overwrites the standard DIMSE timeout and also
  485. * enables the non-blocking mode for receiving the request.
  486. * @return status, EC_Normal if successful, an error code otherwise
  487. */
  488. virtual OFCondition handleEVENTREPORTRequest(DcmDataset *&reqDataset,
  489. Uint16 &eventTypeID,
  490. const int timeout = 0);
  491. /** Closes the association created by this SCU. Also resets the current association.
  492. * @param closeType [in] Define whether to release or abort the association
  493. */
  494. virtual void closeAssociation(const DcmCloseAssociationType closeType);
  495. /* Set methods */
  496. /** Set maximum PDU length (to be received by SCU)
  497. * @param maxRecPDU [in] The maximum PDU size to use in bytes
  498. */
  499. void setMaxReceivePDULength(const unsigned long maxRecPDU);
  500. /** Set whether to send in DIMSE blocking or non-blocking mode
  501. * @param blockingMode [in] Either blocking or non-blocking mode
  502. */
  503. void setDIMSEBlockingMode(const T_DIMSE_BlockingMode blockingMode);
  504. /** Set SCU's AETitle to be used in association negotiation
  505. * @param myAETtitle [in] The SCU's AETitle to be used
  506. */
  507. void setAETitle(const OFString &myAETtitle);
  508. /** Set SCP's host (hostname or IP address) to talk to in association negotiation
  509. * @param peerHostName [in] The SCP's hostname or IP address to be used
  510. */
  511. void setPeerHostName(const OFString &peerHostName);
  512. /** Set SCP's AETitle to talk to in association negotiation
  513. * @param peerAETitle [in] The SCP's AETitle to be used
  514. */
  515. void setPeerAETitle(const OFString &peerAETitle);
  516. /** Set SCP's port number to connect to for association negotiation
  517. * @param peerPort [in] The SCP's port number
  518. */
  519. void setPeerPort(const Uint16 peerPort);
  520. /** Set timeout for receiving DIMSE messages
  521. * @param dimseTimeout [in] DIMSE Timeout in seconds for receiving data. If the blocking
  522. * mode is DIMSE_NONBLOCKING and we are trying to read data from
  523. * the incoming socket stream and no data has been received.
  524. */
  525. void setDIMSETimeout(const Uint32 dimseTimeout);
  526. /** Set timeout for receiving ACSE messages
  527. * @param acseTimeout [in] ACSE Timeout in seconds used by timer for message timeouts
  528. * during association negotiation
  529. */
  530. void setACSETimeout(const Uint32 acseTimeout);
  531. /** Set an association configuration file and profile to be used
  532. * @param filename [in] File name of the association configuration file
  533. * @param profile [in] Profile inside the association negotiation file
  534. */
  535. void setAssocConfigFileAndProfile(const OFString &filename,
  536. const OFString &profile);
  537. /** Set the directory that should be used by the standard C-GET handler to store objects
  538. * that come in with the corresponding C-STORE rqeuests
  539. * @param storeDir [in] The directory to store to. It is checked in handleSTORERequest()
  540. * whether the directory is writeable and readable. Per default, the
  541. * received objects are stored in the current working directory.
  542. */
  543. void setStorageDir(const OFString &storeDir);
  544. /** Set the storage mode to be used. Default is DCMSCU_STORAGE_DISK.
  545. * @param storageMode The storage mode to be used.
  546. */
  547. void setStorageMode(const DcmStorageMode storageMode);
  548. /** Set whether to show presentation contexts in verbose or debug mode
  549. * @param mode [in] Show presentation contexts in verbose mode if OFTrue. By default, the
  550. * presentation contexts are shown in debug mode.
  551. */
  552. void setVerbosePCMode(const OFBool mode);
  553. /** Set the mode that specifies whether the transfer syntax of the dataset can be changed
  554. * for network transmission. This mainly covers the compression/decompression of datasets,
  555. * which is disabled by default.
  556. * @param mode [in] Allow dataset conversion if OFTrue
  557. */
  558. void setDatasetConversionMode(const OFBool mode);
  559. /** Set the mode that specifies whether the progress of sending and receiving DIMSE
  560. * messages is notified by calling notifySENDProgress() and notifyRECEIVEProgress(),
  561. * respectively. The progress notification is enabled by default.
  562. * @param mode [in] Disable progress notification if OFFalse
  563. */
  564. void setProgressNotificationMode(const OFBool mode);
  565. /* Get methods */
  566. /** Get current connection status
  567. * @return OFTrue if SCU is currently connected, OFFalse otherwise
  568. */
  569. OFBool isConnected() const;
  570. /** Returns maximum PDU length configured to be received by SCU
  571. * @return Maximum PDU length in bytes
  572. */
  573. Uint32 getMaxReceivePDULength() const;
  574. /** Returns whether DIMSE messaging is configured to be blocking or unblocking
  575. * @return The blocking mode configured
  576. */
  577. T_DIMSE_BlockingMode getDIMSEBlockingMode() const;
  578. /** Returns the SCU's own configured AETitle
  579. * @return The AETitle configured for this SCU
  580. */
  581. const OFString &getAETitle() const;
  582. /** Returns the SCP's (peer's) host name configured
  583. * @return The hostname (or IP) configured to be talked to
  584. */
  585. const OFString &getPeerHostName() const;
  586. /** Returns the SCP's (peer's) AETitle configured
  587. * @return The AETitle configured to be talked to
  588. */
  589. const OFString &getPeerAETitle() const;
  590. /** Returns the SCP's (peer's) TCP/IP port configured
  591. * @return The port configured to talked to
  592. */
  593. Uint16 getPeerPort() const;
  594. /** Returns the DIMSE timeout configured defining how long SCU will wait for DIMSE responses
  595. * @return The DIMSE timeout configured
  596. */
  597. Uint32 getDIMSETimeout() const;
  598. /** Returns the timeout configured defining how long SCU will wait for messages during ACSE
  599. * messaging (association negotiation)
  600. * @return The ACSE timeout configured
  601. */
  602. Uint32 getACSETimeout() const;
  603. /** Returns the storage directory used for storing objects received with C-STORE requests
  604. * in the context of C-GET sessions. Default is empty string which refers to the current
  605. * working directory.
  606. * @return The storage directory
  607. */
  608. OFString getStorageDir() const;
  609. /** Returns the storage mode enabled
  610. * @return The storage mode enabled
  611. */
  612. DcmStorageMode getStorageMode() const;
  613. /** Returns the verbose presentation context mode configured specifying whether details
  614. * on the presentation contexts (negotiated during association setup) should be shown in
  615. * verbose or debug mode. The latter is the default.
  616. * @return The current verbose presentation context mode. Show details on the
  617. * presentation contexts on INFO log level (verbose) if OFTrue and on DEBUG
  618. * level if OFFalse.
  619. */
  620. OFBool getVerbosePCMode() const;
  621. /** Returns the mode that specifies whether the transfer syntax of the dataset can be
  622. * changed for network transmission. This mainly covers the compression/decompression
  623. * of datasets, which is disabled by default.
  624. * @return The current dataset conversion mode, enabled if OFTrue
  625. */
  626. OFBool getDatasetConversionMode() const;
  627. /** Returns the mode that specifies whether the progress of sending and receiving DIMSE
  628. * messages is notified by calling notifySENDProgress() and notifyRECEIVEProgress(),
  629. * respectively. The progress notification is enabled by default.
  630. * @return The current progress notification mode, enabled if OFTrue
  631. */
  632. OFBool getProgressNotificationMode() const;
  633. /** Returns whether SCU is configured to create a TLS connection with the SCP
  634. * @return OFFalse for this class but may be overridden by derived classes
  635. */
  636. OFBool getTLSEnabled() const;
  637. /** Deletes internal networking structures from memory */
  638. void freeNetwork();
  639. protected:
  640. /** Sends a DIMSE command and possibly also a dataset from a data object via network to
  641. * another DICOM application
  642. * @param presID [in] Presentation context ID to be used for message
  643. * @param msg [in] Structure that represents a certain DIMSE command which
  644. * shall be sent
  645. * @param dataObject [in] The instance data which shall be sent to the other DICOM
  646. * application; NULL, if there is none
  647. * @param commandSet [out] If this parameter is not NULL it will return a copy of the
  648. * DIMSE command which is sent to the other DICOM application
  649. * @return EC_Normal if sending request was successful, an error code otherwise
  650. */
  651. OFCondition sendDIMSEMessage(const T_ASC_PresentationContextID presID,
  652. T_DIMSE_Message *msg,
  653. DcmDataset *dataObject,
  654. DcmDataset **commandSet = NULL);
  655. /** Returns SOP Class UID, SOP Instance UID and original transfer syntax for a given dataset.
  656. * If the dataset is NULL, all returned values will be undefined (i.e. empty or EXS_Unknown).
  657. * @param dataset [in] The dataset to read from
  658. * @param sopClassUID [out] The value of attribute SOP Class UID if present
  659. * @param sopInstanceUID [out] The value of attribute SOP Instance UID if present
  660. * @param transferSyntax [out] The value of transfer syntax that originally was read from
  661. * disk. Will be unknown if the dataset was created in memory.
  662. * @return EC_Normal if all information could be retrieved and is valid, an error code
  663. * otherwise
  664. */
  665. OFCondition getDatasetInfo(DcmDataset *dataset,
  666. OFString &sopClassUID,
  667. OFString &sopInstanceUID,
  668. E_TransferSyntax &transferSyntax);
  669. /** Tells DcmSCU to use a secure TLS connection described by the given TLS layer
  670. * @param tlayer [in] The TLS transport layer including all TLS parameters
  671. * @return EC_Normal if given transport layer is ok, an error code otherwise
  672. */
  673. OFCondition useSecureConnection(DcmTransportLayer *tlayer);
  674. /** Receive DIMSE command (excluding dataset!) over the currently open association
  675. * @param presID [out] Contains in the end the ID of the presentation context
  676. * which was specified in the DIMSE command received
  677. * @param msg [out] The message received
  678. * @param statusDetail [out] If a non-NULL value is passed this variable will in the end
  679. * contain detailed information with regard to the status
  680. * information which is captured in the status element
  681. * (0000,0900). Note that the value for element (0000,0900) is
  682. * not contained in this return value but in internal msg. For
  683. * details on the structure of this object, see DICOM standard
  684. * part 7, annex C).
  685. * @param commandSet [out] If this parameter is not NULL, it will return a copy of the
  686. * DIMSE command which was received from the other DICOM
  687. * application. The caller is responsible to de-allocate the
  688. * returned object!
  689. * @param timeout [in] If this parameter is not 0, it specifies the timeout (in
  690. * seconds) to be used for receiving the DIMSE command.
  691. * Otherwise, the default timeout value is used (see
  692. * setDIMSETimeout()).
  693. * @return EC_Normal if command could be received successfully, an error code otherwise
  694. */
  695. OFCondition receiveDIMSECommand(T_ASC_PresentationContextID *presID,
  696. T_DIMSE_Message *msg,
  697. DcmDataset **statusDetail,
  698. DcmDataset **commandSet = NULL,
  699. const Uint32 timeout = 0);
  700. /** Receives one dataset (of instance data) via network from another DICOM application
  701. * @param presID [out] Contains in the end the ID of the presentation context which
  702. * was used in the PDVs that were received on the network. If the
  703. * PDVs show different presentation context IDs, this function
  704. * will return an error.
  705. * @param dataObject [out] Contains in the end the information which was received over
  706. * the network
  707. * @return EC_Normal if dataset could be received successfully, an error code otherwise
  708. */
  709. OFCondition receiveDIMSEDataset(T_ASC_PresentationContextID *presID,
  710. DcmDataset **dataObject);
  711. /** clear list of presentation contexts. In addition, any currently selected association
  712. * configuration file is disabled.
  713. */
  714. void clearPresentationContexts();
  715. /** After negotiation association, this call returns the presentation context belonging
  716. * to the given presentation context ID
  717. * @param presID [in] The presentation context ID to look for
  718. * @param abstractSyntax [out] The abstract syntax (UID) for that ID.
  719. * Empty, if such a presentation context does not exist.
  720. * @param transferSyntax [out] The transfer syntax (UID) for that ID.
  721. * Empty, if such a presentation context does not exist.
  722. */
  723. void findPresentationContext(const T_ASC_PresentationContextID presID,
  724. OFString &abstractSyntax,
  725. OFString &transferSyntax);
  726. /* ***********************************************************************
  727. * Functions particularly interesting for overwriting in derived classes
  728. * *********************************************************************** */
  729. /** This function is called if an object was received due to a C-GET request and can be
  730. * overwritten by a user in order to be informed about such an event. The default
  731. * implementation just prints a DEBUG message. Note that this function is not called if
  732. * the SCU is in storage mode DCMSCU_STORAGE_IGNORE.
  733. * @param filename [in] The filename written
  734. * @param sopClassUID [in] The SOP Class UID of the object written
  735. * @param sopInstanceUID [in] The SOP Instance UID of the object written
  736. */
  737. virtual void notifyInstanceStored(const OFString &filename,
  738. const OFString &sopClassUID,
  739. const OFString &sopInstanceUID) const;
  740. /** This function is called while sending DIMSE messages, i.e.\ on each PDV of a dataset.
  741. * The default implementation just prints a TRACE message on the number of bytes sent so
  742. * far. By overwriting this method, the progress of the send process can be shown to the
  743. * user in a more appropriate way. The progress notification can also be disabled
  744. * completely by calling setProgressNotificationMode().
  745. * @param byteCount [in] Number of bytes sent so far
  746. */
  747. virtual void notifySENDProgress(const unsigned long byteCount);
  748. /** This function is called while receiving DIMSE messages, i.e.\ on each PDV of a dataset.
  749. * The default implementation just prints a TRACE message on the number of bytes received
  750. * so far. By overwriting this method, the progress of the receive process can be shown to
  751. * the user in a more appropriate way. The progress notification can also be disabled
  752. * completely by calling setProgressNotificationMode().
  753. * @param byteCount [in] Number of bytes received so far
  754. */
  755. virtual void notifyRECEIVEProgress(const unsigned long byteCount);
  756. /** Check given N-EVENT-REPORT request and dataset for validity. This method is called by
  757. * handleEVENTREPORTRequest() before sending the response in order to determine the
  758. * DIMSE status code to be used for the response message.
  759. * @param request [in] The N-EVENT-REPORT request message data structure
  760. * @param reqDataset [in] The N-EVENT-REPORT request dataset received. Might be NULL.
  761. * @return DIMSE status code to be used for the N-EVENT-REPORT response.
  762. * Always returns STATUS_Success (0). Derived classes should, therefore,
  763. * overwrite this method and return a more appropriate value based on the
  764. * result of the checks performed.
  765. */
  766. virtual Uint16 checkEVENTREPORTRequest(T_DIMSE_N_EventReportRQ &request,
  767. DcmDataset *reqDataset);
  768. /** Sends back a C-STORE response on the given presentation context, with the designated
  769. * status, fitting the corresponding C-STORE request.
  770. * @param presID [in] The presentation context ID to be used.
  771. * @param status [in] The storage DIMSE status to be used.
  772. * @param request [in] The C-STORE request that should be responded to.
  773. * @result EC_Normal if the response could be sent, error otherwise.
  774. */
  775. virtual OFCondition sendSTOREResponse(T_ASC_PresentationContextID presID,
  776. Uint16 status,
  777. const T_DIMSE_C_StoreRQ &request);
  778. /** Helper function that generates a storage filename by extracting SOP Class and SOP
  779. * Instance UID from a dataset and combining that with the configured storage directory.
  780. * The SOP class is used to create an initial two letter abbreviation for the
  781. * corresponding modality, e.g. CT. An example for a storage filename created by this
  782. * function is /storage_dir/CT.1.2.3.4.5 for a CT with SOP Instance UID 1.2.3.4.
  783. * This function might be overwritten to change the filename behaviour completely. This
  784. * function is only called if the SCU is in DCMSCU_STORAGE_DISK mode.
  785. * @param dataset [in] The dataset that should be stored to disk
  786. * @result Non-empty string if successful, otherwise empty string.
  787. */
  788. virtual OFString createStorageFilename(DcmDataset *dataset);
  789. /** Receives a DICOM dataset on a given presentation context ID but does not store it in
  790. * memory or disk, thus ignoring it.
  791. * @param presID [in] The presentation context to be used
  792. * @param request [in] The corresponding C-STORE request
  793. * @return EC_Normal if ignoring worked, error code otherwise.
  794. */
  795. virtual OFCondition ignoreSTORERequest(T_ASC_PresentationContextID presID,
  796. const T_DIMSE_C_StoreRQ &request);
  797. /* Callback functions (static) */
  798. /** Callback function used for sending DIMSE messages.
  799. * @param callbackContext [in] The desired user callback data
  800. * @param byteCount [in] Progress bytes count
  801. */
  802. static void callbackSENDProgress(void *callbackContext,
  803. unsigned long byteCount);
  804. /** Callback function used for receiving DIMSE messages.
  805. * @param callbackContext [in] The desired user callback data
  806. * @param byteCount [in] Progress bytes count
  807. */
  808. static void callbackRECEIVEProgress(void *callbackContext,
  809. unsigned long byteCount);
  810. private:
  811. Q_DISABLE_COPY(DcmSCU);
  812. /// Associaton of this SCU. This class only handles 1 association at a time.
  813. T_ASC_Association *m_assoc;
  814. /// The DICOM network the association is based on
  815. T_ASC_Network *m_net;
  816. /// Association parameters
  817. T_ASC_Parameters *m_params;
  818. /// Configuration file for presentation contexts (optional)
  819. OFString m_assocConfigFilename;
  820. /// Profile in configuration file that should be used (optional)
  821. OFString m_assocConfigProfile;
  822. /// Defines presentation context, consisting of one abstract syntax name
  823. /// and a list of transfer syntaxes for this abstract syntax
  824. struct DcmSCUPresContext {
  825. /** Default constructor
  826. */
  827. DcmSCUPresContext()
  828. : abstractSyntaxName()
  829. , transferSyntaxes()
  830. , roleSelect(ASC_SC_ROLE_DEFAULT)
  831. {
  832. }
  833. /// Abstract Syntax Name of Presentation Context
  834. OFString abstractSyntaxName;
  835. /// List of Transfer Syntaxes for Presentation Context
  836. OFList<OFString> transferSyntaxes;
  837. /// Role Selection
  838. T_ASC_SC_ROLE roleSelect;
  839. };
  840. /// List of presentation contexts that should be negotiated
  841. OFList<DcmSCUPresContext> m_presContexts;
  842. /// Configuration file containing association parameters
  843. OFString m_assocConfigFile;
  844. /// The last DIMSE successfully sent, unresponded DIMSE request
  845. T_DIMSE_Message *m_openDIMSERequest;
  846. /// Maximum PDU size
  847. Uint32 m_maxReceivePDULength;
  848. /// DIMSE blocking mode
  849. T_DIMSE_BlockingMode m_blockMode;
  850. /// AEtitle of this application
  851. OFString m_ourAETitle;
  852. /// Peer hostname
  853. OFString m_peer;
  854. /// AEtitle of remote application
  855. OFString m_peerAETitle;
  856. /// Port of remote application entity
  857. Uint16 m_peerPort;
  858. /// DIMSE timeout
  859. Uint32 m_dimseTimeout;
  860. /// ACSE timeout
  861. Uint32 m_acseTimeout;
  862. /// Storage directory for objects received with C-STORE due to a
  863. /// running C-GET session. Per default, the received objects
  864. /// are stored in the current working directory.
  865. OFString m_storageDir;
  866. /// Set whether bit preserving storage should be enabled, i.e.\ any objects
  867. /// retrieved via C-STORE should be written directly to disk without
  868. /// any data correction/re-computation (e.g.\ group length calculations,
  869. /// padding, etc.). This is especially interesting for retaining valid
  870. /// signatures, and also to receive huge files which cannot be fully received
  871. /// in memory. Default is OFFalse (no bit preserving) storage.
  872. DcmStorageMode m_storageMode;
  873. /// Verbose PC mode
  874. OFBool m_verbosePCMode;
  875. /// Dataset conversion mode
  876. OFBool m_datasetConversionMode;
  877. /// Progress notification mode
  878. OFBool m_progressNotificationMode;
  879. /** Returns next available message ID free to be used by SCU
  880. * @return Next free message ID
  881. */
  882. Uint16 nextMessageID();
  883. };
  884. #endif // DCMTK 3.6.0
  885. #endif // SCU_H