MADARA  3.4.1
FileHeader.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <sstream>
3 #include <time.h>
4 
6 #include "FileHeader.h"
8 
10  : size(encoded_size()),
11  states(0),
12  initial_timestamp(utility::get_time()),
13  last_timestamp(utility::get_time()),
14  karl_version(madara::utility::get_uint_version())
15 {
16  memcpy(file_type, "KaRL1.5", 7);
17  file_type[7] = 0;
18 
19  originator[0] = 0;
20 
22 }
23 
25 
27 {
28  return sizeof(uint64_t) * 4 + sizeof(char) * (64 + 8) + sizeof(uint32_t);
29 }
30 
32  const char* buffer, int64_t& buffer_remaining)
33 {
34  // Remove size field from the buffer and update accordingly
35  if ((size_t)buffer_remaining >= sizeof(size))
36  {
37  size = madara::utility::endian_swap(*(uint64_t*)buffer);
38  buffer += sizeof(size);
39  }
40  else
41  {
42  std::stringstream buffer;
43  buffer << "FileHeader::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 states field from the buffer and update accordingly
53  if ((size_t)buffer_remaining >= sizeof(states))
54  {
55  states = madara::utility::endian_swap(*(uint64_t*)buffer);
56  buffer += sizeof(states);
57  }
58  else
59  {
60  std::stringstream buffer;
61  buffer << "FileHeader::read: ";
62  buffer << sizeof(states) << " byte states 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(states);
69 
70  // Remove initial_timestamp field from the buffer and update accordingly
71  if ((size_t)buffer_remaining >= sizeof(initial_timestamp))
72  {
73  initial_timestamp = madara::utility::endian_swap(*(uint64_t*)buffer);
74  buffer += sizeof(initial_timestamp);
75  }
76  else
77  {
78  std::stringstream buffer;
79  buffer << "FileHeader::read: ";
80  buffer << sizeof(initial_timestamp)
81  << " byte initial_timestamp 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(initial_timestamp);
88 
89  // Remove initial_timestamp field from the buffer and update accordingly
90  if ((size_t)buffer_remaining >= sizeof(last_timestamp))
91  {
92  last_timestamp = madara::utility::endian_swap(*(uint64_t*)buffer);
93  buffer += sizeof(last_timestamp);
94  }
95  else
96  {
97  std::stringstream buffer;
98  buffer << "FileHeader::read: ";
99  buffer << sizeof(last_timestamp) << " byte last_timestamp encoding cannot";
100  buffer << " fit in ";
101  buffer << buffer_remaining << " byte buffer\n";
102 
103  throw exceptions::MemoryException(buffer.str());
104  }
105  buffer_remaining -= sizeof(last_timestamp);
106 
107  // Remove file_type from the buffer and update accordingly
108  if ((size_t)buffer_remaining >= sizeof(char) * 8)
109  {
110  utility::strncpy_safe(file_type, buffer, 8);
111  buffer += sizeof(char) * 8;
112  }
113  else
114  {
115  std::stringstream buffer;
116  buffer << "FileHeader::read: ";
117  buffer << 8 << " byte file_type encoding cannot";
118  buffer << " fit in ";
119  buffer << buffer_remaining << " byte buffer\n";
120 
121  throw exceptions::MemoryException(buffer.str());
122  }
123  buffer_remaining -= sizeof(char) * 8;
124 
125  // Remove karl_version field from the buffer and update accordingly
126  if ((size_t)buffer_remaining >= sizeof(karl_version))
127  {
128  karl_version = madara::utility::endian_swap(*(uint32_t*)buffer);
129  buffer += sizeof(karl_version);
130  }
131  else
132  {
133  std::stringstream buffer;
134  buffer << "FileHeader::read: ";
135  buffer << sizeof(karl_version) << " byte version encoding cannot";
136  buffer << " fit in ";
137  buffer << buffer_remaining << " byte buffer\n";
138 
139  throw exceptions::MemoryException(buffer.str());
140  }
141  buffer_remaining -= sizeof(karl_version);
142 
143  // Remove originator from the buffer and update accordingly
144  if ((size_t)buffer_remaining >= sizeof(char) * 64)
145  {
146  utility::strncpy_safe(originator, buffer, 64);
147  buffer += sizeof(char) * 64;
148  }
149  else
150  {
151  std::stringstream buffer;
152  buffer << "FileHeader::read: ";
153  buffer << sizeof(originator) << " byte originator encoding cannot";
154  buffer << " fit in ";
155  buffer << buffer_remaining << " byte buffer\n";
156 
157  throw exceptions::MemoryException(buffer.str());
158  }
159  buffer_remaining -= sizeof(char) * 64;
160 
161  return buffer;
162 }
163 
165  char* buffer, int64_t& buffer_remaining)
166 {
167  // Write size field to the buffer and update accordingly
168  if ((size_t)buffer_remaining >= sizeof(size))
169  {
170  *(uint64_t*)buffer = madara::utility::endian_swap(size);
171  buffer += sizeof(size);
172  }
173  else
174  {
175  std::stringstream buffer;
176  buffer << "FileHeader::read: ";
177  buffer << sizeof(size) << " byte size encoding cannot";
178  buffer << " fit in ";
179  buffer << buffer_remaining << " byte buffer\n";
180 
181  throw exceptions::MemoryException(buffer.str());
182  }
183  buffer_remaining -= sizeof(size);
184 
185  // Write states field to the buffer and update accordingly
186  if ((size_t)buffer_remaining >= sizeof(states))
187  {
188  *(uint64_t*)buffer = madara::utility::endian_swap(states);
189  buffer += sizeof(states);
190  }
191  else
192  {
193  std::stringstream buffer;
194  buffer << "FileHeader::read: ";
195  buffer << sizeof(states) << " byte states encoding cannot";
196  buffer << " fit in ";
197  buffer << buffer_remaining << " byte buffer\n";
198 
199  throw exceptions::MemoryException(buffer.str());
200  }
201  buffer_remaining -= sizeof(states);
202 
203  // Write initial_timestamp field to the buffer and update accordingly
204  if ((size_t)buffer_remaining >= sizeof(initial_timestamp))
205  {
206  *(uint64_t*)buffer = madara::utility::endian_swap(initial_timestamp);
207  buffer += sizeof(initial_timestamp);
208  }
209  else
210  {
211  std::stringstream buffer;
212  buffer << "FileHeader::read: ";
213  buffer << sizeof(initial_timestamp)
214  << " byte initial_timestamp encoding cannot";
215  buffer << " fit in ";
216  buffer << buffer_remaining << " byte buffer\n";
217 
218  throw exceptions::MemoryException(buffer.str());
219  }
220  buffer_remaining -= sizeof(initial_timestamp);
221 
222  // Write last_timestamp field to the buffer and update accordingly
223  if ((size_t)buffer_remaining >= sizeof(last_timestamp))
224  {
225  *(uint64_t*)buffer = madara::utility::endian_swap(last_timestamp);
226  buffer += sizeof(last_timestamp);
227  }
228  else
229  {
230  std::stringstream buffer;
231  buffer << "FileHeader::read: ";
232  buffer << sizeof(last_timestamp) << " byte last_timestamp encoding cannot";
233  buffer << " fit in ";
234  buffer << buffer_remaining << " byte buffer\n";
235 
236  throw exceptions::MemoryException(buffer.str());
237  }
238  buffer_remaining -= sizeof(last_timestamp);
239 
240  // Write file_type field from the buffer and update accordingly
241  if ((size_t)buffer_remaining >= sizeof(char) * 8)
242  {
243  utility::strncpy_safe(buffer, file_type, 8);
244  buffer += sizeof(char) * 8;
245  }
246  else
247  {
248  std::stringstream buffer;
249  buffer << "FileHeader::read: ";
250  buffer << 8 << " byte last_timestamp encoding cannot";
251  buffer << " fit in ";
252  buffer << buffer_remaining << " byte buffer\n";
253 
254  throw exceptions::MemoryException(buffer.str());
255  }
256  buffer_remaining -= sizeof(char) * 8;
257 
258  // Write karl_version field to the buffer and update accordingly
259  if ((size_t)buffer_remaining >= sizeof(karl_version))
260  {
261  *(uint32_t*)buffer = madara::utility::endian_swap(karl_version);
262  buffer += sizeof(karl_version);
263  }
264  else
265  {
266  std::stringstream buffer;
267  buffer << "FileHeader::read: ";
268  buffer << sizeof(karl_version) << " byte version encoding cannot";
269  buffer << " fit in ";
270  buffer << buffer_remaining << " byte buffer\n";
271 
272  throw exceptions::MemoryException(buffer.str());
273  }
274  buffer_remaining -= sizeof(karl_version);
275 
276  // Write originator field from the buffer and update accordingly
277  if ((size_t)buffer_remaining >= sizeof(char) * 64)
278  {
279  utility::strncpy_safe(buffer, originator, 64);
280  buffer += sizeof(char) * 64;
281  }
282  else
283  {
284  std::stringstream buffer;
285  buffer << "FileHeader::read: ";
286  buffer << sizeof(originator) << " byte originator encoding cannot";
287  buffer << " fit in ";
288  buffer << buffer_remaining << " byte buffer\n";
289 
290  throw exceptions::MemoryException(buffer.str());
291  }
292  buffer_remaining -= sizeof(char) * 64;
293 
294  return buffer;
295 }
296 
298 {
299  return size == other.size && states == other.states &&
300  initial_timestamp == other.initial_timestamp &&
301  last_timestamp == other.last_timestamp &&
302  karl_version == other.karl_version &&
303  strncmp(file_type, other.file_type, 4) == 0 &&
304  strncmp(originator, other.originator, 64) == 0;
305 }
An exception for general memory errors like out-of-memory.
Defines a file header which is the default for KaRL checkpointing.
Definition: FileHeader.h:37
virtual ~FileHeader()
Destructor.
Definition: FileHeader.cpp:24
static uint32_t encoded_size(void)
Returns the size of the encoded FileHeader class, which may be different from sizeof (FileHeader) bec...
Definition: FileHeader.cpp:26
uint64_t initial_timestamp
the timestamp for the initial checkpointing
Definition: FileHeader.h:107
virtual char * write(char *buffer, int64_t &buffer_remaining)
Writes a FileHeader instance to a buffer and updates the amount of buffer room remaining.
Definition: FileHeader.cpp:164
char originator[64]
the originator of the message (host:port)
Definition: FileHeader.h:127
uint64_t size
the size of this header plus the updates
Definition: FileHeader.h:97
uint64_t last_timestamp
the timestamp for the last checkpoint
Definition: FileHeader.h:112
uint64_t states
the number of states checkpointed in the file stream
Definition: FileHeader.h:102
virtual bool equals(const FileHeader &other)
Compares the fields of this instance to another instance.
Definition: FileHeader.cpp:297
virtual const char * read(const char *buffer, int64_t &buffer_remaining)
Reads a FileHeader instance from a buffer and updates the amount of buffer room remaining.
Definition: FileHeader.cpp:31
uint32_t karl_version
Version of KaRL installed when file was created.
Definition: FileHeader.h:122
char file_type[8]
file type identifier ("KaRL1.5")
Definition: FileHeader.h:117
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:15
uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.inl:134
int64_t get_time(void)
Returns a time of day in nanoseconds If simtime feature is enabled, this may be simulation time inste...
Definition: Utility.inl:265
MADARA_EXPORT void strncpy_safe(char *dst, const char *src, size_t dst_size)
Performs a strncpy in a way that will compile without warnings.
Definition: Utility.cpp:376
uint32_t get_uint_version(const std::string &str_version)
Converts a string version to a uint32.
Definition: Utility.cpp:30
Copyright(c) 2020 Galois.