class Cql::Protocol::FrameDecoder

@private

Constants

NULL_FRAME

Public Class Methods

new(compressor=nil) click to toggle source
# File lib/cql/protocol/frame_decoder.rb, line 7
def initialize(compressor=nil)
  @compressor = compressor
end

Public Instance Methods

decode_frame(buffer, partial_frame=nil) click to toggle source
# File lib/cql/protocol/frame_decoder.rb, line 11
def decode_frame(buffer, partial_frame=nil)
  partial_frame ||= NULL_FRAME
  if partial_frame == NULL_FRAME
    buffer_length = buffer.length
    return NULL_FRAME if buffer_length < 8
    fields = buffer.read_int
    size = buffer.read_int
    if buffer_length - 8 >= size
      actual_decode(buffer, fields, size)
    else
      PartialFrame.new(fields, size)
    end
  elsif buffer.length >= partial_frame.size
    actual_decode(buffer, partial_frame.fields, partial_frame.size)
  else
    partial_frame
  end
end

Private Instance Methods

actual_decode(buffer, fields, size) click to toggle source
# File lib/cql/protocol/frame_decoder.rb, line 32
def actual_decode(buffer, fields, size)
  if (fields >> 24) & 0x80 == 0
    raise UnsupportedFrameTypeError, 'Request frames are not supported'
  end
  protocol_version = (fields >> 24) & 0x7f
  compression = (fields >> 16) & 0x01
  tracing = (fields >> 16) & 0x02
  stream_id = (fields >> 8) & 0xff
  stream_id = (stream_id & 0x7f) - (stream_id & 0x80)
  opcode = fields & 0xff
  if compression == 1
    buffer = decompress(buffer, size)
    size = buffer.size
  end
  if tracing == 2
    trace_id = buffer.read_uuid
    size -= 16
  else
    trace_id = nil
  end
  extra_length = buffer.length - size
  response = Response.decode(opcode, protocol_version, buffer, size, trace_id)
  if buffer.length > extra_length
    buffer.discard(buffer.length - extra_length)
  end
  CompleteFrame.new(stream_id, response)
end
decompress(buffer, size) click to toggle source
# File lib/cql/protocol/frame_decoder.rb, line 60
def decompress(buffer, size)
  if @compressor
    compressed_body = buffer.read(size)
    CqlByteBuffer.new(@compressor.decompress(compressed_body))
  else
    raise UnexpectedCompressionError, 'Compressed frame received, but no compressor configured'
  end
end