MADARA  3.1.8
ReducedMessageHeader.cpp
Go to the documentation of this file.
1 #include "ReducedMessageHeader.h"
3 #include <algorithm>
4 #include <string.h>
5 #include <sstream>
6 
8 : MessageHeader ()
9 {
10  memcpy (madara_id, REDUCED_MADARA_ID, 7);
11  madara_id[7] = 0;
12 }
13 
15 {
16 }
17 
18 uint32_t
20 {
21  return sizeof (uint64_t) * 3 // size, clock, timestamp
22  + sizeof (char) * (MADARA_IDENTIFIER_LENGTH + 1) // KaRL1.0 + ttl
23  + sizeof (uint32_t) * 1; // updates
24 }
25 
26 const char *
28  int64_t & buffer_remaining)
29 {
30  // Remove size field from the buffer and update accordingly
31  if ((size_t)buffer_remaining >= sizeof (size))
32  {
33  memcpy (&size, buffer, sizeof (size));
35  buffer += sizeof (size);
36  }
37  buffer_remaining -= sizeof (size);
38 
39  // Remove madara_id field from the buffer and update accordingly
40  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
41  {
42  strncpy (madara_id, buffer, MADARA_IDENTIFIER_LENGTH);
43  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
44  }
45  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
46 
47  // Remove updates field from the buffer and update accordingly
48  if ((size_t)buffer_remaining >= sizeof (updates))
49  {
50  memcpy (&updates, buffer, sizeof (updates));
52  buffer += sizeof (updates);
53  }
54  buffer_remaining -= sizeof (updates);
55 
56  // Remove clock field from the buffer and update accordingly
57  if ((size_t)buffer_remaining >= sizeof (clock))
58  {
59  memcpy (&clock, buffer, sizeof (clock));
61  buffer += sizeof (clock);
62  }
63  buffer_remaining -= sizeof (clock);
64 
65  // Remove timestamp field from the buffer and update accordingly
66  if ((size_t)buffer_remaining >= sizeof (timestamp))
67  {
68  memcpy (&timestamp, buffer, sizeof (timestamp));
70  buffer += sizeof (timestamp);
71  }
72  buffer_remaining -= sizeof (timestamp);
73 
74  // Remove the time to live field from the buffer
75  if (buffer_remaining >= 1)
76  {
77  memcpy (&ttl, buffer, 1);
78  buffer += 1;
79  }
80  buffer_remaining -= 1;
81 
82  return buffer;
83 }
84 
85 char *
87  int64_t & buffer_remaining)
88 {
89  // Write size field from the buffer and update accordingly
90  if ((size_t)buffer_remaining >= sizeof (size))
91  {
92  *(uint64_t *) buffer = madara::utility::endian_swap (size);
93  buffer += sizeof (size);
94  }
95  buffer_remaining -= sizeof (size);
96 
97  // Write madara_id field from the buffer and update accordingly
98  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
99  {
100  strncpy (buffer, madara_id, MADARA_IDENTIFIER_LENGTH);
101  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
102  }
103  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
104 
105  // Write updates field from the buffer and update accordingly
106  if ((size_t)buffer_remaining >= sizeof (updates))
107  {
108  *(uint32_t *) buffer = madara::utility::endian_swap (updates);
109  buffer += sizeof (updates);
110  }
111  buffer_remaining -= sizeof (updates);
112 
113  // Write clock field from the buffer and update accordingly
114  if ((size_t)buffer_remaining >= sizeof (clock))
115  {
116  *(uint64_t *) buffer = madara::utility::endian_swap (clock);
117  buffer += sizeof (clock);
118  }
119  buffer_remaining -= sizeof (clock);
120 
121  // Write timestamp field from the buffer and update accordingly
122  if ((size_t)buffer_remaining >= sizeof (timestamp))
123  {
124  *(uint64_t *) buffer = madara::utility::endian_swap (timestamp);
125  buffer += sizeof (timestamp);
126  }
127  buffer_remaining -= sizeof (timestamp);
128 
129  // Write the time to live field
130  if (buffer_remaining >= 1)
131  {
132  memcpy (buffer, &ttl, 1);
133  buffer += 1;
134  }
135  buffer_remaining -= 1;
136 
137  return buffer;
138 }
139 
142 {
143  std::stringstream buffer;
144  buffer << "29: size (8:" << size << "), ";
145  buffer << "encoding (8:" << madara_id << "), ";
146  buffer << "numupdates (4:" << updates << "), ";
147  buffer << "clock (8:" << clock << "), ";
148  buffer << "ttl (1:" << ttl << "), ";
149 
150  return buffer.str ();
151 }
152 
153 
154 bool
156  const MessageHeader & other)
157 {
158  return size == other.size &&
159  updates == other.updates &&
160  clock == other.clock &&
161  timestamp == other.timestamp;
162 }
163 
char madara_id[8]
the identifier of this transport (MADARA_IDENTIFIER)
#define REDUCED_MADARA_ID
uint32_t updates
the number of knowledge variable updates in the message
virtual bool equals(const MessageHeader &other)
Compares the fields of this instance to another instance.
MADARA_Export uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.cpp:625
virtual uint32_t encoded_size(void) const
Returns the size of the encoded MessageHeader class, which may be different from sizeof (MessageHeade...
uint64_t size
the size of this header plus the updates
unsigned char ttl
time to live (number of rebroadcasts to perform after original send
uint64_t timestamp
the timestamp of the sender when the message was generated
static constexpr struct madara::knowledge::tags::string_t string
virtual const char * read(const char *buffer, int64_t &buffer_remaining)
Reads a MessageHeader instance from a buffer and updates the amount of buffer room remaining...
#define MADARA_IDENTIFIER_LENGTH
Definition: MessageHeader.h:20
uint64_t clock
the clock of the sender when the message was generated
Defines a robust message header which is the default for KaRL messages.
Definition: MessageHeader.h:56
virtual std::string to_string(void)
Converts the relevant fields to a printable string.
virtual char * write(char *buffer, int64_t &buffer_remaining)
Writes a MessageHeader instance to a buffer and updates the amount of buffer room remaining...