public IFramePixelData LoadFramePixelData(LoadFramePixelDataArgs args)
 {
     try
     {
         var client = new StreamingClient(_wadoUri);
         var result = client.RetrievePixelData(_aeTitle, args.StudyInstanceUid, args.SeriesInstanceUid, args.SopInstanceUid, args.FrameNumber - 1);
         return(new ImageServerFramePixelData(result));
     }
     catch (Exception e)
     {
         throw TranslateStreamingException(e);
     }
 }
 public IFramePixelData LoadFramePixelData(LoadFramePixelDataArgs args)
 {
     try
     {
         var uri    = new Uri(string.Format(StreamingSettings.Default.FormatWadoUriPrefix, _hostName, _wadoServicePort));
         var client = new StreamingClient(uri);
         var result = client.RetrievePixelData(_aeTitle, args.StudyInstanceUid, args.SeriesInstanceUid, args.SopInstanceUid, args.FrameNumber - 1);
         return(new ImageServerFramePixelData(result));
     }
     catch (Exception e)
     {
         throw TranslateStreamingException(e);
     }
 }
            private RetrievePixelDataResult TryClientRetrievePixelData(out Exception lastRetrieveException)
            {
                // retry parameters
                const int maxRetryCount = 10;
                const int retryTimeout  = 1500;
                int       retryDelay    = 50;
                int       retryCounter  = 0;

                StreamingClient         client = new StreamingClient(this.BaseUrl);
                RetrievePixelDataResult result = null;

                lastRetrieveException = null;

                CodeClock timeoutClock = new CodeClock();

                timeoutClock.Start();

                while (true)
                {
                    try
                    {
                        if (retryCounter > 0)
                        {
                            Platform.Log(LogLevel.Info, "Retrying retrieve pixel data for Sop '{0}' (Attempt #{1})", this.SopInstanceUid, retryCounter);
                        }

                        CodeClock statsClock = new CodeClock();
                        statsClock.Start();

                        result = client.RetrievePixelData(this.AETitle, this.StudyInstanceUid, this.SeriesInstanceUid, this.SopInstanceUid, this.FrameNumber - 1);

                        statsClock.Stop();

                        Platform.Log(LogLevel.Debug, "[Retrieve Info] Sop/Frame: {0}/{1}, Transfer Syntax: {2}, Bytes transferred: {3}, Elapsed (s): {4}, Retries: {5}",
                                     this.SopInstanceUid, this.FrameNumber, this.TransferSyntaxUid,
                                     result.MetaData.ContentLength, statsClock.Seconds, retryCounter);

                        break;
                    }
                    catch (Exception ex)
                    {
                        lastRetrieveException = ex;

                        timeoutClock.Stop();
                        if (timeoutClock.Seconds * 1000 >= retryTimeout || retryCounter >= maxRetryCount)
                        {
                            // log an alert that we are aborting (exception trace at debug level only)
                            int elapsed = (int)(1000 * timeoutClock.Seconds);
                            Platform.Log(LogLevel.Warn, "Failed to retrieve pixel data for Sop '{0}'; Aborting after {1} attempts in {2} ms", this.SopInstanceUid, retryCounter, elapsed);
                            Platform.Log(LogLevel.Debug, ex, "[Retrieve Fail-Abort] Sop/Frame: {0}/{1}, Retry Attempts: {2}, Elapsed: {3} ms", this.SopInstanceUid, this.FrameNumber - 1, retryCounter, elapsed);
                            break;
                        }
                        timeoutClock.Start();

                        retryCounter++;

                        // log the retry (exception trace at debug level only)
                        Platform.Log(LogLevel.Warn, "Failed to retrieve pixel data for Sop '{0}'; Retrying in {1} ms", this.SopInstanceUid, retryDelay);
                        Platform.Log(LogLevel.Debug, ex, "[Retrieve Fail-Retry] Sop/Frame: {0}/{1}, Retry in: {2} ms", this.SopInstanceUid, this.FrameNumber - 1, retryDelay);
                        MemoryManager.Collect(retryDelay);
                        retryDelay *= 2;

                        if (retryDelay > MaxRetryDelay)
                        {
                            retryDelay = MaxRetryDelay; // cap it to avoid overflow, which will cause exception when calling MemoryManager.Collect()
                        }
                    }
                }

                return(result);
            }
Пример #4
0
        static void Main(string[] args)
        {
            //get server details from args
            _cmdLine = new TestStreamingClientCommandLine();
            try
            {
                _cmdLine.Parse(args);

                //perform query for studies; maybe optional search parameters in xml?

                _studies = new List <StudyInfo>();

                Console.WriteLine("Querying for study list from AE: {0} (Host: {1} / Port: {2})", _cmdLine.AeTitle, _cmdLine.Host, _cmdLine.DicomServicePort);

                CFindSCU cfind = new CFindSCU();
                cfind.AETitle           = "TESTSTREAM";
                cfind.OnResultReceive  += new CFindSCU.ResultReceivedHandler(cfind_OnResultReceive);
                cfind.OnQueryCompleted += new CFindSCU.QueryCompletedHandler(cfind_OnQueryCompleted);
                cfind.Query(_cmdLine.AeTitle, _cmdLine.Host, _cmdLine.DicomServicePort);
                _waitHandle.WaitOne();

                if (_studies != null && _studies.Count > 0)
                {
                    foreach (StudyInfo s in _studies)
                    {
                        //iterate through in thread:
                        //  fetching header
                        //  fetching each sop
                        //  log after each sop

                        Console.WriteLine("Retrieving header for Study: {0}", s.StudyUid);
                        XmlDocument doc = RetrieveHeaderXml(s.StudyUid);

                        Uri             uri    = new Uri(String.Format("http://{0}:{1}/WADO", _cmdLine.Host, _cmdLine.WadoServicePort));
                        StreamingClient client = new StreamingClient(uri);
                        byte[]          pixelData;

                        Console.WriteLine("Streaming images for Study: {0}", s.StudyUid);
                        StudyXml studyXml = new StudyXml(s.StudyUid);
                        studyXml.SetMemento(doc);

                        foreach (SeriesXml seriesXml in studyXml)
                        {
                            foreach (InstanceXml instanceXml in seriesXml)
                            {
                                pixelData = client.RetrievePixelData(
                                    _cmdLine.AeTitle,
                                    s.StudyUid,
                                    seriesXml.SeriesInstanceUid,
                                    instanceXml.SopInstanceUid,
                                    0).GetPixelData();
                            }
                        }
                    }
                }
            }
            catch (CommandLineException e)
            {
                Console.WriteLine(e.Message);
                _cmdLine.PrintUsage(Console.Out);
            }
        }
