pangdudu-ruby-dbus-daemon 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +36 -0
  2. data/lib/dbus-daemon.rb +84 -0
  3. metadata +54 -0
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = Ruby-Dbus-Daemon
2
+
3
+ Hi girls and boys!
4
+
5
+ This is a small wrapper lib around the dbus-daemon executable.
6
+
7
+ == Installation
8
+
9
+ sudo gem install pangdudu-ruby-dbus-daemon --source=http://gems.github.com
10
+
11
+ == Usage
12
+
13
+ to start a dbus daemon:
14
+
15
+ require 'dbus-daemon'
16
+ dbd = DbusDaemon.new "dbus.configfile.conf"
17
+ dbd.start_daemon
18
+
19
+ to kill the daemon (and the processes that spawned on the console):
20
+
21
+ dbd.stop_daemon
22
+
23
+ == Warning
24
+
25
+ If you CTRL-C your app, the spawned daemon will continue running. You'll need to
26
+ "ps xaf | grep dbus-daemon" and "kill -9 pid" on your own. Got no time for a pretty
27
+ solution right now. Once ruby-dbus is further on the way, i'll rewrite it with a
28
+ real *.so and a ruby lib wrapping it.
29
+
30
+ == Rule the universe!
31
+
32
+ Ride the dbus with me, yeah yeah.
33
+
34
+ == License
35
+
36
+ GPL -> http://www.gnu.org/licenses/gpl.txt
@@ -0,0 +1,84 @@
1
+ =begin
2
+ Ok, this is a really simple wrapper around the dbus-daemon console tool
3
+
4
+ It's awfully dirty, but I need to concentrate on other stuff now :)
5
+
6
+ Should become an app wrapping the .c into a nice .so
7
+ =end
8
+
9
+ class DbusDaemon
10
+ attr_accessor :config_file,:output,:thread
11
+
12
+ def initialize config_file #a dbus config file
13
+ @daemon = `which dbus-daemon`.strip #search for the daemon
14
+ @config_file = config_file
15
+ #if the specified config file does not exist, break!
16
+ unless File.exist? @config_file
17
+ puts "ERROR: dbus-daemon: specified config-file: #{@config_file}, not readable."
18
+ return nil
19
+ end
20
+ @cmd = "#{@daemon} --config-file=#{@config_file}"
21
+ #no daemon found, sorry
22
+ if @daemon.empty?
23
+ puts "ERROR: dbus-daemon: no executable dbus-daemon found, sorry!"
24
+ return nil
25
+ end
26
+ end
27
+
28
+
29
+ #start the dbus-daemon
30
+ def start_daemon
31
+ got_twins = got_twin_daemon?
32
+ unless got_twins
33
+ #create a new thread for the daemon
34
+ @thread = Thread.new do
35
+ begin
36
+ @output = `#{@cmd}`
37
+ rescue
38
+ puts "ERROR: dbus-daemon: dbus-daemon couldn't be started!"
39
+ return nil
40
+ end
41
+ end
42
+ #just wait a really short time, so the console can answer
43
+ sleep 0.1
44
+ #ok, the output of the `` will be in @output
45
+ puts "INFO: #{@cmd} is running!" if @output.nil? #so @output.nil? means, the daemon is running
46
+ unless @output.nil?
47
+ #@output.empty? = true means, we had an error, and it went to stderr
48
+ puts "ERROR: dbus-daemon: dbus-daemon couldn't be started!" if @output.empty?
49
+ #if there's something in @output (maybe we called --version), show it
50
+ puts "INFO: dbus-daemon says: \n\t#{@output}" unless @output.empty?
51
+ end
52
+ end
53
+ puts "WARNING: dbus-daemon: #{@cmd} not started, already seems to be running." if got_twins
54
+ end
55
+
56
+ #killing a ruby thread that spawned sth. on the console is not that easy to kill
57
+ def stop_daemon
58
+ @thread.kill
59
+ #HACK, ok, this is not only dangerous, but hackish, we sould use ruby 1.9...
60
+ pids_tokill = []
61
+ ps = `ps x | grep "#{@cmd}"`.split("\n")
62
+ ps.each {|p| pids_tokill << p.split("pts/")[0].to_i}
63
+ #oki, pids_tokill now holds a list of pids, that we probably started, we'll kill them now
64
+ pids_tokill.each {|p| `kill -9 #{p}`}
65
+ end
66
+
67
+ #check if another daemon doing the same is already running
68
+ def got_twin_daemon?
69
+ pids = []
70
+ ps = `ps x | grep "#{@cmd}"`.split("\n")
71
+ ps.each {|p| pids << p.split("pts/")[0].to_i unless p.include? "grep"}
72
+ return false if pids.empty?
73
+ #failsafe
74
+ return true
75
+ end
76
+ end
77
+
78
+ =begin
79
+ #for testing
80
+ dbd = DbusDaemon.new "somedbusconfigfile.conf"
81
+ dbd.start_daemon
82
+ sleep 2
83
+ dbd.stop_daemon
84
+ =end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pangdudu-ruby-dbus-daemon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - pangdudu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-26 00:00:00 -07:00
13
+ default_executable: ruby-dbus-daemon
14
+ dependencies: []
15
+
16
+ description: a small dbus-daemon wrapper for ruby.
17
+ email: pangdudu@github
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - lib/dbus-daemon.rb
27
+ has_rdoc: true
28
+ homepage: http://github.com/pangdudu/ruby-dbus-daemon
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project: http://github.com/pangdudu/ruby-dbus-daemon
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: ride the ruby-dbus-daemon, jiiiihaaaa!
53
+ test_files: []
54
+