comparison Applications/IBasicApplication.cpp @ 260:c887eddd48f1 am-2

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