-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipPosition.cpp
213 lines (176 loc) · 5.04 KB
/
ShipPosition.cpp
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
/*
* Copyright (C) 2024 - 2025 Mikhail Sapozhnikov
*
* This file is part of ship-position.
*
* ship-position 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
* (at your option) any later version.
*
* ship-position 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 ship-position. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ShipPosition.hpp"
#include <signal.h>
#include <cstring>
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
extern void signal_handler(int sig);
namespace ship_position
{
ShipPosition::ShipPosition() :
_syslog(nullptr),
_consoleLog(nullptr),
_config(nullptr),
_stopRequested(false),
_bn880gpsReader(nullptr),
_qmc5883lReader(nullptr),
_unixListener(nullptr)
{
_log = Log::getInstance();
}
ShipPosition::~ShipPosition()
{
Log::release();
delete _syslog;
delete _consoleLog;
delete _config;
delete _bn880gpsReader;
delete _qmc5883lReader;
delete _unixListener;
}
int ShipPosition::run(int argc, char **argv)
{
_log->write(LogLevel::NOTICE, "Ship position starting\n");
int ret = init(argc, argv);
if (ret != RETVAL_OK)
{
if (ret == RETVAL_HELP)
{
return RETVAL_OK;
}
else
{
_log->write(LogLevel::ERROR, "ShipPosition::init() returned %d\n", ret);
return ret;
}
}
_bn880gpsReader->start();
_qmc5883lReader->start();
_unixListener->start();
while (true)
{
if (_stopRequested)
{
_log->write(LogLevel::NOTICE, "Ship position stopping\n");
_unixListener->stop();
_bn880gpsReader->stop();
break;
}
std::vector<int> clients = _unixListener->getRunningClients();
_log->write(LogLevel::NOTICE, "Number of IPC clients: %d\n", clients.size());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return RETVAL_OK;
}
void ShipPosition::stop()
{
_stopRequested = true;
}
int ShipPosition::init(int argc, char **argv)
{
int ret = handleCmdLine(argc, argv);
if (ret != RETVAL_OK)
{
return ret;
}
_config = new Config(_configPath);
if (!_config->isOk())
{
return RETVAL_INVALID_CONFIG;
}
// setup logging
if (_config->isSyslogEnabled())
{
_syslog = new SysLog();
_log->add_backend(_syslog);
}
if (_config->isConsoleLogEnabled())
{
_consoleLog = new ConsoleLog();
_log->add_backend(_consoleLog);
}
_log->set_level(_config->getLogLevel());
_config->getBN880GPSConfig(_bn880gpsConfig);
_config->getQMC5883LConfig(_qmc5883lConfig);
_config->getIPCConfig(_ipcConfig);
_bn880gpsReader = new BN880GPSReader(_bn880gpsConfig);
_qmc5883lReader = new QMC5883LReader(_qmc5883lConfig);
_unixListener = new UnixListener(_ipcConfig, *_bn880gpsReader, *_qmc5883lReader);
setupSignals();
return RETVAL_OK;
}
int ShipPosition::handleCmdLine(int argc, char **argv)
{
try
{
po::variables_map opts;
po::options_description opt_descr("Available options:");
opt_descr.add_options()
("help", "print help message")
("config", po::value<std::string>(), "config file path");
po::store(po::parse_command_line(argc, argv, opt_descr), opts);
po::notify(opts);
if (opts.count("help"))
{
std::cout << "Usage: " << argv[0] << " [options]\n";
std::cout << opt_descr << "\n";
return RETVAL_HELP;
}
if (opts.count("config"))
{
_configPath = opts["config"].as<std::string>();
}
else
{
_configPath = DEFAULT_CONFIG_PATH;
}
return RETVAL_OK;
}
catch(const std::exception& e)
{
_log->write(LogLevel::ERROR, "Error processing command line: %s", e.what());
return RETVAL_INVALID_CMDLINE;
}
}
void ShipPosition::setupSignals()
{
struct sigaction act;
sigset_t sigset;
// set of signals to be blocked during signal handling
sigemptyset(&sigset);
sigaddset(&sigset, SIGINT);
sigaddset(&sigset, SIGQUIT);
sigaddset(&sigset, SIGTERM);
act.sa_handler = signal_handler;
act.sa_mask = sigset;
act.sa_flags = 0;
sigaction(SIGINT, &act, nullptr);
sigaction(SIGQUIT, &act, nullptr);
sigaction(SIGTERM, &act, nullptr);
// ignore SIGPIPE
struct sigaction ignore_act;
std::memset(static_cast<void *>(&ignore_act), 0, sizeof(struct sigaction));
ignore_act.sa_handler = SIG_IGN;
ignore_act.sa_flags = 0;
sigaction(SIGPIPE, &ignore_act, nullptr);
}
}