GComm  0.2.3
view.hpp
1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  */
4 
5 
10 #ifndef _GCOMM_VIEW_HPP_
11 #define _GCOMM_VIEW_HPP_
12 
13 
14 #include "gcomm/uuid.hpp"
15 #include "gcomm/types.hpp"
16 #include "gcomm/map.hpp"
17 
18 namespace gcomm
19 {
20  typedef enum
21  {
22  V_NONE = -1,
23  V_REG = 0,
24  V_TRANS = 1,
25  V_NON_PRIM = 2,
26  V_PRIM = 3
27  } ViewType;
28 
29  class ViewId
30  {
31  public:
32 
33 
34  ViewId(const ViewType type = V_NONE,
35  const UUID& uuid = UUID::nil(),
36  const uint32_t seq = 0) :
37  type_(type),
38  uuid_(uuid),
39  seq_ (seq)
40  { }
41 
42  ViewId(const ViewType type,
43  const ViewId& vi) :
44  type_(type),
45  uuid_(vi.uuid()),
46  seq_ (vi.seq())
47  { }
48 
49  virtual ~ViewId() { }
50 
51  ViewType type() const { return type_; }
52 
53  const UUID& uuid() const { return uuid_; }
54 
55  uint32_t seq() const { return seq_; }
56 
57  size_t unserialize(const gu::byte_t* buf, size_t buflen, size_t offset);
58 
59  size_t serialize(gu::byte_t* buf, size_t buflen, size_t offset) const;
60 
61  static size_t serial_size()
62  {
63  return UUID::serial_size() + sizeof(reinterpret_cast<ViewId*>(0)->seq_);
64  }
65 
66  bool operator<(const ViewId& cmp) const
67  {
68  // View ordering:
69  // 1) view seq less than
70  // 2) uuid newer than
71  // 3) type less than
72  return (seq_ < cmp.seq_ ||
73  (seq_ == cmp.seq_ &&
74  (cmp.uuid_.older(uuid_) ||
75  (uuid_ == cmp.uuid_ && type_ < cmp.type_) ) ) );
76  }
77 
78  bool operator==(const ViewId& cmp) const
79  {
80  return (seq_ == cmp.seq_ &&
81  type_ == cmp.type_ &&
82  uuid_ == cmp.uuid_);
83  }
84 
85  bool operator!=(const ViewId& cmp) const
86  {
87  return !(*this == cmp);
88  }
89 
90  private:
91  ViewType type_;
92  UUID uuid_; // uniquely identifies the sequence of group views (?)
93  uint32_t seq_; // position in the sequence (?)
94  };
95 
96  std::ostream& operator<<(std::ostream&, const ViewId&);
97 
98  typedef uint8_t SegmentId;
99 
100  class Node
101  {
102  public:
103  Node(SegmentId segment) : segment_(segment)
104  { }
105  SegmentId segment() const { return segment_; }
106  bool operator==(const Node& cmp) const { return true; }
107  private:
108  SegmentId segment_;
109  };
110 
111  inline std::ostream& operator<<(std::ostream& os, const Node& n)
112  {
113  return (os << static_cast<int>(n.segment()) );
114  }
115 
116 
117  class NodeList : public gcomm::Map<UUID, Node> { };
118 
119  class View
120  {
121  public:
122 
123  View() :
124  bootstrap_ (false),
125  view_id_ (V_NONE),
126  members_ (),
127  joined_ (),
128  left_ (),
129  partitioned_ ()
130  { }
131 
132  View(const ViewId& view_id, bool bootstrap = false) :
133  bootstrap_ (bootstrap),
134  view_id_ (view_id),
135  members_ (),
136  joined_ (),
137  left_ (),
138  partitioned_ ()
139  { }
140 
141  ~View() {}
142 
143  void add_member (const UUID& pid, SegmentId segment);
144 
145  void add_members (NodeList::const_iterator begin,
146  NodeList::const_iterator end);
147 
148  void add_joined (const UUID& pid, SegmentId segment);
149  void add_left (const UUID& pid, SegmentId segment);
150  void add_partitioned (const UUID& pid, SegmentId segment);
151 
152  const NodeList& members () const;
153  const NodeList& joined () const;
154  const NodeList& left () const;
155  const NodeList& partitioned () const;
156 
157  NodeList& members() { return members_; }
158 
159  bool is_member(const UUID& uuid) const
160  { return members_.find(uuid) != members_.end(); }
161 
162  bool is_joining(const UUID& uuid) const
163  { return joined_.find(uuid) != joined_.end(); }
164 
165  bool is_leaving(const UUID& uuid) const
166  { return left_.find(uuid) != left_.end(); }
167 
168  bool is_partitioning(const UUID& uuid) const
169  { return partitioned_.find(uuid) != partitioned_.end(); }
170 
171 
172  ViewType type () const;
173  const ViewId& id () const;
174  const UUID& representative () const;
175 
176  bool is_empty() const;
177  bool is_bootstrap() const { return bootstrap_; }
178 
179  private:
180  bool bootstrap_; // Flag indicating if view was bootstrapped
181  ViewId view_id_; // View identifier
182  NodeList members_; // List of members in view
183  NodeList joined_; // List of newly joined members in view
184  NodeList left_; // Fracefully left members from previous view
185  NodeList partitioned_; // Partitioned members from previous view
186  };
187 
188  bool operator==(const gcomm::View&, const gcomm::View&);
189  std::ostream& operator<<(std::ostream&, const View&);
190 
191 } // namespace gcomm
192 
193 #endif // _GCOMM_VIEW_HPP_
Definition: view.hpp:29
Definition: map.hpp:210
Definition: view.hpp:119
Definition: view.hpp:100
Definition: view.hpp:117
Definition: uuid.hpp:26