# HG changeset patch # User Alain Mazy # Date 1653923775 -7200 # Node ID 450f0efebe4f55e953b16d7b494df5defe84372d # Parent f282da89c1c1910e7a7e80c8ead2bc0584470e17 FilterIncomingCStoreInstance python sample diff -r f282da89c1c1 -r 450f0efebe4f Sphinx/source/plugins/python.rst --- a/Sphinx/source/plugins/python.rst Mon May 30 11:07:58 2022 +0200 +++ b/Sphinx/source/plugins/python.rst Mon May 30 17:16:15 2022 +0200 @@ -784,6 +784,19 @@ :language: python +Filtering incoming C-Store instances (new in 4.0) +................................................. + +Starting with release 4.0 of the Python plugin, it is possible to +filter instances received from C-Store and return a specific error +code to the sending modality. + +This can be used, e.g, to implement a quota per modality or return +an ``out-of-resources`` status if the Orthanc storage is almost full. + +.. literalinclude:: python/incoming-cstore-filter.py + :language: python + Performance and concurrency --------------------------- diff -r f282da89c1c1 -r 450f0efebe4f Sphinx/source/plugins/python/incoming-cstore-filter.py --- a/Sphinx/source/plugins/python/incoming-cstore-filter.py Mon May 30 11:07:58 2022 +0200 +++ b/Sphinx/source/plugins/python/incoming-cstore-filter.py Mon May 30 17:16:15 2022 +0200 @@ -1,15 +1,30 @@ import json import orthanc -def Filter(receivedDicom): + +# this script accepts 3 instances from STORESCU and then, rejects the next ones + +storeScuInstanceCounter = 0 + +def FilterIncomingCStoreInstance(receivedDicom): # The list ofvalid status codes for DIMSE C-STORE can be found: # https://dicom.nema.org/medical/Dicom/2021e/output/chtml/part04/sect_B.2.3.html - tags = json.loads(receivedDicom.GetInstanceSimplifiedJson()) - if tags['PatientID'].startswith('001-'): - # Non-zero return value: The DICOM instance is discarded - return 0xA900 # DIMSE Failure: Data Set does not match SOP Class - else: - return 0 # Success: Accept the DICOM instance + global storeScuInstanceCounter + + origin = receivedDicom.GetInstanceOrigin() + + if origin == orthanc.InstanceOrigin.DICOM_PROTOCOL: # should always be true in the CStore callback ! -orthanc.RegisterIncomingCStoreInstanceFilter(Filter) + remoteAet = receivedDicom.GetInstanceRemoteAet() + + if remoteAet == "STORESCU": + storeScuInstanceCounter += 1 + + if storeScuInstanceCounter > 3: + # Non-zero return value: The DICOM instance is discarded + return 0xA700 + + return 0 # Success: Accept the DICOM instance + +orthanc.RegisterIncomingCStoreInstanceFilter(FilterIncomingCStoreInstance)