libvirt_ffi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require 'libvirt/util'
5
+ require 'libvirt/ffi/common'
6
+
7
+ module Libvirt
8
+ module FFI
9
+ module Event
10
+ extend ::FFI::Library
11
+ ffi_lib Util.library_path
12
+
13
+ # typedef void (*virEventHandleCallback) (
14
+ # int watch,
15
+ # int fd,
16
+ # int events,
17
+ # void * opaque
18
+ # )
19
+ EVENT_HANDLE_CALLBACK = callback :virEventHandleCallback, [:int, :int, :int, :pointer], :void
20
+
21
+ # typedef void (*virEventTimeoutCallback) (
22
+ # int timer,
23
+ # void * opaque
24
+ # )
25
+ EVENT_TIMEOUT_CALLBACK = callback :virEventTimeoutCallback, [:int, :pointer], :void
26
+
27
+ # typedef int (*virEventAddHandleFunc) (
28
+ # int fd,
29
+ # int event,
30
+ # virEventHandleCallback cb,
31
+ # void * opaque,
32
+ # virFreeCallback ff
33
+ # )
34
+ callback :virEventAddHandleFunc, [
35
+ :int,
36
+ :int,
37
+ :virEventHandleCallback,
38
+ :pointer,
39
+ FFI::Common::FREE_CALLBACK
40
+ ], :int
41
+
42
+ # typedef void (*virEventUpdateHandleFunc) (
43
+ # int watch,
44
+ # int event
45
+ # )
46
+ callback :virEventUpdateHandleFunc, [:int, :int], :void
47
+
48
+ # typedef int (*virEventRemoveHandleFunc) (
49
+ # int watch
50
+ # )
51
+ callback :virEventRemoveHandleFunc, [:int], :int
52
+
53
+ # typedef int (*virEventAddTimeoutFunc) (
54
+ # int timeout,
55
+ # virEventTimeoutCallback cb,
56
+ # void * opaque,
57
+ # virFreeCallback ff
58
+ # )
59
+ callback :virEventAddTimeoutFunc, [
60
+ :int,
61
+ :virEventTimeoutCallback,
62
+ :pointer,
63
+ FFI::Common::FREE_CALLBACK
64
+ ], :int
65
+
66
+ # typedef void (*virEventUpdateTimeoutFunc) (
67
+ # int timer,
68
+ # int timeout
69
+ # )
70
+ callback :virEventUpdateTimeoutFunc, [:int, :int], :void
71
+
72
+ # typedef int (*virEventRemoveTimeoutFunc) (
73
+ # int timer
74
+ # )
75
+ callback :virEventRemoveTimeoutFunc, [:int], :int
76
+
77
+ # void virEventRegisterImpl (
78
+ # virEventAddHandleFunc addHandle,
79
+ # virEventUpdateHandleFunc updateHandle,
80
+ # virEventRemoveHandleFunc removeHandle,
81
+ # virEventAddTimeoutFunc addTimeout,
82
+ # virEventUpdateTimeoutFunc updateTimeout,
83
+ # virEventRemoveTimeoutFunc removeTimeout
84
+ # )
85
+ attach_function :virEventRegisterImpl, [
86
+ :virEventAddHandleFunc,
87
+ :virEventUpdateHandleFunc,
88
+ :virEventRemoveHandleFunc,
89
+ :virEventAddTimeoutFunc,
90
+ :virEventUpdateTimeoutFunc,
91
+ :virEventRemoveTimeoutFunc
92
+ ], :void
93
+
94
+ def self.event_add_handle_func(&block)
95
+ ::FFI::Function.new(:int, [:int, :int, EVENT_HANDLE_CALLBACK, :pointer, FFI::Common::FREE_CALLBACK], &block)
96
+ end
97
+
98
+ def self.event_add_timeout_func(&block)
99
+ ::FFI::Function.new(:int, [:int, EVENT_TIMEOUT_CALLBACK, :pointer, FFI::Common::FREE_CALLBACK], &block)
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require 'libvirt/util'
5
+
6
+ module Libvirt
7
+ module FFI
8
+ module Libvirt
9
+ extend ::FFI::Library
10
+ ffi_lib Util.library_path
11
+
12
+ # int virGetVersion (
13
+ # unsigned long *libVer,
14
+ # const char *type,
15
+ # unsigned long *typeVer
16
+ # )
17
+ attach_function :virGetVersion, [:pointer, :string, :pointer], :int
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require 'libvirt/ffi/libvirt'
5
+ require 'libvirt/util'
6
+
7
+ module Libvirt
8
+ module LibVersion
9
+ def self.call
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ module Util
5
+ class << self
6
+
7
+ def logger=(logger)
8
+ @logger = logger
9
+ end
10
+
11
+ def logger
12
+ @logger
13
+ end
14
+
15
+ def log(severity, prog = nil, &block)
16
+ return if @logger.nil?
17
+ @logger.public_send(severity, prog, &block)
18
+ end
19
+
20
+ def library_path
21
+ 'libvirt'
22
+ end
23
+
24
+ # @param [Integer] version_number ulong
25
+ def parse_version(version_number)
26
+ major = version_number / 1_000_000
27
+ minor = (version_number - major * 1_000_000) / 1_000
28
+ release = version_number - major * 1_000_000 - minor * 1_000
29
+ "#{major}.#{minor}.#{release}"
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ VERSION = '0.1.0'
5
+ end
data/lib/libvirt.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+ require 'libvirt/util'
5
+ require 'libvirt/error'
6
+ require 'libvirt/ffi/common'
7
+ require 'libvirt/ffi/connection'
8
+ require 'libvirt/ffi/domain'
9
+ require 'libvirt/lib_version'
10
+ require 'libvirt/event'
11
+ require 'libvirt/connection'
12
+ require 'libvirt/domain'
13
+ require 'libvirt/version'
14
+
15
+ module Libvirt
16
+ EVENT_HANDLE_READABLE = 1
17
+ EVENT_HANDLE_WRITABLE = 2
18
+ EVENT_HANDLE_ERROR = 4
19
+ EVENT_HANDLE_HANGUP = 8
20
+
21
+ DOMAIN_EVENT_ID_LIFECYCLE = 0
22
+
23
+ class << self
24
+ def lib_version
25
+ version_ptr = ::FFI::MemoryPointer.new(:ulong)
26
+ code = FFI::Libvirt.virGetVersion(version_ptr, nil, nil)
27
+ raise Error, 'failed to get version' if code < 0
28
+ version_number = version_ptr.get_ulong(0)
29
+ Libvirt::Util.parse_version(version_number)
30
+ end
31
+
32
+ def logger
33
+ Util.logger
34
+ end
35
+
36
+ def logger=(logger)
37
+ Util.logger = logger
38
+ end
39
+ end
40
+ end
@@ -0,0 +1 @@
1
+ require 'libvirt'
data/libvirt.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/libvirt/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'libvirt_ffi'
5
+ spec.version = Libvirt::VERSION
6
+ spec.authors = ['Denis Talakevich']
7
+ spec.email = ['senid231@gmail.com']
8
+
9
+ spec.summary = 'Libvirt FFI'
10
+ spec.description = 'Libvirt FFI'
11
+ spec.homepage = 'https://github.com/senid231/libvirt_ffi'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.metadata['homepage_uri'] = spec.homepage
15
+ spec.metadata['source_code_uri'] = spec.homepage
16
+ spec.metadata['changelog_uri'] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'ffi', '>= 1.0'
28
+ end