Class: LibvirtAsync::StreamRead
- Inherits:
-
Object
- Object
- LibvirtAsync::StreamRead
- 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
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #add_callback(block) ⇒ Object
- #call {|success, reason, io| ... } ⇒ Object
- #cancel ⇒ Object
- #initialize(connection, io) ⇒ LibvirtAsync::Stream constructor
- #inspect ⇒ Object
- #run ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(connection, io) ⇒ LibvirtAsync::Stream
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
#io ⇒ Object (readonly)
Returns the value of attribute io.
17 18 19 |
# File 'lib/libvirt_async/stream_read.rb', line 17 def io @io end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
17 18 19 |
# File 'lib/libvirt_async/stream_read.rb', line 17 def state @state end |
#stream ⇒ Object (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
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
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 |
#cancel ⇒ Object
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.}\n#{e.backtrace.join("\n")}" } @stream = nil ensure @cb_opaque = nil end |
#inspect ⇒ Object
85 86 87 |
# File 'lib/libvirt_async/stream_read.rb', line 85 def inspect to_s end |
#run ⇒ Object
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.}\n#{e.backtrace.join("\n")}" } @state = STATE_FAILED stream&.finish rescue nil on_error(e) @cb_opaque = nil end |
#to_s ⇒ Object
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 |