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  {
185  return m_value > m_circularRegion;
186  }
187 
194  friend bool operator==(const LollipopCounter& lhs, const LollipopCounter& rhs)
195  {
197  "Can not compare two Lollipop Counters with different sequence windows");
198 
199  return lhs.m_value == rhs.m_value;
200  }
201 
208  friend bool operator>(const LollipopCounter& lhs, const LollipopCounter& rhs)
209  {
211  "Can not compare two Lollipop Counters with different sequence windows");
212 
213  if (lhs.m_value == rhs.m_value)
214  {
215  return false;
216  }
217 
218  if ((lhs.m_value <= m_circularRegion && rhs.m_value <= m_circularRegion) ||
220  {
221  // both counters are in the same region
222 
223  T absDiff = lhs.AbsoluteMagnitudeOfDifference(rhs);
224  if (absDiff > lhs.m_sequenceWindow)
225  {
226  // They are desynchronized - comparison is impossible.
227  // return false because we can not return anything else.
228  return false;
229  }
230 
231  // They are synchronized - comparison according to RFC1982.
232  T serialRegion = ((m_circularRegion >> 1) + 1);
233  return (((lhs.m_value < rhs.m_value) && ((rhs.m_value - lhs.m_value) > serialRegion)) ||
234  ((lhs.m_value > rhs.m_value) && ((lhs.m_value - rhs.m_value) < serialRegion)));
235  }
236 
237  // One counter is in the "high" region and the other is in the in the "lower" region
238  bool lhsIsHigher;
239  T difference;
240 
241  if (lhs.m_value > m_circularRegion && rhs.m_value <= m_circularRegion)
242  {
243  lhsIsHigher = true;
244  // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
245  difference = lhs.m_value - rhs.m_value;
246  }
247  else
248  {
249  lhsIsHigher = false;
250  // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
251  difference = rhs.m_value - lhs.m_value;
252  }
253 
254  T distance = (m_maxValue - difference) +
255  1; // this is guaranteed to be positive and between [1...m_lollipopMaxValue].
256  if (distance > lhs.m_sequenceWindow)
257  {
258  return lhsIsHigher;
259  }
260  else
261  {
262  return !lhsIsHigher;
263  }
264 
265  // this should never be reached.
266  return false;
267  }
268 
275  friend bool operator<(const LollipopCounter& lhs, const LollipopCounter& rhs)
276  {
277  if (!lhs.IsComparable(rhs))
278  {
279  return false;
280  }
281 
282  if (lhs > rhs)
283  {
284  return false;
285  }
286  else if (lhs == rhs)
287  {
288  return false;
289  }
290 
291  return true;
292  }
293 
299  friend LollipopCounter operator++(LollipopCounter& val) // prefix ++
300  {
301  val.m_value++;
302 
303  if (val.m_value == val.m_circularRegion + 1)
304  {
305  val.m_value = 0;
306  }
307 
308  return val;
309  }
310 
317  friend LollipopCounter operator++(LollipopCounter& val, int noop) // postfix ++
318  {
319  LollipopCounter ans = val;
320  ++(val); // or just call operator++()
321  return ans;
322  }
323 
329  T GetValue() const
330  {
331  return m_value;
332  }
333 
341  friend std::ostream& operator<<(std::ostream& os, const LollipopCounter& counter)
342  {
343  os << +counter.m_value;
344  return os;
345  }
346 
347  private:
360  {
361  // useless because it is computed always on counters on their respective regions.
362  // Left (commented) for debugging purposes in case there is a code change.
363  // NS_ASSERT_MSG ((m_value <= m_circularRegion && val.m_value <= m_circularRegion) ||
364  // (m_value > m_circularRegion && val.m_value > m_circularRegion),
365  // "Absolute Magnitude Of Difference can be computed only on two values in
366  // the circular region " << +m_value << " - " << +val.m_value);
367 
368  T absDiffDirect = std::max(m_value, val.m_value) - std::min(m_value, val.m_value);
369  T absDiffWrapped = (std::min(m_value, val.m_value) + m_circularRegion + 1) -
370  std::max(m_value, val.m_value);
371  T absDiff = std::min(absDiffDirect, absDiffWrapped);
372  return absDiff;
373  }
374 
377  static constexpr T m_maxValue =
379  static constexpr T m_circularRegion = m_maxValue >> 1;
380 };
381 
392 
393 } /* namespace ns3 */
394 
395 #endif /* LOLLIPOP_COUNTER_H */
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
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:48