30template <
typename Channel>
35 enum class Type : std::uint8_t {
45 typedef typename Channel::Address
Origin;
47 typedef typename Channel::Port
Port;
48 typedef std::uint32_t
Unit;
49 typedef std::vector<std::uint8_t>
Array;
66 const std::uint8_t*
value()
const;
68 const void*
data()
const;
69 const unsigned int size()
const;
99 static Type extract_type(
const std::uint8_t*
data,
unsigned int&
offset,
unsigned int size);
100 static Unit extract_unit(
const std::uint8_t*
data,
unsigned int&
offset,
unsigned int size);
114 Array _serialized_data;
122template <
typename Channel>
124 _message_type(
Type::UNKNOWN),
139 <<
", origin: " <<
origin.to_string()
140 <<
", unit: " <<
unit
141 <<
", period: " <<
period.count()
147 switch (_message_type) {
163template <
typename Channel>
166 _message_type =
other.message_type();
167 _origin =
other.origin();
168 _timestamp =
other.timestamp();
169 _period =
other.period();
170 _unit =
other.unit();
171 _external =
other.external();
174 if (
other.value_size() > 0) {
179 _serialized_data.clear();
182 <<
static_cast<int>(_message_type)
183 <<
", origin: " << _origin.to_string()
184 <<
", unit: " << _unit
185 <<
", period: " << _period.count()
186 <<
", value_size: " << _value.size() <<
"\n";
189 if (_message_type != Type::UNKNOWN && _message_type != Type::INVALID &&
190 _message_type != Type::INTEREST && _message_type != Type::RESPONSE &&
191 _message_type != Type::STATUS && _message_type != Type::REQ &&
192 _message_type != Type::KEY_RESPONSE) {
194 <<
static_cast<int>(_message_type) <<
" - marking as INVALID\n";
195 _message_type = Type::INVALID;
199template <
typename Channel>
201 return _message_type;
204template <
typename Channel>
209template <
typename Channel>
214template <
typename Channel>
219template <
typename Channel>
224template <
typename Channel>
226 return _value.data();
229template <
typename Channel>
231 return _value.size();
234template <
typename Channel>
237 const_cast<Message*
>(
this)->serialize();
240 return static_cast<const void*
>(_serialized_data.data());
243template <
typename Channel>
246 const_cast<Message*
>(
this)->serialize();
249 return static_cast<const unsigned int>(_serialized_data.size());
252template <
typename Channel>
257template <
typename Channel>
259 const std::uint8_t* bytes =
static_cast<const std::uint8_t*
>(
serialized);
265 if (
msg.message_type() != Type::UNKNOWN &&
msg.message_type() != Type::INVALID) {
266 msg.origin(extract_origin(bytes,
offset, size));
267 msg.timestamp(extract_microseconds(bytes,
offset, size));
268 msg.unit(extract_unit(bytes,
offset, size));
270 switch (
msg.message_type()) {
271 case Type::INTEREST: {
272 msg.period(extract_microseconds(bytes,
offset, size));
278 case Type::KEY_RESPONSE: {
284 db<Message>(
TRC) <<
"Non positive value length set: Message marked INVALID \n";
285 msg.message_type(Type::INVALID);
296 <<
", origin: " <<
msg.origin().to_string()
297 <<
", unit: " <<
msg.unit()
298 <<
", input size: " << size
299 <<
", final offset: " <<
offset <<
"\n";
301 db<Message<Channel>>(
WRN) <<
"Message::deserialize() - failed to deserialize message of size " << size <<
"\n";
307template <
typename Channel>
309 _message_type = message_type;
312template <
typename Channel>
317template <
typename Channel>
319 if (timestamp <= ZERO) {
320 db<Message>(
TRC) <<
"Negative timestamp set: Message marked INVALID \n";
321 message_type(Type::INVALID);
325 _timestamp = timestamp;
328template <
typename Channel>
333template <
typename Channel>
335 if (period <= ZERO) {
336 db<Message>(
TRC) <<
"Negative PERIOD set: Message marked INVALID \n";
337 message_type(Type::INVALID);
344template <
typename Channel>
346 if (!
data || !size) {
351 std::memcpy(_value.data(),
data, size);
354template <
typename Channel>
356 _external = external;
360template <
typename Channel>
363 _serialized_data.clear();
367 append_microseconds(_timestamp);
370 if (_message_type == Type::INTEREST)
371 append_microseconds(_period);
372 else if (_message_type == Type::RESPONSE || _message_type == Type::STATUS ||
373 _message_type == Type::REQ || _message_type == Type::KEY_RESPONSE)
377 <<
", origin: " << _origin.to_string()
378 <<
", unit: " << _unit
379 <<
", serialized size: " << _serialized_data.size() <<
"\n";
382template <
typename Channel>
385 const auto& paddr = _origin.paddr();
386 const std::uint8_t*
paddr_bytes =
reinterpret_cast<const std::uint8_t*
>(&paddr);
390 const auto& port = _origin.port();
391 const std::uint8_t*
port_bytes =
reinterpret_cast<const std::uint8_t*
>(&port);
395template <
typename Channel>
397 _serialized_data.push_back(
static_cast<std::uint8_t
>(_message_type));
400template <
typename Channel>
402 const std::uint8_t*
unit_bytes =
reinterpret_cast<const std::uint8_t*
>(&_unit);
406template <
typename Channel>
413template <
typename Channel>
415 _serialized_data.insert(_serialized_data.end(), _value.begin(), _value.end());
418template <
typename Channel>
420 unsigned int paddr_size =
sizeof(Physical_Address);
424 Physical_Address
addr;
436 return Origin(
addr, port);
439template <
typename Channel>
441 if (
offset +
sizeof(std::uint8_t) > size)
442 return Type::UNKNOWN;
453 <<
static_cast<int>(
raw_type) <<
" - marking as INVALID\n";
454 return Type::INVALID;
460template <
typename Channel>
474template <
typename Channel>
476 unsigned int rep_size =
sizeof(Microseconds::rep);
489template <
typename Protocol>
497template <
typename Protocol>
500 return clock.isFullySynchronized();
static Clock & getInstance()
Get the singleton instance.
Definition clock.h:145
Template class for network messages with Clock integration.
Definition message.h:31
const Type & message_type() const
Definition message.h:200
Channel::Physical_Address Physical_Address
Definition message.h:46
const bool external() const
Definition message.h:253
std::vector< std::uint8_t > Array
Definition message.h:49
static Microseconds getSynchronizedTimestamp()
Definition message.h:490
Channel::Port Port
Definition message.h:47
const Unit & unit() const
Definition message.h:215
const unsigned int value_size() const
Definition message.h:230
std::chrono::microseconds Microseconds
Definition message.h:50
Channel::Address Origin
Definition message.h:45
const Origin & origin() const
Definition message.h:205
const Microseconds & timestamp() const
Definition message.h:210
const std::uint8_t * value() const
Definition message.h:225
static Message deserialize(const void *serialized, const unsigned int size)
Definition message.h:258
const unsigned int size() const
Definition message.h:244
static bool isClockSynchronized()
Definition message.h:498
static constexpr Microseconds ZERO
Definition message.h:51
Type
Definition message.h:35
std::uint32_t Unit
Definition message.h:48
const Microseconds & period() const
Definition message.h:220
const void * data() const
Definition message.h:235
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
@ ERR
Definition debug.h:162
@ TRC
Definition debug.h:231
@ WRN
Definition debug.h:185
T * data()
Definition protocol.h:24