Class: Libvirt::HostCallbackStorage

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

Defined Under Namespace

Classes: CallbackDataStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HostCallbackStorage

Returns a new instance of HostCallbackStorage.



12
13
14
15
# File 'lib/libvirt/host_callback_storage.rb', line 12

def initialize(name)
  @name = name
  @inner_storage = Hash.new { |h, key| h[key] = {} }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/libvirt/host_callback_storage.rb', line 10

def name
  @name
end

Instance Method Details

#allocate_structArray<2>

Returns cb_data [Libvirt::HostCallbackStorage::CallbackDataStruct], cb_data_free_func [FFI::Function].

Returns:

  • (Array<2>)

    cb_data [Libvirt::HostCallbackStorage::CallbackDataStruct], cb_data_free_func [FFI::Function]



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/libvirt/host_callback_storage.rb', line 20

def allocate_struct
  dbg { '#allocate_struct' }

  cb_data_ptr = ::FFI::MemoryPointer.new(:char, CallbackDataStruct.size, false)
  cb_data = CallbackDataStruct.new(cb_data_ptr)
  cb_data_free_func = FFI::Common.free_function do |pointer|
    dbg { "cb_data_free_func triggered pointer=#{pointer}" }
    remove_struct(pointer)
  end
  [cb_data, cb_data_free_func]
end

#remove_struct(pointer) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/libvirt/host_callback_storage.rb', line 48

def remove_struct(pointer)
  dbg { "#remove_struct pointer=#{pointer}," }

  cb_data_struct = CallbackDataStruct.new(pointer)
  connection_pointer = cb_data_struct[:connection_pointer]
  callback_id = cb_data_struct[:callback_id]
  dbg { "#remove_struct pointer=#{pointer}, connection_pointer=#{connection_pointer}, callback_id=#{callback_id}," }

  cb_data = @inner_storage[connection_pointer.address].delete(callback_id)
  @inner_storage.delete(connection_pointer.address) if @inner_storage[connection_pointer.address].empty?

  cb_data[:opaque]
end

#retrieve_from_pointer(pointer) ⇒ Array<2>

Returns cb [Proc], opaque [Object].

Parameters:

  • (::FFI::Pointer)

Returns:

  • (Array<2>)

    cb [Proc], opaque [Object]



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

def retrieve_from_pointer(pointer)
  dbg { "#retrieve_from_pointer pointer=#{pointer}," }

  cb_data_struct = CallbackDataStruct.new(pointer)
  connection_pointer = cb_data_struct[:connection_pointer]
  callback_id = cb_data_struct[:callback_id]
  cb_data = @inner_storage[connection_pointer.address][callback_id]
  [cb_data[:cb], cb_data[:opaque]]
end

#store_struct(cb_data, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/libvirt/host_callback_storage.rb', line 32

def store_struct(cb_data, options)
  dbg { '#store_struct' }

  options.assert_valid_keys(:connection_pointer, :callback_id, :cb, :opaque, :free_func)
  connection_pointer = options.fetch(:connection_pointer)
  callback_id = options.fetch(:callback_id)
  cb = options.fetch(:cb)
  opaque = options.fetch(:opaque)
  free_func = options.fetch(:free_func)
  cb_data[:connection_pointer] = connection_pointer
  cb_data[:callback_id] = callback_id
  @inner_storage[connection_pointer.address][callback_id] = {
      cb: cb, opaque: opaque, pointer: cb_data.pointer, free_func: free_func
  }
end