rbus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/CHANGELOG.txt +9 -0
  2. data/COPYING.txt +341 -0
  3. data/HACKING.txt +104 -0
  4. data/Manifest.txt +58 -0
  5. data/README.txt +17 -0
  6. data/Rakefile +97 -0
  7. data/TUTORIAL.txt +303 -0
  8. data/bin/rbus-send +165 -0
  9. data/examples/async_rb_and_notify.rb +56 -0
  10. data/examples/async_rb_loop.rb +52 -0
  11. data/examples/glib_async_rb_loop.rb +57 -0
  12. data/examples/glib_rhythmbox.rb +54 -0
  13. data/examples/hal_device_info.rb +54 -0
  14. data/examples/listnames.rb +37 -0
  15. data/examples/network_manager_get_properties.rb +50 -0
  16. data/examples/notification_bubble.rb +46 -0
  17. data/examples/rhythmbox_print_playing_uri.rb +41 -0
  18. data/examples/rhythmbox_signal_print_playing.rb +54 -0
  19. data/examples/rhythmbox_start_service.rb +39 -0
  20. data/examples/rhythmbox_toggle_playing.rb +46 -0
  21. data/lib/rbus.rb +25 -0
  22. data/lib/rbus/auth/auth.rb +53 -0
  23. data/lib/rbus/auth/dbus_cookie_sha1.rb +66 -0
  24. data/lib/rbus/auth/dummy.rb +37 -0
  25. data/lib/rbus/auth/external.rb +34 -0
  26. data/lib/rbus/auth/state_machine.rb +168 -0
  27. data/lib/rbus/bus/bus.rb +101 -0
  28. data/lib/rbus/bus/proxy.rb +137 -0
  29. data/lib/rbus/bus/transport.rb +125 -0
  30. data/lib/rbus/default.rb +29 -0
  31. data/lib/rbus/etc/exception.rb +44 -0
  32. data/lib/rbus/etc/log.rb +100 -0
  33. data/lib/rbus/etc/types.rb +56 -0
  34. data/lib/rbus/etc/version.rb +34 -0
  35. data/lib/rbus/glib.rb +29 -0
  36. data/lib/rbus/mainloop/glib.rb +77 -0
  37. data/lib/rbus/mainloop/mainloop.rb +84 -0
  38. data/lib/rbus/mainloop/observers.rb +149 -0
  39. data/lib/rbus/mainloop/thread.rb +78 -0
  40. data/lib/rbus/message/constants.rb +51 -0
  41. data/lib/rbus/message/marshal.rb +139 -0
  42. data/lib/rbus/message/message.rb +110 -0
  43. data/lib/rbus/message/reader.rb +108 -0
  44. data/lib/rbus/message/serial_generator.rb +48 -0
  45. data/lib/rbus/message/unmarshal.rb +171 -0
  46. data/lib/rbus/message/writer.rb +69 -0
  47. data/setup.rb +1608 -0
  48. data/spec/auth_spec.rb +123 -0
  49. data/spec/bus_spec.rb +178 -0
  50. data/spec/helpers/bus_mocks.rb +64 -0
  51. data/spec/helpers/spec_helper.rb +24 -0
  52. data/spec/mainloop_spec.rb +74 -0
  53. data/spec/marshal_spec.rb +91 -0
  54. data/spec/message_spec.rb +61 -0
  55. data/spec/observers_spec.rb +28 -0
  56. data/spec/proxy_spec.rb +120 -0
  57. data/spec/transport_spec.rb +187 -0
  58. data/spec/unmarshal_spec.rb +186 -0
  59. metadata +118 -0
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Get properties from Network Manager interface
28
+ # From the Python D-Bus tutorial
29
+ #
30
+ # I have no idea what most of those values are, though :)
31
+ #
32
+
33
+
34
+ # Add lib to load path during developmennt
35
+ $: << '../lib/'
36
+
37
+ require 'pp'
38
+ require 'rbus'
39
+
40
+ # eth1 is the wireless on my machine,
41
+ # eth0 may be the most common one to try
42
+ dev = 'eth1'
43
+
44
+ bus = RBus.system_bus()
45
+
46
+ device = bus.get_object('org.freedesktop.NetworkManager', "/org/freedesktop/NetworkManager/Devices/#{dev}")
47
+ device.interface!('org.freedesktop.NetworkManager.Devices')
48
+
49
+ # Pretty print
50
+ pp device.getProperties()
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Display notification bubble
28
+ #
29
+
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus'
35
+
36
+ session_bus = RBus.session_bus()
37
+
38
+ notify = session_bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications')
39
+ notify.interface!('org.freedesktop.Notifications')
40
+
41
+ # app_name, id, icon, summary, body, actions, hints, timeout
42
+ # icon should be able to be the name of a stock-icon, an url or a pixmap,
43
+ # but I have only tried stock so far, in this case stock-info.
44
+ return_id =
45
+ notify.Notify('R-Bus',0,'info','R-Bus Notification',
46
+ 'A test example to see that everything works.',[],{},-1)
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Print the URI of the currently playing song in Rhythmbox.
28
+ #
29
+
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus'
35
+
36
+ session_bus = RBus.session_bus()
37
+
38
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
39
+ rb.interface!('org.gnome.Rhythmbox.Player')
40
+
41
+ puts rb.getPlayingUri()
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Print some info about the song each time a new is played in Rhythmbox.
28
+ #
29
+
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus'
35
+ Thread.abort_on_exception=true
36
+
37
+ session_bus = RBus.session_bus()
38
+
39
+ rb_player = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
40
+ rb_player.interface!('org.gnome.Rhythmbox.Player')
41
+
42
+ rb_shell = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
43
+ rb_shell.interface!('org.gnome.Rhythmbox.Shell')
44
+
45
+ rb_player.connect!("playingUriChanged") { |uri|
46
+ properties = rb_shell.getSongProperties(uri)
47
+ duration = Time.at(properties['duration'].to_i).strftime("%M:%S")
48
+ artist = properties['artist']
49
+ title = properties['title']
50
+ puts "Now playing: #{artist} - #{title} (#{duration})"
51
+ printf "File-size: %.2f MB\n", properties['file-size'].to_f/1024/1024
52
+ }
53
+
54
+ RBus.mainloop
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Start Rhythmbox and "press play"
28
+ #
29
+
30
+ # Add lib to load path during developmennt
31
+ $: << '../lib/'
32
+
33
+ require 'rbus'
34
+
35
+ session_bus = RBus.session_bus()
36
+ retval = session_bus.StartServiceByName('org.gnome.Rhythmbox', 0)
37
+
38
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
39
+ rb.playPause(true)
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ #
4
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
5
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
6
+ #
7
+ # This program is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 2 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with this program; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
21
+ # also available at http://www.gnu.org/copyleft/gpl.html.
22
+ #
23
+ #++
24
+ #
25
+ #
26
+ # RBus Example:
27
+ # Toggle play/pause in Rhythmbox.
28
+ #
29
+
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus'
35
+
36
+ session_bus = RBus.session_bus()
37
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
38
+ rb.interface!('org.gnome.Rhythmbox.Player')
39
+
40
+ # Get current status and print what we are about to do
41
+ puts rb.getPlaying() ? 'Pausing...' : 'Playing...'
42
+
43
+ # playPause takes a mandatory boolean, but just
44
+ # toggles play/pause regardless of the value.
45
+ # This might be a bug.
46
+ rb.playPause(true)
@@ -0,0 +1,25 @@
1
+ #--
2
+ #
3
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
4
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
20
+ # also available at http://www.gnu.org/copyleft/gpl.html.
21
+ #
22
+ #++
23
+ #
24
+
25
+ require File.dirname(__FILE__) + '/rbus/default'
@@ -0,0 +1,53 @@
1
+ #--
2
+ #
3
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
4
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
20
+ # also available at http://www.gnu.org/copyleft/gpl.html.
21
+ #
22
+ #++
23
+ #
24
+ Dir[File.join(File.dirname(__FILE__), '*.rb')].sort.each { |lib| require lib }
25
+
26
+ module RBus
27
+ module Auth
28
+
29
+ MECHANISMS = {
30
+ 'EXTERNAL' => External,
31
+ 'DBUS_COOKIE_SHA1' => DBusCookieSHA1
32
+ }
33
+ FIRST_MECHANISM = Dummy
34
+
35
+ # Authorize with the bus via its Transport object.
36
+ def self.authorize(transport)
37
+ raise AuthException, 'transport is nil' if transport.nil?
38
+
39
+ # Init Auth process
40
+ transport.send("\0")
41
+
42
+ # use a statemachine as the spec suggests
43
+ state_machine = StateMachine.new(transport)
44
+ begin
45
+ # return server guid on success - not used today though.
46
+ return state_machine.authorize
47
+ rescue AuthException
48
+ transport.close
49
+ raise
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ #--
2
+ #
3
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
4
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
20
+ # also available at http://www.gnu.org/copyleft/gpl.html.
21
+ #
22
+ #++
23
+ #
24
+ module RBus
25
+ module Auth
26
+
27
+ # Implements the AUTH DBUS_COOKIE_SHA1 mechanism.
28
+ class DBusCookieSHA1
29
+
30
+ def auth
31
+ require 'etc'
32
+
33
+ ['DBUS_COOKIE_SHA1', Etc.getlogin, :DATA]
34
+ end
35
+
36
+ def data(data)
37
+ require 'digest/sha1'
38
+
39
+ # name of cookie file, id of cookie in file, servers random challenge
40
+ context, id, s_challenge = data.split(' ')
41
+
42
+ # Random client challenge
43
+ c_challenge = Array.new(s_challenge.length/2).map{|obj|obj=rand(255).to_s}.join
44
+
45
+ # Search cookie file for id
46
+ path = File.join(ENV['HOME'], '.dbus-keyrings', context)
47
+ File.foreach(path) do |line|
48
+ if line.index(id) == 0
49
+
50
+ # Right line of file, read cookie
51
+ cookie = line.split(' ')[2].chomp
52
+
53
+ # Concatenate and encrypt
54
+ to_encrypt = [s_challenge, c_challenge, cookie].join(':')
55
+ sha = Digest::SHA1.hexdigest(to_encrypt)
56
+
57
+ # Return response
58
+ return ["#{c_challenge} #{sha}", :OK]
59
+ end
60
+ end
61
+
62
+ raise AuthException, 'Unable to locate cookie'
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,37 @@
1
+ #--
2
+ #
3
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
4
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
20
+ # also available at http://www.gnu.org/copyleft/gpl.html.
21
+ #
22
+ #++
23
+ #
24
+ module RBus
25
+ module Auth
26
+
27
+ # This is a Dummy Auth mechanism used first to get supported
28
+ # mechanisms from server. It's possible to just chance it
29
+ # and start with one of the other directly.
30
+ # Set RBus::Auth::FIRST_MECHANISM to proper class to override
31
+ class Dummy
32
+ def auth(data = nil)
33
+ [nil, nil, :REJECTED]
34
+ end
35
+ end
36
+ end
37
+ end