Class: Libvirt::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/libvirt/stream.rb

Constant Summary collapse

NONBLOCK =
0x1
EVENT_READABLE =
0x1
EVENT_WRITABLE =
0x2

Instance Method Summary collapse

Constructor Details

#initialize(stream_ptr) ⇒ Stream

Returns a new instance of Stream.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/libvirt/stream.rb', line 9

def initialize(stream_ptr)
  @stream_ptr = stream_ptr
  @cb = nil
  @opaque = nil

  free = ->(obj_id) do
    Util.log(:debug) { "Finalize Libvirt::Stream 0x#{obj_id.to_s(16)} @stream_ptr=#{@stream_ptr}, @cb=#{@cb}, @opaque=#{@opaque}," }
    return unless @stream_ptr

    if @cb
      rcb_result = FFI::Stream.virStreamEventRemoveCallback(@stream_ptr)
      warn("Couldn't remove callback Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if rcb_result.negative?
      ab_result = FFI::Stream.virStreamAbort(@stream_ptr)
      warn("Couldn't abort Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if ab_result.negative?
    end
    fr_result = FFI::Stream.virStreamFree(@stream_ptr)
    warn("Couldn't free Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if fr_result.negative?
  end
  ObjectSpace.define_finalizer(self, free)
end

Instance Method Details

#abort_streamObject

Raises:



84
85
86
87
88
89
90
# File 'lib/libvirt/stream.rb', line 84

def abort_stream
  result = FFI::Stream.virStreamAbort(@stream_ptr)
  raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?

  @cb = nil
  @opaque = nil
end

#event_add_callback(events, opaque) {|Stream| ... } ⇒ Object

Parameters:

  • events (Integer)

    bit OR of EVENT_READABLE, EVENT_READABLE

  • opaque (Object)

Yields:

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/libvirt/stream.rb', line 37

def event_add_callback(events, opaque, &block)
  dbg { "#event_add_callback events=#{events}, opaque=#{opaque}" }

  raise Errors::LibError, 'callback already added' unless @cb.nil?

  @opaque = opaque
  @cb = FFI::Stream.callback_function(:virStreamEventCallback) do |_stream_ptr, evs, _op|
    # stream = Stream.new(stream_ptr)
    block.call(self, evs, @opaque)
  end

  result = FFI::Stream.virStreamEventAddCallback(@stream_ptr, events, @cb, nil, nil)
  raise Errors::LibError, "Couldn't add stream event callback" if result.negative?

  true
end

#event_remove_callbackObject

Raises:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/libvirt/stream.rb', line 64

def event_remove_callback
  dbg { '#event_remove_callback' }

  result = FFI::Stream.virStreamEventRemoveCallback(@stream_ptr)
  raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?

  opaque = @opaque
  @cb = nil
  @opaque = nil
  opaque
end

#event_update_callback(events) ⇒ Object

Parameters:

  • events (Integer)

    bit OR of EVENT_READABLE, EVENT_READABLE

Raises:



55
56
57
58
59
60
61
62
# File 'lib/libvirt/stream.rb', line 55

def event_update_callback(events)
  dbg { "#event_update_callback events=#{events}" }

  result = FFI::Stream.virStreamEventUpdateCallback(@stream_ptr, events)
  raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?

  true
end

#finishObject

Raises:



76
77
78
79
80
81
82
# File 'lib/libvirt/stream.rb', line 76

def finish
  result = FFI::Stream.virStreamFinish(@stream_ptr)
  raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?

  @cb = nil
  @opaque = nil
end

#free_streamObject

Raises:



92
93
94
95
96
97
98
99
# File 'lib/libvirt/stream.rb', line 92

def free_stream
  result = FFI::Stream.virStreamFree(@stream_ptr)
  raise Errors::LibError, "Couldn't free stream event callback" if result.negative?

  @cb = nil
  @opaque = nil
  @stream_ptr = nil
end

#recv(bytes) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/libvirt/stream.rb', line 101

def recv(bytes)
  buffer = ::FFI::MemoryPointer.new(:char, bytes)
  result = FFI::Stream.virStreamRecv(@stream_ptr, buffer, bytes)
  if result == -1
    abort_stream
    [-1, nil]
  elsif result.zero?
    [0, nil]
  elsif result == -2
    [-2, nil]
  elsif result.positive?
    [result, buffer.read_bytes(result)]
  else
    raise Errors::LibError, "Invalid response from virStreamRecv #{result.inspect}"
  end
end

#to_ptrObject



30
31
32
# File 'lib/libvirt/stream.rb', line 30

def to_ptr
  @stream_ptr
end