Class: MTP::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/mtp/container.rb

Direct Known Subclasses

Data, Event, ParametersContainer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



23
24
25
# File 'lib/mtp/container.rb', line 23

def code
  @code
end

#lengthObject

Returns the value of attribute length.



23
24
25
# File 'lib/mtp/container.rb', line 23

def length
  @length
end

#length_readObject

Returns the value of attribute length_read.



3
4
5
# File 'lib/mtp/container.rb', line 3

def length_read
  @length_read
end

#payloadObject

Returns the value of attribute payload.



23
24
25
# File 'lib/mtp/container.rb', line 23

def payload
  @payload
end

#transaction_idObject

Returns the value of attribute transaction_id.



23
24
25
# File 'lib/mtp/container.rb', line 23

def transaction_id
  @transaction_id
end

Class Method Details

.parse(str) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mtp/container.rb', line 4

def self.parse(str)
  length, type, code, transaction_id, payload = str.unpack_without_mtp("ISSIa*")
  container = case type
              when 1
                Request.new
              when 2 
                Data.new
              when 3
                Response.new
              when 4
                Event.new
              else
                raise MTPError.new("unable to parse packet: #{str.inspect}")
              end
  container.length, container.code, container.transaction_id, container.payload = 
    length, code, transaction_id, payload
  container
end

Instance Method Details

#packObject



33
34
35
36
# File 'lib/mtp/container.rb', line 33

def pack
  blob = [ type, code, transaction_id ].pack_without_mtp("SSI") + pack_payload
  return [ blob.length + 4 ].pack_without_mtp("I") + blob
end

#pack_headerObject



38
39
40
41
# File 'lib/mtp/container.rb', line 38

def pack_header
  blob = [ type, code, transaction_id ].pack_without_mtp("SSI") + pack_payload
  return [ blob.length + 4, type, code, transaction_id ].pack_without_mtp("ISSI") 
end

#pack_payloadObject



43
44
45
# File 'lib/mtp/container.rb', line 43

def pack_payload
  @payload
end

#to_sObject



47
48
49
# File 'lib/mtp/container.rb', line 47

def to_s
  "#{self.class} @code=#{code.inspect}, @transaction_id=#{transaction_id}"
end

#typeObject



29
30
31
# File 'lib/mtp/container.rb', line 29

def type
  0
end