-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.cpp
50 lines (39 loc) · 1.38 KB
/
receiver.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
#include <boost/spirit/include/qi.hpp>
#include <memory>
#include <stdexcept>
#include <vector>
#include "receiver.hpp"
namespace qi = boost::spirit::qi;
namespace x_company::geometry {
void trigger_listener::operator()(std::string const &payload, int backend_pid) {
static bool initialized = false;
// start receiving messages only after geoindex::initialize()
if (!initialized) {
if (conn().backendpid() == backend_pid)
initialized = true;
else
return;
}
// e.g. payload is "(7634,gz_polygon,INSERT)", we will parse that
message msg;
auto first = payload.begin();
qi::rule<std::string::const_iterator, std::string()> r =
+(qi::char_ - qi::char_(",)"));
bool success = qi::parse(first, payload.end(),
'(' >> qi::int_ >> ",gz_" >> r >> "," >> r >> ')',
msg.id, msg.shape, msg.op);
if (!success || first != payload.end()) {
std::cerr << "Parsing failed:" + payload << std::endl;
throw std::logic_error("Parsing failed:" + payload);
}
// add message to message queue
msgs_.push(std::move(msg));
}
auto trigger_listener::size() const -> size_t { return msgs_.size(); }
auto trigger_listener::empty() const -> size_t { return msgs_.empty(); }
auto trigger_listener::pop() -> message {
auto msg = std::move(msgs_.front());
msgs_.pop();
return msg;
}
} // namespace x_company::geometry