Mercurial > hg > orthanc-stone
annotate Applications/Generic/NativeStoneApplicationRunner.cpp @ 1205:6009c59d8676 broker
fix to sdl
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 02 Dec 2019 14:32:05 +0100 |
parents | 8d3936f06541 |
children | 0ca50d275b9a |
rev | line source |
---|---|
285 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
285 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
748 | 22 #if ORTHANC_ENABLE_THREADS != 1 |
23 #error this file shall be included only with the ORTHANC_ENABLE_THREADS set to 1 | |
285 | 24 #endif |
25 | |
291 | 26 #include "NativeStoneApplicationRunner.h" |
285 | 27 |
792
4fe4b221a31f
deprecating MessagingToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
28 #include "../../Framework/Deprecated/Toolbox/MessagingToolbox.h" |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
29 #include "../../Platforms/Generic/OracleWebService.h" |
431
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
30 #include "../../Platforms/Generic/OracleDelayedCallExecutor.h" |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
31 #include "NativeStoneApplicationContext.h" |
285 | 32 |
33 #include <Core/Logging.h> | |
34 #include <Core/HttpClient.h> | |
35 #include <Core/Toolbox.h> | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
36 #include <Core/OrthancException.h> |
285 | 37 #include <Plugins/Samples/Common/OrthancHttpConnection.h> |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
38 |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
39 #include <boost/program_options.hpp> |
285 | 40 |
41 namespace OrthancStone | |
42 { | |
43 // Anonymous namespace to avoid clashes against other compilation modules | |
44 namespace | |
45 { | |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
659
diff
changeset
|
46 class LogStatusBar : public Deprecated::IStatusBar |
285 | 47 { |
48 public: | |
49 virtual void ClearMessage() | |
50 { | |
51 } | |
52 | |
53 virtual void SetMessage(const std::string& message) | |
54 { | |
55 LOG(WARNING) << message; | |
56 } | |
57 }; | |
58 } | |
59 | |
291 | 60 int NativeStoneApplicationRunner::Execute(int argc, |
285 | 61 char* argv[]) |
62 { | |
63 /****************************************************************** | |
64 * Initialize all the subcomponents of Orthanc Stone | |
65 ******************************************************************/ | |
66 | |
67 Orthanc::Logging::Initialize(); | |
68 Orthanc::Toolbox::InitializeOpenSsl(); | |
69 Orthanc::HttpClient::GlobalInitialize(); | |
70 | |
71 Initialize(); | |
72 | |
73 /****************************************************************** | |
74 * Declare and parse the command-line options of the application | |
75 ******************************************************************/ | |
76 | |
77 boost::program_options::options_description options; | |
78 | |
79 { // generic options | |
80 boost::program_options::options_description generic("Generic options"); | |
81 generic.add_options() | |
82 ("help", "Display this help and exit") | |
83 ("verbose", "Be verbose in logs") | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
84 ("orthanc", boost::program_options::value<std::string>()-> |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
85 default_value("http://localhost:8042/"), |
285 | 86 "URL to the Orthanc server") |
87 ("username", "Username for the Orthanc server") | |
88 ("password", "Password for the Orthanc server") | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
89 ("https-verify", boost::program_options::value<bool>()-> |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
90 default_value(true), "Check HTTPS certificates") |
285 | 91 ; |
92 | |
93 options.add(generic); | |
94 } | |
95 | |
96 // platform specific options | |
97 DeclareCommandLineOptions(options); | |
98 | |
99 // application specific options | |
1068
04a95ee91327
recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
100 application_->DeclareStartupOptions(options); |
285 | 101 |
102 boost::program_options::variables_map parameters; | |
103 bool error = false; | |
104 | |
105 try | |
106 { | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
107 boost::program_options::store( |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
108 boost::program_options::command_line_parser(argc, argv). |
658 | 109 options(options).allow_unregistered().run(), parameters); |
285 | 110 boost::program_options::notify(parameters); |
111 } | |
112 catch (boost::program_options::error& e) | |
113 { | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
114 LOG(ERROR) << |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
115 "Error while parsing the command-line arguments: " << e.what(); |
285 | 116 error = true; |
117 } | |
118 | |
119 | |
120 /****************************************************************** | |
121 * Configure the application with the command-line parameters | |
122 ******************************************************************/ | |
123 | |
124 if (error || parameters.count("help")) | |
125 { | |
659
68b5688241d5
Fixed broken previous merge
Benjamin Golinvaux <bgo@osimis.io>
parents:
658
diff
changeset
|
126 std::cout << std::endl; |
285 | 127 |
128 std::cout << options << "\n"; | |
129 return error ? -1 : 0; | |
130 } | |
131 | |
132 if (parameters.count("https-verify") && | |
133 !parameters["https-verify"].as<bool>()) | |
134 { | |
135 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)"; | |
136 Orthanc::HttpClient::ConfigureSsl(false, ""); | |
137 } | |
138 | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
139 LOG(ERROR) << "???????? if (parameters.count(\"verbose\"))"; |
285 | 140 if (parameters.count("verbose")) |
141 { | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
142 LOG(ERROR) << "parameters.count(\"verbose\") != 0"; |
285 | 143 Orthanc::Logging::EnableInfoLevel(true); |
541 | 144 LOG(INFO) << "Verbose logs are enabled"; |
145 } | |
146 | |
604
86dfde451f4c
Fixed typo + added flags for MSVC in Stone Sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
603
diff
changeset
|
147 LOG(ERROR) << "???????? if (parameters.count(\"trace\"))"; |
541 | 148 if (parameters.count("trace")) |
149 { | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
150 LOG(ERROR) << "parameters.count(\"trace\") != 0"; |
541 | 151 Orthanc::Logging::EnableTraceLevel(true); |
152 VLOG(1) << "Trace logs are enabled"; | |
285 | 153 } |
154 | |
155 ParseCommandLineOptions(parameters); | |
156 | |
157 bool success = true; | |
158 try | |
159 { | |
160 /**************************************************************** | |
161 * Initialize the connection to the Orthanc server | |
162 ****************************************************************/ | |
163 | |
164 Orthanc::WebServiceParameters webServiceParameters; | |
165 | |
166 if (parameters.count("orthanc")) | |
167 { | |
168 webServiceParameters.SetUrl(parameters["orthanc"].as<std::string>()); | |
169 } | |
170 | |
296 | 171 if (parameters.count("username") && parameters.count("password")) |
285 | 172 { |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
173 webServiceParameters.SetCredentials(parameters["username"]. |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
174 as<std::string>(), |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
175 parameters["password"].as<std::string>()); |
285 | 176 } |
177 | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
178 LOG(WARNING) << "URL to the Orthanc REST API: " << |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
179 webServiceParameters.GetUrl(); |
285 | 180 |
181 { | |
182 OrthancPlugins::OrthancHttpConnection orthanc(webServiceParameters); | |
792
4fe4b221a31f
deprecating MessagingToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
183 if (!Deprecated::MessagingToolbox::CheckOrthancVersion(orthanc)) |
285 | 184 { |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
185 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of " |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
186 << "Orthanc, please upgrade"; |
285 | 187 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); |
188 } | |
189 } | |
190 | |
191 | |
192 /**************************************************************** | |
193 * Initialize the application | |
194 ****************************************************************/ | |
195 | |
196 LOG(WARNING) << "Creating the widgets of the application"; | |
197 | |
198 LogStatusBar statusBar; | |
199 | |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
792
diff
changeset
|
200 NativeStoneApplicationContext context; |
285 | 201 |
202 { | |
603
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
203 // use multiple threads to execute asynchronous tasks like |
70992b38aa8a
new routable logging system in STDIO mode + flag support (with no value) in StartupParametersBuilder + 80 col indent
Benjamin Golinvaux <bgo@osimis.io>
parents:
541
diff
changeset
|
204 // download content |
726
4f2416d519b4
moving layers, widgets and loaders to Deprecated namespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
659
diff
changeset
|
205 Deprecated::Oracle oracle(6); |
377 | 206 oracle.Start(); |
207 | |
208 { | |
1073 | 209 boost::shared_ptr<Deprecated::OracleWebService> webService |
210 (new Deprecated::OracleWebService(oracle, webServiceParameters, context)); | |
211 context.SetWebService(webService); | |
418 | 212 context.SetOrthancBaseUrl(webServiceParameters.GetUrl()); |
377 | 213 |
1066
b537002f83a9
removing broker from deprecated classes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
792
diff
changeset
|
214 Deprecated::OracleDelayedCallExecutor delayedExecutor(oracle, context); |
431
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
215 context.SetDelayedCallExecutor(delayedExecutor); |
26b90b110719
added DelayedCallExecutor to avoid using sleep() in C++ that consumes 100% CPU once executed in WASM
am@osimis.io
parents:
418
diff
changeset
|
216 |
1068
04a95ee91327
recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
217 application_->Initialize(&context, statusBar, parameters); |
377 | 218 |
219 { | |
220 NativeStoneApplicationContext::GlobalMutexLocker locker(context); | |
1068
04a95ee91327
recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
221 locker.SetCentralWidget(application_->GetCentralWidget()); |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
222 locker.GetCentralViewport().SetStatusBar(statusBar); |
377 | 223 } |
285 | 224 |
1068
04a95ee91327
recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
225 std::string title = application_->GetTitle(); |
377 | 226 if (title.empty()) |
227 { | |
228 title = "Stone of Orthanc"; | |
229 } | |
230 | |
231 /**************************************************************** | |
232 * Run the application | |
233 ****************************************************************/ | |
234 | |
235 Run(context, title, argc, argv); | |
236 | |
237 /**************************************************************** | |
238 * Finalize the application | |
239 ****************************************************************/ | |
240 | |
241 oracle.Stop(); | |
242 } | |
285 | 243 } |
244 | |
245 LOG(WARNING) << "The application is stopping"; | |
1068
04a95ee91327
recovery of OrthancStoneSingleFrame
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1066
diff
changeset
|
246 application_->Finalize(); |
285 | 247 } |
248 catch (Orthanc::OrthancException& e) | |
249 { | |
250 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
251 success = false; | |
252 } | |
253 | |
254 | |
255 /****************************************************************** | |
256 * Finalize all the subcomponents of Orthanc Stone | |
257 ******************************************************************/ | |
258 | |
259 Finalize(); | |
260 Orthanc::HttpClient::GlobalFinalize(); | |
261 Orthanc::Toolbox::FinalizeOpenSsl(); | |
262 | |
263 return (success ? 0 : -1); | |
264 } | |
265 } |