forked from bondagit/aes67-linux-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
304 lines (255 loc) · 9.09 KB
/
main.cpp
File metadata and controls
304 lines (255 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// main.cpp
//
// Copyright (c) 2019 2020 Andrea Bondavalli. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <boost/program_options.hpp>
#include <iostream>
#include <thread>
#include "browser.hpp"
#include "config.hpp"
#include "driver_interface.hpp"
#include "http_server.hpp"
#include "interface.hpp"
#include "log.hpp"
#include "mdns_server.hpp"
#include "rtsp_server.hpp"
#include "session_manager.hpp"
#ifdef _USE_STREAMER_
#include "streamer.hpp"
#endif
#ifdef _USE_SYSTEMD_
#include <systemd/sd-daemon.h>
#endif
namespace po = boost::program_options;
namespace postyle = boost::program_options::command_line_style;
namespace logging = boost::log;
static const std::string version("bondagit-2.0.2");
static std::atomic<bool> terminate = false;
void termination_handler(int signum) {
BOOST_LOG_TRIVIAL(info) << "main:: got signal " << signum;
// Terminate program
terminate = true;
}
bool is_terminated() {
return terminate.load();
}
const std::string& get_version() {
return version;
}
int main(int argc, char* argv[]) {
int rc(EXIT_SUCCESS);
po::options_description desc("Options");
desc.add_options()("version,v", "Print daemon version and exit")(
"config,c", po::value<std::string>()->default_value("/etc/daemon.conf"),
"daemon configuration file")("http_addr,a", po::value<std::string>(),
"HTTP server addr")(
"http_port,p", po::value<int>(), "HTTP server port")("help,h",
"Print this help "
"message");
int unix_style = postyle::unix_style | postyle::short_allow_next;
bool driver_restart(true);
#ifdef _USE_SYSTEMD_
// with which interval we should pet the dog
uint64_t current_watchdog_usec;
#endif
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(desc)
.style(unix_style)
.run(),
vm);
po::notify(vm);
if (vm.count("version")) {
std::cout << version << '\n';
return EXIT_SUCCESS;
}
if (vm.count("help")) {
std::cout << "USAGE: " << argv[0] << '\n' << desc << '\n';
return EXIT_SUCCESS;
}
} catch (po::error& poe) {
std::cerr << poe.what() << '\n'
<< "USAGE: " << argv[0] << '\n'
<< desc << '\n';
return EXIT_FAILURE;
}
signal(SIGINT, termination_handler);
signal(SIGTERM, termination_handler);
signal(SIGCHLD, SIG_IGN);
std::srand(std::time(nullptr));
std::string filename = vm["config"].as<std::string>();
#ifdef _USE_SYSTEMD_
sd_watchdog_enabled(0, ¤t_watchdog_usec);
if (current_watchdog_usec > 0) {
// Inform systemd that if we're not petting the dog in 5s we're bust.
sd_notify(0, "WATCHDOG_USEC=5000000");
current_watchdog_usec = 5000000;
}
#endif
while (!is_terminated() && rc == EXIT_SUCCESS) {
/* load configuration from file */
auto config = Config::parse(filename, driver_restart);
if (config == nullptr) {
return EXIT_FAILURE;
}
if (vm.count("http_addr")) {
config->set_http_addr_str(vm["http_addr"].as<std::string>());
}
/* override configuration according to command line args */
if (vm.count("http_port")) {
config->set_http_port(vm["http_port"].as<int>());
}
/* init logging */
log_init(*config);
if (config->get_ip_addr_str().empty()) {
#ifdef _USE_SYSTEMD_
if (current_watchdog_usec > 0)
sd_notify(0, "WATCHDOG=1");
sd_notify(0, "STATUS=no IP address, waiting ...");
#endif
BOOST_LOG_TRIVIAL(info) << "main:: no IP address, waiting ...";
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
BOOST_LOG_TRIVIAL(debug) << "main:: initializing daemon";
try {
auto driver = DriverManager::create();
/* setup and init driver */
if (driver == nullptr || !driver->init(*config)) {
throw std::runtime_error(std::string("DriverManager:: init failed"));
}
/* start browser */
auto browser = Browser::create(config);
if (browser == nullptr || !browser->init()) {
throw std::runtime_error(std::string("Browser:: init failed"));
}
/* start session manager */
auto session_manager = SessionManager::create(driver, browser, config);
if (session_manager == nullptr || !session_manager->init()) {
throw std::runtime_error(std::string("SessionManager:: init failed"));
}
/* start mDNS server */
MDNSServer mdns_server(session_manager, config);
if (config->get_mdns_enabled() && !mdns_server.init()) {
throw std::runtime_error(std::string("MDNSServer:: init failed"));
}
/* start rtsp server */
RtspServer rtsp_server(session_manager, config);
if (!rtsp_server.init()) {
throw std::runtime_error(std::string("RtspServer:: init failed"));
}
/* start streamer */
#ifdef _USE_STREAMER_
auto streamer = Streamer::create(session_manager, config);
if (config->get_streamer_enabled() &&
(streamer == nullptr || !streamer->init())) {
throw std::runtime_error(std::string("Streamer:: init failed"));
}
/* start http server */
HttpServer http_server(session_manager, browser, streamer, config);
#else
HttpServer http_server(session_manager, browser, config);
#endif
if (!http_server.init()) {
throw std::runtime_error(std::string("HttpServer:: init failed"));
}
/* load session status from file */
session_manager->load_status();
BOOST_LOG_TRIVIAL(debug) << "main:: init done, entering loop...";
#ifdef _USE_SYSTEMD_
// To be able to use sd_notify at all have to set service NotifyAccess
// (e.g. to main)
sd_notify(0, "READY=1"); // If service Type=notify the service is only
// considered ready once we send this (this is
// independent of watchdog capability)
sd_notify(0, "STATUS=Working");
#endif
while (!is_terminated()) {
#ifdef _USE_SYSTEMD_
if (current_watchdog_usec > 0)
sd_notify(0, "WATCHDOG=1");
#endif
auto [ip_addr, ip_str] = get_interface_ip(config->get_interface_name());
if (config->get_ip_addr_str() != ip_str) {
BOOST_LOG_TRIVIAL(warning)
<< "main:: IP address changed, restarting ...";
break;
}
driver_restart = config->get_driver_restart();
if (config->get_daemon_restart()) {
BOOST_LOG_TRIVIAL(warning) << "main:: config changed, restarting ...";
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
#ifdef _USE_SYSTEMD_
if (is_terminated()) {
sd_notify(0, "STOPPING=1");
sd_notify(0, "STATUS=Stopping");
} else {
sd_notify(0, "RELOADING=1");
sd_notify(0, "STATUS=Restarting");
}
#endif
/* save session status to file */
session_manager->save_status();
/* stop http server */
if (!http_server.terminate()) {
throw std::runtime_error(std::string("HttpServer:: terminate failed"));
}
/* stop streamer */
#ifdef _USE_STREAMER_
if (config->get_streamer_enabled()) {
if (!streamer->terminate()) {
throw std::runtime_error(std::string("Streamer:: terminate failed"));
}
}
#endif
/* stop rtsp server */
if (!rtsp_server.terminate()) {
throw std::runtime_error(std::string("RtspServer:: terminate failed"));
}
/* stop mDNS server */
if (config->get_mdns_enabled()) {
if (!mdns_server.terminate()) {
throw std::runtime_error(std::string("MDNServer:: terminate failed"));
}
}
/* stop session manager */
if (!session_manager->terminate()) {
throw std::runtime_error(
std::string("SessionManager:: terminate failed"));
}
/* stop browser */
if (!browser->terminate()) {
throw std::runtime_error(std::string("Browser:: terminate failed"));
}
/* stop driver manager */
if (!driver->terminate(*config)) {
throw std::runtime_error(
std::string("DriverManager:: terminate failed"));
}
} catch (std::exception& e) {
BOOST_LOG_TRIVIAL(fatal) << "main:: fatal exception error: " << e.what();
rc = EXIT_FAILURE;
}
BOOST_LOG_TRIVIAL(info) << "main:: end ";
}
std::cout << "daemon exiting with code: " << rc << std::endl;
return rc;
}