MADARA  3.2.3
ReducedMessageHeader.cpp
Go to the documentation of this file.
1 
2 #include <algorithm>
3 #include <string.h>
4 #include <sstream>
5 
7 #include "ReducedMessageHeader.h"
9 
11 : MessageHeader ()
12 {
13  memcpy (madara_id, REDUCED_MADARA_ID, 7);
14  madara_id[7] = 0;
15 }
16 
18 {
19 }
20 
21 uint32_t
23 {
24  return sizeof (uint64_t) * 3 // size, clock, timestamp
25  + sizeof (char) * (MADARA_IDENTIFIER_LENGTH + 1) // KaRL1.0 + ttl
26  + sizeof (uint32_t) * 1; // updates
27 }
28 
29 const char *
31  int64_t & buffer_remaining)
32 {
33  // Remove size field from the buffer and update accordingly
34  if ((size_t)buffer_remaining >= sizeof (size))
35  {
36  memcpy (&size, buffer, sizeof (size));
38  buffer += sizeof (size);
39  }
40  else
41  {
42  std::stringstream buffer;
43  buffer << "ReducedMessageHeader::read: ";
44  buffer << sizeof (size) << " byte size encoding cannot";
45  buffer << " fit in ";
46  buffer << buffer_remaining << " byte buffer\n";
47 
48  throw exceptions::MemoryException (buffer.str ());
49  }
50  buffer_remaining -= sizeof (size);
51 
52  // Remove madara_id field from the buffer and update accordingly
53  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
54  {
55  strncpy (madara_id, buffer, MADARA_IDENTIFIER_LENGTH);
56  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
57  }
58  else
59  {
60  std::stringstream buffer;
61  buffer << "ReducedMessageHeader::read: ";
62  buffer << MADARA_IDENTIFIER_LENGTH << " byte id encoding cannot";
63  buffer << " fit in ";
64  buffer << buffer_remaining << " byte buffer\n";
65 
66  throw exceptions::MemoryException (buffer.str ());
67  }
68  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
69 
70  // Remove updates field from the buffer and update accordingly
71  if ((size_t)buffer_remaining >= sizeof (updates))
72  {
73  memcpy (&updates, buffer, sizeof (updates));
75  buffer += sizeof (updates);
76  }
77  else
78  {
79  std::stringstream buffer;
80  buffer << "ReducedMessageHeader::read: ";
81  buffer << sizeof (updates) << " byte updates encoding cannot";
82  buffer << " fit in ";
83  buffer << buffer_remaining << " byte buffer\n";
84 
85  throw exceptions::MemoryException (buffer.str ());
86  }
87  buffer_remaining -= sizeof (updates);
88 
89  // Remove clock field from the buffer and update accordingly
90  if ((size_t)buffer_remaining >= sizeof (clock))
91  {
92  memcpy (&clock, buffer, sizeof (clock));
94  buffer += sizeof (clock);
95  }
96  else
97  {
98  std::stringstream buffer;
99  buffer << "ReducedMessageHeader::read: ";
100  buffer << sizeof (clock) << " byte clock encoding cannot";
101  buffer << " fit in ";
102  buffer << buffer_remaining << " byte buffer\n";
103 
104  throw exceptions::MemoryException (buffer.str ());
105  }
106  buffer_remaining -= sizeof (clock);
107 
108  // Remove timestamp field from the buffer and update accordingly
109  if ((size_t)buffer_remaining >= sizeof (timestamp))
110  {
111  memcpy (&timestamp, buffer, sizeof (timestamp));
113  buffer += sizeof (timestamp);
114  }
115  else
116  {
117  std::stringstream buffer;
118  buffer << "ReducedMessageHeader::read: ";
119  buffer << sizeof (timestamp) << " byte timestamp encoding cannot";
120  buffer << " fit in ";
121  buffer << buffer_remaining << " byte buffer\n";
122 
123  throw exceptions::MemoryException (buffer.str ());
124  }
125  buffer_remaining -= sizeof (timestamp);
126 
127  // Remove the time to live field from the buffer
128  if (buffer_remaining >= 1)
129  {
130  memcpy (&ttl, buffer, 1);
131  buffer += 1;
132  }
133  buffer_remaining -= 1;
134 
135  return buffer;
136 }
137 
138 char *
140  int64_t & buffer_remaining)
141 {
142  // Write size field from the buffer and update accordingly
143  if ((size_t)buffer_remaining >= sizeof (size))
144  {
145  *(uint64_t *) buffer = madara::utility::endian_swap (size);
146  buffer += sizeof (size);
147  }
148  else
149  {
150  std::stringstream buffer;
151  buffer << "ReducedMessageHeader::write: ";
152  buffer << sizeof (size) << " byte size encoding cannot";
153  buffer << " fit in ";
154  buffer << buffer_remaining << " byte buffer\n";
155 
156  throw exceptions::MemoryException (buffer.str ());
157  }
158  buffer_remaining -= sizeof (size);
159 
160  // Write madara_id field from the buffer and update accordingly
161  if ((size_t)buffer_remaining >= sizeof (char) * MADARA_IDENTIFIER_LENGTH)
162  {
163  strncpy (buffer, madara_id, MADARA_IDENTIFIER_LENGTH);
164  buffer += sizeof (char) * MADARA_IDENTIFIER_LENGTH;
165  }
166  else
167  {
168  std::stringstream buffer;
169  buffer << "ReducedMessageHeader::write: ";
170  buffer << MADARA_IDENTIFIER_LENGTH << " byte id encoding cannot";
171  buffer << " fit in ";
172  buffer << buffer_remaining << " byte buffer\n";
173 
174  throw exceptions::MemoryException (buffer.str ());
175  }
176  buffer_remaining -= sizeof (char) * MADARA_IDENTIFIER_LENGTH;
177 
178  // Write updates field from the buffer and update accordingly
179  if ((size_t)buffer_remaining >= sizeof (updates))
180  {
181  *(uint32_t *) buffer = madara::utility::endian_swap (updates);
182  buffer += sizeof (updates);
183  }
184  else
185  {
186  std::stringstream buffer;
187  buffer << "ReducedMessageHeader::write: ";
188  buffer << sizeof (updates) << " byte updates encoding cannot";
189  buffer << " fit in ";
190  buffer << buffer_remaining << " byte buffer\n";
191 
192  throw exceptions::MemoryException (buffer.str ());
193  }
194  buffer_remaining -= sizeof (updates);
195 
196  // Write clock field from the buffer and update accordingly
197  if ((size_t)buffer_remaining >= sizeof (clock))
198  {
199  *(uint64_t *) buffer = madara::utility::endian_swap (clock);
200  buffer += sizeof (clock);
201  }
202  else
203  {
204  std::stringstream buffer;
205  buffer << "ReducedMessageHeader::write: ";
206  buffer << sizeof (clock) << " byte clock encoding cannot";
207  buffer << " fit in ";
208  buffer << buffer_remaining << " byte buffer\n";
209 
210  throw exceptions::MemoryException (buffer.str ());
211  }
212  buffer_remaining -= sizeof (clock);
213 
214  // Write timestamp field from the buffer and update accordingly
215  if ((size_t)buffer_remaining >= sizeof (timestamp))
216  {
217  *(uint64_t *) buffer = madara::utility::endian_swap (timestamp);
218  buffer += sizeof (timestamp);
219  }
220  else
221  {
222  std::stringstream buffer;
223  buffer << "ReducedMessageHeader::write: ";
224  buffer << sizeof (timestamp) << " byte timestamp encoding cannot";
225  buffer << " fit in ";
226  buffer << buffer_remaining << " byte buffer\n";
227 
228  throw exceptions::MemoryException (buffer.str ());
229  }
230  buffer_remaining -= sizeof (timestamp);
231 
232  // Write the time to live field
233  if (buffer_remaining >= 1)
234  {
235  memcpy (buffer, &ttl, 1);
236  buffer += 1;
237  }
238  buffer_remaining -= 1;
239 
240  return buffer;
241 }
242 
245 {
246  std::stringstream buffer;
247  buffer << "29: size (8:" << size << "), ";
248  buffer << "encoding (8:" << madara_id << "), ";
249  buffer << "numupdates (4:" << updates << "), ";
250  buffer << "clock (8:" << clock << "), ";
251  buffer << "ttl (1:" << ttl << "), ";
252 
253  return buffer.str ();
254 }
255 
256 
257 bool
259  const MessageHeader & other)
260 {
261  return size == other.size &&
262  updates == other.updates &&
263  clock == other.clock &&
264  timestamp == other.timestamp;
265 }
266 
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.
static struct madara::knowledge::tags::string_t string
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.inl:115
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
An exception for general memory errors like out-of-memory.
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:21
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:57
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...