LaurelBridge.DCF.Examples.StorageCommitmentSCU Namespace
LAUREL BRIDGE

LaurelBridge.DCF.Examples.StorageCommitmentSCU Namespace

DICOM Connectivity Framework V3.4
The StorageCommitmentSCU example demonstrates how to use DCF to implement a storage commitment service class user.
Classes

  ClassDescription
Public classProgram
Storage commitment service class user example which should be run with the StorageCommitmentSCP example.
Examples

StorageCommitmentSCU Sample Code
public class Program
{
    private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

    /// <summary>
    /// Main entry point for StorageCommitmentSCU.
    /// </summary>
    [STAThread]
    public static void Main()
    {
        try
        {
            // Start an AssociationManager and listen for NEventReport requests as a result of this
            // SCU sending an NAction request to the StorageCommitment SCP.
            NEventReportServer nEventServer = new NEventReportServer(null);
            AssociationManager mgr = new AssociationManager();
            mgr.ServerTcpPort = 105;
            mgr.AssociationConfigPolicyMgr = nEventServer;
            mgr.AddAssociationListener(nEventServer);

            Thread t = new Thread(mgr.Run);
            t.Start();
            if (!mgr.WaitForRunning(2000))
            {
                throw new TimeoutException("AssociationManager did not start in an acceptable amount of time");
            }

            Dicom.Store.StorageCommitmentSCU scu = null;

            try
            {
                StorageCommitmentRequest request = CreateStorageCommitmentRq();
                scu = CreateSCU();
                scu.RequestAssociation();

                Console.WriteLine("Outgoing NAction Request: {0}{1}", Environment.NewLine, request);
                DimseMessage response = scu.NAction(request, 30, 30);
                Console.WriteLine("Incoming NAction Response: {0}{1}", Environment.NewLine, response);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught sending nAction request: {0}{1}Please verify the Storage Commitment SCP is up and running", e, Environment.NewLine);
            }
            finally
            {
                if (scu != null && scu.Connected)
                    scu.ReleaseAssociation();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception caught during execution: {0}", e);
        }
    }

    private static Dicom.Store.StorageCommitmentSCU CreateSCU()
    {
        AssociationInfo ainfo = new AssociationInfo();
        ainfo.CallingTitle = "SCU";
        ainfo.CalledTitle = "SCP";
        ainfo.CalledPresentationAddress = "localhost:" + 104;

        // Add the requested presentation context
        RequestedPresentationContext ctx = new RequestedPresentationContext(
            1,
            Uids.StorageCommitmentPushModelSOPClass,
            Uids.ELE, Uids.ILE);

        ainfo.AddRequestedPresentationContext(ctx);
        return new Dicom.Store.StorageCommitmentSCU(ainfo);
    }

    /// <summary>
    /// Build our storage commitment request containing the datasets to be stored.
    /// </summary>
    /// <returns>The StorageCommitmentRequest to be send to the StorageCommitment SCP.</returns>
    private static StorageCommitmentRequest CreateStorageCommitmentRq()
    {
        using (DicomFileInput dfi = new DicomFileInput("mr-knee.dcm", new DicomSessionSettings()))
        {
            DicomDataSet dds = dfi.ReadDataSetNoPixels();
            StorageCommitmentRequest request = new StorageCommitmentRequest();
            request.TransactionUid = DataDictionary.CreateUid();

            List<ReferencedSopSequence> items = new List<ReferencedSopSequence>();
            // item 1: MR-Knee
            ReferencedSopSequence refSopSequenceItem = new ReferencedSopSequence();
            refSopSequenceItem.ReferencedSopClassUid = dds.GetElementStringValue(Tags.SOPClassUID);
            refSopSequenceItem.ReferencedSopInstanceUid = dds.GetElementStringValue(Tags.SOPInstanceUID);
            items.Add(refSopSequenceItem);

            // item 2: Bogus image used to demonstrate failures in the NEventReport sent from the StorageCommitment SCP
            refSopSequenceItem = new ReferencedSopSequence();
            refSopSequenceItem.ReferencedSopClassUid = "1.2.3.4.5";
            refSopSequenceItem.ReferencedSopInstanceUid = "1.2.3.4.5.6.7.8.9";
            items.Add(refSopSequenceItem);

            request.ReferencedSopSequence(items.ToArray());

            return request;
        }
    }

    /// <summary>
    /// Class to handle sending and receiving storage commitment requests/responses.
    /// </summary>
    class NEventReportServer : AssociationListenerAdapter, IAssociationConfigPolicyManager
    {
        readonly IList<AllowedPresentationContext> _presentationContexts;

        /// <summary>
        /// Create a StorageCommitmentSCU to send requests to the StorageCommitmentSCP on localhost
        /// using the given port number.
        /// </summary>
        public NEventReportServer(IList<AllowedPresentationContext> allowedPresentationContexts)
        {
            _presentationContexts = allowedPresentationContexts;
        }

        /// <summary>
        /// Returns a DicomSessionSettings object to be used for the association.
        /// </summary>
        /// <param name="assoc">The AssociationAcceptor for the given association</param>
        /// <returns>A default DicomSessionSettings object</returns>
        public DicomSessionSettings GetSessionSettings(AssociationAcceptor assoc)
        {
            return new DicomSessionSettings();
        }

        /// <summary>
        /// Create a storage commitment SCU message handler to handle the interaction between the SCU and SCP
        /// regarding the status of the storage commitment.
        /// </summary>
        /// <param name="assoc">The AssociationAcceptor for the given association.</param>
        public override void BeginAssociation(AssociationAcceptor assoc)
        {
            assoc.RegisterServiceClassProvider(new StorageCommitmentNEventReportSCP(assoc, _presentationContexts, NEventReport));
        }

        private NEventReportResponse NEventReport(AssociationAcceptor acceptor, NEventReportRequest request)
        {
            Logger.InfoFormat("Incoming NEventReport Request: {0}{1}", Environment.NewLine, request);
            NEventReportResponse response = new NEventReportResponse(request);
            Logger.InfoFormat("Outgoing NEventReport Response: {0}{1}", Environment.NewLine, response);
            return response;
        }
    }
}