rbus 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.
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,165 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ #--
4
+ #
5
+ # R-Bus is a native Ruby implementation of the D-Bus protocol.
6
+ # Copyright (C) 2007 Kristoffer Lundén (kristoffer.lunden@gmail.com)
7
+ #
8
+ # This program is free software; you can redistribute it and/or modify
9
+ # it under the terms of the GNU General Public License as published by
10
+ # the Free Software Foundation; either version 2 of the License, or
11
+ # (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program; if not, write to the Free Software
20
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
+ # MA 02110-1301, USA. A copy of the GNU General Public License is
22
+ # also available at http://www.gnu.org/copyleft/gpl.html.
23
+ #
24
+ #++
25
+ #
26
+ # <em>rbus-send</em>
27
+ # Pure ruby version of, and compatible with dbus-send
28
+ #
29
+ # Invocation:
30
+ # rbus-send <destination object path> <message name> [options] [arguments ...]
31
+ #
32
+ # Format of arguments:
33
+ # <arguments> ::= <item> | <container> [ <item> | <container>...]
34
+ # <item> ::= <type>:<value>
35
+ # <container> ::= <array> | <dict> | <variant>
36
+ # <array> ::= array:<type>:<value>[,<value>...]
37
+ # <dict> ::= dict:<type>:<type>:<key>,<value>[,<key>,<value>...]
38
+ # <variant> ::= variant:<type>:<value>
39
+ # <type> ::= string | int16 | uint16 | int32 | uint32 | int64 | uint64 | double | byte | boolean | objpath
40
+ #
41
+ # See the TUTORIAL[link:files/TUTORIAL_txt.html] for example invocations.
42
+ #
43
+ # Stuff known to be missing/not working:
44
+ # --reply-timeout=MSEC
45
+ #
46
+
47
+
48
+ require 'optparse'
49
+ require 'rbus'
50
+ require 'yaml'
51
+
52
+ def make_signature # :nodoc:
53
+ translation = {
54
+ 'byte' => 'y',
55
+ 'boolean' => 'b',
56
+ 'int16' => 'n',
57
+ 'uint16' => 'q',
58
+ 'int32' => 'i',
59
+ 'uint32' => 'u',
60
+ 'int64' => 'x',
61
+ 'uint64' => 't',
62
+ 'double' => 'd',
63
+ 'string' => 's',
64
+ 'objpath' => 'o',
65
+ 'signature' => 'g',
66
+ 'variant' => 'v',
67
+ 'array' => 'a',
68
+ 'dict' => 'a{',
69
+ }
70
+
71
+ signature = ''
72
+ array = false
73
+
74
+ ARGV.map!{|a|
75
+ type, arg = a.split(':',2)
76
+ fail "Unknown type '#{type}'" unless translation.has_key?(type)
77
+ signature << translation[type]
78
+ case type
79
+ when 'dict'
80
+ key,value,arg = arg.split(':',3)
81
+ signature << translation[key]
82
+ signature << translation[value]
83
+ signature << '}'
84
+ arg.nil? ? {} : Hash[*arg.split(',')]
85
+ when 'array'
86
+ array = true
87
+ a = arg
88
+ redo
89
+ when 'variant'
90
+ a = arg
91
+ redo
92
+ else
93
+ rv = (array) ? arg.split(',') : arg
94
+ array = false
95
+ rv
96
+ end
97
+ }
98
+ signature
99
+ end
100
+
101
+ klass = RBus::Signal
102
+ destination = nil
103
+ bus = nil
104
+ print_reply = false
105
+
106
+ opts = OptionParser.new do |opts|
107
+ opts.banner = 'rbus-send <destination object path> <message name> [options] [arguments ...]'
108
+
109
+ opts.on('--dest=NAME',
110
+ String,
111
+ 'Specify the name of the connection to receive the message.') {|dest|
112
+ destination = dest
113
+ }
114
+
115
+ opts.on('--print-reply',
116
+ 'Block for a reply to the message sent, and print any reply received.') {
117
+ print_reply = true
118
+ klass = RBus::MethodCall
119
+ }
120
+
121
+ opts.on('--system', 'Send to the system message bus.') {
122
+ bus = RBus.system_bus
123
+ }
124
+
125
+ opts.on('--session', 'Send to the session message bus. (This is the default.)') {
126
+ bus = RBus.session_bus
127
+ }
128
+
129
+ opts.on('--type=TYPE', %w{method_call signal},
130
+ 'Specify "method_call" or "signal" (defaults to "signal").') { |type|
131
+ klass = case type
132
+ when 'method_call' : RBus::MethodCall
133
+ when 'signal' : RBus::Signal
134
+ end
135
+ }
136
+ end
137
+
138
+ opts.parse!
139
+
140
+ if ARGV.length < 2
141
+ puts opts.to_s
142
+ exit
143
+ end
144
+
145
+ object_path = ARGV.shift
146
+ member,interface = ARGV.shift.reverse.split('.',2).map{|s|s.reverse}
147
+
148
+ bus = RBus.session_bus if bus.nil?
149
+
150
+ message = klass.new do |m|
151
+ m.destination = destination
152
+ m.object_path = object_path
153
+ m.interface = interface
154
+ m.member = member
155
+ m.signature = make_signature
156
+ m.arguments = ARGV
157
+ end
158
+
159
+ #puts YAML.dump(message);exit
160
+
161
+ if print_reply
162
+ puts YAML.dump(bus.message_loop.send_message(message, nil))
163
+ else
164
+ bus.message_loop.send_message(message, lambda{})
165
+ end
@@ -0,0 +1,56 @@
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
+ # Asynchronous calls, very contrived example :)
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
+ # Rhythmbox player
39
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
40
+ rb.interface!('org.gnome.Rhythmbox.Player')
41
+
42
+ # Libnotify
43
+ notify = session_bus.get_object('org.freedesktop.Notifications','/org/freedesktop/Notifications')
44
+ notify.interface!('org.freedesktop.Notifications')
45
+
46
+ # Async call, should be run in parallell
47
+ rb.getPlayingUri() do |uri|
48
+ notify.Notify('R-Bus',0,'info','Async Notification 1',uri,[],{},-1)
49
+ sleep(1)
50
+ notify.Notify('R-Bus',0,'info','Async Notification 2',uri,[],{},-1)
51
+ end
52
+
53
+ # Sync call, runs in main thread
54
+ notify.Notify('R-Bus',0,'info','Sync Notification',rb.getPlayingUri(),[],{},-1)
55
+ sleep(2)
56
+
@@ -0,0 +1,52 @@
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
+ # Asynchronous calls, in loops
28
+ # Prints RB song 10 times in random order
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
+ # Rhythmbox player
39
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
40
+ rb.interface!('org.gnome.Rhythmbox.Player')
41
+
42
+ # Async calls, should be run in parallell
43
+ 10.times do |i|
44
+ rb.getPlayingUri() do |uri|
45
+ sleep(rand(2))
46
+ puts "#{i}: #{uri}"
47
+ end
48
+ end
49
+
50
+ # Give it time to finish
51
+ sleep(2)
52
+
@@ -0,0 +1,57 @@
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
+ # Asynchronous calls, in loops
28
+ # Prints RB song 10 times in random order
29
+ #
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus/glib'
35
+
36
+ session_bus = RBus.session_bus()
37
+
38
+ # Rhythmbox player
39
+ rb = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
40
+ rb.interface!('org.gnome.Rhythmbox.Player')
41
+
42
+ # Or we won't see anything... :)
43
+ STDOUT.sync=true
44
+
45
+ # Async calls, however in with a GLib mainloop
46
+ # it will still be exectued in sequence, and not
47
+ # until the mainloop is started.
48
+ 10.times do |i|
49
+ rb.getPlayingUri() do |uri|
50
+ sleep(rand(2))
51
+ puts "#{i}: #{uri}"
52
+ end
53
+ end
54
+
55
+ # Start the mainloop for GLib
56
+ # Same as Gtk.main
57
+ RBus.mainloop
@@ -0,0 +1,54 @@
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
+ # RBus Example:
26
+ # - GLib main loop
27
+ # - Display song info from Rhytmbox on song change
28
+ # - exact same code, except include 'rbus/glib'
29
+
30
+
31
+ # Add lib to load path during developmennt
32
+ $: << '../lib/'
33
+
34
+ require 'rbus/glib'
35
+
36
+ session_bus = RBus.session_bus()
37
+
38
+ rb_player = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
39
+ rb_player.interface!('org.gnome.Rhythmbox.Player')
40
+
41
+ rb_shell = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Shell')
42
+ rb_shell.interface!('org.gnome.Rhythmbox.Shell')
43
+
44
+ rb_player.connect!("playingUriChanged") { |uri|
45
+ properties = rb_shell.getSongProperties(uri)
46
+ duration = Time.at(properties['duration'].to_i).strftime("%M:%S")
47
+ artist = properties['artist']
48
+ title = properties['title']
49
+ puts "Now playing: #{artist} - #{title} (#{duration})"
50
+ printf "File-size: %.2f MB\n", properties['file-size'].to_f/1024/1024
51
+ }
52
+
53
+
54
+ RBus.mainloop
@@ -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
+ # Get information about a random HAL device.
28
+ #
29
+
30
+ # Add lib to load path during development
31
+ $: << '../lib/'
32
+
33
+ require 'rbus'
34
+
35
+ bus = RBus.system_bus()
36
+
37
+ # Connect to HAL Manager first:
38
+ hal = bus.get_object('org.freedesktop.Hal','/org/freedesktop/Hal/Manager')
39
+ hal.interface!('org.freedesktop.Hal.Manager')
40
+
41
+ # Get a random device from list:
42
+ devs = hal.GetAllDevices()
43
+ random = devs[rand(devs.size)]
44
+
45
+ # Get object for that device:
46
+ random_device = bus.get_object('org.freedesktop.Hal',random)
47
+ random_device.interface!('org.freedesktop.Hal.Device')
48
+
49
+ # Define method manually, as HAL doesn't provide introspection, at least
50
+ # in my version.
51
+ random_device.method!(:GetProperty, 's')
52
+
53
+ # Print info and device path:
54
+ puts ("#{random_device.GetProperty('info.product')} (#{random})")
@@ -0,0 +1,37 @@
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
+ # A Bus is a Proxy too...
28
+ #
29
+
30
+ # Add lib to load path during developmennt
31
+ $: << '../lib/'
32
+
33
+ require 'rbus'
34
+
35
+ session_bus = RBus.session_bus()
36
+ puts session_bus.ListNames
37
+