comparison Applications/BasicApplication.cpp @ 221:d7b2590744f8 am

wip: building applications reusable in SDL and WASM
author am@osimis.io
date Mon, 11 Jun 2018 14:01:02 +0200
parents
children
comparison
equal deleted inserted replaced
219:26e3bfe30e66 221:d7b2590744f8
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 #include "IBasicApplication.h"
23
24 #include "../Framework/Toolbox/MessagingToolbox.h"
25 #include "Sdl/SdlEngine.h"
26
27 #include <Core/Logging.h>
28 #include <Core/HttpClient.h>
29 #include <Plugins/Samples/Common/OrthancHttpConnection.h>
30
31 namespace OrthancStone
32 {
33 // Anonymous namespace to avoid clashes against other compilation modules
34 namespace
35 {
36 class LogStatusBar : public IStatusBar
37 {
38 public:
39 virtual void ClearMessage()
40 {
41 }
42
43 virtual void SetMessage(const std::string& message)
44 {
45 LOG(WARNING) << message;
46 }
47 };
48 }
49
50
51 #if ORTHANC_ENABLE_SDL == 1
52 static void DeclareSdlCommandLineOptions(boost::program_options::options_description& options)
53 {
54 // Declare the supported parameters
55 boost::program_options::options_description generic("Generic options");
56 generic.add_options()
57 ("help", "Display this help and exit")
58 ("verbose", "Be verbose in logs")
59 ("orthanc", boost::program_options::value<std::string>()->default_value("http://localhost:8042/"),
60 "URL to the Orthanc server")
61 ("username", "Username for the Orthanc server")
62 ("password", "Password for the Orthanc server")
63 ("https-verify", boost::program_options::value<bool>()->default_value(true), "Check HTTPS certificates")
64 ;
65
66 options.add(generic);
67
68 boost::program_options::options_description sdl("SDL options");
69 sdl.add_options()
70 ("width", boost::program_options::value<int>()->default_value(1024), "Initial width of the SDL window")
71 ("height", boost::program_options::value<int>()->default_value(768), "Initial height of the SDL window")
72 ("opengl", boost::program_options::value<bool>()->default_value(true), "Enable OpenGL in SDL")
73 ;
74
75 options.add(sdl);
76 }
77
78
79 int IBasicApplication::ExecuteWithSdl(IBasicApplication& application,
80 int argc,
81 char* argv[])
82 {
83 /******************************************************************
84 * Initialize all the subcomponents of Orthanc Stone
85 ******************************************************************/
86
87 Orthanc::Logging::Initialize();
88 Orthanc::HttpClient::InitializeOpenSsl();
89 Orthanc::HttpClient::GlobalInitialize();
90 SdlWindow::GlobalInitialize();
91
92
93 /******************************************************************
94 * Declare and parse the command-line options of the application
95 ******************************************************************/
96
97 boost::program_options::options_description options;
98 DeclareSdlCommandLineOptions(options);
99 application.DeclareCommandLineOptions(options);
100
101 boost::program_options::variables_map parameters;
102 bool error = false;
103
104 try
105 {
106 boost::program_options::store(boost::program_options::command_line_parser(argc, argv).
107 options(options).run(), parameters);
108 boost::program_options::notify(parameters);
109 }
110 catch (boost::program_options::error& e)
111 {
112 LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what();
113 error = true;
114 }
115
116
117 /******************************************************************
118 * Configure the application with the command-line parameters
119 ******************************************************************/
120
121 if (error || parameters.count("help"))
122 {
123 std::cout << std::endl
124 << "Usage: " << argv[0] << " [OPTION]..."
125 << std::endl
126 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research."
127 << std::endl << std::endl
128 << "Demonstration application of Orthanc Stone using SDL."
129 << std::endl;
130
131 std::cout << options << "\n";
132 return error ? -1 : 0;
133 }
134
135 if (parameters.count("https-verify") &&
136 !parameters["https-verify"].as<bool>())
137 {
138 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)";
139 Orthanc::HttpClient::ConfigureSsl(false, "");
140 }
141
142 if (parameters.count("verbose"))
143 {
144 Orthanc::Logging::EnableInfoLevel(true);
145 }
146
147 if (!parameters.count("width") ||
148 !parameters.count("height") ||
149 !parameters.count("opengl"))
150 {
151 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing";
152 return -1;
153 }
154
155 int w = parameters["width"].as<int>();
156 int h = parameters["height"].as<int>();
157 if (w <= 0 || h <= 0)
158 {
159 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive";
160 return -1;
161 }
162
163 unsigned int width = static_cast<unsigned int>(w);
164 unsigned int height = static_cast<unsigned int>(h);
165 LOG(WARNING) << "Initial display size: " << width << "x" << height;
166
167 bool opengl = parameters["opengl"].as<bool>();
168 if (opengl)
169 {
170 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes";
171 }
172 else
173 {
174 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance";
175 }
176
177 bool success = true;
178 try
179 {
180 /****************************************************************
181 * Initialize the connection to the Orthanc server
182 ****************************************************************/
183
184 Orthanc::WebServiceParameters webService;
185
186 if (parameters.count("orthanc"))
187 {
188 webService.SetUrl(parameters["orthanc"].as<std::string>());
189 }
190
191 if (parameters.count("username"))
192 {
193 webService.SetUsername(parameters["username"].as<std::string>());
194 }
195
196 if (parameters.count("password"))
197 {
198 webService.SetPassword(parameters["password"].as<std::string>());
199 }
200
201 LOG(WARNING) << "URL to the Orthanc REST API: " << webService.GetUrl();
202
203 {
204 OrthancPlugins::OrthancHttpConnection orthanc(webService);
205 if (!MessagingToolbox::CheckOrthancVersion(orthanc))
206 {
207 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of Orthanc, please upgrade";
208 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);
209 }
210 }
211
212
213 /****************************************************************
214 * Initialize the application
215 ****************************************************************/
216
217 LOG(WARNING) << "Creating the widgets of the application";
218
219 LogStatusBar statusBar;
220 BasicApplicationContext context(webService);
221
222 application.Initialize(context, statusBar, parameters);
223
224 {
225 BasicApplicationContext::ViewportLocker locker(context);
226 locker.GetViewport().SetStatusBar(statusBar);
227 }
228
229 std::string title = application.GetTitle();
230 if (title.empty())
231 {
232 title = "Stone of Orthanc";
233 }
234
235 {
236 /**************************************************************
237 * Run the application inside a SDL window
238 **************************************************************/
239
240 LOG(WARNING) << "Starting the application";
241
242 SdlWindow window(title.c_str(), width, height, opengl);
243 SdlEngine sdl(window, context);
244
245 {
246 BasicApplicationContext::ViewportLocker locker(context);
247 locker.GetViewport().Register(sdl); // (*)
248 }
249
250 context.Start();
251 sdl.Run();
252
253 LOG(WARNING) << "Stopping the application";
254
255 // Don't move the "Stop()" command below out of the block,
256 // otherwise the application might crash, because the
257 // "SdlEngine" is an observer of the viewport (*) and the
258 // update thread started by "context.Start()" would call a
259 // destructed object (the "SdlEngine" is deleted with the
260 // lexical scope).
261 context.Stop();
262 }
263
264
265 /****************************************************************
266 * Finalize the application
267 ****************************************************************/
268
269 LOG(WARNING) << "The application has stopped";
270 application.Finalize();
271 }
272 catch (Orthanc::OrthancException& e)
273 {
274 LOG(ERROR) << "EXCEPTION: " << e.What();
275 success = false;
276 }
277
278
279 /******************************************************************
280 * Finalize all the subcomponents of Orthanc Stone
281 ******************************************************************/
282
283 SdlWindow::GlobalFinalize();
284 Orthanc::HttpClient::GlobalFinalize();
285 Orthanc::HttpClient::FinalizeOpenSsl();
286
287 return (success ? 0 : -1);
288 }
289 #endif
290
291 }