-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cpp
349 lines (300 loc) · 10.7 KB
/
Client.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "Client.h"
#include <cmath>
#include <iostream>
#include <map>
#include <sstream>
#include "cc_demo1/MsgId.h"
#include "comms/units.h"
#include "comms/process.h"
namespace cc_demo1
{
namespace client
{
Client::Client(
common::boost_wrap::io& io,
const std::string& server,
std::uint16_t port)
: m_io(io),
m_socket(io),
m_timer(io),
m_server(server),
m_port(port)
{
if (m_server.empty()) {
m_server = "localhost";
}
}
bool Client::start()
{
boost::asio::ip::tcp::resolver resolver(m_io);
auto query = boost::asio::ip::tcp::resolver::query(m_server, std::to_string(m_port));
boost::system::error_code ec;
auto iter = resolver.resolve(query, ec);
if (ec) {
std::cerr << "ERROR: Failed to resolve \"" << m_server << ':' << m_port << "\" " <<
"with error: " << ec.message() << std::endl;
return false;
}
auto endpoint = iter->endpoint();
m_socket.connect(endpoint, ec);
if (ec) {
std::cerr << "ERROR: Failed to connect to \"" << endpoint << "\" " <<
"with error: " << ec.message() << std::endl;
return false;
}
std::cout << "INFO: Connected to " << m_socket.remote_endpoint() << std::endl;
readDataFromServer();
readDataFromStdin();
return true;
}
void Client::handle(InAckMsg& msg)
{
if (msg.field_msgId().value() != m_sentId) {
std::cerr << "WARNING: Ack for the wrong ID: " <<
static_cast<unsigned>(msg.field_msgId().value()) << std::endl;
return;
}
std::cout << "INFO: Message acknowledged" << std::endl;
m_timer.cancel();
readDataFromStdin();
}
void Client::handle(InputMsg&)
{
std::cerr << "WARNING: Unexpected message received" << std::endl;
}
void Client::readDataFromServer()
{
m_socket.async_read_some(
boost::asio::buffer(m_readBuf),
[this](const boost::system::error_code& ec, std::size_t bytesCount)
{
if (ec == boost::asio::error::operation_aborted) {
return;
}
if (ec) {
std::cerr << "ERROR: Failed to read with error: " << ec.message() << std::endl;
m_io.stop();
return;
}
std::cout << "<-- " << std::hex;
std::copy_n(m_readBuf.begin(), bytesCount, std::ostream_iterator<unsigned>(std::cout, " "));
std::cout << std::dec << std::endl;
m_inputBuf.insert(m_inputBuf.end(), m_readBuf.begin(), m_readBuf.begin() + bytesCount);
processInput();
readDataFromServer();
});
}
void Client::readDataFromStdin()
{
std::cout << "\nEnter (new) message ID to send: " << std::endl;
m_sentId = cc_demo1::MsgId_Ack;
do {
// Unfortunatelly Windows doesn't provide an easy way to
// asynchronously read from stdin with boost::asio,
// read synchronously. DON'T COPY-PASTE TO PRODUCTION CODE!!!
unsigned msgId = 0U;
std::cin >> msgId;
if (!std::cin.good()) {
std::cerr << "ERROR: Unexpected input, use numeric value" << std::endl;
std::cin.clear();
std::cin.ignore();
break;
}
using SendFunc = void (Client::*)();
static const std::map<unsigned, SendFunc> Map = {
std::make_pair(cc_demo1::MsgId_SimpleInts, &Client::sendSimpleInts),
std::make_pair(cc_demo1::MsgId_ScaledInts, &Client::sendScaledInts),
std::make_pair(cc_demo1::MsgId_Floats, &Client::sendFloats),
std::make_pair(cc_demo1::MsgId_Enums, &Client::sendEnums),
std::make_pair(cc_demo1::MsgId_Sets, &Client::sendSets),
std::make_pair(cc_demo1::MsgId_Bitfields, &Client::sendBitfields),
std::make_pair(cc_demo1::MsgId_Strings, &Client::sendStrings),
std::make_pair(cc_demo1::MsgId_Datas, &Client::sendDatas),
std::make_pair(cc_demo1::MsgId_Lists, &Client::sendLists),
std::make_pair(cc_demo1::MsgId_Optionals, &Client::sendOptionals),
std::make_pair(cc_demo1::MsgId_Variants, &Client::sendVariants),
};
auto iter = Map.find(msgId);
if (iter == Map.end()) {
std::cerr << "ERROR: Unknown message ID, try another one" << std::endl;
break;
}
auto func = iter->second;
(this->*func)();
return; // Don't read STDIN right away, wait for ACK first
} while (false);
common::boost_wrap::post(
m_io,
[this]()
{
readDataFromStdin();
});
}
void Client::sendSimpleInts()
{
cc_demo1::message::SimpleInts<OutputMsg> msg;
msg.field_f9().value() = 12345;
msg.field_f10().value() = 678910;
// Keep default value of other fields
sendMessage(msg);
}
void Client::sendScaledInts()
{
cc_demo1::message::ScaledInts<OutputMsg> msg;
comms::units::setDegrees(msg.field_lat(), -27.470125);
comms::units::setDegrees(msg.field_lon(), 153.021072);
comms::units::setMeters(msg.field_height(), 80.123);
msg.field_someScaledVal().setScaled(123.45);
sendMessage(msg);
}
void Client::sendFloats()
{
cc_demo1::message::Floats<OutputMsg> msg;
msg.field_timeout().setInvalid();
assert(std::isnan(msg.field_timeout().value()));
comms::units::setCentimeters(msg.field_distance(), 34.56);
sendMessage(msg);
}
void Client::sendEnums()
{
using OutMsg = cc_demo1::message::Enums<OutputMsg>;
OutMsg msg;
msg.field_f1().value() = OutMsg::Field_f1::ValueType::V2;
msg.field_f4().value() = OutMsg::Field_f4::ValueType::V2;
// Keep default value of other fields
sendMessage(msg);
}
void Client::sendSets()
{
cc_demo1::message::Sets<OutputMsg> msg;
msg.field_f1().setBitValue_Bit2(true);
msg.field_f2().setBitValue_Bit0(true);
msg.field_f3().value() = 0x7; // sets also reserved bit 1
// Keep default value of other bits
sendMessage(msg);
}
void Client::sendBitfields()
{
using OutMsg = cc_demo1::message::Bitfields<OutputMsg>;
OutMsg msg;
msg.field_f1().field_mem1().value() = 5;
msg.field_f1().field_mem2().setBitValue_Bit2(true);
msg.field_f1().field_mem3().value() = OutMsg::Field_f1::Field_mem3::ValueType::V2;
sendMessage(msg);
}
void Client::sendStrings()
{
cc_demo1::message::Strings<OutputMsg> msg;
msg.field_f1().value() = "bla";
msg.field_f3().value() = "str";
msg.field_f4().value() = "oooo";
msg.field_f5().value() = "aa";
// Keep default value of other fields
msg.doRefresh(); // Bring message into consistent state, i.e. update F4Len
sendMessage(msg);
}
void Client::sendDatas()
{
cc_demo1::message::Datas<OutputMsg> msg;
msg.field_f1().value() = {0x10, 0x20, 0x30};
msg.field_f3().value() = {0x01, 0x02, 0x03};
msg.field_f4().value() = {0x06, 0x07};
// Keep default value of other fields
msg.doRefresh(); // Bring message into consistent state, i.e. update F3Len
sendMessage(msg);
}
void Client::sendLists()
{
cc_demo1::message::Lists<OutputMsg> msg;
msg.field_f1().value().resize(2);
msg.field_f1().value()[0].value() = 0x11223344;
msg.field_f1().value()[1].value() = 0xaabbccdd;
msg.field_f2().value().resize(1);
msg.field_f2().value()[0].value() = 0xffffffff;
msg.field_f3().value().resize(1);
msg.field_f3().value()[0].field_mem1().value() = 5;
msg.field_f3().value()[0].field_mem2().value() = -5;
msg.field_f4().value().resize(1);
msg.field_f4().value()[0].field_mem1().value() = 7;
msg.field_f4().value()[0].field_mem2().value() = "hello";
msg.field_f5().value().resize(2);
msg.field_f5().value()[0].field_mem1().value() = 10;
msg.field_f5().value()[0].field_mem2().value() = -10;
msg.field_f5().value()[1].field_mem1().value() = 15;
msg.field_f5().value()[1].field_mem2().value() = -15;
msg.doRefresh(); // Bring message into consistent state, i.e. update F2Len
sendMessage(msg);
}
void Client::sendOptionals()
{
cc_demo1::message::Optionals<OutputMsg> msg;
// Note usage of .field() to get access to inner field of optional
msg.field_f2().field().value() = 0xaaaa;
msg.field_f3().field().value() = 0xbbbb;
msg.field_flags().setBitValue_F2Exists(true);
msg.field_flags().setBitValue_F3Missing(true);
msg.doRefresh(); // Bring message into consistent state, i.e. update optional modes
sendMessage(msg);
}
void Client::sendVariants()
{
cc_demo1::message::Variants<OutputMsg> msg;
auto& props1Vec = msg.field_props1().value();
props1Vec.resize(3U);
props1Vec[0].initField_prop1().field_val().value() = 1234;
props1Vec[1].initField_prop3().field_val().value() = "hello";
props1Vec[2].initField_prop2().field_val().value() = 5555555;
auto& props2Vec = msg.field_props2().value();
props2Vec.resize(3U);
props2Vec[0].initField_prop1().field_val().value() = 4321;
props2Vec[1].initField_prop3().field_val().value() = "blabla";
props2Vec[2].initField_prop2().field_val().value() = 88888;
msg.doRefresh(); // Bring all the lengths into consistent state
sendMessage(msg);
}
void Client::sendMessage(const OutputMsg& msg)
{
std::vector<std::uint8_t> outputBuf;
outputBuf.reserve(m_frame.length(msg));
auto iter = std::back_inserter(outputBuf);
auto es = m_frame.write(msg, iter, outputBuf.max_size());
if (es == comms::ErrorStatus::UpdateRequired) {
auto updateIter = &outputBuf[0];
es = m_frame.update(updateIter, outputBuf.size());
}
if (es != comms::ErrorStatus::Success) {
static constexpr bool Unexpected_error = false;
static_cast<void>(Unexpected_error);
assert(Unexpected_error);
return;
}
std::cout << "INFO: Sending message: " << msg.name() << '\n';
std::cout << "--> " << std::hex;
std::copy(outputBuf.begin(), outputBuf.end(), std::ostream_iterator<unsigned>(std::cout, " "));
std::cout << std::dec << std::endl;
m_socket.send(boost::asio::buffer(outputBuf));
m_sentId = msg.getId();
waitForAck();
}
void Client::waitForAck()
{
m_timer.expires_from_now(boost::posix_time::seconds(2));
m_timer.async_wait(
[this](const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted) {
return;
}
std::cerr << "ERROR: Previous message hasn't been acknowledged" << std::endl;
readDataFromStdin();
});
}
void Client::processInput()
{
if (!m_inputBuf.empty()) {
auto consumed = comms::processAllWithDispatch(&m_inputBuf[0], m_inputBuf.size(), m_frame, *this);
m_inputBuf.erase(m_inputBuf.begin(), m_inputBuf.begin() + consumed);
}
}
} // namespace client
} // namespace cc_demo1