Class: SPing::LastAcks

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeLastAcks

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

#acksArray

Returns a copy of the last 32 acks.

Returns:

  • (Array)

    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

Parameters:

  • ack (Hash)

    New ack to be added



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