comparison Applications/ApplicationToolbox.cpp @ 0:4a7a53257c7d

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 22 Oct 2016 21:48:33 +0200
parents
children bc3ca410b765
comparison
equal deleted inserted replaced
-1:000000000000 0:4a7a53257c7d
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "ApplicationToolbox.h"
22
23 #include "../Framework/Inputs/OpenSlideLibrary.h"
24 #include "../Framework/Orthanc/Core/HttpClient.h"
25 #include "../Framework/Orthanc/Core/Logging.h"
26 #include "../Framework/Orthanc/Core/MultiThreading/BagOfTasksProcessor.h"
27 #include "../Framework/Orthanc/OrthancServer/FromDcmtkBridge.h"
28
29 #include <boost/lexical_cast.hpp>
30 #include <boost/regex.hpp>
31
32 namespace OrthancWSI
33 {
34 namespace ApplicationToolbox
35 {
36 void GlobalInitialize()
37 {
38 Orthanc::Logging::Initialize();
39 Orthanc::HttpClient::InitializeOpenSsl();
40 Orthanc::HttpClient::GlobalInitialize();
41 Orthanc::FromDcmtkBridge::InitializeDictionary();
42 }
43
44
45 void GlobalFinalize()
46 {
47 OrthancWSI::OpenSlideLibrary::Finalize();
48 Orthanc::HttpClient::GlobalFinalize();
49 Orthanc::HttpClient::FinalizeOpenSsl();
50 }
51
52
53 static void PrintProgress(Orthanc::BagOfTasksProcessor::Handle* handle,
54 bool* done)
55 {
56 unsigned int previous = 0;
57
58 while (!*done)
59 {
60 unsigned int progress = static_cast<unsigned int>(100.0f * handle->GetProgress());
61 if (previous != progress)
62 {
63 LOG(WARNING) << "Progress: " << progress << "%";
64 previous = progress;
65 }
66
67 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
68 }
69 }
70
71
72 void Execute(Orthanc::BagOfTasks& tasks,
73 unsigned int threadsCount)
74 {
75 if (threadsCount > 1)
76 {
77 LOG(WARNING) << "Running " << tasks.GetSize() << " tasks";
78 LOG(WARNING) << "Using " << threadsCount << " threads for the computation";
79 Orthanc::BagOfTasksProcessor processor(threadsCount);
80 std::auto_ptr<Orthanc::BagOfTasksProcessor::Handle> handle(processor.Submit(tasks));
81
82 bool done = false;
83 boost::thread progress(PrintProgress, handle.get(), &done);
84
85 if (handle->Join())
86 {
87 done = true;
88 LOG(WARNING) << "All tasks have finished";
89 }
90 else
91 {
92 done = true;
93 LOG(ERROR) << "Error has occurred, aborting";
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
95 }
96
97 if (progress.joinable())
98 {
99 progress.join();
100 }
101 }
102 else
103 {
104 LOG(WARNING) << "Running " << tasks.GetSize() << " tasks without multithreading";
105
106 unsigned int previous = 0;
107 unsigned int size = tasks.GetSize();
108
109 // No multithreading
110 while (!tasks.IsEmpty())
111 {
112 std::auto_ptr<Orthanc::ICommand> task(tasks.Pop());
113 if (task->Execute())
114 {
115 unsigned int progress = static_cast<unsigned int>(100.0f *
116 static_cast<float>((size - tasks.GetSize())) /
117 static_cast<float>(size));
118 if (progress != previous)
119 {
120 LOG(WARNING) << "Progress: " << progress << "%";
121 previous = progress;
122 }
123 }
124 else
125 {
126 LOG(ERROR) << "Error has occurred, aborting";
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
128 }
129 }
130 }
131 }
132
133
134 void ParseColor(uint8_t& red,
135 uint8_t& green,
136 uint8_t& blue,
137 const std::string& color)
138 {
139 boost::regex pattern("([0-9]*),([0-9]*),([0-9]*)");
140
141 bool ok = false;
142 boost::cmatch what;
143
144 try
145 {
146 if (regex_match(color.c_str(), what, pattern))
147 {
148 int r = boost::lexical_cast<int>(what[1]);
149 int g = boost::lexical_cast<int>(what[2]);
150 int b = boost::lexical_cast<int>(what[3]);
151
152 if (r >= 0 && r <= 255 &&
153 g >= 0 && g <= 255 &&
154 b >= 0 && b <= 255)
155 {
156 red = static_cast<uint8_t>(r);
157 green = static_cast<uint8_t>(g);
158 blue = static_cast<uint8_t>(b);
159 ok = true;
160 }
161 }
162 }
163 catch (boost::bad_lexical_cast&)
164 {
165 }
166
167 if (!ok)
168 {
169 LOG(ERROR) << "Bad color specification: " << color;
170 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
171 }
172 }
173 }
174 }