Пример #1
0
		public override bool OnReceiveRequest(Dicom.Network.DicomServer server,
		                                      ServerAssociationParameters association, byte presentationID, DicomMessage message)
		{
			string studyInstanceUid;
			string seriesInstanceUid;
			DicomUid sopInstanceUid;

			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)
			{
				Platform.Log(LogLevel.Error, "Unable to retrieve UIDs from request message, sending failure status.");

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

				return true;
			}

			if (_importContext == null)
			{
				LoadImportContext(association);
			}

			var importer = new ImportFilesUtility(_importContext);

			var result = importer.Import(message, BadFileBehaviourEnum.Ignore, FileImportBehaviourEnum.Save);
			if (result.Successful)
			{
				if (!String.IsNullOrEmpty(result.AccessionNumber))
					Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (A#:{3} StudyUid:{4})",
					             result.SopInstanceUid, association.CallingAE, association.CalledAE, result.AccessionNumber,
					             result.StudyInstanceUid);
				else
					Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (StudyUid:{3})",
					             result.SopInstanceUid, association.CallingAE, association.CalledAE,
					             result.StudyInstanceUid);
				server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus);
			}
			else
			{
				if (result.DicomStatus == DicomStatuses.ProcessingFailure)
					Platform.Log(LogLevel.Error, "Failure importing sop: {0}", result.ErrorMessage);

				//OnReceiveError(message, result.ErrorMessage, association.CallingAE);
				server.SendCStoreResponse(presentationID, message.MessageId, message.AffectedSopInstanceUid,
				                          result.DicomStatus, result.ErrorMessage);
			}

			return true;
		}
		public bool CompleteStream(Dicom.Network.DicomServer server, ServerAssociationParameters assoc, byte presentationId, DicomMessage message)
		{
			DicomProcessingResult result;
			var importer = new ImportFilesUtility(_importContext);

			if (_rejectFile)
			{
				result = new DicomProcessingResult();
				result.SetError(DicomStatuses.StorageStorageOutOfResources, string.Format("Import failed, disk space usage exceeded"));

				string studyInstanceUid = message.DataSet[DicomTags.StudyInstanceUid].GetString(0, string.Empty);
				WorkItem workItem;
				lock (_importContext.StudyWorkItemsSyncLock)
					_importContext.StudyWorkItems.TryGetValue(studyInstanceUid, out workItem);

				importer.InsertFailedWorkItemUid(workItem, message, result);

				_importContext.FatalError = true;
				importer.AuditFailure(result);

				Platform.Log(LogLevel.Warn, "Failure receiving sop, out of disk space: {0}", message.AffectedSopInstanceUid);
				server.SendCStoreResponse(presentationId, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus);
				return true;
			}

			try
			{
				if (_fileStream != null)
				{
					_fileStream.Flush(true);
					_fileStream.Close();
					_fileStream.Dispose();
					_fileStream = null;
				}

				// Convert to file to pass in the source filename
				var theFile = new DicomFile(message,_sourceFilename);

				result = importer.Import(theFile, BadFileBehaviourEnum.Delete, FileImportBehaviourEnum.Move);

				if (result.Successful)
				{
					if (!String.IsNullOrEmpty(result.AccessionNumber))
						Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (A#:{3} StudyUid:{4})",
									 result.SopInstanceUid, assoc.CallingAE, assoc.CalledAE, result.AccessionNumber,
									 result.StudyInstanceUid);
					else
						Platform.Log(LogLevel.Info, "Received SOP Instance {0} from {1} to {2} (StudyUid:{3})",
									 result.SopInstanceUid, assoc.CallingAE, assoc.CalledAE,
									 result.StudyInstanceUid);
				}
			}
			catch (Exception e)
			{
				result = new DicomProcessingResult { DicomStatus = DicomStatuses.ProcessingFailure, ErrorMessage = e.Message };
			}

			if (!result.Successful)
			{
				Platform.Log(LogLevel.Warn, "Failure importing sop: {0}", result.ErrorMessage);
			}

			CleanupFile();

			server.SendCStoreResponse(presentationId, message.MessageId, message.AffectedSopInstanceUid, result.DicomStatus);
			return true;
		}