libvirt_ffi 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf7335e98f7be6c9145bad5d2fdc6322b9dda60f31ddb92afd2ce76ebfd95d34
4
- data.tar.gz: 2c8fe7a75a8778709860128b466faa04d511ed1eb6eec0913a15bee9202a1d67
3
+ metadata.gz: 43c4cf7b7fa510b96605a9f05e8f04680280459d48572085740a71f145ce1442
4
+ data.tar.gz: a3ca5f72fdeb7de0cc95b440f58c5b894970add2cb746c12e3191d12269dcc42
5
5
  SHA512:
6
- metadata.gz: 756e46ad598baa4996961a191398b924059333ea248ae4df26c950a340a8a683e44a3bef80995f07b1d2968e56be3a27965c882dafc891f879114c703ab1cd4c
7
- data.tar.gz: eb82fede907f3c40474a894d428da233058ef502e6f0fe891c135c1733fe3bcb02f54df0951188bd0a8c779e1800b309ecc4b985d6ca8e504adbeff13f8143f3
6
+ metadata.gz: f1b7448f8ebd67349c59a365d1b30072bf99d5f78ffc1b07c87f99a312365f4d42474e62886c71e797fd0deab4c6f726af6440d6c4bb350fb8a65ee751b68ed5
7
+ data.tar.gz: 24056f945020275a102517bd6f4fba560be15c0f4fea7725b9c4b9b6032a1e94695b1520094c4027bf4ea0e34291e00148efefbb60d114640980e6c69e9f6cae
@@ -5,8 +5,9 @@ module Libvirt
5
5
  DOMAIN_EVENT_IDS = FFI::Domain.enum_type(:event_id).symbols.dup.freeze
6
6
  POOL_EVENT_IDS = FFI::Storage.enum_type(:event_id).symbols.dup.freeze
7
7
 
8
- DOMAIN_STORAGE = HostCallbackStorage.new
9
- POOL_STORAGE = HostCallbackStorage.new
8
+ DOMAIN_STORAGE = HostCallbackStorage.new(:domain_event)
9
+ POOL_STORAGE = HostCallbackStorage.new(:storage_pool_event)
10
+ CLOSE_STORAGE = HostCallbackStorage.new(:close)
10
11
 
11
12
  DOMAIN_EVENT_CALLBACKS = DOMAIN_EVENT_IDS.map do |event_id_sym|
12
13
  func = FFI::Domain.event_callback_for(event_id_sym) do |conn_ptr, dom_ptr, *args, op_ptr|
@@ -34,6 +35,13 @@ module Libvirt
34
35
  [event_id_sym, func]
35
36
  end.to_h.freeze
36
37
 
38
+ CLOSE_CALLBACK = FFI::Host.callback_function(:virConnectCloseFunc) do |conn_ptr, reason, op_ptr|
39
+ Util.log(:debug, 'CLOSE_CALLBACK') { "inside callback conn_ptr=#{conn_ptr}, reason=#{reason}, op_ptr=#{op_ptr}" }
40
+ connection = Connection.load_ref(conn_ptr)
41
+ block, opaque = CLOSE_STORAGE.retrieve_from_pointer(op_ptr)
42
+ block.call(connection, reason, opaque)
43
+ end
44
+
37
45
  def self.load_ref(conn_ptr)
38
46
  ref_result = FFI::Host.virConnectRef(conn_ptr)
39
47
  raise Errors::LibError, "Couldn't retrieve connection reference" if ref_result.negative?
@@ -154,23 +162,27 @@ module Libvirt
154
162
 
155
163
  def register_close_callback(opaque = nil, &block)
156
164
  dbg { "#register_close_callback opaque=#{opaque}" }
