File: data.cpp

package info (click to toggle)
broker 1.4.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,420 kB
  • sloc: cpp: 20,886; python: 1,382; ansic: 1,077; sh: 321; makefile: 103
file content (252 lines) | stat: -rw-r--r-- 9,868 bytes parent folder | download
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

#include <cstdint>
#include <utility>
#include <array>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include <pybind11/pybind11.h>
#pragma GCC diagnostic pop

#include "set_bind.h"

#include "broker/data.hh"
#include "broker/convert.hh"
#include "broker/detail/assert.hh"
#include "broker/detail/operators.hh"

namespace py = pybind11;
using namespace pybind11::literals;

void init_data(py::module& m) {

  py::class_<broker::address> address_type{m, "Address"};
  address_type.def(py::init<>())
    .def(py::init([](const py::bytes& bytes, int family) {
        BROKER_ASSERT(family == 4 || family == 6);
        auto str = static_cast<std::string>(bytes);
        auto ptr = reinterpret_cast<const uint32_t*>(str.data());
        auto f = family == 4 ? broker::address::family::ipv4 :
                               broker::address::family::ipv6;
        return broker::address{ptr, f, broker::address::byte_order::network};
        }))
    .def("mask", &broker::address::mask, "top_bits_to_keep"_a)
    .def("is_v4", &broker::address::is_v4)
    .def("is_v6", &broker::address::is_v6)
    .def("bytes", [](const broker::address& a) {
        return py::bytes(std::string(std::begin(a.bytes()), std::end(a.bytes())));
        })
    .def("__repr__",
         [](const broker::address& a) { return broker::to_string(a); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::enum_<broker::address::family>(address_type, "Family")
    .value("IPv4", broker::address::family::ipv4)
    .value("IPv6", broker::address::family::ipv6);

  py::enum_<broker::address::byte_order>(address_type, "ByteOrder")
    .value("Host", broker::address::byte_order::host)
    .value("Network", broker::address::byte_order::network);

  // A thin wrapper around the 'count' type, because Python has no notion of
  // unsigned integers.
  struct count_type {
    count_type(broker::count c) : value{c} {}
    bool operator==(const count_type& other) const { return value == other.value; }
    bool operator!=(const count_type& other) const { return value != other.value; }
    bool operator<(const count_type& other) const { return value < other.value; }
    bool operator<=(const count_type& other) const { return value <= other.value; }
    bool operator>(const count_type& other) const { return value > other.value; }
    bool operator>=(const count_type& other) const { return value >= other.value; }
    broker::count value;
  };

  py::class_<count_type>(m, "Count")
    .def(py::init<py::int_>())
    .def_readwrite("value", &count_type::value)
    .def("__str__", [](const count_type& c) { return broker::to_string(c.value); })
    .def("__repr__", [](const count_type& c) { return "Count(" + broker::to_string(c.value) + ")"; })
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::class_<broker::enum_value>{m, "Enum"}
    .def(py::init<std::string>())
    .def_readwrite("name", &broker::enum_value::name)
    .def("__repr__", [](const broker::enum_value& e) { return broker::to_string(e); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::class_<broker::port> port_type{m, "Port"};
  port_type
    .def(py::init<>())
    .def(py::init<broker::port::number_type, broker::port::protocol>())
    .def("number", &broker::port::number)
    .def("get_type", &broker::port::type)
    .def("__repr__", [](const broker::port& p) { return broker::to_string(p); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::enum_<broker::port::protocol>(port_type, "Protocol")
    .value("ICMP", broker::port::protocol::icmp)
    .value("TCP", broker::port::protocol::tcp)
    .value("UDP", broker::port::protocol::udp)
    .value("Unknown", broker::port::protocol::unknown)
    .export_values();

  py::bind_set<broker::set>(m, "Set");

  py::bind_map<broker::table>(m, "Table");

  py::class_<broker::subnet>(m, "Subnet")
    .def(py::init<>())
    .def(py::init([](broker::address addr, uint8_t length) {
        return broker::subnet(std::move(addr), length);
        }))
    .def("contains", &broker::subnet::contains, "addr"_a)
    .def("network", &broker::subnet::network)
    .def("length", &broker::subnet::length)
    .def("__repr__", [](const broker::subnet& sn) { return to_string(sn); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::class_<broker::timespan>(m, "Timespan")
    .def(py::init<>())
    .def(py::init<broker::integer>())
    .def(py::init([](double secs) {
        return broker::to_timespan(secs);
        }))
    .def("count", &broker::timespan::count)
    .def("__repr__", [](const broker::timespan& s) { return broker::to_string(s); })
    .def(hash(py::self))
    .def(py::self + py::self)
    .def(py::self - py::self)
    .def(py::self * broker::timespan::rep{})
    .def(broker::timespan::rep{} * py::self)
    .def(py::self / py::self)
    .def(py::self / broker::timespan::rep{})
    .def(py::self % py::self)
    .def(py::self % broker::timespan::rep{})
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::class_<broker::timestamp>(m, "Timestamp")
    .def(py::init<>())
    .def(py::init<broker::timespan>())
    .def(py::init([](double secs) {
        return broker::to_timestamp(secs);
        }))
    .def("time_since_epoch", &broker::timestamp::time_since_epoch)
    .def("__repr__", [](const broker::timestamp& ts) { return broker::to_string(ts); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::bind_vector<broker::vector>(m, "Vector");

  py::class_<broker::data> data_type{m, "Data"};
  data_type
    .def(py::init<>())
    .def(py::init<broker::data>())
    .def(py::init<broker::address>())
    .def(py::init<broker::boolean>())
    .def(py::init([](count_type c) {
         return broker::data{c.value};
         }))
    .def(py::init([](broker::enum_value e) {
         return broker::data{e};
         }))
    .def(py::init<broker::integer>())
    .def(py::init<broker::port>())
    .def(py::init<broker::real>())
    .def(py::init<broker::set>())
    .def(py::init<std::string>())
    .def(py::init<broker::subnet>())
    .def(py::init<broker::table>())
    .def(py::init<broker::timespan>())
    .def(py::init<broker::timestamp>())
    .def(py::init<broker::vector>())
    .def("as_address", [](const broker::data& d) { return caf::get<broker::address>(d); })
    .def("as_boolean", [](const broker::data& d) { return caf::get<broker::boolean>(d); })
    .def("as_count", [](const broker::data& d) { return caf::get<broker::count>(d); })
    .def("as_enum_value", [](const broker::data& d) { return caf::get<broker::enum_value>(d); })
    .def("as_integer", [](const broker::data& d) { return caf::get<broker::integer>(d); })
    .def("as_none", [](const broker::data& d) { return caf::get<broker::none>(d); })
    .def("as_port", [](const broker::data& d) { return caf::get<broker::port>(d); })
    .def("as_real", [](const broker::data& d) { return caf::get<broker::real>(d); })
    .def("as_set", [](const broker::data& d) { return caf::get<broker::set>(d); })
    .def("as_string", [](const broker::data& d) { return py::bytes(caf::get<std::string>(d)); })
    .def("as_subnet", [](const broker::data& d) { return caf::get<broker::subnet>(d); })
    .def("as_table", [](const broker::data& d) { return caf::get<broker::table>(d); })
    .def("as_timespan", [](const broker::data& d) {
        double s;
        broker::convert(caf::get<broker::timespan>(d), s);
	return s;
	})
    .def("as_timestamp", [](const broker::data& d) {
        double s;
        broker::convert(caf::get<broker::timestamp>(d), s);
	return s;
	})
    .def("as_vector", [](const broker::data& d) { return caf::get<broker::vector>(d); })
    .def("get_type", &broker::data::get_type)
    .def("__str__", [](const broker::data& d) { return broker::to_string(d); })
    .def(hash(py::self))
    .def(py::self < py::self)
    .def(py::self <= py::self)
    .def(py::self > py::self)
    .def(py::self >= py::self)
    .def(py::self == py::self)
    .def(py::self != py::self);

  py::enum_<broker::data::type>(data_type, "Type")
    .value("Nil", broker::data::type::none)
    .value("Address", broker::data::type::address)
    .value("Boolean", broker::data::type::boolean)
    .value("Count", broker::data::type::count)
    .value("EnumValue", broker::data::type::enum_value)
    .value("Integer", broker::data::type::integer)
    .value("None", broker::data::type::none)
    .value("Port", broker::data::type::port)
    .value("Real", broker::data::type::real)
    .value("Set", broker::data::type::set)
    .value("String", broker::data::type::string)
    .value("Subnet", broker::data::type::subnet)
    .value("Table", broker::data::type::table)
    .value("Timespan", broker::data::type::timespan)
    .value("Timestamp", broker::data::type::timestamp)
    .value("Vector", broker::data::type::vector);
}