Class: SPing::LastAcks
- Inherits:
-
Object
- Object
- SPing::LastAcks
- Defined in:
- lib/last_acks.rb
Overview
A kind of circular buffer, which consists of 32 acks at any given time. If no acks are available, empty ones are created. If a new ack is added (which would result in 33 acks), one is removed.
Instance Method Summary collapse
-
#acks ⇒ Array
Returns a copy of the last 32 acks.
-
#add_ack(ack) ⇒ Object
Adds a new ack and deletes the oldest one.
-
#initialize ⇒ LastAcks
constructor
Creates a new circular buffer for acks.
Constructor Details
#initialize ⇒ LastAcks
Creates a new circular buffer for acks
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/last_acks.rb', line 10 def initialize # Array in which the acks are stored. @acks = [] # Mutex, which ensures that the array is not accessed simultaneously. @acks_mutex = Mutex.new # Initiallize the circular buffer with 32 empty acks. zero_time = Time.at(0) 32.times do |index| @acks[index] = { 'R' => 0, 'U' => zero_time, 'X' => zero_time } end end |
Instance Method Details
#acks ⇒ Array
Returns a copy of the last 32 acks.
29 30 31 32 33 |
# File 'lib/last_acks.rb', line 29 def acks @acks_mutex.synchronize do return @acks.dup end end |
#add_ack(ack) ⇒ Object
Adds a new ack and deletes the oldest one
37 38 39 40 41 42 |
# File 'lib/last_acks.rb', line 37 def add_ack(ack) @acks_mutex.synchronize do @acks << ack @acks.shift end end |