MADARA  3.1.8
FileHeader.cpp
Go to the documentation of this file.
1 #include "FileHeader.h"
3 #include <algorithm>
4 #include <time.h>
5 
7 : size (encoded_size ()),
8  states (0), initial_timestamp (time (NULL)),
9  last_timestamp (0),
10  karl_version (madara::utility::get_uint_version ())
11 {
12  memcpy (file_type, "KaRL", 4);
13  file_type[4] = 0;
14  file_type[5] = 0;
15  file_type[6] = 0;
16  file_type[7] = 0;
17 
18  originator[0] = 0;
19 
21 }
22 
23 
25 {
26 }
27 
28 uint32_t
30 {
31  return sizeof (uint64_t) * 4
32  + sizeof (char) * (64 + 8)
33  + sizeof (uint32_t);
34 }
35 
36 const char *
38  int64_t & buffer_remaining)
39 {
40  // Remove size field from the buffer and update accordingly
41  if ((size_t)buffer_remaining >= sizeof (size))
42  {
43  size = madara::utility::endian_swap (*(uint64_t *)buffer);
44  buffer += sizeof (size);
45  }
46  buffer_remaining -= sizeof (size);
47 
48  // Remove states field from the buffer and update accordingly
49  if ((size_t)buffer_remaining >= sizeof (states))
50  {
51  states = madara::utility::endian_swap (*(uint64_t *)buffer);
52  buffer += sizeof (states);
53  }
54  buffer_remaining -= sizeof (states);
55 
56  // Remove initial_timestamp field from the buffer and update accordingly
57  if ((size_t)buffer_remaining >= sizeof (initial_timestamp))
58  {
59  initial_timestamp = madara::utility::endian_swap (*(uint64_t *)buffer);
60  buffer += sizeof (initial_timestamp);
61  }
62  buffer_remaining -= sizeof (initial_timestamp);
63 
64  // Remove initial_timestamp field from the buffer and update accordingly
65  if ((size_t)buffer_remaining >= sizeof (last_timestamp))
66  {
67  last_timestamp = madara::utility::endian_swap (*(uint64_t *)buffer);
68  buffer += sizeof (last_timestamp);
69  }
70  buffer_remaining -= sizeof (last_timestamp);
71 
72  // Remove file_type from the buffer and update accordingly
73  if ((size_t)buffer_remaining >= sizeof (char) * 8)
74  {
75  strncpy (file_type, buffer, 8);
76  buffer += sizeof (char) * 8;
77  }
78  buffer_remaining -= sizeof (char) * 8;
79 
80  // Remove karl_version field from the buffer and update accordingly
81  if ((size_t)buffer_remaining >= sizeof (karl_version))
82  {
83  karl_version = madara::utility::endian_swap (*(uint32_t *)buffer);
84  buffer += sizeof (karl_version);
85  }
86  buffer_remaining -= sizeof (karl_version);
87 
88  // Remove originator from the buffer and update accordingly
89  if ((size_t)buffer_remaining >= sizeof (char) * 64)
90  {
91  strncpy (originator, buffer, 64);
92  buffer += sizeof (char) * 64;
93  }
94  buffer_remaining -= sizeof (char) * 64;
95 
96  return buffer;
97 }
98 
99 char *
101  int64_t & buffer_remaining)
102 {
103  // Write size field to the buffer and update accordingly
104  if ((size_t)buffer_remaining >= sizeof (size))
105  {
106  *(uint64_t *) buffer = madara::utility::endian_swap (size);
107  buffer += sizeof (size);
108  }
109  buffer_remaining -= sizeof (size);
110 
111  // Write states field to the buffer and update accordingly
112  if ((size_t)buffer_remaining >= sizeof (states))
113  {
114  *(uint64_t *) buffer = madara::utility::endian_swap (states);
115  buffer += sizeof (states);
116  }
117  buffer_remaining -= sizeof (states);
118 
119  // Write initial_timestamp field to the buffer and update accordingly
120  if ((size_t)buffer_remaining >= sizeof (initial_timestamp))
121  {
122  *(uint64_t *) buffer = madara::utility::endian_swap (initial_timestamp);
123  buffer += sizeof (initial_timestamp);
124  }
125  buffer_remaining -= sizeof (initial_timestamp);
126 
127  // Write last_timestamp field to the buffer and update accordingly
128  if ((size_t)buffer_remaining >= sizeof (last_timestamp))
129  {
130  *(uint64_t *) buffer = madara::utility::endian_swap (last_timestamp);
131  buffer += sizeof (last_timestamp);
132  }
133  buffer_remaining -= sizeof (last_timestamp);
134 
135  // Write file_type field from the buffer and update accordingly
136  if ((size_t)buffer_remaining >= sizeof (char) * 8)
137  {
138  strncpy (buffer, file_type, 8);
139  buffer += sizeof (char) * 8;
140  }
141  buffer_remaining -= sizeof (char) * 8;
142 
143  // Write karl_version field to the buffer and update accordingly
144  if ((size_t)buffer_remaining >= sizeof (karl_version))
145  {
146  *(uint32_t *) buffer = madara::utility::endian_swap (karl_version);
147  buffer += sizeof (karl_version);
148  }
149  buffer_remaining -= sizeof (karl_version);
150 
151 
152  // Write originator field from the buffer and update accordingly
153  if ((size_t)buffer_remaining >= sizeof (char) * 64)
154  {
155  strncpy (buffer, originator, 64);
156  buffer += sizeof (char) * 64;
157  }
158  buffer_remaining -= sizeof (char) * 64;
159 
160  return buffer;
161 }
162 
163 bool
165 {
166  return size == other.size &&
167  states == other.states &&
169  last_timestamp == other.last_timestamp &&
170  karl_version == other.karl_version &&
171  strncmp (file_type, other.file_type, 4) == 0 &&
172  strncmp (originator, other.originator, 64) == 0;
173 }
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:37
Defines a file header which is the default for KaRL checkpointing.
Definition: FileHeader.h:35
char file_type[8]
file type identifier ("KaRL")
Definition: FileHeader.h:116
char originator[64]
the originator of the message (host:port)
Definition: FileHeader.h:126
uint64_t initial_timestamp
the timestamp for the initial checkpointing
Definition: FileHeader.h:106
uint64_t last_timestamp
the timestamp for the last checkpoint
Definition: FileHeader.h:111
uint64_t size
the size of this header plus the updates
Definition: FileHeader.h:96
MADARA_Export uint32_t get_uint_version(void)
Gets the MADARA version number.
Definition: Utility.cpp:35
uint32_t karl_version
Version of KaRL installed when file was created.
Definition: FileHeader.h:121
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:29
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:100
MADARA_Export uint64_t endian_swap(uint64_t value)
Converts a host format uint64_t into big endian.
Definition: Utility.cpp:625
virtual bool equals(const FileHeader &other)
Compares the fields of this instance to another instance.
Definition: FileHeader.cpp:164
uint64_t states
the number of states checkpointed in the file stream
Definition: FileHeader.h:101
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
virtual ~FileHeader()
Destructor.
Definition: FileHeader.cpp:24
Copyright (c) 2015 Carnegie Mellon University.