Class: MTP::Protocol::Transaction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ph) ⇒ Transaction

Returns a new instance of Transaction.



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

def initialize(ph)
  @ph = ph
  @io = io
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/mtp/protocol.rb', line 87

def method_missing(method, *args, &block)
  method = method.to_s
  if method.match(/^(.*)_with_io$/)
    method = $1
    @io = args.shift 
  end
  @request = Request.for(method.to_s.camelize, *args)
  execute(&block)
  self
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



31
32
33
# File 'lib/mtp/protocol.rb', line 31

def data
  @data
end

#ioObject

Returns the value of attribute io.



31
32
33
# File 'lib/mtp/protocol.rb', line 31

def io
  @io
end

#requestObject

Returns the value of attribute request.



31
32
33
# File 'lib/mtp/protocol.rb', line 31

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



32
33
34
# File 'lib/mtp/protocol.rb', line 32

def response
  @response
end

Instance Method Details

#execute(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/mtp/protocol.rb', line 70

def execute(&block)
  @ph.send(@request)
  send_data(&block) unless @data.nil?
  @response = @ph.receive
  if @response.is_a?(Data)
    read_data(&block) 
    @response = @ph.receive
  end
  self
end

#expect(*expects) ⇒ Object



81
82
83
84
85
# File 'lib/mtp/protocol.rb', line 81

def expect(*expects)
  unless expects.empty? or @response.one_of?(*expects)
    raise CommandError.new(@ph, @request, @response)
  end
end

#io?Boolean

Returns:

  • (Boolean)


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

def io?
  !@io.nil?
end

#read_data(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/mtp/protocol.rb', line 59

def read_data(&block)
  @data = @response
  if @ph.split_data_packets?
    if io?
      @data.payload = @ph.raw_read_io(@io, @data.length - 12, &block)
    else
      @data.payload = @ph.raw_read
    end
  end  
end

#send_data(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mtp/protocol.rb', line 42

def send_data(&block)
  @data.transaction_id = @request.transaction_id
  @data.code = @request.code
    
  if @ph.split_data_packets?
    Protocol.logger.debug(sprintf("=> %-8s: 0x%08x, 0x%04x - %s", "DATA", @data.transaction_id, @data.code, @data.code.name))
    @ph.raw_send(@data.pack_header)
    if io?
      @ph.raw_send_io(@io, @data.size, &block)
    else
      @ph.raw_send(@data.pack_payload, &block)
    end
  else
    @ph.send(@data) 
  end        
end