Class: Rapid::Services::ResponseBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/ury_rapid/services/response_buffer.rb

Overview

A simple buffer for binary playout system responses

The ResponseBuffer takes in requests for binary data, and fulfils them as soon as it has enough data in its buffer.

The ResponseBuffer is not thread-safe.

Direct Known Subclasses

Baps::Reader

Instance Method Summary collapse

Constructor Details

#initializeResponseBuffer

Creates a Reader


11
12
13
14
15
# File 'lib/ury_rapid/services/response_buffer.rb', line 11

def initialize
  @buffer = ''
  @requests = []
  @satisfying_requests = false
end

Instance Method Details

#add(data) ⇒ void Also known as: receive_data

This method returns an undefined value.

Adds more data to the internal buffer

Parameters:

  • data (String)

    Data bytes to add to the processing buffer.


66
67
68
69
# File 'lib/ury_rapid/services/response_buffer.rb', line 66

def add(data)
  @buffer << data
  try_satisfy_requests unless @satisfying_requests
end

#packed_request(num_bytes, format, front = false) {|unpacked| ... } ⇒ void

This method returns an undefined value.

Adds a request for packed data to the request queue

Examples:

Asking for a 32-bit network-endian integer.

@result = nil
packed_request(4, 'N')
reader.add([2001].pack('N'))
@result
#=> [2001]

Parameters:

  • num_bytes (Integer)

    The number of bytes to ask for.

  • format (String)

    The format string to use when unpacking.

Yield Parameters:

  • unpacked (Array)

    The unpacked data.


33
34
35
# File 'lib/ury_rapid/services/response_buffer.rb', line 33

def packed_request(num_bytes, format, front = false, &block)
  request(num_bytes, front) { |bytes| block.call(bytes.unpack(format)) }
end

#request(num_bytes, front = false) {|bytes| ... } ⇒ void

This method returns an undefined value.

Adds a request for a given number of bytes to the request queue

Examples:

Asking for 6 bytes.

@result = nil
reader.request(5) { |bytes| @result = bytes }
reader.add('abcdefghi')
@result
#=> 'abcde'

Parameters:

  • num_bytes (Integer)

    The number of bytes to ask for.

  • front (Boolean) (defaults to: false)

    If true, the request is added onto the front of the queue, rather than the back. This should be used for nested requests.

Yield Parameters:

  • bytes (String)

    The bytes received.


55
56
57
58
59
# File 'lib/ury_rapid/services/response_buffer.rb', line 55

def request(num_bytes, front = false, &block)
  @requests << [num_bytes, block] unless front
  @requests.unshift([num_bytes, block]) if front
  try_satisfy_requests unless @satisfying_requests
end