A Discrete-Event Network Simulator
API
lollipop-counter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Universita' di Firenze, Italy
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #ifndef LOLLIPOP_COUNTER_H
21 #define LOLLIPOP_COUNTER_H
22 
23 #include "ns3/abort.h"
24 
25 #include <limits>
26 
27 namespace ns3
28 {
29 
66 template <class T>
68 {
69  public:
77  {
79  "Lollipop counters must be defined on unsigned integer types");
80 
81  uint16_t numberofDigits = std::numeric_limits<T>::digits;
82  m_sequenceWindow = 1 << (numberofDigits / 2);
83 
85  }
86 
96  {
97  uint16_t numberofDigits = std::numeric_limits<T>::digits;
98  m_sequenceWindow = 1 << (numberofDigits / 2);
99 
100  m_value = val;
101  }
102 
110  {
111  m_value = o.m_value;
112  return *this;
113  }
114 
118  void Reset()
119  {
121  }
122 
132  void SetSequenceWindowSize(uint16_t numberOfBits)
133  {
134  uint16_t numberofDigits = std::numeric_limits<T>::digits;
135 
137  numberOfBits >= numberofDigits,
138  "The size of the Sequence Window should be less than the counter size (which is "
139  << +m_maxValue << ")");
140 
141  m_sequenceWindow = 1 << numberOfBits;
142 
144  }
145 
160  bool IsComparable(const LollipopCounter& val) const
161  {
163  "Can not compare two Lollipop Counters with different sequence windows");
164 
165  if ((m_value <= m_circularRegion && val.m_value <= m_circularRegion) ||
167  {
168  // They are desynchronized - comparison is impossible.
169  T absDiff = AbsoluteMagnitudeOfDifference(val);
170  if (absDiff > m_sequenceWindow)
171  {
172  return false;
173  }
174  }
175  return true;
176  }
177 
183  bool IsInit() const
184  {
186  {
187  return true;
188  }
189  return false;
190  }
191 
198  friend bool operator==(const LollipopCounter& lhs, const LollipopCounter& rhs)
199  {
201  "Can not compare two Lollipop Counters with different sequence windows");
202 
203  if (lhs.m_value == rhs.m_value)
204  {
205  return true;
206  }
207  return false;
208  }
209 
216  friend bool operator>(const LollipopCounter& lhs, const LollipopCounter& rhs)
217  {
219  "Can not compare two Lollipop Counters with different sequence windows");
220 
221  if (lhs.m_value == rhs.m_value)
222  {
223  return false;
224  }
225 
226  if ((lhs.m_value <= m_circularRegion && rhs.m_value <= m_circularRegion) ||
228  {
229  // both counters are in the same region
230 
231  T absDiff = lhs.AbsoluteMagnitudeOfDifference(rhs);
232  if (absDiff > lhs.m_sequenceWindow)
233  {
234  // They are desynchronized - comparison is impossible.
235  // return false because we can not return anything else.
236  return false;
237  }
238 
239  // They are synchronized - comparison according to RFC1982.
240  T serialRegion = ((m_circularRegion >> 1) + 1);
241  return (((lhs.m_value < rhs.m_value) && ((rhs.m_value - lhs.m_value) > serialRegion)) ||
242  ((lhs.m_value > rhs.m_value) && ((lhs.m_value - rhs.m_value) < serialRegion)));
243  }
244 
245  // One counter is in the "high" region and the other is in the in the "lower" region
246  bool lhsIsHigher;
247  T difference;
248 
249  if (lhs.m_value > m_circularRegion && rhs.m_value <= m_circularRegion)
250  {
251  lhsIsHigher = true;
252  // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
253  difference = lhs.m_value - rhs.m_value;
254  }
255  else
256  {
257  lhsIsHigher = false;
258  // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
259  difference = rhs.m_value - lhs.m_value;
260  }
261 
262  T distance = (m_maxValue - difference) +
263  1; // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
264  if (distance > lhs.m_sequenceWindow)
265  {
266  if (lhsIsHigher)
267  {
268  return true;
269  }
270  else
271  {
272  return false;
273  }
274  }
275  else
276  {
277  if (lhsIsHigher)
278  {
279  return false;
280  }
281  else
282  {
283  return true;
284  }
285  }
286 
287  // this should never be reached.
288  return false;
289  }
290 
297  friend bool operator<(const LollipopCounter& lhs, const LollipopCounter& rhs)
298  {
299  if (!lhs.IsComparable(rhs))
300  {
301  return false;
302  }
303 
304  if (lhs > rhs)
305  {
306  return false;
307  }
308  else if (lhs == rhs)
309  {
310  return false;
311  }
312 
313  return true;
314  }
315 
321  friend LollipopCounter operator++(LollipopCounter& val) // prefix ++
322  {
323  val.m_value++;
324 
325  if (val.m_value == val.m_circularRegion + 1)
326  {
327  val.m_value = 0;
328  }
329 
330  return val;
331  }
332 
339  friend LollipopCounter operator++(LollipopCounter& val, int noop) // postfix ++
340  {
341  LollipopCounter ans = val;
342  ++(val); // or just call operator++()
343  return ans;
344  }
345 
351  T GetValue() const
352  {
353  return m_value;
354  }
355 
363  friend std::ostream& operator<<(std::ostream& os, const LollipopCounter& counter)
364  {
365  os << +counter.m_value;
366  return os;
367  }
368 
369  private:
382  {
383  // useless because it is computed always on counters on their respective regions.
384  // Left (commented) for debugging purposes in case there is a code change.
385  // NS_ASSERT_MSG ((m_value <= m_circularRegion && val.m_value <= m_circularRegion) ||
386  // (m_value > m_circularRegion && val.m_value > m_circularRegion),
387  // "Absolute Magnitude Of Difference can be computed only on two values in
388  // the circular region " << +m_value << " - " << +val.m_value);
389 
390  T absDiffDirect = std::max(m_value, val.m_value) - std::min(m_value, val.m_value);
391  T absDiffWrapped = (std::min(m_value, val.m_value) + m_circularRegion + 1) -
392  std::max(m_value, val.m_value);
393  T absDiff = std::min(absDiffDirect, absDiffWrapped);
394  return absDiff;
395  }
396 
399  static constexpr T m_maxValue =
401  static constexpr T m_circularRegion = m_maxValue >> 1;
402 };
403 
414 
415 } /* namespace ns3 */
416 
417 #endif /* LOLLIPOP_COUNTER_H */
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Template class implementing a Lollipop counter as defined in RFC 8505, RFC 6550, and [Perlman83].
static constexpr T m_maxValue
Maximum value of the counter.
friend std::ostream & operator<<(std::ostream &os, const LollipopCounter &counter)
Output streamer for LollipopCounter.
friend LollipopCounter operator++(LollipopCounter &val, int noop)
Postfix increment operator.
T AbsoluteMagnitudeOfDifference(const LollipopCounter &val) const
Compute the Absolute Magnitude Of Difference between two counters.
T m_value
Value of the Lollipop Counter.
LollipopCounter()
Builds a Lollipop counter with a default initial value.
friend LollipopCounter operator++(LollipopCounter &val)
Prefix increment operator.
T GetValue() const
Get the counter value.
T m_sequenceWindow
Sequence window used for comparing two counters.
friend bool operator<(const LollipopCounter &lhs, const LollipopCounter &rhs)
Arithmetic operator less-than.
bool IsInit() const
Checks if a counter is in its starting region.
LollipopCounter & operator=(const LollipopCounter &o)
Assignment.
static constexpr T m_circularRegion
Circular region of the counter.
LollipopCounter(T val)
Builds a Lollipop counter with a specific initial value.
friend bool operator==(const LollipopCounter &lhs, const LollipopCounter &rhs)
Arithmetic operator equal-to.
void SetSequenceWindowSize(uint16_t numberOfBits)
Set the Sequence Window Size and resets the counter.
bool IsComparable(const LollipopCounter &val) const
Checks if the counter is comparable with another counter (i.e., not desynchronized).
friend bool operator>(const LollipopCounter &lhs, const LollipopCounter &rhs)
Arithmetic operator greater-than.
void Reset()
Resets the counter to its initial value.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
LollipopCounter< uint8_t > LollipopCounter8
8 bit Lollipop Counter.
LollipopCounter< uint16_t > LollipopCounter16
16 bit Lollipop Counter.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
value
Definition: second.py:41