context: #region License
  
  // Copyright (c) 2013, ClearCanvas Inc.
  // All rights reserved.
  // http://www.clearcanvas.ca
  //
  // This file is part of the ClearCanvas RIS/PACS open source project.
  //
  // The ClearCanvas RIS/PACS open source project is free software: you can
  // redistribute it and/or modify it under the terms of the GNU General Public
  // License as published by the Free Software Foundation, either version 3 of the
  // License, or (at your option) any later version.
  //
  // The ClearCanvas RIS/PACS open source project is distributed in the hope that it
  // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  // Public License for more details.
  //
  // You should have received a copy of the GNU General Public License along with
  // the ClearCanvas RIS/PACS open source project.  If not, see
  // <http://www.gnu.org/licenses/>.
  
  #endregion
  
  using System;
  using System.IO;
  using System.Net;
  using System.Text;
  using ClearCanvas.Common;
  using ClearCanvas.Dicom.Network;
  using ClearCanvas.Dicom.Utilities.Statistics;
  
  namespace ClearCanvas.Dicom.Samples
  {
      /// <summary>
      /// DICOM Storage SCP Sample Application
      /// </summary>
      class StorageScp : IDicomServerHandler
      {
          #region Private Members
          private static bool _started;
          private static ServerAssociationParameters _staticAssocParameters;
          private AssociationStatisticsRecorder _statsRecorder;
          #endregion
  
          #region Constructors
          private StorageScp(DicomServer server, ServerAssociationParameters assoc)
          {
              _statsRecorder = new AssociationStatisticsRecorder(server); 
          }
          #endregion
  
          #region Public Properties
          public static bool Started
          {
              get { return _started; }
          }
  
          /// <summary>
          /// The path (directory) to store incoming images.
          /// </summary>
          public static string StorageLocation { get; set; }
  
          public static bool Bitbucket { get; set; }
          public static bool List { get; set; }
  
          public static bool JpegLossless { get; set; }
          public static bool JpegLossy { get; set; }
          public static bool Rle { get; set; }
          public static bool J2KLossy { get; set; }
          public static bool J2KLossless { get; set; }
  
          #endregion
  
          #region Private Methods
          private static void SetImageTransferSyntaxes(byte pcid, ServerAssociationParameters assoc)
          {
              if (JpegLossless)
                  assoc.AddTransferSyntax(pcid,TransferSyntax.JpegLosslessNonHierarchicalFirstOrderPredictionProcess14SelectionValue1);
              if (Rle)
                  assoc.AddTransferSyntax(pcid, TransferSyntax.RleLossless);
              if (J2KLossy)
                  assoc.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompression);
              if (J2KLossless)
                  assoc.AddTransferSyntax(pcid, TransferSyntax.Jpeg2000ImageCompressionLosslessOnly);
              if (JpegLossy)
              {
                  assoc.AddTransferSyntax(pcid, TransferSyntax.JpegBaselineProcess1);
                  assoc.AddTransferSyntax(pcid, TransferSyntax.JpegExtendedProcess24);
              }
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
          }
  
          private static void AddPresentationContexts(ServerAssociationParameters assoc)
          {
              byte pcid = assoc.AddPresentationContext(SopClass.VerificationSopClass);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.GrayscaleSoftcopyPresentationStateStorageSopClass);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.KeyObjectSelectionDocumentStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.ComprehensiveSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.BlendingSoftcopyPresentationStateStorageSopClass);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.ColonCadSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.DeformableSpatialRegistrationStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.BasicTextSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.EncapsulatedPdfStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.XRayRadiationDoseSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.ChestCadSrStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.EncapsulatedCdaStorage);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ExplicitVrLittleEndian);
              assoc.AddTransferSyntax(pcid, TransferSyntax.ImplicitVrLittleEndian);
  
              pcid = assoc.AddPresentationContext(SopClass.MrImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.CtImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.SecondaryCaptureImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.UltrasoundImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.UltrasoundImageStorageRetired);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.UltrasoundMultiFrameImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.UltrasoundMultiFrameImageStorageRetired);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.NuclearMedicineImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalIntraOralXRayImageStorageForPresentation);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalIntraOralXRayImageStorageForProcessing);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalMammographyXRayImageStorageForPresentation);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalMammographyXRayImageStorageForProcessing);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalXRayImageStorageForPresentation);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.DigitalXRayImageStorageForProcessing);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.ComputedRadiographyImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.OphthalmicPhotography16BitImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.OphthalmicPhotography8BitImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VideoEndoscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VideoMicroscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VideoPhotographicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VlEndoscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VlMicroscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VlPhotographicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.VlSlideCoordinatesMicroscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.XRayAngiographicBiPlaneImageStorageRetired);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.XRayAngiographicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.XRayRadiofluoroscopicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.XRay3dAngiographicImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.XRay3dCraniofacialImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.OphthalmicTomographyImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedCtImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedMrColorImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedMrImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedPetImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.BreastTomosynthesisImageStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
              pcid = assoc.AddPresentationContext(SopClass.EnhancedUsVolumeStorage);
              SetImageTransferSyntaxes(pcid, assoc);
  
          }
          #endregion
  
          #region Static Public Methods
          public static void StartListening(string aeTitle, int port)
          {
              if (_started)
                  return;
  
              _staticAssocParameters = new ServerAssociationParameters(aeTitle, new IPEndPoint(IPAddress.Any, port));
  
              AddPresentationContexts(_staticAssocParameters);
  
              DicomServer.StartListening(_staticAssocParameters,
  
gt:                                        (server, assoc) => new StorageScp(server, assoc));