0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StatsSerializable.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #include "Compat.h"
27 #include "Logger.h"
28 #include "Serialization.h"
29 #include "StatsSerializable.h"
30 
31 using namespace Hypertable;
32 
33 StatsSerializable::StatsSerializable(uint16_t _id, uint8_t _group_count)
34  : id(_id), group_count(_group_count) {
35  HT_ASSERT(group_count < sizeof(group_ids));
36 }
37 
39  id = other.id;
40  group_count = other.group_count;
41  memcpy(group_ids, other.group_ids, sizeof(group_ids));
42 }
43 
45  id = other.id;
46  group_count = other.group_count;
47  memcpy(group_ids, other.group_ids, sizeof(group_ids));
48  return *this;
49 }
50 
52  size_t len = 2;
53  for (uint8_t i = 0; i < group_count; i++) {
54  len += 3;
56  }
57  return len;
58 }
59 
60 void StatsSerializable::encode(uint8_t **bufp) const {
61  uint8_t *lenp;
62  uint16_t len;
63 
64  *(*bufp)++ = id;
65  *(*bufp)++ = group_count;
66 
67  for (uint8_t i = 0; i < group_count; i++) {
68  *(*bufp)++ = group_ids[i];
69  lenp = *bufp;
70  (*bufp) += 2;
71  encode_group(group_ids[i], bufp);
72  len = ((*bufp) - lenp) - 2;
73  Serialization::encode_i16(&lenp, len);
74  }
75 }
76 
77 void StatsSerializable::decode(const uint8_t **bufp, size_t *remainp) {
78  uint16_t len;
79 
80  // Read Section indicator
81  if (*remainp == 0)
82  HT_THROW_INPUT_OVERRUN(*remainp, 1);
83  HT_ASSERT(*(*bufp) == id);
84  (*bufp)++;
85  (*remainp)--;
86 
87  // Read Group Count
88  if (*remainp == 0)
89  HT_THROW_INPUT_OVERRUN(*remainp, 1);
90  group_count = (size_t)*(*bufp)++;
91  (*remainp)--;
92 
93  for (uint8_t i = 0; i < group_count; i++) {
94  // Read Group ID
95  if (*remainp == 0)
96  HT_THROW_INPUT_OVERRUN(*remainp, 1);
97  group_ids[i] = *(*bufp)++;
98  (*remainp)--;
99 
100  // Read Group length
101  if (*remainp < 2)
102  HT_THROW_INPUT_OVERRUN(*remainp, 2);
103  len = Serialization::decode_i16(bufp, remainp);
104  if (*remainp < len)
105  HT_THROW_INPUT_OVERRUN(*remainp, len);
106 
107  decode_group(group_ids[i], len, bufp, remainp);
108  }
109 }
110 
112  if (id == other.id &&
113  group_count == other.group_count &&
114  !memcmp(group_ids, other.group_ids, group_count))
115  return true;
116  return false;
117 }
Abstract base class for managing serialized statistics.
#define HT_THROW_INPUT_OVERRUN(_s_)
Base class managing serialized statistics.
StatsSerializable(uint16_t _id, uint8_t _group_count)
Constructor; creates a new object with an ID and a number of groups.
#define HT_ASSERT(_e_)
Definition: Logger.h:396
uint8_t group_count
Number of groups in group_ids.
virtual void decode_group(int group, uint16_t len, const uint8_t **bufp, size_t *remainp)=0
Abstruct function to deserialize a group from a memory buffer.
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
virtual void encode_group(int group, uint8_t **bufp) const =0
Abstruct function to serialize a group into a memory buffer.
size_t encoded_length() const
Returns the encoded length of this object.
Logging routines and macros.
Compatibility Macros for C/C++.
void encode_i16(uint8_t **bufp, uint16_t val)
Encode a 16-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
void decode(const uint8_t **bufp, size_t *remainp)
Deserializes this object from a buffer.
bool operator==(const StatsSerializable &other) const
Equal operator.
Hypertable definitions
virtual size_t encoded_length_group(int group) const =0
Abstruct function returning the serialized length of a group.
StatsSerializable & operator=(const StatsSerializable &other)
Assignment operator.
uint16_t id
The statistics ID.
uint8_t group_ids[32]
The actual group IDs.
void encode(uint8_t **bufp) const
Encodes the statistics to a serialized buffer.