Example #1
0
        void IDicomServerHandler.OnReceiveAssociateRequest(DicomServer server, ServerAssociationParameters association)
        {
            if (_verifier != null)
            {
                DicomRejectResult result;
                DicomRejectReason reason;
                bool verified = _verifier(_context, association, out result, out reason);
                if (verified == false)
                {
                    server.SendAssociateReject(result, DicomRejectSource.ServiceUser, reason);
                    LogAdapter.Logger.Info("Association rejected from {0} to {1}",
                                           association.CallingAE, association.CalledAE);
                    return;
                }
            }

            // Let the extensions have its say on whether a presentation context is really acceptable.
            //
            bool atLeastOneAccepted = false;

            foreach (byte pcid in association.GetPresentationContextIDs())
            {
                if (association.GetPresentationContextResult(pcid) == DicomPresContextResult.Accept)
                {
                    IDicomScp <TContext>   scp = _extensionList[pcid];
                    DicomPresContextResult res = scp.VerifyAssociation(association, pcid);
                    if (res != DicomPresContextResult.Accept)
                    {
                        association.GetPresentationContext(pcid).ClearTransfers();
                        association.SetPresentationContextResult(pcid, res);
                    }
                    else
                    {
                        atLeastOneAccepted = true;
                    }
                }
            }

            if (!atLeastOneAccepted)
            {
                LogAdapter.Logger.Info("None of the proposed presentation context is accepted. Rejecting association from {0} to {1}", association.CallingAE, association.CalledAE);
                server.SendAssociateReject(DicomRejectResult.Permanent, DicomRejectSource.ServiceUser, DicomRejectReason.NoReasonGiven);
                return;
            }

            server.SendAssociateAccept(association);

            // Optimization to speed query performance
            Task.Factory.StartNew(() => LogAdapter.Logger.Info("Received association:\r\n{0}", association.ToString()));
        }
