Class: LibvirtAsync::StreamRead

Inherits:
Object
  • Object
show all
Includes:
WithDbg
Defined in:
lib/libvirt_async/stream_read.rb

Defined Under Namespace

Classes: RecvError

Constant Summary collapse

STATE_COMPLETED =

StreamRead allows to work with stream in non-block read mode.

'completed'.freeze
STATE_CANCELLED =
'cancelled'.freeze
STATE_FAILED =
'failed'.freeze
STATE_PENDING =
'pending'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, io) ⇒ LibvirtAsync::Stream

Parameters:

  • connection (Libvirt::Connection)
  • io (IO)


22
23
24
25
26
27
28
# File 'lib/libvirt_async/stream_read.rb', line 22

def initialize(connection, io)
  @connection = connection
  @io = io
  @callback = nil
  @state = STATE_PENDING
  @stream = @connection.stream(Libvirt::Stream::NONBLOCK)
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



17
18
19
# File 'lib/libvirt_async/stream_read.rb', line 17

def io
  @io
end

#stateObject (readonly)

Returns the value of attribute state.



17
18
19
# File 'lib/libvirt_async/stream_read.rb', line 17

def state
  @state
end

#streamObject (readonly)

Returns the value of attribute stream.



17
18
19
# File 'lib/libvirt_async/stream_read.rb', line 17

def stream
  @stream
end

Instance Method Details

#add_callback(block) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/libvirt_async/stream_read.rb', line 40

def add_callback(block)
  raise ArgumentError, 'block must be a Proc' unless block.is_a?(Proc)
  @callback = block
end

#call {|success, reason, io| ... } ⇒ Object

Yields:

  • asynchronously on complete or error

Yield Parameters:

  • success (Boolean)
  • reason (String, NilClass)
  • io (IO)


34
35
36
37
38
# File 'lib/libvirt_async/stream_read.rb', line 34

def call(&block)
  add_callback(block) if block_given?

  run
end

#cancelObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/libvirt_async/stream_read.rb', line 66

def cancel
  dbg { "#{to_s}#cancel" }
  return if stream.nil?

  @state = STATE_CANCELLED
  stream.event_remove_callback
  stream.finish
  @stream = nil
rescue Libvirt::Error => e
  dbg { "#{to_s}#cancel error occurred\n<#{e.class}>: #{e.message}\n#{e.backtrace.join("\n")}" }
  @stream = nil
ensure
  @cb_opaque = nil
end

#inspectObject



85
86
87
# File 'lib/libvirt_async/stream_read.rb', line 85

def inspect
  to_s
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/libvirt_async/stream_read.rb', line 45

def run
  raise ArgumentError, 'block must be given' if @callback.nil?

  dbg { "#{to_s}#call event_add_callback calling" }
  proc = -> (_stream, events, _opaque) { stream_callback(events) }
  @cb_opaque = stream.event_add_callback(
      Libvirt::Stream::EVENT_READABLE,
      self,
      &proc
  )
  dbg { "#{to_s}#call event_add_callback called" }

  nil
rescue Libvirt::Error => e
  dbg { "#{to_s}#call error occurred\n<#{e.class}>: #{e.message}\n#{e.backtrace.join("\n")}" }
  @state = STATE_FAILED
  stream&.finish rescue nil
  on_error(e)
  @cb_opaque = nil
end

#to_sObject



81
82
83
# File 'lib/libvirt_async/stream_read.rb', line 81

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} @state=#{@state}>"
end