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,29 @@
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
+ base_dir = File.dirname(__FILE__)
25
+ %w{auth bus etc message}.each do |dir|
26
+ Dir[File.join(base_dir, dir, '*.rb')].sort.each { |lib| require lib }
27
+ end
28
+
29
+ require File.dirname(__FILE__) + '/mainloop/thread.rb'
@@ -0,0 +1,44 @@
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
+
26
+ # Catch-all, simply do:
27
+ # rescue RBus::Exception => error
28
+ # if in doubt. :)
29
+ class Exception < RuntimeError; end
30
+
31
+ class AuthException < Exception
32
+ def initialize(msg = "Failed to authenticate with any mechanism.")
33
+ super(msg)
34
+ end
35
+ end
36
+ class InvalidAddressException < Exception;end
37
+ class InvalidTransportException < Exception;end
38
+ class NotImplementedError < Exception;end
39
+ class TypeException < Exception;end
40
+ class MessageException < Exception;end
41
+ class IllegalEndianException < Exception;end
42
+ class InvalidProtocolException < Exception;end
43
+ class InvalidNameException < Exception;end
44
+ end
@@ -0,0 +1,100 @@
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
+ require 'logger'
25
+ require 'pp'
26
+ require 'stringio'
27
+ module RBus
28
+
29
+ # A Logger based Log system, with levels. See Logger for more info.
30
+ #
31
+ # RBus::Log logs to STDOUT by default, it's possible to redirect this
32
+ # output to a file by setting a filename in the environment variable
33
+ # $RBUS_LOG_FILE.
34
+ # The default log level is ERROR, this can be adjusted by setting the
35
+ # environment variable $RBUS_LOG_LEVEL to FATAL, ERROR, WARN, INFO or
36
+ # DEBUG, or by setting RBus::Log.level=(new_level)
37
+ #
38
+ class Log
39
+
40
+ # Set a new log level. Takes either a symbol, like :ERROR or a string,
41
+ # like 'FATAL'.
42
+ def self.level=(new_level)
43
+ new_level = new_level.to_s
44
+ log_level = case new_level
45
+ when 'FATAL'
46
+ Logger::FATAL
47
+ when 'ERROR'
48
+ Logger::ERROR
49
+ when 'WARN'
50
+ Logger::WARN
51
+ when 'INFO'
52
+ Logger::INFO
53
+ when 'DEBUG'
54
+ Logger::DEBUG
55
+ else
56
+ Logger::ERROR
57
+ end
58
+ @@logger.level = log_level
59
+ end
60
+
61
+ def self.pp(*objs)
62
+ sio = StringIO.new('')
63
+ sio.write(caller(2)[0])
64
+ sio.write("\n")
65
+ objs.each do |obj|
66
+ PP.pp(obj,sio)
67
+ end
68
+ sio.string
69
+ end
70
+
71
+ def self.error(*objs)
72
+ @@logger.error(pp(objs))
73
+ end
74
+
75
+ def self.warn(*objs)
76
+ @@logger.warn(pp(objs))
77
+ end
78
+
79
+ def self.info(*objs)
80
+ @@logger.info(pp(objs))
81
+ end
82
+
83
+ def self.debug(*objs)
84
+ @@logger.debug(pp(objs))
85
+ end
86
+
87
+ def self.fatal(*objs)
88
+ @@logger.fatal(pp(objs))
89
+ end
90
+
91
+ # Initialize log object
92
+ log_target = ENV['RBUS_LOG_FILE'] || STDOUT
93
+ @@logger = Logger.new(log_target)
94
+ self.level = ENV['RBUS_LOG_LEVEL']
95
+ @@logger.formatter = lambda { |severity, time, progname, msg|
96
+ "%s: %s\n" % [severity, msg]
97
+ }
98
+
99
+ end
100
+ end
@@ -0,0 +1,56 @@
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
+
26
+ class ObjectPath < String
27
+ end
28
+
29
+ class Variant
30
+ attr_reader :object_signature, :object
31
+ def initialize(object, signature)
32
+ @object_signature = signature
33
+ @object = object
34
+ end
35
+
36
+ def method_missing(name, *args, &block)
37
+ @object.send(name, *args, &block)
38
+ end
39
+
40
+ def ==(other)
41
+ other.object_signature == @object_signature && other.object == @object
42
+ end
43
+
44
+ def to_s
45
+ @object.to_s
46
+ end
47
+
48
+ def to_i
49
+ @object.to_i
50
+ end
51
+
52
+ def to_f
53
+ @object.to_f
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,34 @@
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 #:nodoc:
25
+ module VERSION #:nodoc:
26
+ PROTOCOL = 1
27
+
28
+ MAJOR = 0
29
+ MINOR = 1
30
+ TINY = 0
31
+
32
+ STRING = [MAJOR, MINOR, TINY].join('.')
33
+ end
34
+ end
@@ -0,0 +1,29 @@
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
+ base_dir = File.dirname(__FILE__)
25
+ %w{auth bus etc message}.each do |dir|
26
+ Dir[File.join(base_dir, dir, '*.rb')].sort.each { |lib| require lib }
27
+ end
28
+
29
+ require File.dirname(__FILE__) + '/mainloop/glib.rb'
@@ -0,0 +1,77 @@
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
+ require File.dirname(__FILE__) + '/mainloop'
25
+ require File.dirname(__FILE__) + '/observers'
26
+ require 'gtk2'
27
+
28
+ module RBus
29
+ class Mainloop
30
+ def setup
31
+ @active = true
32
+ @io_channel = GLib::IOChannel.new(@transport.socket.fileno)
33
+ @io_channel.add_watch(GLib::IOChannel::IN) do |ch, con|
34
+ if @active
35
+ message = @reader.read_message
36
+ handle_incoming(message)
37
+ end
38
+ true
39
+ end
40
+ end
41
+
42
+ def send_message(message, callback = nil)
43
+ # TODO: if message has no_reply, use dummy observer
44
+ if message.member == 'AddMatch'
45
+ observer = SignalObserver.new(self, message.arguments[0], callback)
46
+ message.arguments = message.arguments[0].map{|key,value|"#{key}='#{value}'"}.join(',')
47
+ elsif callback
48
+ observer = AsyncObserver.new(self, message.serial, callback)
49
+ else
50
+ observer = nil
51
+ end
52
+
53
+ handle_outgoing(message)
54
+
55
+ # Handle sync reply right here.
56
+ if observer.nil?
57
+ @active = false
58
+ loop do
59
+ reply = @reader.read_message
60
+ next if reply.nil?
61
+ if message.serial == reply.serial
62
+ @active = true
63
+ return reply.arguments
64
+ else
65
+ handle_incoming(reply)
66
+ Gtk.main_iteration_do(false)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+ def self.mainloop # :nodoc:
75
+ Gtk.main
76
+ end
77
+ end
@@ -0,0 +1,84 @@
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
+ require 'monitor'
25
+ require 'observer'
26
+
27
+ module RBus
28
+
29
+ class Mainloop
30
+ include Observable
31
+ include MonitorMixin
32
+
33
+ def initialize(transport)
34
+ super()
35
+ @transport = transport
36
+ @writer = Message::Writer.new(@transport)
37
+ @reader = Message::Reader.new(@transport)
38
+ setup
39
+ end
40
+
41
+ # Mainloop implementations should override this method to setup
42
+ # everything they need.
43
+ def setup
44
+ raise NotImplementedError, "No mainloop implemented"
45
+ end
46
+
47
+ # Mainloop implementations should call this method with
48
+ # any message received from the Bus.
49
+ # Handles +nil+ messages for non-blocking loops.
50
+ def handle_incoming(message)
51
+ return if message.nil?
52
+
53
+ # - empty NAMES is when Bus is connecting only
54
+ # - empty destination is for signals only
55
+ # - otherwise, NAMES must include the destination
56
+ return unless Bus::NAMES.empty? || message.destination.nil? || Bus::NAMES.include?(message.destination)
57
+
58
+ # The audience is listening... broadcast the new message.
59
+ changed
60
+ notify_observers(message)
61
+ end
62
+
63
+ # Mainloop implementations should call this method with
64
+ # any message to be sent to the Bus, and a callback, if available.
65
+ # Returns appropriate +observer+
66
+ def handle_outgoing(message)
67
+ # Make sure only one message is sent at each time.
68
+ synchronize do
69
+ @writer.send_message(message)
70
+ end
71
+ end
72
+
73
+ # Send message, tell loop to expect an answer. Overridden by
74
+ # implementations.
75
+ def send_message(message, callback = nil)
76
+ raise NotImplementedError, "No mainloop implemented"
77
+ end
78
+ end
79
+
80
+ # Start idle mainloop, overridden by different implementations.
81
+ def self.mainloop
82
+ raise NotImplementedError, "No mainloop implemented"
83
+ end
84
+ end