diff -r 1b8a736858cc src/common/sequence.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.cc Tue Jun 15 14:24:53 2010 -0400 @@ -0,0 +1,149 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include + +#include "ns3/assert.h" +#include "ns3/log.h" +#include "ns3/fatal-error.h" +#include "ns3/test.h" +#include "sequence.h" + +namespace ns3 { + +template +void +Sequence::Set (T * sequence, T value) +{ + *sequence = value; +} + +template +void +Sequence::Inc (T * sequence) +{ + *sequence = *sequence + 1; +} + +template +void +Sequence::Inc (T * sequence, T count) +{ + *sequence = *sequence + count; +} + +template +void +Sequence::Dec (T * sequence) +{ + *sequence = *sequence - 1; +} + +template +void +Sequence::Dec (T * sequence, T count) +{ + *sequence = *sequence - count; +} + +template +bool +Sequence::IsEqual (T sequence, T other) +{ + return sequence == other; +} + +// From RFC 3626: +// +// The sequence number S1 is said to be "greater than" the sequence +// number S2 if: +// +// S1 > S2 AND S1 - S2 <= MAXVALUE/2 OR +// +// S2 > S1 AND S2 - S1 > MAXVALUE/2 +template +bool +Sequence::IsGreater (T sequence, T other) +{ + static const T halfMaxValue = std::numeric_limits::max () / 2; + + return (((sequence > other) && (sequence - other) <= halfMaxValue) + || ((other > sequence) && (other - sequence) > halfMaxValue)); +} + +template +bool +Sequence::IsGreaterOrEqual (T sequence, T other) +{ + return (IsGreater (sequence, other) || IsEqual (sequence, other)); +} + +template +bool +Sequence::IsLess (T sequence, T other) +{ + return !(IsGreaterOrEqual (sequence, other)); +} + +template +bool +Sequence::IsLessOrEqual (T sequence, T other) +{ + return !(IsGreater (sequence, other)); +} + +//----------------------------------------------------------------------------- +// Unit tests +//----------------------------------------------------------------------------- +class SequenceTest: public TestCase { +public: + virtual bool DoRun (void); + SequenceTest (); +}; + + +SequenceTest::SequenceTest () + : TestCase ("Sequence Number") {} + +bool +SequenceTest::DoRun (void) +{ + /* Will actually write some tests once it is clear this is + headed in the right direction + */ + uint16_t num1 (60900); + NS_TEST_EXPECT_MSG_EQ (Sequence::IsEqual(num1, num1), true, "IsEqual"); + + return GetErrorStatus (); +} +//----------------------------------------------------------------------------- +class SequenceTestSuite : public TestSuite +{ +public: + SequenceTestSuite (); +}; + +SequenceTestSuite::SequenceTestSuite () + : TestSuite ("sequence-number", UNIT) +{ + AddTestCase (new SequenceTest); +} + +SequenceTestSuite g_sequenceTestSuite; + +} // namespace ns3 diff -r 1b8a736858cc src/common/sequence.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/common/sequence.h Tue Jun 15 14:24:53 2010 -0400 @@ -0,0 +1,114 @@ +/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef SEQUENCE_H +#define SEQUENCE_H + +namespace ns3 { + +/** + * \brief Generic sequence number class + * + * This generic sequence number class is templated to easily support + * 8, 16, 32, or 64 bit sequence numbers. The design of this class + * was chosen to facilitate tracing these sequence numbers. + */ + +class Sequence +{ +public: + /** + * \param sequence the sequence number to set + * \param value the value to set the sequence number + */ + template static void Set (T * sequence, T value); + + /** + * \param sequence the sequence number to increment + * + * Increment the sequence number by one. + */ + template static void Inc (T * sequence); + + /** + * \param sequence the sequence number to increment + * \param count the number to increment the sequence number + * + * Increment the sequence number by count. + */ + template static void Inc (T * sequence, T count); + + /** + * \param sequence the sequence number to decrement + * + * Decrement the sequence number by one. + */ + template static void Dec (T * sequence); + + /** + * \param sequence the sequence number to decrement + * \param count the number to decrement the sequence number + * + * Decrement sequence number by count. + */ + template static void Dec (T * sequence, T count); + + /** + * \return true if parameters equal, false otherwise. + * + * \param sequence the sequence number + * \param other the other sequence number for comparison + */ + template static bool IsEqual (T sequence, T other); + + /** + * \return true if first argument is greater than the second + * + * \param sequence the sequence number + * \param other the other sequence number for comparison + */ + template static bool IsGreater (T sequence, T other); + + /** + * \return true if first argument is greater than or equal to the second + * + * \param sequence the sequence number + * \param other the other sequence number for comparison + */ + template static bool IsGreaterOrEqual (T sequence, T other); + + /** + * \return true if first argument is less than the second + * + * \param sequence the sequence number + * \param other the other sequence number for comparison + */ + template static bool IsLess (T sequence, T other); + + /** + * \return true if first argument is less than or equal to the second + * + * \param sequence the sequence number + * \param other the other sequence number for comparison + */ + template static bool IsLessOrEqual (T sequence, T other); + +}; + +} // namespace ns3 + +#endif /* SEQUENCE_H */ diff -r 1b8a736858cc src/common/wscript --- a/src/common/wscript Mon Jun 14 17:35:42 2010 +0100 +++ b/src/common/wscript Tue Jun 15 14:24:53 2010 -0400 @@ -34,6 +34,7 @@ 'spectrum-type.cc', 'spectrum-propagation-loss-model.cc', 'friis-spectrum-propagation-loss.cc', + 'sequence.cc', ] headers = bld.new_task_gen('ns3header') @@ -67,4 +68,5 @@ 'spectrum-type.h', 'spectrum-propagation-loss-model.h', 'friis-spectrum-propagation-loss.h', + 'sequence.h', ]