8 #include "gcomm/view.hpp"
9 #include "gcomm/types.hpp"
10 #include "gcomm/uuid.hpp"
13 #include "gu_serialize.hpp"
27 std::ostream& operator<<(std::ostream&,
const Node&);
28 std::ostream& operator<<(std::ostream&,
const Message&);
29 bool operator==(
const Message&,
const Message&);
44 Node(
const bool prim =
false,
45 const bool un =
false,
46 const uint32_t last_seq = std::numeric_limits<uint32_t>::max(),
48 const int64_t to_seq = -1,
49 const int weight = -1,
50 const SegmentId segment = 0)
54 last_seq_ (last_seq ),
55 last_prim_ (last_prim),
61 void set_prim (
const bool val) { prim_ = val ; }
62 void set_un (
const bool un) { un_ = un ; }
63 void set_last_seq (
const uint32_t seq) { last_seq_ = seq ; }
64 void set_last_prim (
const ViewId& last_prim) { last_prim_ = last_prim; }
65 void set_to_seq (
const uint64_t seq) { to_seq_ = seq ; }
66 void set_weight (
const int weight) { weight_ = weight ; }
67 void set_segment (
const SegmentId segment) { segment_ = segment ; }
69 bool prim()
const {
return prim_ ; }
70 bool un()
const {
return un_ ; }
71 uint32_t last_seq()
const {
return last_seq_ ; }
72 const ViewId& last_prim()
const {
return last_prim_; }
73 int64_t to_seq()
const {
return to_seq_ ; }
74 int weight()
const {
return weight_ ; }
75 SegmentId segment()
const {
return segment_ ; }
77 size_t unserialize(
const gu::byte_t* buf,
const size_t buflen,
const size_t offset)
82 gu_trace (off = gu::unserialize4(buf, buflen, off, flags));
84 prim_ = flags & F_PRIM;
88 weight_ = flags >> 24;
94 segment_ = (flags >> 16) & 0xff;
95 gu_trace (off = gu::unserialize4(buf, buflen, off, last_seq_));
96 gu_trace (off = last_prim_.unserialize(buf, buflen, off));
97 gu_trace (off = gu::unserialize8(buf, buflen, off, to_seq_));
102 size_t serialize(gu::byte_t* buf,
const size_t buflen,
const size_t offset)
const
107 flags |= prim_ ? F_PRIM : 0;
108 flags |= un_ ? F_UN : 0;
112 flags |= weight_ << 24;
114 flags |=
static_cast<uint32_t
>(segment_) << 16;
115 gu_trace (off = gu::serialize4(flags, buf, buflen, off));
116 gu_trace (off = gu::serialize4(last_seq_, buf, buflen, off));
117 gu_trace (off = last_prim_.serialize(buf, buflen, off));
118 gu_trace (off = gu::serialize8(to_seq_, buf, buflen, off));
120 assert (serial_size() == (off - offset));
125 static size_t serial_size()
127 Node* node(reinterpret_cast<Node*>(0));
130 return (
sizeof(uint32_t) +
sizeof(node->last_seq_) +
131 ViewId::serial_size() +
sizeof(node->to_seq_));
134 bool operator==(
const Node& cmp)
const
136 return (prim() == cmp.prim() &&
138 last_seq() == cmp.last_seq() &&
139 last_prim() == cmp.last_prim() &&
140 to_seq() == cmp.to_seq() &&
141 weight() == cmp.weight() &&
142 segment() == cmp.segment() );
145 std::string to_string()
const
147 std::ostringstream ret;
149 ret <<
"prim=" << prim_
151 <<
",last_seq=" << last_seq_
152 <<
",last_prim=" << last_prim_
153 <<
",to_seq=" << to_seq_
154 <<
",weight=" << weight_
155 <<
",segment=" <<
static_cast<int>(segment_);
172 inline std::ostream& gcomm::pc::operator<<(std::ostream& os,
const Node& n)
174 return (os << n.to_string());
185 enum Type {T_NONE, T_STATE, T_INSTALL, T_USER, T_MAX};
190 F_WEIGHT_CHANGE = 0x4
193 static const char* to_string(Type t)
195 static const char* str[T_MAX] =
196 {
"NONE",
"STATE",
"INSTALL",
"USER" };
198 if (t < T_MAX)
return str[t];
204 Message(
const int version = -1,
205 const Type type = T_NONE,
206 const uint32_t seq = 0,
219 version_ (msg.version_ ),
220 flags_ (msg.flags_ ),
223 crc16_ (msg.crc16_ ),
224 node_map_(msg.node_map_)
230 int version()
const {
return version_; }
231 Type type()
const {
return type_; }
232 uint32_t seq()
const {
return seq_; }
234 void flags(
int flags) { flags_ = flags; }
235 int flags()
const {
return flags_; }
236 void checksum(uint16_t crc16,
bool flag)
248 uint16_t checksum()
const {
return crc16_; }
250 const NodeMap& node_map()
const {
return node_map_; }
251 NodeMap& node_map() {
return node_map_; }
253 const Node& node(
const UUID& uuid)
const
254 {
return NodeMap::value(node_map_.find_checked(uuid)); }
256 {
return NodeMap::value(node_map_.find_checked(uuid)); }
258 size_t unserialize(
const gu::byte_t* buf,
const size_t buflen,
const size_t offset)
265 gu_trace (off = gu::unserialize4(buf, buflen, offset, b));
268 flags_ = (b & 0xf0) >> 4;
270 gu_throw_error (EPROTONOSUPPORT)
271 <<
"Unsupported protocol varsion: " << version_;
273 type_ =
static_cast<Type
>((b >> 8) & 0xff);
274 if (type_ <= T_NONE || type_ >= T_MAX)
275 gu_throw_error (EINVAL) <<
"Bad type value: " << type_;
277 crc16_ = ((b >> 16) & 0xffff);
279 gu_trace (off = gu::unserialize4(buf, buflen, off, seq_));
281 if (type_ == T_STATE || type_ == T_INSTALL)
283 gu_trace (off = node_map_.unserialize(buf, buflen, off));
289 size_t serialize(gu::byte_t* buf,
const size_t buflen,
const size_t offset)
const
298 b |= version_ & 0x0f;
299 b |= (flags_ << 4) & 0xf0;
301 gu_trace (off = gu::serialize4(b, buf, buflen, offset));
302 gu_trace (off = gu::serialize4(seq_, buf, buflen, off));
305 if (type_ == T_STATE || type_ == T_INSTALL)
307 gu_trace (off = node_map_.serialize(buf, buflen, off));
310 assert (serial_size() == (off - offset));
315 size_t serial_size()
const
318 return (
sizeof(uint32_t)
320 + (type_ == T_STATE || type_ == T_INSTALL ?
321 node_map_.serial_size() :
326 std::string to_string()
const
328 std::ostringstream ret;
330 ret <<
"pcmsg{ type=" << to_string(type_) <<
", seq=" << seq_;
331 ret <<
", flags=" << std::setw(2) << std::hex << flags_;
332 ret <<
", node_map {" << node_map() <<
"}";
350 inline std::ostream& gcomm::pc::operator<<(std::ostream& os,
const Message& m)
352 return (os << m.to_string());
377 inline bool gcomm::pc::operator==(
const Message& a,
const Message& b)
379 return (a.version() == b.version() &&
380 a.checksum() == b.checksum() &&
381 a.type() == b.type() &&
382 a.seq() == b.seq() &&
383 a.node_map() == b.node_map());
387 #endif // PC_MESSAGE_HPP
Definition: pc_message.hpp:34
Definition: pc_message.hpp:181
Definition: pc_message.hpp:363
Definition: pc_message.hpp:356
Definition: pc_message.hpp:178
Definition: pc_message.hpp:370