comparison Applications/Generic/BasicNativeApplicationRunner.cpp @ 285:3c190e2bb3af am-2

refactoring: ApplicationRunner + app hierarchy
author am@osimis.io
date Tue, 28 Aug 2018 15:26:46 +0200
parents
children
comparison
equal deleted inserted replaced
284:38b0ac8055b9 285:3c190e2bb3af
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-2018 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_NATIVE != 1
23 #error this file shall be included only with the ORTHANC_ENABLE_NATIVE set to 1
24 #endif
25
26 #include "BasicNativeApplicationRunner.h"
27 #include "BasicNativeApplicationContext.h"
28 #include <boost/program_options.hpp>
29
30 #include "../../Framework/Toolbox/MessagingToolbox.h"
31
32 #include <Core/Logging.h>
33 #include <Core/HttpClient.h>
34 #include <Core/Toolbox.h>
35 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
36 #include "../../Platforms/Generic/OracleWebService.h"
37
38 namespace OrthancStone
39 {
40 // Anonymous namespace to avoid clashes against other compilation modules
41 namespace
42 {
43 class LogStatusBar : public IStatusBar
44 {
45 public:
46 virtual void ClearMessage()
47 {
48 }
49
50 virtual void SetMessage(const std::string& message)
51 {
52 LOG(WARNING) << message;
53 }
54 };
55 }
56
57 int BasicNativeApplicationRunner::Execute(int argc,
58 char* argv[])
59 {
60 /******************************************************************
61 * Initialize all the subcomponents of Orthanc Stone
62 ******************************************************************/
63
64 Orthanc::Logging::Initialize();
65 Orthanc::Toolbox::InitializeOpenSsl();
66 Orthanc::HttpClient::GlobalInitialize();
67
68 Initialize();
69
70 /******************************************************************
71 * Declare and parse the command-line options of the application
72 ******************************************************************/
73
74 boost::program_options::options_description options;
75
76 { // generic options
77 boost::program_options::options_description generic("Generic options");
78 generic.add_options()
79 ("help", "Display this help and exit")
80 ("verbose", "Be verbose in logs")
81 ("orthanc", boost::program_options::value<std::string>()->default_value("http://localhost:8042/"),
82 "URL to the Orthanc server")
83 ("username", "Username for the Orthanc server")
84 ("password", "Password for the Orthanc server")
85 ("https-verify", boost::program_options::value<bool>()->default_value(true), "Check HTTPS certificates")
86 ;
87
88 options.add(generic);
89 }
90
91 // platform specific options
92 DeclareCommandLineOptions(options);
93
94 // application specific options
95 application_.DeclareStartupOptions(options);
96
97 boost::program_options::variables_map parameters;
98 bool error = false;
99
100 try
101 {
102 boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
103 options(options).run(), parameters);
104 boost::program_options::notify(parameters);
105 }
106 catch (boost::program_options::error& e)
107 {
108 LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what();
109 error = true;
110 }
111
112
113 /******************************************************************
114 * Configure the application with the command-line parameters
115 ******************************************************************/
116
117 if (error || parameters.count("help"))
118 {
119 std::cout << std::endl
120 << "Usage: " << argv[0] << " [OPTION]..."
121 << std::endl
122 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research."
123 << std::endl << std::endl
124 << "Demonstration application of Orthanc Stone using SDL."
125 << std::endl;
126
127 std::cout << options << "\n";
128 return error ? -1 : 0;
129 }
130
131 if (parameters.count("https-verify") &&
132 !parameters["https-verify"].as<bool>())
133 {
134 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)";
135 Orthanc::HttpClient::ConfigureSsl(false, "");
136 }
137
138 if (parameters.count("verbose"))
139 {
140 Orthanc::Logging::EnableInfoLevel(true);
141 }
142
143 ParseCommandLineOptions(parameters);
144
145
146 bool success = true;
147 try
148 {
149 /****************************************************************
150 * Initialize the connection to the Orthanc server
151 ****************************************************************/
152
153 Orthanc::WebServiceParameters webServiceParameters;
154
155 if (parameters.count("orthanc"))
156 {
157 webServiceParameters.SetUrl(parameters["orthanc"].as<std::string>());
158 }
159
160 if (parameters.count("username"))
161 {
162 webServiceParameters.SetUsername(parameters["username"].as<std::string>());
163 }
164
165 if (parameters.count("password"))
166 {
167 webServiceParameters.SetPassword(parameters["password"].as<std::string>());
168 }
169
170 LOG(WARNING) << "URL to the Orthanc REST API: " << webServiceParameters.GetUrl();
171
172 {
173 OrthancPlugins::OrthancHttpConnection orthanc(webServiceParameters);
174 if (!MessagingToolbox::CheckOrthancVersion(orthanc))
175 {
176 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of Orthanc, please upgrade";
177 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
178 }
179 }
180
181
182 /****************************************************************
183 * Initialize the application
184 ****************************************************************/
185
186 LOG(WARNING) << "Creating the widgets of the application";
187
188 LogStatusBar statusBar;
189
190 BasicNativeApplicationContext context;
191 Oracle oracle(4); // use 4 threads to download content
192 OracleWebService webService(broker_, oracle, webServiceParameters, context);
193 context.SetWebService(webService);
194
195 application_.Initialize(&context, statusBar, parameters);
196
197 {
198 BasicNativeApplicationContext::GlobalMutexLocker locker(context);
199 context.SetCentralWidget(application_.GetCentralWidget());
200 context.GetCentralViewport().SetStatusBar(statusBar);
201 }
202
203 std::string title = application_.GetTitle();
204 if (title.empty())
205 {
206 title = "Stone of Orthanc";
207 }
208
209 /****************************************************************
210 * Run the application
211 ****************************************************************/
212
213 Run(context, title, argc, argv);
214
215 /****************************************************************
216 * Finalize the application
217 ****************************************************************/
218
219 LOG(WARNING) << "The application is stopping";
220 application_.Finalize();
221 }
222 catch (Orthanc::OrthancException& e)
223 {
224 LOG(ERROR) << "EXCEPTION: " << e.What();
225 success = false;
226 }
227
228
229 /******************************************************************
230 * Finalize all the subcomponents of Orthanc Stone
231 ******************************************************************/
232
233 Finalize();
234 Orthanc::HttpClient::GlobalFinalize();
235 Orthanc::Toolbox::FinalizeOpenSsl();
236
237 return (success ? 0 : -1);
238 }
239
240 }