comparison Resources/Graveyard/Deprecated/Applications/Generic/NativeStoneApplicationRunner.cpp @ 1503:553084468225

moving /Deprecated/ to /Resources/Graveyard/Deprecated/
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2020 11:38:13 +0200
parents Deprecated/Applications/Generic/NativeStoneApplicationRunner.cpp@ff8d2e46ac63
children
comparison
equal deleted inserted replaced
1502:e5729dab3f67 1503:553084468225
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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_THREADS != 1
23 #error this file shall be included only with the ORTHANC_ENABLE_THREADS set to 1
24 #endif
25
26 #include "NativeStoneApplicationRunner.h"
27
28 #include "../../Framework/Deprecated/Toolbox/MessagingToolbox.h"
29 #include "../../Platforms/Generic/OracleWebService.h"
30 #include "../../Platforms/Generic/OracleDelayedCallExecutor.h"
31 #include "NativeStoneApplicationContext.h"
32
33 #include <Core/Logging.h>
34 #include <Core/HttpClient.h>
35 #include <Core/Toolbox.h>
36 #include <Core/OrthancException.h>
37 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
38
39 #include <boost/program_options.hpp>
40
41 namespace OrthancStone
42 {
43 // Anonymous namespace to avoid clashes against other compilation modules
44 namespace
45 {
46 class LogStatusBar : public Deprecated::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
60 int NativeStoneApplicationRunner::Execute(int argc,
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")
84 ("orthanc", boost::program_options::value<std::string>()->
85 default_value("http://localhost:8042/"),
86 "URL to the Orthanc server")
87 ("username", "Username for the Orthanc server")
88 ("password", "Password for the Orthanc server")
89 ("https-verify", boost::program_options::value<bool>()->
90 default_value(true), "Check HTTPS certificates")
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 {
107 boost::program_options::store(
108 boost::program_options::command_line_parser(argc, argv).
109 options(options).allow_unregistered().run(), parameters);
110 boost::program_options::notify(parameters);
111 }
112 catch (boost::program_options::error& e)
113 {
114 LOG(ERROR) <<
115 "Error while parsing the command-line arguments: " << e.what();
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;
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
139 LOG(ERROR) << "???????? if (parameters.count(\"verbose\"))";
140 if (parameters.count("verbose"))
141 {
142 LOG(ERROR) << "parameters.count(\"verbose\") != 0";
143 Orthanc::Logging::EnableInfoLevel(true);
144 LOG(INFO) << "Verbose logs are enabled";
145 }
146
147 LOG(ERROR) << "???????? if (parameters.count(\"trace\"))";
148 if (parameters.count("trace"))
149 {
150 LOG(ERROR) << "parameters.count(\"trace\") != 0";
151 Orthanc::Logging::EnableTraceLevel(true);
152 VLOG(1) << "Trace logs are enabled";
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
171 if (parameters.count("username") && parameters.count("password"))
172 {
173 webServiceParameters.SetCredentials(parameters["username"].
174 as<std::string>(),
175 parameters["password"].as<std::string>());
176 }
177
178 LOG(WARNING) << "URL to the Orthanc REST API: " <<
179 webServiceParameters.GetUrl();
180
181 {
182 OrthancPlugins::OrthancHttpConnection orthanc(webServiceParameters);
183 if (!Deprecated::MessagingToolbox::CheckOrthancVersion(orthanc))
184 {
185 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of "
186 << "Orthanc, please upgrade";
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
200 NativeStoneApplicationContext context;
201
202 {
203 // use multiple threads to execute asynchronous tasks like
204 // download content
205 Deprecated::Oracle oracle(6);
206 oracle.Start();
207
208 {
209 boost::shared_ptr<Deprecated::OracleWebService> webService
210 (new Deprecated::OracleWebService(oracle, webServiceParameters, context));
211 context.SetWebService(webService);
212 context.SetOrthancBaseUrl(webServiceParameters.GetUrl());
213
214 Deprecated::OracleDelayedCallExecutor delayedExecutor(oracle, context);
215 context.SetDelayedCallExecutor(delayedExecutor);
216
217 application_->Initialize(&context, statusBar, parameters);
218
219 {
220 NativeStoneApplicationContext::GlobalMutexLocker locker(context);
221 locker.SetCentralWidget(application_->GetCentralWidget());
222 locker.GetCentralViewport().SetStatusBar(statusBar);
223 }
224
225 std::string title = application_->GetTitle();
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 }
243 }
244
245 LOG(WARNING) << "The application is stopping";
246 application_->Finalize();
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 }