Class: Estore::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/estore/package.rb

Overview

Package is a length-prefixed binary frame transferred over TCP

Class Method Summary collapse

Class Method Details

.encode(code, correlation_id, msg) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# 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) ⇒ Object



22
23
24
# 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) ⇒ Object



26
27
28
29
30
31
# File 'lib/estore/package.rb', line 26

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) ⇒ Object



15
16
17
18
19
20
# 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