Mercurial > hg > orthanc-stone
annotate Applications/Generic/NativeStoneApplicationRunner.cpp @ 622:8a3a25f2d42c
uncoupling oracle from context
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 May 2019 12:32:21 +0200 |
parents | 86dfde451f4c |
children | 63982c8c294a |
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). |
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
|
109 options(options).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 { | |
126 std::cout << std::endl | |
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
|
127 << "Usage: " << argv[0] << " [OPTION]..." << std::endl |
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
|
128 << "Orthanc, lightweight, RESTful DICOM server for healthcare " |
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
|
129 << "and medical research." << std::endl << std::endl |
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
|
130 << "Demonstration application of Orthanc Stone in native " |
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
|
131 << "environment." << std::endl; |
285 | 132 |
133 std::cout << options << "\n"; | |
134 return error ? -1 : 0; | |
135 } | |
136 | |
137 if (parameters.count("https-verify") && | |
138 !parameters["https-verify"].as<bool>()) | |
139 { | |
140 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)"; | |
141 Orthanc::HttpClient::ConfigureSsl(false, ""); | |
142 } | |
143 | |
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
|
144 LOG(ERROR) << "???????? if (parameters.count(\"verbose\"))"; |
285 | 145 if (parameters.count("verbose")) |
146 { | |
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
|
147 LOG(ERROR) << "parameters.count(\"verbose\") != 0"; |
285 | 148 Orthanc::Logging::EnableInfoLevel(true); |
541 | 149 LOG(INFO) << "Verbose logs are enabled"; |
150 } | |
151 | |
604
86dfde451f4c
Fixed typo + added flags for MSVC in Stone Sample
Benjamin Golinvaux <bgo@osimis.io>
parents:
603
diff
changeset
|
152 LOG(ERROR) << "???????? if (parameters.count(\"trace\"))"; |
541 | 153 if (parameters.count("trace")) |
154 { | |
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
|
155 LOG(ERROR) << "parameters.count(\"trace\") != 0"; |
541 | 156 Orthanc::Logging::EnableTraceLevel(true); |
157 VLOG(1) << "Trace logs are enabled"; | |
285 | 158 } |
159 | |
160 ParseCommandLineOptions(parameters); | |
161 | |
162 bool success = true; | |
163 try | |
164 { | |
165 /**************************************************************** | |
166 * Initialize the connection to the Orthanc server | |
167 ****************************************************************/ | |
168 | |
169 Orthanc::WebServiceParameters webServiceParameters; | |
170 | |
171 if (parameters.count("orthanc")) | |
172 { | |
173 webServiceParameters.SetUrl(parameters["orthanc"].as<std::string>()); | |
174 } | |
175 | |
296 | 176 if (parameters.count("username") && parameters.count("password")) |
285 | 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 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
|
179 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
|
180 parameters["password"].as<std::string>()); |
285 | 181 } |
182 | |
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
|
183 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
|
184 webServiceParameters.GetUrl(); |
285 | 185 |
186 { | |
187 OrthancPlugins::OrthancHttpConnection orthanc(webServiceParameters); | |
188 if (!MessagingToolbox::CheckOrthancVersion(orthanc)) | |
189 { | |
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
|
190 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
|
191 << "Orthanc, please upgrade"; |
285 | 192 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); |
193 } | |
194 } | |
195 | |
196 | |
197 /**************************************************************** | |
198 * Initialize the application | |
199 ****************************************************************/ | |
200 | |
201 LOG(WARNING) << "Creating the widgets of the application"; | |
202 | |
203 LogStatusBar statusBar; | |
204 | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
205 NativeStoneApplicationContext context(broker_); |
285 | 206 |
207 { | |
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
|
208 // 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
|
209 // 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
|
210 Oracle oracle(6); |
377 | 211 oracle.Start(); |
212 | |
213 { | |
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
|
214 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
|
215 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
|
216 |
418 | 217 context.SetWebService(webService); |
218 context.SetOrthancBaseUrl(webServiceParameters.GetUrl()); | |
377 | 219 |
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
|
220 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
|
221 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
|
222 |
377 | 223 application_.Initialize(&context, statusBar, parameters); |
224 | |
225 { | |
226 NativeStoneApplicationContext::GlobalMutexLocker locker(context); | |
385
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
227 locker.SetCentralWidget(application_.GetCentralWidget()); |
6cc3ce74dc05
using message broker in widgets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
377
diff
changeset
|
228 locker.GetCentralViewport().SetStatusBar(statusBar); |
377 | 229 } |
285 | 230 |
377 | 231 std::string title = application_.GetTitle(); |
232 if (title.empty()) | |
233 { | |
234 title = "Stone of Orthanc"; | |
235 } | |
236 | |
237 /**************************************************************** | |
238 * Run the application | |
239 ****************************************************************/ | |
240 | |
241 Run(context, title, argc, argv); | |
242 | |
243 /**************************************************************** | |
244 * Finalize the application | |
245 ****************************************************************/ | |
246 | |
247 oracle.Stop(); | |
248 } | |
285 | 249 } |
250 | |
251 LOG(WARNING) << "The application is stopping"; | |
252 application_.Finalize(); | |
253 } | |
254 catch (Orthanc::OrthancException& e) | |
255 { | |
256 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
257 success = false; | |
258 } | |
259 | |
260 | |
261 /****************************************************************** | |
262 * Finalize all the subcomponents of Orthanc Stone | |
263 ******************************************************************/ | |
264 | |
265 Finalize(); | |
266 Orthanc::HttpClient::GlobalFinalize(); | |
267 Orthanc::Toolbox::FinalizeOpenSsl(); | |
268 | |
269 return (success ? 0 : -1); | |
270 } | |
271 } |