Mercurial > hg > orthanc-stone
annotate Framework/Applications/IBasicApplication.cpp @ 38:bfce0add15f2
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 19 Dec 2016 12:55:14 +0100 |
parents | 9aace933cb64 |
children | 7207a407bcd8 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "IBasicApplication.h" | |
34 | |
16 | 35 #include "../../Resources/Orthanc/Core/Logging.h" |
36 #include "../../Resources/Orthanc/Core/HttpClient.h" | |
31
9aace933cb64
sharing code with the Orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
37 #include "../../Resources/Orthanc/Plugins/Samples/Common/OrthancHttpConnection.h" |
0 | 38 #include "Sdl/SdlEngine.h" |
39 | |
40 namespace OrthancStone | |
41 { | |
42 // Anonymous namespace to avoid clashes against other compilation modules | |
43 namespace | |
44 { | |
45 class LogStatusBar : public IStatusBar | |
46 { | |
47 public: | |
48 virtual void ClearMessage() | |
49 { | |
50 } | |
51 | |
52 virtual void SetMessage(const std::string& message) | |
53 { | |
54 LOG(WARNING) << message; | |
55 } | |
56 }; | |
57 } | |
58 | |
59 | |
60 #if ORTHANC_ENABLE_SDL == 1 | |
61 static void DeclareSdlCommandLineOptions(boost::program_options::options_description& options) | |
62 { | |
63 // Declare the supported parameters | |
64 boost::program_options::options_description generic("Generic options"); | |
65 generic.add_options() | |
66 ("help", "Display this help and exit") | |
67 ("verbose", "Be verbose in logs") | |
68 ("orthanc", boost::program_options::value<std::string>()->default_value("http://localhost:8042/"), | |
69 "URL to the Orthanc server") | |
70 ("username", "Username for the Orthanc server") | |
71 ("password", "Password for the Orthanc server") | |
72 ("https-verify", boost::program_options::value<bool>()->default_value(true), "Check HTTPS certificates") | |
73 ; | |
74 | |
75 options.add(generic); | |
76 | |
77 boost::program_options::options_description sdl("SDL options"); | |
78 sdl.add_options() | |
79 ("width", boost::program_options::value<int>()->default_value(1024), "Initial width of the SDL window") | |
80 ("height", boost::program_options::value<int>()->default_value(768), "Initial height of the SDL window") | |
81 ("opengl", boost::program_options::value<bool>()->default_value(true), "Enable OpenGL in SDL") | |
82 ; | |
83 | |
84 options.add(sdl); | |
85 } | |
86 | |
87 | |
88 int IBasicApplication::ExecuteWithSdl(IBasicApplication& application, | |
89 int argc, | |
90 char* argv[]) | |
91 { | |
92 /****************************************************************** | |
93 * Initialize all the subcomponents of Orthanc Stone | |
94 ******************************************************************/ | |
95 | |
96 Orthanc::Logging::Initialize(); | |
97 Orthanc::HttpClient::InitializeOpenSsl(); | |
98 Orthanc::HttpClient::GlobalInitialize(); | |
99 SdlEngine::GlobalInitialize(); | |
100 | |
101 | |
102 /****************************************************************** | |
103 * Declare and parse the command-line options of the application | |
104 ******************************************************************/ | |
105 | |
106 boost::program_options::options_description options; | |
107 DeclareSdlCommandLineOptions(options); | |
108 application.DeclareCommandLineOptions(options); | |
109 | |
110 boost::program_options::variables_map parameters; | |
111 bool error = false; | |
112 | |
113 try | |
114 { | |
115 boost::program_options::store(boost::program_options::command_line_parser(argc, argv). | |
116 options(options).run(), parameters); | |
117 boost::program_options::notify(parameters); | |
118 } | |
119 catch (boost::program_options::error& e) | |
120 { | |
121 LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what(); | |
122 error = true; | |
123 } | |
124 | |
125 | |
126 /****************************************************************** | |
127 * Configure the application with the command-line parameters | |
128 ******************************************************************/ | |
129 | |
130 if (error || parameters.count("help")) | |
131 { | |
132 std::cout << std::endl | |
133 << "Usage: " << argv[0] << " [OPTION]..." | |
134 << std::endl | |
135 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." | |
136 << std::endl << std::endl | |
137 << "Demonstration application of Orthanc Stone using SDL." | |
138 << std::endl; | |
139 | |
140 std::cout << options << "\n"; | |
141 return error ? -1 : 0; | |
142 } | |
143 | |
144 if (parameters.count("https-verify") && | |
145 !parameters["https-verify"].as<bool>()) | |
146 { | |
147 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)"; | |
148 Orthanc::HttpClient::ConfigureSsl(false, ""); | |
149 } | |
150 | |
151 if (parameters.count("verbose")) | |
152 { | |
153 Orthanc::Logging::EnableInfoLevel(true); | |
154 } | |
155 | |
156 if (!parameters.count("width") || | |
157 !parameters.count("height") || | |
158 !parameters.count("opengl")) | |
159 { | |
160 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing"; | |
161 return -1; | |
162 } | |
163 | |
164 int w = parameters["width"].as<int>(); | |
165 int h = parameters["height"].as<int>(); | |
166 if (w <= 0 || h <= 0) | |
167 { | |
168 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive"; | |
169 return -1; | |
170 } | |
171 | |
172 unsigned int width = static_cast<unsigned int>(w); | |
173 unsigned int height = static_cast<unsigned int>(h); | |
174 LOG(WARNING) << "Initial display size: " << width << "x" << height; | |
175 | |
176 bool opengl = parameters["opengl"].as<bool>(); | |
177 if (opengl) | |
178 { | |
179 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes"; | |
180 } | |
181 else | |
182 { | |
183 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance"; | |
184 } | |
185 | |
186 bool success = true; | |
187 try | |
188 { | |
189 /**************************************************************** | |
190 * Initialize the connection to the Orthanc server | |
191 ****************************************************************/ | |
192 | |
193 Orthanc::WebServiceParameters webService; | |
194 | |
195 if (parameters.count("orthanc")) | |
196 { | |
197 webService.SetUrl(parameters["orthanc"].as<std::string>()); | |
198 } | |
199 | |
200 if (parameters.count("username")) | |
201 { | |
202 webService.SetUsername(parameters["username"].as<std::string>()); | |
203 } | |
204 | |
205 if (parameters.count("password")) | |
206 { | |
207 webService.SetPassword(parameters["password"].as<std::string>()); | |
208 } | |
209 | |
210 LOG(WARNING) << "URL to the Orthanc REST API: " << webService.GetUrl(); | |
31
9aace933cb64
sharing code with the Orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
211 OrthancPlugins::OrthancHttpConnection orthanc(webService); |
0 | 212 |
213 if (!MessagingToolbox::CheckOrthancVersion(orthanc)) | |
214 { | |
215 LOG(ERROR) << "Your version of Orthanc is incompatible with Orthanc Stone, please upgrade"; | |
216 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
217 } | |
218 | |
219 | |
220 /**************************************************************** | |
221 * Initialize the application | |
222 ****************************************************************/ | |
223 | |
224 LogStatusBar statusBar; | |
225 BasicApplicationContext context(orthanc); | |
226 | |
227 application.Initialize(context, statusBar, parameters); | |
228 context.GetViewport().SetStatusBar(statusBar); | |
229 | |
230 std::string title = application.GetTitle(); | |
231 if (title.empty()) | |
232 { | |
233 title = "Stone of Orthanc"; | |
234 } | |
235 | |
236 context.Start(); | |
237 | |
238 { | |
239 /************************************************************** | |
240 * Run the application inside a SDL window | |
241 **************************************************************/ | |
242 | |
243 LOG(WARNING) << "Starting the application"; | |
244 | |
245 SdlWindow window(title.c_str(), width, height, opengl); | |
246 SdlEngine sdl(window, context.GetViewport()); | |
247 | |
248 sdl.Run(); | |
249 | |
250 LOG(WARNING) << "Stopping the application"; | |
251 } | |
252 | |
253 | |
254 /**************************************************************** | |
255 * Finalize the application | |
256 ****************************************************************/ | |
257 | |
258 context.Stop(); | |
259 | |
260 LOG(WARNING) << "The application has stopped"; | |
261 | |
262 context.GetViewport().ResetStatusBar(); | |
263 application.Finalize(); | |
264 } | |
265 catch (Orthanc::OrthancException& e) | |
266 { | |
267 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
268 success = false; | |
269 } | |
270 | |
271 | |
272 /****************************************************************** | |
273 * Finalize all the subcomponents of Orthanc Stone | |
274 ******************************************************************/ | |
275 | |
276 SdlEngine::GlobalFinalize(); | |
277 Orthanc::HttpClient::GlobalFinalize(); | |
278 Orthanc::HttpClient::FinalizeOpenSsl(); | |
279 | |
280 return (success ? 0 : -1); | |
281 } | |
282 #endif | |
283 | |
284 } |