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)


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

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.



15
16
17
# File 'lib/libvirt_async/stream_read.rb', line 15

def io
  @io
end

#stateObject (readonly)

Returns the value of attribute state.



15
16
17
# File 'lib/libvirt_async/stream_read.rb', line 15

def state
  @state
end

#streamObject (readonly)

Returns the value of attribute stream.



15
16
17
# File 'lib/libvirt_async/stream_read.rb', line 15

def stream
  @stream
end

Instance Method Details

#add_callback(block) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
# File 'lib/libvirt_async/stream_read.rb', line 38

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)


32
33
34
35
36
# File 'lib/libvirt_async/stream_read.rb', line 32

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

  run
end

#cancelObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libvirt_async/stream_read.rb', line 63

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



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

def inspect
  to_s
end

#runObject



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

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

  dbg { "#{to_s}#call event_add_callback calling" }
  @cb_opaque = stream.event_add_callback(
      Libvirt::Stream::EVENT_READABLE,
      -> (_stream, events, _opaque) { stream_callback(events) },
      self
  )
  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



78
79
80
# File 'lib/libvirt_async/stream_read.rb', line 78

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