Mercurial > hg > orthanc-stone
annotate Applications/Generic/NativeStoneApplicationRunner.cpp @ 708:51976977d2d3
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 20 May 2019 11:20:01 +0200 |
parents | 68b5688241d5 |
children | 4f2416d519b4 |
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 | |
22 #if ORTHANC_ENABLE_NATIVE != 1 | |
23 #error this file shall be included only with the ORTHANC_ENABLE_NATIVE set to 1 | |
24 #endif | |
25 | |
291 | 26 #include "NativeStoneApplicationRunner.h" |
285 | 27 |
28 #include "../../Framework/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 { | |
46 class LogStatusBar : public IStatusBar | |
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 | |
100 application_.DeclareStartupOptions(options); | |
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); | |
183 if (!MessagingToolbox::CheckOrthancVersion(orthanc)) | |
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 | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
200 NativeStoneApplicationContext context(broker_); |
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 |
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
|
205 Oracle oracle(6); |
377 | 206 oracle.Start(); |
207 | |
208 { | |
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
|
209 OracleWebService webService( |
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
|
210 broker_, oracle, webServiceParameters, context); |
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
|
211 |
418 | 212 context.SetWebService(webService); |
213 context.SetOrthancBaseUrl(webServiceParameters.GetUrl()); | |
377 | 214 |
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 OracleDelayedCallExecutor delayedExecutor(broker_, oracle, context); |
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 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
|
217 |
377 | 218 application_.Initialize(&context, statusBar, parameters); |
219 | |
220 { | |
221 NativeStoneApplicationContext::GlobalMutexLocker locker(context); | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
222 locker.SetCentralWidget(application_.GetCentralWidget()); |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
223 locker.GetCentralViewport().SetStatusBar(statusBar); |
377 | 224 } |
285 | 225 |
377 | 226 std::string title = application_.GetTitle(); |
227 if (title.empty()) | |
228 { | |
229 title = "Stone of Orthanc"; | |
230 } | |
231 | |
232 /**************************************************************** | |
233 * Run the application | |
234 ****************************************************************/ | |
235 | |
236 Run(context, title, argc, argv); | |
237 | |
238 /**************************************************************** | |
239 * Finalize the application | |
240 ****************************************************************/ | |
241 | |
242 oracle.Stop(); | |
243 } | |
285 | 244 } |
245 | |
246 LOG(WARNING) << "The application is stopping"; | |
247 application_.Finalize(); | |
248 } | |
249 catch (Orthanc::OrthancException& e) | |
250 { | |
251 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
252 success = false; | |
253 } | |
254 | |
255 | |
256 /****************************************************************** | |
257 * Finalize all the subcomponents of Orthanc Stone | |
258 ******************************************************************/ | |
259 | |
260 Finalize(); | |
261 Orthanc::HttpClient::GlobalFinalize(); | |
262 Orthanc::Toolbox::FinalizeOpenSsl(); | |
263 | |
264 return (success ? 0 : -1); | |
265 } | |
266 } |