Class: Legion::Gaia::SensoryBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/gaia/sensory_buffer.rb

Constant Summary collapse

MAX_BUFFER_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initializeSensoryBuffer

Returns a new instance of SensoryBuffer.



8
9
10
11
# File 'lib/legion/gaia/sensory_buffer.rb', line 8

def initialize
  @mutex = Mutex.new
  @buffer = []
end

Instance Method Details

#drainObject



20
21
22
23
24
25
26
# File 'lib/legion/gaia/sensory_buffer.rb', line 20

def drain
  @mutex.synchronize do
    signals = @buffer.dup
    @buffer.clear
    signals
  end
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/legion/gaia/sensory_buffer.rb', line 32

def empty?
  @mutex.synchronize { @buffer.empty? }
end

#push(signal) ⇒ Object



13
14
15
16
17
18
# File 'lib/legion/gaia/sensory_buffer.rb', line 13

def push(signal)
  @mutex.synchronize do
    @buffer.shift if @buffer.size >= MAX_BUFFER_SIZE
    @buffer << normalize_signal(signal)
  end
end

#sizeObject



28
29
30
# File 'lib/legion/gaia/sensory_buffer.rb', line 28

def size
  @mutex.synchronize { @buffer.size }
end