public static void InstanceDataThread(object obj) { AsyncInstanceData aidata = obj as AsyncInstanceData; aidata.semaphore.WaitOne(); try { WebRequest request = WebRequest.Create(aidata.url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream dataStream = response.GetResponseStream()) { StreamReader reader = new StreamReader(dataStream); string res = reader.ReadToEnd(); reader.Close(); aidata.item.fname = string.Format("{0:D4}", (int)JObject.Parse(res)["IndexInSeries"]); } response.Close(); } catch (Exception e) { Log.WriteLog(e.Message); } aidata.semaphore.Release(); }
public static string Download(Config config, string patientID, string studyID, bool radiant) { string host = config.GetConfigSourceOption("resthost"); string tempPath = GetTempPath(); string baseDir = ""; try { //base folder baseDir = UniqueFolderName(tempPath); string baseTempPath = tempPath + baseDir + "\\"; Directory.CreateDirectory(baseTempPath); //patient string jsonPatient = GetJSON(host, "patients", patientID); JObject jPatient = JObject.Parse(jsonPatient); DICOMFolder dfPatient = new DICOMFolder(); dfPatient.id = patientID; dfPatient.path = baseTempPath + GetUniqueName(baseTempPath, (string)jPatient["MainDicomTags"]["PatientName"], false) + "\\"; Directory.CreateDirectory(dfPatient.path); //study(ies) List <DICOMFolder> dfStudies = new List <DICOMFolder>(); if (studyID != null && studyID.Length > 0) { DICOMFolder dfStudy = new DICOMFolder(); dfStudy.id = studyID; dfStudies.Add(dfStudy); } else { foreach (JToken jStudyID in (JArray)jPatient["Studies"]) { DICOMFolder dfStudy = new DICOMFolder(); dfStudy.id = (string)jStudyID; dfStudies.Add(dfStudy); } } List <DICOMFile> dfInstances = new List <DICOMFile>(); foreach (DICOMFolder dfStudy in dfStudies) { try { string jsonStudy = GetJSON(host, "studies", dfStudy.id); JObject jStudy = JObject.Parse(jsonStudy); dfStudy.path = dfPatient.path + GetUniqueName(dfPatient.path, (string)jStudy["MainDicomTags"]["StudyDate"] + " " + (string)jStudy["MainDicomTags"]["StudyTime"] + " " + (string)jStudy["MainDicomTags"]["StudyDescription"], false) + "\\"; Directory.CreateDirectory(dfStudy.path); //series List <DICOMFolder> dfSeries = new List <DICOMFolder>(); foreach (JToken jSeriesID in (JArray)jStudy["Series"]) { try { DICOMFolder dfSer = new DICOMFolder(); dfSer.id = (string)jSeriesID; dfSeries.Add(dfSer); string jsonSer = GetJSON(host, "series", dfSer.id); JObject jSer = JObject.Parse(jsonSer); dfSer.path = dfStudy.path + GetUniqueName(dfStudy.path, (string)jSer["MainDicomTags"]["SeriesNumber"] + " " + (string)jSer["MainDicomTags"]["SeriesDescription"], false) + "\\"; Directory.CreateDirectory(dfSer.path); //instances int i = 0; foreach (JToken jInstanceID in (JArray)jSer["Instances"]) { try { DICOMFile dfInstance = new DICOMFile(); dfInstance.id = (string)jInstanceID; dfInstance.path = dfSer.path; dfInstance.fname = string.Format("{0:D4}", ++i); dfInstances.Add(dfInstance); } catch (Exception e) { Log.WriteLog("Failed to get instance's data: " + e.Message); } } } catch (Exception e) { Log.WriteLog("Failed to get series' data: " + e.Message); } } } catch (Exception e) { Log.WriteLog("Failed to get study's data: " + e.Message); } } //correct instance file name int simultaneousNum = config.GetConfigInt("simultaneousThreads", 10); Semaphore semaphore = new Semaphore(simultaneousNum, simultaneousNum); List <Thread> threads = new List <Thread>(dfInstances.Count); foreach (DICOMFile instance in dfInstances) { try { AsyncInstanceData aid = new AsyncInstanceData(); aid.semaphore = semaphore; aid.url = host + "instances/" + instance.id; aid.item = instance; Thread thread = new Thread(InstanceDataThread); threads.Add(thread); thread.Start(aid); } catch (Exception e) { Log.WriteLog("Failed to get instance data: " + e.Message); } } bool allDone = false; while (!allDone) { bool join = true; foreach (Thread thread in threads) { join = thread.Join(30); if (!join) { break; } } allDone = join; } threads.Clear(); //instances' files simultaneousNum = config.GetConfigInt("simultaneousInstanceThreads", 10); semaphore = new Semaphore(simultaneousNum, simultaneousNum); threads = new List <Thread>(dfInstances.Count); foreach (DICOMFile instance in dfInstances) { try { AsyncFileData afd = new AsyncFileData(); afd.semaphore = semaphore; afd.url = host + "instances/" + instance.id + "/file"; afd.path = instance.path; afd.fname = instance.fname; afd.timeout = config.GetConfigInt("restTimeout", 300000); Thread thread = new Thread(DownloadThread); threads.Add(thread); thread.Start(afd); } catch (Exception e) { Log.WriteLog("Failed to download instances: " + e.Message); } } allDone = false; while (!allDone) { bool join = true; foreach (Thread thread in threads) { join = thread.Join(30); if (!join) { break; } } allDone = join; } threads.Clear(); } catch (Exception e) { Log.WriteLog("Failed to get patient's data: " + e.Message); } return(Compress(baseDir, radiant, config.GetConfigInt("compressionLevel", 0))); }