ruby-dbus-wrapper-process-watch 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edd034d2f9c6b834a17fea60c068a306e729dab9
4
+ data.tar.gz: ccac79e6952417186662cceb23cd4fab28acec8a
5
+ SHA512:
6
+ metadata.gz: cb45ab3195afa5799c41da4589c5a9032167fd44798beaeb42b44c320ddd72e24fa8957da372e9415e6362fbf57a348a951317e9153d749859a4be00a754431f
7
+ data.tar.gz: 51cad04784b645ba45a3ca5bc1324c6a934ac9cb3e16ffa98eb93162b693d632b57d344447ff8d3e5d04dc8cde5e1a74b254704be2289de3a8990c6eb35be46f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 mspanc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ ruby-dbus-wrapper-process-watch
2
+ ===============================
3
+
4
+ Allows to register nice callbacks when D-Bus processess appear or disappear from the bus.
5
+
6
+
7
+ # Installation
8
+
9
+ ## Step 1: Add this gem to your Gemfile
10
+
11
+ Just add the following line to your `Gemfile`:
12
+
13
+ gem 'ruby-dbus-wrapper-process-watch'
14
+
15
+ ## Step 2: Read ruby-dbus-wrapper docs
16
+
17
+ This gem relies on [ruby-dbus-wrapper](https://github.com/mspanc/ruby-dbus-wrapper).
18
+ It expects that you manually add ruby-dbus gem to your Gemfile, follow its docs.
19
+
20
+ # Usage
21
+
22
+ ## Watching
23
+
24
+ # Initialize connection and wrapper
25
+ conn = DBus::SessionBus.instance
26
+ bus = DBus::Wrapper::Bus.new(conn, /^monitored\.dbus\.name\.wildcard.*/)
27
+
28
+ # Initialize process watch
29
+ ps = DBus::Wrapper::ProcessWatch.new(bus)
30
+
31
+ # Bind to events
32
+ ps.on(:registered) { |a| puts "Registered #{a}" }
33
+ ps.on(:unregistered) { |a| puts "Unregistered #{a}" }
34
+ ps.on(:resolved) { puts "Boot process finished, resolved IDs of all existing processes" };
35
+
36
+ # Start watching
37
+ ps.start!
38
+
39
+ # Start mainloop
40
+ main = DBus::Main.new
41
+ main << conn
42
+ main.run
43
+
44
+
45
+ ## Resolving names/IDs
46
+
47
+ Once process became watched, you can call following methods to make D-Bus ID/name resolution:
48
+
49
+ ps.resolve_name_to_id("my.process.name") # => :1:231
50
+ ps.resolve_id_to_name(":1:213") # => "my.process.name"
51
+
52
+ These methods can be useful if you try to convert D-Bus ID received as sender in signal callback.
53
+
54
+ If there's no match, these methods will return nil.
55
+
56
+ # License
57
+
58
+ MIT
59
+
60
+ # Author
61
+
62
+ (c) 2014 Marcin Lewandowski
63
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,2 @@
1
+ require "ruby-dbus-wrapper"
2
+ require_relative "ruby-dbus-wrapper-process-watch/process_watch"
@@ -0,0 +1,111 @@
1
+ module DBus
2
+ module Wrapper
3
+ class ProcessWatch
4
+ class NameResolutionFatalError < Exception; end
5
+ class NameResolutionRecoverableError < Exception; end
6
+
7
+ DBUS_ID_REGEXP = /\A(:1\.[0-9]+)\z/.freeze
8
+
9
+ def initialize(bus, process_name_regexp = /.*/)
10
+ @bus = bus
11
+ @process_name_regexp = process_name_regexp
12
+
13
+ @known_id_to_name = {}
14
+ @known_name_to_id = {}
15
+ @pending = {}
16
+ @events = { :resolved => [], :registered => [], :unregistered => [] }
17
+ end
18
+
19
+
20
+ def start!
21
+ @bus.monitor_signal("org.freedesktop.DBus", nil, "org.freedesktop.DBus", "NameOwnerChanged") { |message| on_name_owner_changed_signal(message) }
22
+ @bus.method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames") { |message| on_list_names_return(message) }
23
+ end
24
+
25
+
26
+ def on(event_name, &block)
27
+ raise ArgumentError, "Invalid event name #{event_name}" unless @events.keys.include? event_name.to_sym
28
+ raise ArgumentError, "You must pass a block" unless block_given?
29
+
30
+ @events[event_name.to_sym] << block
31
+ end
32
+
33
+
34
+ def resolve_name_to_id(name)
35
+ @known_name_to_id[name]
36
+ end
37
+
38
+
39
+ def resolve_id_to_name(id)
40
+ @known_id_to_name[id]
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def trigger(event_name, *args)
47
+ @events[event_name].each do |callback|
48
+ callback.call *args
49
+ end
50
+ end
51
+
52
+
53
+ def register(name, id)
54
+ @known_name_to_id[name] = id
55
+ @known_id_to_name[id] = name
56
+
57
+ trigger :registered, name
58
+ end
59
+
60
+
61
+ def unregister(name)
62
+ @known_id_to_name.delete @known_name_to_id[name]
63
+ @known_name_to_id.delete name
64
+
65
+ trigger :unregistered, name
66
+ end
67
+
68
+
69
+ def is_monitored_process_name?(name)
70
+ not name.match(DBUS_ID_REGEXP) and not name == "org.freedesktop.DBus" and name.match(@process_name_regexp)
71
+ end
72
+
73
+
74
+ def on_name_owner_changed_signal(message)
75
+ if is_monitored_process_name?(message.params[0])
76
+ if message.params[1] == '' and message.params[2] =~ DBUS_ID_REGEXP
77
+ register message.params[0], message.params[2]
78
+
79
+ elsif message.params[2] == '' and message.params[1] =~ DBUS_ID_REGEXP
80
+ unregister message.params[0]
81
+ end
82
+ end
83
+ end
84
+
85
+
86
+ def on_list_names_return(message)
87
+ raise NameResolutionFatalError, "Unable to query list of DBus names (#{message})" if message.is_a? DBus::Error
88
+
89
+ message.params.first.each do |name|
90
+ if is_monitored_process_name?(name)
91
+ @pending[name] = true
92
+ end
93
+ end
94
+
95
+ @pending.keys.each do |name|
96
+ @bus.method_call("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "GetNameOwner", [[ DBus::Type::STRING, name]]) do |message|
97
+ if message.is_a? DBus::Error
98
+ raise NameResolutionRecoverableError, "Unable to get DBus ID of of DBus process #{name} (#{message}) - most probably it has disappeared during query"
99
+ else
100
+ register name, message.params.first
101
+
102
+ @pending.delete name
103
+
104
+ trigger :resolved if @pending.size == 0
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "rake"
3
+
4
+ GEMSPEC = Gem::Specification.new do |s|
5
+ s.name = "ruby-dbus-wrapper-process-watch"
6
+ s.summary = "Monitor D-Bus processes"
7
+ s.description = "Allows to register nice callbacks when D-Bus processess appear or disappear from the bus"
8
+ s.version = File.read("VERSION").strip
9
+ s.license = "MIT"
10
+ s.author = "Marcin Lewandowski"
11
+ s.email = "marcin@saepia.net"
12
+ s.homepage = "https://github.com/mspanc/ruby-dbus-wrapper-process-watch"
13
+ s.files = FileList["{lib}/**/*", "LICENSE", "README.md", "ruby-dbus-wrapper-process-watch.gemspec", "VERSION"].to_a.sort
14
+ s.require_path = "lib"
15
+ s.required_ruby_version = ">= 1.9.3"
16
+ s.add_runtime_dependency "ruby-dbus-wrapper"
17
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-dbus-wrapper-process-watch
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Lewandowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-dbus-wrapper
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Allows to register nice callbacks when D-Bus processess appear or disappear
28
+ from the bus
29
+ email: marcin@saepia.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - VERSION
37
+ - lib/ruby-dbus-wrapper-process-watch.rb
38
+ - lib/ruby-dbus-wrapper-process-watch/process_watch.rb
39
+ - ruby-dbus-wrapper-process-watch.gemspec
40
+ homepage: https://github.com/mspanc/ruby-dbus-wrapper-process-watch
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.9.3
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Monitor D-Bus processes
64
+ test_files: []