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
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
#io ⇒ Object (readonly)
Returns the value of attribute io.
15 16 17 |
# File 'lib/libvirt_async/stream_read.rb', line 15 def io @io end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
15 16 17 |
# File 'lib/libvirt_async/stream_read.rb', line 15 def state @state end |
#stream ⇒ Object (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
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
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 |
#cancel ⇒ Object
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.}\n#{e.backtrace.join("\n")}" } @stream = nil ensure @cb_opaque = nil end |
#inspect ⇒ Object
82 83 84 |
# File 'lib/libvirt_async/stream_read.rb', line 82 def inspect to_s end |
#run ⇒ Object
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.}\n#{e.backtrace.join("\n")}" } @state = STATE_FAILED stream&.finish rescue nil on_error(e) @cb_opaque = nil end |
#to_s ⇒ Object
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 |