-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPCClient.cpp
131 lines (115 loc) · 3.49 KB
/
IPCClient.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
/*
* Copyright (C) 2024 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 "IPCClient.hpp"
#include "IPCMessages.hpp"
#include <unistd.h>
using json = nlohmann::json;
namespace ship_position
{
IPCClient::IPCClient(int id, int fd, const IPCConfig &config, GPSReader &gpsReader, std::function<void(int)> stopCb)
: _id(id),
_fd(fd),
_config(config),
_gpsReader(gpsReader),
_stopCb(stopCb)
{
_buf = static_cast<char *>(new char[_config.bufSize]);
_log = Log::getInstance();
}
IPCClient::~IPCClient()
{
Log::release();
delete _buf;
}
void IPCClient::run()
{
if (_fd == -1)
{
return;
}
_log->write(LogLevel::NOTICE, "IPCClient %d started\n", _id);
while (true)
{
if (need_to_stop() == true)
{
break;
}
int len = read(_fd, reinterpret_cast<void *>(_buf), _config.bufSize - 1);
if (len == -1)
{
_log->write(LogLevel::ERROR, "IPCClient %d failed to read data from socket, error code %d\n", _id, errno);
break;
}
if (len == 0)
{
_log->write(LogLevel::NOTICE, "IPCClient %d failed to read data from socket, socket closed\n", _id);
break;
}
_buf[len] = '\0';
std::string resp = handleRequest(std::string(_buf));
if (write(_fd, reinterpret_cast<const void *>(resp.c_str()), resp.length()) == -1)
{
_log->write(LogLevel::ERROR, "IPCClient %d failed to write data into socket, error code %d\n", _id, errno);
break;
}
}
close(_fd);
_stopCb(_id);
_log->write(LogLevel::NOTICE, "IPCClient %d stopped\n", _id);
}
void IPCClient::stop()
{
SingleThread::stop();
}
std::string IPCClient::handleRequest(const std::string &rq)
{
_log->write(LogLevel::DEBUG, "IPCClient %d handling request %s\n", _id, rq.c_str());
try
{
json json_rq = json::parse(rq);
IPCRequest ipcRq = json_rq.get<IPCRequest>();
if (ipcRq.cmd == ipcRq.cmdGetGPS)
{
GPSInfo gpsInfo;
_gpsReader.getGPSInfo(gpsInfo);
GPSInfoResponse resp(gpsInfo);
json json_resp = resp;
std::string respStr = json_resp.dump();
_log->write(LogLevel::DEBUG, "IPCClient %d sending response %s\n", _id, respStr.c_str());
return respStr;
}
else
{
ErrorResponse resp;
resp.errorMessage = "invalid command " + ipcRq.cmd;
json json_resp = resp;
return json_resp.dump();
}
}
catch (const std::exception &e)
{
_log->write(LogLevel::ERROR, "IPCClient %d failed to parse request %s\n", _id, rq.c_str());
ErrorResponse resp;
resp.errorMessage = "failed to parse request " + rq;
json json_resp = resp;
return json_resp.dump();
}
}
}