MADARA  3.1.8
MessageHeader.cpp
Go to the documentation of this file.
1 #include "MessageHeader.h"
3 #include <algorithm>
4 #include <time.h>
5 #include <sstream>
6 
8 : size (encoded_size ()),
9  type (0), updates (0), quality (0), clock (0),
10  timestamp (time (NULL)), ttl (0)
11 {
12  memcpy (madara_id, MADARA_IDENTIFIER, 7);
13  madara_id[7] = 0;
14 
15  originator[0] = 0;
16  domain[0] = 0;
17 }
18 
19 
21 {
22 }
23 
24 uint32_t
26 {
27  return sizeof (uint64_t) * 3 // size, clock, timestamp
30  + sizeof (uint32_t) * 3; // type, updates, quality
31 }
32 
33 uint32_t
35 {
36  return sizeof (uint64_t) * 3 // size, clock, timestamp
39  + sizeof (uint32_t) * 3; // type, updates, quality
40 }
41 
42 const char *
44  int64_t & buffer_remaining)
45 {
46  // Remove size field from the buffer and update accordingly
47  if ((size_t)buffer_remaining >= sizeof (size))
48  {
49  memcpy (&size, buffer, sizeof (size));
51  buffer += sizeof (size);
52  }
53  buffer_remaining -= sizeof (size);
54 
55  // Remove madara_id field from the buffer and update accordingly
56  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
57  {
58  strncpy (madara_id, buffer, MADARA_IDENTIFIER_LENGTH);
59  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
60  }
61  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
62 
63  // Remove domain field from the buffer and update accordingly
64  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_DOMAIN_MAX_LENGTH)
65  {
66  strncpy (domain, buffer, MADARA_DOMAIN_MAX_LENGTH);
67  buffer += sizeof (char) * MADARA_DOMAIN_MAX_LENGTH;
68  }
69  buffer_remaining -= sizeof (char) * MADARA_DOMAIN_MAX_LENGTH;
70 
71  // Remove originator from the buffer and update accordingly
72  if ((size_t)buffer_remaining >= sizeof (char) * MAX_ORIGINATOR_LENGTH)
73  {
74  strncpy (originator, buffer, MAX_ORIGINATOR_LENGTH);
75  buffer += sizeof (char) * MAX_ORIGINATOR_LENGTH;
76  }
77  buffer_remaining -= sizeof (char) * MAX_ORIGINATOR_LENGTH;
78 
79  // Remove type field from the buffer and update accordingly
80  if ((size_t)buffer_remaining >= sizeof (type))
81  {
82  memcpy (&type, buffer, sizeof (type));
84  buffer += sizeof (type);
85  }
86  buffer_remaining -= sizeof (type);
87 
88  // Remove updates field from the buffer and update accordingly
89  if ((size_t)buffer_remaining >= sizeof (updates))
90  {
91  memcpy (&updates, buffer, sizeof (updates));
93  buffer += sizeof (updates);
94  }
95  buffer_remaining -= sizeof (updates);
96 
97  // Remove quality field from the buffer and update accordingly
98  if ((size_t)buffer_remaining >= sizeof (quality))
99  {
100  memcpy (&quality, buffer, sizeof (quality));
102  buffer += sizeof (quality);
103  }
104  buffer_remaining -= sizeof (quality);
105 
106  // Remove clock field from the buffer and update accordingly
107  if ((size_t)buffer_remaining >= sizeof (clock))
108  {
109  memcpy (&clock, buffer, sizeof (clock));
111  buffer += sizeof (clock);
112  }
113  buffer_remaining -= sizeof (clock);
114 
115  // Remove timestamp field from the buffer and update accordingly
116  if ((size_t)buffer_remaining >= sizeof (timestamp))
117  {
118  memcpy (&timestamp, buffer, sizeof (timestamp));
120  buffer += sizeof (timestamp);
121  }
122  buffer_remaining -= sizeof (timestamp);
123 
124  // Remove the time to live field from the buffer
125  if (buffer_remaining >= 1)
126  {
127  memcpy (&ttl, buffer, 1);
128  buffer += 1;
129  }
130  buffer_remaining -= 1;
131 
132  return buffer;
133 }
134 
135 char *
137  int64_t & buffer_remaining)
138 {
139  // Write size field from the buffer and update accordingly
140  if ((size_t)buffer_remaining >= sizeof (size))
141  {
142  *(uint64_t *) buffer = madara::utility::endian_swap (size);
143  buffer += sizeof (size);
144  }
145  buffer_remaining -= sizeof (size);
146 
147  // Write madara_id field from the buffer and update accordingly
148  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
149  {
150  strncpy (buffer, madara_id, MADARA_IDENTIFIER_LENGTH);
151  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
152  }
153  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
154 
155  // Write domain field from the buffer and update accordingly
156  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_DOMAIN_MAX_LENGTH)
157  {
158  strncpy (buffer, domain, MADARA_DOMAIN_MAX_LENGTH);
159  buffer += sizeof (char) * MADARA_DOMAIN_MAX_LENGTH;
160  }
161  buffer_remaining -= sizeof (char) * MADARA_DOMAIN_MAX_LENGTH;
162 
163  // Write originator from the buffer and update accordingly
164  if ((size_t)buffer_remaining >= sizeof (char) * MAX_ORIGINATOR_LENGTH)
165  {
166  strncpy (buffer, originator, MAX_ORIGINATOR_LENGTH);
167  buffer += sizeof (char) * MAX_ORIGINATOR_LENGTH;
168  }
169  buffer_remaining -= sizeof (char) * MAX_ORIGINATOR_LENGTH;
170 
171  // Write type field from the buffer and update accordingly
172  if ((size_t)buffer_remaining >= sizeof (type))
173  {
174  *(uint32_t *) buffer = madara::utility::endian_swap (type);
175  buffer += sizeof (type);
176  }
177  buffer_remaining -= sizeof (type);
178 
179  // Write updates field from the buffer and update accordingly
180  if ((size_t)buffer_remaining >= sizeof (updates))
181  {
182  *(uint32_t *) buffer = madara::utility::endian_swap (updates);
183  buffer += sizeof (updates);
184  }
185  buffer_remaining -= sizeof (updates);
186 
187  // Write quality field from the buffer and update accordingly
188  if ((size_t)buffer_remaining >= sizeof (quality))
189  {
190  *(uint32_t *) buffer = madara::utility::endian_swap (quality);
191  buffer += sizeof (quality);
192  }
193  buffer_remaining -= sizeof (quality);
194 
195  // Write clock field from the buffer and update accordingly
196  if ((size_t)buffer_remaining >= sizeof (clock))
197  {
198  *(uint64_t *) buffer = madara::utility::endian_swap (clock);
199  buffer += sizeof (clock);
200  }
201  buffer_remaining -= sizeof (clock);
202 
203  // Write timestamp field from the buffer and update accordingly
204  if ((size_t)buffer_remaining >= sizeof (timestamp))
205  {
206  *(uint64_t *) buffer = madara::utility::endian_swap (timestamp);
207  buffer += sizeof (timestamp);
208  }
209  buffer_remaining -= sizeof (timestamp);
210 
211  if (buffer_remaining >= 1)
212  {
213  memcpy (buffer, &ttl, 1);
214  buffer += 1;
215  }
216  buffer_remaining -= 1;
217 
218  return buffer;
219 }
220 
223 {
224  std::stringstream buffer;
225 
226  buffer << "140: size (8:" << size << "), ";
227  buffer << "encoding (8:" << madara_id << "), ";
228  buffer << "domain (32:" << domain << "), ";
229  buffer << "orig (64:" << originator << "), ";
230  buffer << "type (4:" << type << "), ";
231  buffer << "numupdates (4:" << updates << "), ";
232  buffer << "quality (4:" << quality << "), ";
233  buffer << "clock (8:" << clock << "), ";
234  buffer << "wallclock (8:" << timestamp << "), ";
235  buffer << "ttl (1:" << (int)ttl << "), ";
236 
237  return buffer.str ();
238 }
239 
240 uint64_t
242 {
243  return (madara::utility::endian_swap (*(uint64_t *)buffer));
244 }
245 
246 bool
248 {
249  return size == other.size &&
250  type == other.type &&
251  updates == other.updates &&
252  quality == other.quality &&
253  clock == other.clock &&
254  timestamp == other.timestamp &&
255  strncmp (madara_id, other.madara_id, MADARA_IDENTIFIER_LENGTH) == 0 &&
256  strncmp (domain, other.domain, MADARA_DOMAIN_MAX_LENGTH) == 0 &&
257  strncmp (originator, other.originator, MAX_ORIGINATOR_LENGTH) == 0;
258 }
virtual uint32_t encoded_size(void) const
Returns the size of the encoded MessageHeader class, which may be different from sizeof (MessageHeade...
char madara_id[8]
the identifier of this transport (MADARA_IDENTIFIER)
#define MADARA_IDENTIFIER
Definition: MessageHeader.h:21
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...
uint32_t updates
the number of knowledge variable updates in the message
#define MAX_ORIGINATOR_LENGTH
Definition: MessageHeader.h:27
uint32_t type
the type of message
static uint32_t static_encoded_size(void)
Returns the size of the encoded MessageHeader class.
virtual std::string to_string(void)
Converts the relevant fields to a printable string.
MADARA_Export uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.cpp:625
uint64_t size
the size of this header plus the updates
static uint64_t get_size(const char *buffer)
Returns the size field of the header.
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
char originator[64]
the originator of the message (host:port)
char domain[32]
the domain that this message is intended for
virtual ~MessageHeader()
Destructor.
#define MADARA_IDENTIFIER_LENGTH
Definition: MessageHeader.h:20
uint64_t clock
the clock of the sender when the message was generated
#define MADARA_DOMAIN_MAX_LENGTH
Definition: MessageHeader.h:22
Defines a robust message header which is the default for KaRL messages.
Definition: MessageHeader.h:56
virtual bool equals(const MessageHeader &other)
Compares the fields of this instance to another instance.
uint32_t quality
the quality of the message sender
virtual char * write(char *buffer, int64_t &buffer_remaining)
Writes a MessageHeader instance to a buffer and updates the amount of buffer room remaining...