class Estore::Package

Package is a length-prefixed binary frame transferred over TCP

Public Class Methods

decode(type, message) click to toggle source
# File lib/estore/package.rb, line 26
def self.decode(type, message)
  message.empty? ? nil : Estore.const_get(type).decode(message)
rescue => error
  puts 'Decoding error:'
  puts type: type, message: message
  raise error
end
encode(code, correlation_id, msg) click to toggle source
# File lib/estore/package.rb, line 4
def self.encode(code, correlation_id, msg)
  command = Beefcake::Buffer.new
  command << code
  command << 0x0 # non authenticated
  uuid_bytes = encode_uuid(correlation_id)
  uuid_bytes.each_byte { |b| command << b }
  msg.encode(command) if msg

  prefix_with_length(command)
end
encode_uuid(uuid) click to toggle source
# File lib/estore/package.rb, line 22
def self.encode_uuid(uuid)
  uuid.scan(/[0-9a-f]{4}/).map { |x| x.to_i(16) }.pack('n*')
end
parse_uuid(bytes) click to toggle source
# File lib/estore/package.rb, line 34
def self.parse_uuid(bytes)
  a, b, c, d, e, f, g, h =
    *bytes.unpack('n*').map { |n| n.to_s(16) }.map { |n| n.rjust(4, '0') }

  [a, b, '-', c, '-', d, '-', e, '-', f, g, h].join('')
end
prefix_with_length(command) click to toggle source
# File lib/estore/package.rb, line 15
def self.prefix_with_length(command)
  package = Beefcake::Buffer.new
  package.append_fixed32(command.length)
  package << command
  package
end