157
- raise ArgumentError, 'close function already registered' if @close_data
158
-
159
- @close_data = { opaque: opaque, block: block }
160
- @close_cb = FFI::Host.callback_function(:virConnectCloseFunc) do |_conn, reason, _op|
161
- dbg { "CONNECTION CLOSED @conn_ptr=#{@conn_ptr} reason=#{reason}" }
162
- block = @close_data[:block]
163
- opaque = @close_data[:opaque]
164
- # we clear @closed_data here, because connection can be opened again and
165
- # add new close callback inside `block`.
166
- @close_data = nil
167
- block.call(self, reason, opaque)
168
- end
169
- @close_free_func = FFI::Common.free_function do
170
- dbg { "CONNECTION CLOSED FREE FUNC @conn_ptr=#{@conn_ptr}" }
171
- @close_cb = @close_free_func = @close_data = nil
165
+
166
+ cb_data, cb_data_free_func = CLOSE_STORAGE.allocate_struct
167
+ result = FFI::Host.virConnectRegisterCloseCallback(
168
+ @conn_ptr,
169
+ CLOSE_CALLBACK,
170
+ cb_data.pointer,
171
+ cb_data_free_func
172
+ )
173
+ if result.negative?
174
+ cb_data.pointer.free
175
+ raise Errors::LibError, "Couldn't register connection close callback"
172
176
  end
173
- FFI::Host.virConnectRegisterCloseCallback(@conn_ptr, @close_cb, nil, @close_free_func)
177
+
178
+ CLOSE_STORAGE.store_struct(
179
+ cb_data,
180
+ connection_pointer: @conn_ptr,
181
+ callback_id: result,
182
+ cb: block,
183
+ opaque: opaque
184
+ )
185
+ result
174
186
  end
175
187
 
176
188
  # @yield conn, dom, *args
@@ -249,6 +261,28 @@ module Libvirt
249
261
  result
250
262
  end
251
263
 
264
+ def deregister_storage_pool_event_callback(callback_id)
265
+ dbg { "#deregister_storage_pool_event_callback callback_id=#{callback_id}" }
266
+
267
+ result = FFI::Storage.virConnectStoragePoolEventDeregisterAny(@conn_ptr, callback_id)
268
+ raise Errors::LibError, "Couldn't deregister storage pool event callback" if result.negative?
269
+
270
+ # virConnectStoragePoolEventDeregisterAny will call free func
271
+ # So we don't need to remove object from POOL_STORAGE here.
272
+ true
273
+ end
274
+
275
+ def deregister_close_callback
276
+ dbg { '#deregister_close_callback' }
277
+
278
+ result = FFI::Host.virConnectUnregisterCloseCallback(@conn_ptr, CLOSE_CALLBACK)
279
+ raise Errors::LibError, "Couldn't deregister close callback" if result.negative?
280
+
281
+ # virConnectUnregisterCloseCallback will call free func
282
+ # So we don't need to remove object from CLOSE_STORAGE here.
283
+ true
284
+ end
285
+
252
286
  def lib_version
253
287
  version_ptr = ::FFI::MemoryPointer.new(:ulong)
254
288
  result = FFI::Host.virConnectGetLibVersion(@conn_ptr, version_ptr)
@@ -7,7 +7,10 @@ module Libvirt
7
7
  :callback_id, :int
8
8
  end
9
9
 
10
- def initialize
10
+ attr_reader :name
11
+
12
+ def initialize(name)
13
+ @name = name
11
14
  @inner_storage = Hash.new { |h, key| h[key] = {} }
12
15
  end
13
16
 
@@ -20,7 +23,7 @@ module Libvirt
20
23
  cb_data_ptr = ::FFI::MemoryPointer.new(:char, CallbackDataStruct.size, false)
21
24
  cb_data = CallbackDataStruct.new(cb_data_ptr)
22
25
  cb_data_free_func = FFI::Common.free_function do |pointer|
23
- dbg { 'Libvirt::HostCallbackStorage cb_data_free_func triggered' }
26
+ dbg { "cb_data_free_func triggered pointer=#{pointer}" }
24
27
  remove_struct(pointer)
25
28
  end
26
29
  [cb_data, cb_data_free_func]
@@ -35,7 +38,7 @@ module Libvirt
35
38
  end
36
39
 
37
40
  def remove_struct(pointer)
38
- dbg { "#remove_struct pointer=#{pointer}" }
41
+ dbg { "#remove_struct pointer=#{pointer}," }
39
42
 
40
43
  cb_data_struct = CallbackDataStruct.new(pointer)
41
44
  connection_pointer = cb_data_struct[:connection_pointer]
@@ -63,7 +66,7 @@ module Libvirt
63
66
  private
64
67
 
65
68
  def dbg(&block)
66
- Util.log(:debug, 'Libvirt::HostCallbackStorage', &block)
69
+ Util.log(:debug, "Libvirt::HostCallbackStorage(#{name})", &block)
67
70
  end
68
71
  end
69
72
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libvirt
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libvirt_ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich