| 1 |
|
---|
| 2 | #include "SequenceNumber.h"
|
---|
| 3 | #include <ctime>
|
---|
| 4 | #include <limits>
|
---|
| 5 |
|
---|
| 6 | namespace ariba {
|
---|
| 7 | namespace overlay {
|
---|
| 8 |
|
---|
| 9 | /** static initializers **/
|
---|
| 10 | // DISABLED_SEQNUM const
|
---|
| 11 | const SequenceNumber SequenceNumber::DISABLED;
|
---|
| 12 |
|
---|
| 13 | // seed the RNG
|
---|
| 14 | boost::mt19937 SequenceNumber::rng_32bit(std::time(NULL));
|
---|
| 15 |
|
---|
| 16 | boost::uniform_int<uint32_t> SequenceNumber::seqnum_distribution_32bit(
|
---|
| 17 | MIN_SEQ_NUM, std::numeric_limits<uint32_t>::max());
|
---|
| 18 |
|
---|
| 19 | boost::uniform_int<uint32_t> SequenceNumber::distribution_full32bit(
|
---|
| 20 | 0, std::numeric_limits<uint32_t>::max());
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | /** class implementation **/
|
---|
| 25 | SequenceNumber::SequenceNumber():
|
---|
| 26 | seqnum_32(SEQ_NUM_DISABLED),
|
---|
| 27 | seqnum_64(SEQ_NUM_DISABLED)
|
---|
| 28 | {
|
---|
| 29 | }
|
---|
| 30 | SequenceNumber::SequenceNumber(uint32_t seqnum):
|
---|
| 31 | seqnum_32(seqnum),
|
---|
| 32 | seqnum_64(SEQ_NUM_DISABLED)
|
---|
| 33 | {
|
---|
| 34 | }
|
---|
| 35 | SequenceNumber::SequenceNumber(uint64_t seqnum):
|
---|
| 36 | seqnum_32(SEQ_NUM_DISABLED),
|
---|
| 37 | seqnum_64(seqnum)
|
---|
| 38 | {
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | SequenceNumber SequenceNumber::createRandomSeqNum_Short()
|
---|
| 43 | {
|
---|
| 44 | uint32_t num = seqnum_distribution_32bit(rng_32bit);
|
---|
| 45 | return SequenceNumber(num);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | SequenceNumber SequenceNumber::createRandomSeqNum_Long()
|
---|
| 49 | {
|
---|
| 50 | uint64_t num = distribution_full32bit(rng_32bit);
|
---|
| 51 | num << 32;
|
---|
| 52 | num += seqnum_distribution_32bit(rng_32bit);
|
---|
| 53 |
|
---|
| 54 | return SequenceNumber(num);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | bool SequenceNumber::isShortSeqNum() const
|
---|
| 58 | {
|
---|
| 59 | return seqnum_32 >= MIN_SEQ_NUM && seqnum_64 == SEQ_NUM_DISABLED;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | bool SequenceNumber::isLongSeqNum() const
|
---|
| 63 | {
|
---|
| 64 | return seqnum_32 == SEQ_NUM_DISABLED && seqnum_64 >= MIN_SEQ_NUM;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | bool SequenceNumber::isDisabled() const
|
---|
| 68 | {
|
---|
| 69 | return seqnum_32 == SEQ_NUM_DISABLED && seqnum_64 == SEQ_NUM_DISABLED;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | bool SequenceNumber::isValid() const
|
---|
| 73 | {
|
---|
| 74 | return isShortSeqNum() || isLongSeqNum();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void SequenceNumber::increment()
|
---|
| 78 | {
|
---|
| 79 | // BRANCH: short seqnum
|
---|
| 80 | if ( isShortSeqNum() )
|
---|
| 81 | {
|
---|
| 82 | seqnum_32++;
|
---|
| 83 |
|
---|
| 84 | // wrap overflow
|
---|
| 85 | if ( seqnum_32 < MIN_SEQ_NUM )
|
---|
| 86 | {
|
---|
| 87 | seqnum_32 = MIN_SEQ_NUM;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // BRANCH: long seqnum
|
---|
| 92 | else if ( isLongSeqNum() )
|
---|
| 93 | {
|
---|
| 94 | seqnum_64++;
|
---|
| 95 |
|
---|
| 96 | // wrap overflow
|
---|
| 97 | if ( seqnum_64 < MIN_SEQ_NUM )
|
---|
| 98 | {
|
---|
| 99 | seqnum_64 = MIN_SEQ_NUM;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | bool SequenceNumber::operator==(const SequenceNumber& rhs) const
|
---|
| 105 | {
|
---|
| 106 | return seqnum_32 + seqnum_64 == rhs.seqnum_32 + rhs.seqnum_64;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | bool SequenceNumber::operator<(const SequenceNumber& rhs) const
|
---|
| 110 | {
|
---|
| 111 | return seqnum_32 + seqnum_64 < rhs.seqnum_32 + rhs.seqnum_64;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | bool SequenceNumber::isSuccessor(const SequenceNumber& rhs)
|
---|
| 115 | {
|
---|
| 116 | // TODO implement
|
---|
| 117 |
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | bool SequenceNumber::isPredecessor(const SequenceNumber& rhs)
|
---|
| 122 | {
|
---|
| 123 | // TODO implement
|
---|
| 124 |
|
---|
| 125 | return false;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | uint32_t SequenceNumber::getShortSeqNum() const
|
---|
| 129 | {
|
---|
| 130 | return seqnum_32;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | uint64_t SequenceNumber::getLongSeqNum() const
|
---|
| 134 | {
|
---|
| 135 | return seqnum_64;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | std::ostream& operator<<(std::ostream& stream, const SequenceNumber& rhs)
|
---|
| 140 | {
|
---|
| 141 | if ( rhs.isDisabled() )
|
---|
| 142 | {
|
---|
| 143 | return stream << "DISABLED";
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | else if ( ! rhs.isValid() )
|
---|
| 147 | {
|
---|
| 148 | return stream << "INVALID";
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | else if ( rhs.isShortSeqNum() )
|
---|
| 152 | {
|
---|
| 153 | return stream << rhs.seqnum_32;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | else if ( rhs.isLongSeqNum() )
|
---|
| 157 | {
|
---|
| 158 | return stream << rhs.seqnum_64;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | else
|
---|
| 162 | {
|
---|
| 163 | return stream << "ERROR";
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | }} // [namespace ariba::overlay]
|
---|