Пример #5
0
        private static void RetrieveImages(string serverAE, string studyPath)
        {
            Console.WriteLine("server={0}", serverAE);
            string          baseUri         = String.Format("http://{0}:{1}", serverHost, serverPort);
            StreamingClient client          = new StreamingClient(new Uri(baseUri));
            int             totalFrameCount = 0;

            DirectoryInfo directoryInfo = new DirectoryInfo(studyPath);
            string        studyUid      = directoryInfo.Name;

            RateStatistics        frameRate    = new RateStatistics("Speed", "frame");
            RateStatistics        speed        = new RateStatistics("Speed", RateType.BYTES);
            AverageRateStatistics averageSpeed = new AverageRateStatistics(RateType.BYTES);
            ByteCountStatistics   totalSize    = new ByteCountStatistics("Size");

            frameRate.Start();
            speed.Start();

            Console.WriteLine("\n------------------------------------------------------------------------------------------------------------------------");

            string[] seriesDirs = Directory.GetDirectories(studyPath);
            foreach (string seriesPath in seriesDirs)
            {
                DirectoryInfo dirInfo       = new DirectoryInfo(seriesPath);
                string        seriesUid     = dirInfo.Name;
                string[]      objectUidPath = Directory.GetFiles(seriesPath, "*.dcm");

                foreach (string uidPath in objectUidPath)
                {
                    FileInfo fileInfo = new FileInfo(uidPath);
                    string   uid      = fileInfo.Name.Replace(".dcm", "");
                    Console.Write("{0,-64}... ", uid);

                    try
                    {
                        Stream imageStream;
                        StreamingResultMetaData      imageMetaData;
                        FrameStreamingResultMetaData frameMetaData;

                        switch (type)
                        {
                        case ContentTypes.Dicom:
                            imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData);
                            totalFrameCount++;
                            averageSpeed.AddSample(imageMetaData.Speed);
                            totalSize.Value += (ulong)imageMetaData.ContentLength;

                            Console.WriteLine("1 dicom sop [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType);

                            break;

                        case ContentTypes.RawPixel:
                            TimeSpanStatistics elapsedTime = new TimeSpanStatistics();
                            elapsedTime.Start();
                            ulong instanceSize = 0;
                            int   frameCount   = 0;
                            do
                            {
                                RetrievePixelDataResult result = client.RetrievePixelData(serverAE, studyUid, seriesUid, uid, frameCount);
                                frameMetaData = result.MetaData;
                                totalFrameCount++;
                                frameCount++;
                                averageSpeed.AddSample(frameMetaData.Speed);
                                totalSize.Value += (ulong)frameMetaData.ContentLength;
                                instanceSize    += (ulong)frameMetaData.ContentLength;
                            } while (!frameMetaData.IsLast);

                            elapsedTime.End();
                            Console.WriteLine("{0,3} frame(s) [{1,10}] in {2,12}\t[mime={3}]", frameCount, ByteCountFormatter.Format(instanceSize), elapsedTime.FormattedValue, frameMetaData.ResponseMimeType);
                            break;

                        default:

                            imageStream = client.RetrieveImage(serverAE, studyUid, seriesUid, uid, out imageMetaData);
                            totalFrameCount++;
                            averageSpeed.AddSample(imageMetaData.Speed);
                            totalSize.Value += (ulong)imageMetaData.ContentLength;

                            Console.WriteLine("1 object [{0,10}] in {1,12}\t[mime={2}]", ByteCountFormatter.Format((ulong)imageStream.Length), TimeSpanFormatter.Format(imageMetaData.Speed.ElapsedTime), imageMetaData.ResponseMimeType);

                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is WebException)
                        {
                            HttpWebResponse rsp = ((ex as WebException).Response as HttpWebResponse);

                            string msg = String.Format("Error: {0} : {1}", rsp.StatusCode, HttpUtility.HtmlDecode(rsp.StatusDescription)
                                                       );
                            Console.WriteLine(msg);
                        }
                        else
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            frameRate.SetData(totalFrameCount);
            frameRate.End();
            speed.SetData(totalSize.Value);
            speed.End();


            Console.WriteLine("\nTotal {0,3} image(s)/frame(s) [{1,10}] in {2,12}   ==>  [ Speed: {3,12} or {4,12}]",
                              totalFrameCount, totalSize.FormattedValue,
                              TimeSpanFormatter.Format(frameRate.ElapsedTime),
                              frameRate.FormattedValue,
                              speed.FormattedValue
                              );
        }