-
Notifications
You must be signed in to change notification settings - Fork 0
seqnum
PTC Wiki ▸ Code overview ▸ seqnum
This module implements sequence numbers (class SequenceNumber). They are used inside every packet and inside the control block for representing sliding window variables bound to sequence numbers (such as SND_UNA or SND_NXT). Internally, this class works with modular arithmetic and overloads standard arithmetic operators in order to use these numbers in contexts where regular integers are expected:
>>> n = SequenceNumber(5, modulus=10)
>>> m = SequenceNumber(9, modulus=10)
>>> n + m
4
>>> n + 16
1
>>> n > 1
True
>>> n > 6
FalseThe class SequenceNumber also provides some class methods that perform range comparisons considering wrap-around. For example, when the upper limit of the send window exceeds the maximum value allowed, it will start again from zero. In consequence, the protocol must tolerate those scenarios when computing comparisons. Consider the method a_leq_b_leq_c. It takes three values, a, b and c, and computes the truth value of a <= b <= c:
>>> eight = SequenceNumber(8, modulus=10)
>>> five = SequenceNumber(5, modulus=10)
>>> three = SequenceNumber(3, modulus=10)
>>> # Regular case
>>> SequenceNumber.a_leq_b_leq_c(three, five, eight)
True
>>> # c exceeded the upper limit
>>> SequenceNumber.a_leq_b_leq_c(five, eight, three)
True
>>> # b and c exceeded the upper limit and b < c
>>> SequenceNumber.a_leq_b_leq_c(eight, three, five)
True
>>> # b and c exceeded the upper limit and b > c
>>> SequenceNumber.a_leq_b_leq_c(eight, five, three)
False
>>> # b > max(a, c)
>>> SequenceNumber.a_leq_b_leq_c(three, eight, five)
False
>>> # b < min(a, c)
>>> SequenceNumber.a_leq_b_leq_c(five, three, eight)
False