comparison Applications/IBasicApplication.cpp @ 51:b340879da9bd

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