comparison Applications/Generic/BasicNativeApplication.cpp @ 274:dc1beee33134 am-2

split SdlApplication into NativeApplication and SdlApplication
author am@osimis.io
date Fri, 24 Aug 2018 13:52:55 +0200
parents
children 5de5699ad570
comparison
equal deleted inserted replaced
273:f21ba2468570 274:dc1beee33134
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 "BasicNativeApplication.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 BasicNativeApplication::Execute(MessageBroker& broker,
58 IBasicApplication& application,
59 int argc,
60 char* argv[])
61 {
62 /******************************************************************
63 * Initialize all the subcomponents of Orthanc Stone
64 ******************************************************************/
65
66 Orthanc::Logging::Initialize();
67 Orthanc::Toolbox::InitializeOpenSsl();
68 Orthanc::HttpClient::GlobalInitialize();
69
70 Initialize();
71
72 /******************************************************************
73 * Declare and parse the command-line options of the application
74 ******************************************************************/
75
76 boost::program_options::options_description options;
77 DeclareCommandLineOptions(options);
78 application.DeclareStartupOptions(options);
79
80 boost::program_options::variables_map parameters;
81 bool error = false;
82
83 try
84 {
85 boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
86 options(options).run(), parameters);
87 boost::program_options::notify(parameters);
88 }
89 catch (boost::program_options::error& e)
90 {
91 LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what();
92 error = true;
93 }
94
95
96 /******************************************************************
97 * Configure the application with the command-line parameters
98 ******************************************************************/
99
100 if (error || parameters.count("help"))
101 {
102 std::cout << std::endl
103 << "Usage: " << argv[0] << " [OPTION]..."
104 << std::endl
105 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research."
106 << std::endl << std::endl
107 << "Demonstration application of Orthanc Stone using SDL."
108 << std::endl;
109
110 std::cout << options << "\n";
111 return error ? -1 : 0;
112 }
113
114 if (parameters.count("https-verify") &&
115 !parameters["https-verify"].as<bool>())
116 {
117 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)";
118 Orthanc::HttpClient::ConfigureSsl(false, "");
119 }
120
121 if (parameters.count("verbose"))
122 {
123 Orthanc::Logging::EnableInfoLevel(true);
124 }
125
126 if (!parameters.count("width") ||
127 !parameters.count("height") ||
128 !parameters.count("opengl"))
129 {
130 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing";
131 return -1;
132 }
133
134 int w = parameters["width"].as<int>();
135 int h = parameters["height"].as<int>();
136 if (w <= 0 || h <= 0)
137 {
138 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive";
139 return -1;
140 }
141
142 unsigned int width = static_cast<unsigned int>(w);
143 unsigned int height = static_cast<unsigned int>(h);
144 LOG(WARNING) << "Initial display size: " << width << "x" << height;
145
146 bool opengl = parameters["opengl"].as<bool>();
147 if (opengl)
148 {
149 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes";
150 }
151 else
152 {
153 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance";
154 }
155
156 bool success = true;
157 try
158 {
159 /****************************************************************
160 * Initialize the connection to the Orthanc server
161 ****************************************************************/
162
163 Orthanc::WebServiceParameters webServiceParameters;
164
165 if (parameters.count("orthanc"))
166 {
167 webServiceParameters.SetUrl(parameters["orthanc"].as<std::string>());
168 }
169
170 if (parameters.count("username"))
171 {
172 webServiceParameters.SetUsername(parameters["username"].as<std::string>());
173 }
174
175 if (parameters.count("password"))
176 {
177 webServiceParameters.SetPassword(parameters["password"].as<std::string>());
178 }
179
180 LOG(WARNING) << "URL to the Orthanc REST API: " << webServiceParameters.GetUrl();
181
182 {
183 OrthancPlugins::OrthancHttpConnection orthanc(webServiceParameters);
184 if (!MessagingToolbox::CheckOrthancVersion(orthanc))
185 {
186 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of 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 BasicNativeApplicationContext context;
201 Oracle oracle(4); // use 4 threads to download content
202 OracleWebService webService(broker, oracle, webServiceParameters, context);
203 context.SetWebService(webService);
204
205 application.Initialize(&context, statusBar, parameters);
206
207 {
208 BasicNativeApplicationContext::GlobalMutexLocker locker(context);
209 context.SetCentralWidget(application.GetCentralWidget());
210 context.GetCentralViewport().SetStatusBar(statusBar);
211 }
212
213 std::string title = application.GetTitle();
214 if (title.empty())
215 {
216 title = "Stone of Orthanc";
217 }
218
219 /****************************************************************
220 * Run the application
221 ****************************************************************/
222
223 Run(context, title, width, height, opengl);
224
225
226 /****************************************************************
227 * Finalize the application
228 ****************************************************************/
229
230 LOG(WARNING) << "The application has stopped";
231 application.Finalize();
232 }
233 catch (Orthanc::OrthancException& e)
234 {
235 LOG(ERROR) << "EXCEPTION: " << e.What();
236 success = false;
237 }
238
239
240 /******************************************************************
241 * Finalize all the subcomponents of Orthanc Stone
242 ******************************************************************/
243
244 Finalize();
245 Orthanc::HttpClient::GlobalFinalize();
246 Orthanc::Toolbox::FinalizeOpenSsl();
247
248 return (success ? 0 : -1);
249 }
250
251 }