|
1 /* |
|
2 * Copyright 2008 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifndef _SEQUENCE_ID_H_ |
|
18 #define _SEQUENCE_ID_H_ |
|
19 |
|
20 #include <oasys/debug/Formatter.h> |
|
21 #include "../naming/EndpointID.h" |
|
22 |
|
23 namespace dtn { |
|
24 |
|
25 /** |
|
26 * A bundle SequenceID is a version vector of (EID, counter) and/or |
|
27 * (EID, identifier) tuples. |
|
28 */ |
|
29 class SequenceID : public oasys::Formatter { |
|
30 public: |
|
31 SequenceID(); |
|
32 |
|
33 /** |
|
34 * Type variable for whether an entry in the vector is a counter |
|
35 * or an unordered identifier. |
|
36 */ |
|
37 typedef enum { |
|
38 EMPTY = 0, |
|
39 COUNTER = 1, |
|
40 IDENTIFIER = 2 |
|
41 } type_t; |
|
42 |
|
43 /** |
|
44 * Valid return values from compare(). |
|
45 */ |
|
46 typedef enum { |
|
47 LT = -1, ///< Less than |
|
48 EQ = 0, ///< Equal to |
|
49 GT = 1, ///< Greater than |
|
50 NEQ = 2, ///< Incomparable |
|
51 ILL = 999 ///< Illegal state |
|
52 } comp_t; |
|
53 |
|
54 /// Pretty printer for comp_t. |
|
55 static const char* comp2str(comp_t eq); |
|
56 |
|
57 /// Add an (EID, counter) tuple to the vector |
|
58 void add(const EndpointID& eid, u_int64_t counter); |
|
59 |
|
60 /// Add an (EID, identifier) tuple to the vector |
|
61 void add(const EndpointID& eid, const std::string& identifier); |
|
62 |
|
63 /** |
|
64 * Get the counter for a particular EID. |
|
65 * If no entry is found or the found entry is an identifier, returns 0. |
|
66 */ |
|
67 u_int64_t get_counter(const EndpointID& eid) const; |
|
68 |
|
69 /** |
|
70 * Get the identifier for a particular EID. If no entry is found |
|
71 * or the found entry is a counter, returns an empty string. |
|
72 */ |
|
73 std::string get_identifier(const EndpointID& eid) const; |
|
74 |
|
75 /// Virtual from Formatter |
|
76 int format(char* buf, size_t sz) const; |
|
77 |
|
78 /// Get a string representation |
|
79 std::string to_str() const; |
|
80 |
|
81 /// Build a sequence id from a string representation. If the value |
|
82 /// part contains only numeric characters, it is treated as a |
|
83 /// counter, otherwise it is treated as an identifier |
|
84 bool parse(const std::string& str); |
|
85 |
|
86 /// Compare two vectors, returning a comp_t. |
|
87 comp_t compare(const SequenceID& v) const; |
|
88 |
|
89 /// @{ Operator overloads |
|
90 bool operator<(const SequenceID& v) const { return compare(v) == LT; } |
|
91 bool operator>(const SequenceID& v) const { return compare(v) == GT; } |
|
92 bool operator==(const SequenceID& v) const { return compare(v) == EQ; } |
|
93 |
|
94 /** Note! This means not comparable -NOT- not equal to */ |
|
95 bool operator!=(const SequenceID& v) const { return compare(v) == NEQ; } |
|
96 |
|
97 bool operator<=(const SequenceID& v) const |
|
98 { |
|
99 int cmp = compare(v); |
|
100 return cmp == LT || cmp == EQ; |
|
101 } |
|
102 |
|
103 bool operator>=(const SequenceID& v) const |
|
104 { |
|
105 int cmp = compare(v); |
|
106 return cmp == GT || cmp == EQ; |
|
107 } |
|
108 /// @} |
|
109 |
|
110 /// Reset the SequenceID to be a copy of the other |
|
111 void assign(const SequenceID& other); |
|
112 |
|
113 /// Update the sequence id to include the max of all current |
|
114 /// entries and the new one. |
|
115 void update(const SequenceID& other); |
|
116 |
|
117 /// An entry in the vector |
|
118 struct Entry { |
|
119 Entry() |
|
120 : eid_(), type_(EMPTY), counter_(0), identifier_("") {} |
|
121 |
|
122 Entry(const EndpointID& eid, u_int64_t counter) |
|
123 : eid_(eid), type_(COUNTER), counter_(counter), identifier_("") {} |
|
124 |
|
125 Entry(const EndpointID& eid, const std::string& id) |
|
126 : eid_(eid), type_(IDENTIFIER), counter_(0), identifier_(id) {} |
|
127 |
|
128 Entry(const Entry& other) |
|
129 : eid_(other.eid_), type_(other.type_), |
|
130 counter_(other.counter_), identifier_(other.identifier_) {} |
|
131 |
|
132 EndpointID eid_; |
|
133 type_t type_; |
|
134 u_int64_t counter_; |
|
135 std::string identifier_; |
|
136 }; |
|
137 |
|
138 /// Get an entry for the given EID. If no matching entry exists in |
|
139 /// the vector, returns a static null entry with counter == 0 and |
|
140 /// identifier == "" |
|
141 const Entry& get_entry(const EndpointID& eid) const; |
|
142 |
|
143 /// @{ Typedefs and wrappers for the entry vector and iterators |
|
144 typedef std::vector<Entry> EntryVec; |
|
145 typedef EntryVec::iterator iterator; |
|
146 typedef EntryVec::const_iterator const_iterator; |
|
147 |
|
148 bool empty() const { return vector_.empty(); } |
|
149 EntryVec::iterator begin() { return vector_.begin(); } |
|
150 EntryVec::iterator end() { return vector_.end(); } |
|
151 EntryVec::const_iterator begin() const { return vector_.begin(); } |
|
152 EntryVec::const_iterator end() const { return vector_.end(); } |
|
153 /// @} |
|
154 |
|
155 private: |
|
156 /// The entry vector |
|
157 EntryVec vector_; |
|
158 |
|
159 /// Compare two entries |
|
160 static comp_t compare_entries(const Entry& left, const Entry& right); |
|
161 |
|
162 /// Compare vectors in a single direction |
|
163 static comp_t compare_one_way(const SequenceID& lv, |
|
164 const SequenceID& rv, |
|
165 comp_t cur_state); |
|
166 }; |
|
167 |
|
168 } // namespace dtn |
|
169 |
|
170 #endif /* _SEQUENCE_ID_H_ */ |