Class: Tamashii::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/tamashii/packet.rb

Constant Summary collapse

DEVICE_TYPE_AGENT =
0
DEVICE_TYPE_CHECKIN =
1
STRING_TRUE =
"0".freeze
STRING_FALSE =
"1".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, tag = 0, body = '') ⇒ Packet

Returns a new instance of Packet.



57
58
59
60
61
62
# File 'lib/tamashii/packet.rb', line 57

def initialize(type, tag = 0, body = '')
  @type = type
  @tag =  tag
  @body = body
  body_conversion
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/tamashii/packet.rb', line 13

def body
  @body
end

#tagObject (readonly)

Returns the value of attribute tag.



12
13
14
# File 'lib/tamashii/packet.rb', line 12

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/tamashii/packet.rb', line 11

def type
  @type
end

Class Method Details

.combine_integer(bytes) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tamashii/packet.rb', line 36

def self.combine_integer(bytes)
  n = 0
  bytes.each do |byte|
    n = n << 8 | byte
  end
  n
end

.dump(obj) ⇒ Object

1 byte: type 2 bytes: tag 2 bytes: size of body variable-length body, in string

Raises:

  • (TypeError)


20
21
22
23
24
25
# File 'lib/tamashii/packet.rb', line 20

def self.dump(obj)
  raise TypeError.new("Only Packet can be dump") unless obj.is_a?(self)
  raise TypeError.new("Body must be a string") unless obj.body.is_a?(String)
  header = [obj.type, *split_integer(obj.tag), *split_integer(obj.body.bytesize)]
  header + obj.body.unpack("C*")
end

.load(byte_array) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tamashii/packet.rb', line 44

def self.load(byte_array)
  header_bytes = byte_array[0..4] 
  body_bytes = byte_array[5..-1] || ""
  packed_data = if body_bytes.is_a? String
                  body_bytes
                else
                  body_bytes.pack("C*")
                end
  new(header_bytes[0], combine_integer(header_bytes[1..2]), packed_data)
rescue
  nil
end

.split_integer(n, byte_num = 2) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/tamashii/packet.rb', line 27

def self.split_integer(n, byte_num = 2)
  res = []
  byte_num.times do 
    res.unshift(n & 0xFF)
    n >>= 8
  end
  res
end

Instance Method Details

#action_codeObject



80
81
82
# File 'lib/tamashii/packet.rb', line 80

def action_code
  @type & 0x7
end

#body_conversionObject



64
65
66
67
68
69
70
# File 'lib/tamashii/packet.rb', line 64

def body_conversion
  if @body.is_a? TrueClass
    @body = STRING_TRUE.clone
  elsif @body.is_a? FalseClass
    @body = STRING_FALSE.clone
  end
end

#dumpObject



72
73
74
# File 'lib/tamashii/packet.rb', line 72

def dump
  self.class.dump(self)
end

#type_codeObject



76
77
78
# File 'lib/tamashii/packet.rb', line 76

def type_code
  @type & (0x7 << 3)
end