Example #2
0
        void IDicomServerHandler.OnReceiveRequestMessage(DicomServer server, ServerAssociationParameters association, byte presentationID, DicomMessage message)
        {
            if (message.CommandField == DicomCommandField.CEchoRequest)
            {
                server.SendCEchoResponse(presentationID, message.MessageId, DicomStatuses.Success);
                return;
            }

            String   studyInstanceUid  = null;
            String   seriesInstanceUid = null;
            DicomUid sopInstanceUid;
            String   patientName = null;

            bool ok = message.DataSet[DicomTags.SopInstanceUid].TryGetUid(0, out sopInstanceUid);

            if (ok)
            {
                ok = message.DataSet[DicomTags.SeriesInstanceUid].TryGetString(0, out seriesInstanceUid);
            }
            if (ok)
            {
                ok = message.DataSet[DicomTags.StudyInstanceUid].TryGetString(0, out studyInstanceUid);
            }
            if (ok)
            {
                ok = message.DataSet[DicomTags.PatientsName].TryGetString(0, out patientName);
            }

            if (!ok)
            {
                Platform.Log(LogLevel.Error, "Unable to retrieve UIDs from request message, sending failure status.");

                server.SendCStoreResponse(presentationID, message.MessageId, sopInstanceUid.UID,
                                          DicomStatuses.ProcessingFailure);
                return;
            }
            TransferSyntax syntax = association.GetPresentationContext(presentationID).AcceptedTransferSyntax;

            if (List)
            {
                Platform.Log(LogLevel.Info, message.Dump());
            }

            if (Bitbucket)
            {
                Platform.Log(LogLevel.Info, "Received SOP Instance: {0} for patient {1} in syntax {2}", sopInstanceUid,
                             patientName, syntax.Name);

                server.SendCStoreResponse(presentationID, message.MessageId,
                                          sopInstanceUid.UID,
                                          DicomStatuses.Success);
                return;
            }

            if (!Directory.Exists(StorageLocation))
            {
                Directory.CreateDirectory(StorageLocation);
            }

            var path = new StringBuilder();

            path.AppendFormat("{0}{1}{2}{3}{4}", StorageLocation, Path.DirectorySeparatorChar,
                              studyInstanceUid, Path.DirectorySeparatorChar, seriesInstanceUid);

            Directory.CreateDirectory(path.ToString());

            path.AppendFormat("{0}{1}.dcm", Path.DirectorySeparatorChar, sopInstanceUid.UID);

            var dicomFile = new DicomFile(message, path.ToString())
            {
                TransferSyntaxUid            = syntax.UidString,
                MediaStorageSopInstanceUid   = sopInstanceUid.UID,
                ImplementationClassUid       = DicomImplementation.ClassUID.UID,
                ImplementationVersionName    = DicomImplementation.Version,
                SourceApplicationEntityTitle = association.CallingAE,
                MediaStorageSopClassUid      = message.SopClass.Uid
            };


            dicomFile.Save(DicomWriteOptions.None);

            Platform.Log(LogLevel.Info, "Received SOP Instance: {0} for patient {1} in syntax {2}", sopInstanceUid,
                         patientName, syntax.Name);

            server.SendCStoreResponse(presentationID, message.MessageId,
                                      sopInstanceUid.UID,
                                      DicomStatuses.Success);
        }
        /// <summary>
        /// Handle an incoming request
        /// </summary>
        /// <param name="server"></param>
        /// <param name="association"></param>
        /// <param name="presentationID"></param>
        /// <param name="message"></param>
        void IDicomServerHandler.OnReceiveRequestMessage(DicomServer server, ServerAssociationParameters association, byte presentationID, DicomMessage message)
        {
            log.Info("Message received ... ");

            try
            {
                log.Info(
                    string.Format("Request recieved with command field {0} from ae {1}",
                                  message.CommandField,
                                  message.CommandSet[DicomTags.SourceApplicationEntityTitle].ToString()));

                if (message.CommandField == DicomCommandField.CEchoRequest)
                {
                    server.SendCEchoResponse(presentationID, message.MessageId, DicomStatuses.Success);
                    return;
                }

                String   studyInstanceUid  = null;
                String   seriesInstanceUid = null;
                DicomUid sopInstanceUid    = null;
                DicomUid sopClassUid       = null;
                String   patientName       = null;

                bool ok = message.DataSet[DicomTags.SopInstanceUid].TryGetUid(0, out sopInstanceUid);
                if (ok)
                {
                    ok = message.DataSet[DicomTags.SeriesInstanceUid].TryGetString(0, out seriesInstanceUid);
                }
                if (ok)
                {
                    ok = message.DataSet[DicomTags.StudyInstanceUid].TryGetString(0, out studyInstanceUid);
                }
                if (ok)
                {
                    ok = message.DataSet[DicomTags.PatientsName].TryGetString(0, out patientName);
                }
                if (ok)
                {
                    ok = message.DataSet[DicomTags.SopClassUid].TryGetUid(0, out sopClassUid);
                }

                if (!ok)
                {
                    log.Info("Unable to retrieve UIDs from request message, sending failure status.");

                    server.SendCStoreResponse(presentationID, message.MessageId, sopInstanceUid.UID,
                                              DicomStatuses.ProcessingFailure);
                    return;
                }

                TransferSyntax syntax = association.GetPresentationContext(presentationID).AcceptedTransferSyntax;

                if (!CacheManager.StorageSpaceAvailable(message, Configuration.Root, Configuration.MaxDiskPercentage))
                {
                    log.Error("Insufficient space to store.");

                    server.SendCStoreResponse(presentationID, message.MessageId, sopInstanceUid.UID,
                                              DicomStatuses.StorageStorageOutOfResources);
                    return;
                }

                log.Info("Adding to cache");
                CacheManager.AddToCache(
                    Configuration,
                    association,
                    message,
                    syntax,
                    studyInstanceUid,
                    seriesInstanceUid,
                    sopInstanceUid.UID,
                    patientName);

                log.Info(string.Format("Received SOP Instance: {0} for patient {1} in syntax {2}", sopInstanceUid,
                                       patientName, syntax.Name));

                server.SendCStoreResponse(presentationID, message.MessageId,
                                          sopInstanceUid.UID,
                                          DicomStatuses.Success);
            }
            catch (Exception ex)
            {
                server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid,
                                          DicomStatuses.ProcessingFailure);
                log.Error("Exception thrown " + ex.ToString());
            }
        }