ruby-dbus 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/COPYING +504 -0
  2. data/NEWS +137 -0
  3. data/README +53 -0
  4. data/Rakefile +54 -0
  5. data/VERSION +1 -0
  6. data/doc/tutorial/index.html +356 -0
  7. data/doc/tutorial/index.markdown +467 -0
  8. data/examples/gdbus/gdbus +255 -0
  9. data/examples/gdbus/gdbus.glade +184 -0
  10. data/examples/gdbus/launch.sh +4 -0
  11. data/examples/no-introspect/nm-test.rb +21 -0
  12. data/examples/no-introspect/tracker-test.rb +16 -0
  13. data/examples/rhythmbox/playpause.rb +25 -0
  14. data/examples/service/call_service.rb +25 -0
  15. data/examples/service/service_newapi.rb +51 -0
  16. data/examples/simple/call_introspect.rb +34 -0
  17. data/examples/utils/listnames.rb +11 -0
  18. data/examples/utils/notify.rb +19 -0
  19. data/lib/dbus.rb +91 -0
  20. data/lib/dbus/auth.rb +258 -0
  21. data/lib/dbus/bus.rb +816 -0
  22. data/lib/dbus/core_ext/class/attribute.rb +91 -0
  23. data/lib/dbus/core_ext/kernel/singleton_class.rb +14 -0
  24. data/lib/dbus/core_ext/module/remove_method.rb +12 -0
  25. data/lib/dbus/error.rb +44 -0
  26. data/lib/dbus/export.rb +132 -0
  27. data/lib/dbus/introspect.rb +553 -0
  28. data/lib/dbus/marshall.rb +443 -0
  29. data/lib/dbus/matchrule.rb +100 -0
  30. data/lib/dbus/message.rb +310 -0
  31. data/lib/dbus/type.rb +222 -0
  32. data/ruby-dbus.gemspec +18 -0
  33. data/test/binding_test.rb +56 -0
  34. data/test/bus_driver_test.rb +22 -0
  35. data/test/dbus-launch-simple +35 -0
  36. data/test/dbus-limited-session.conf +28 -0
  37. data/test/server_robustness_test.rb +41 -0
  38. data/test/server_test.rb +53 -0
  39. data/test/service_newapi.rb +129 -0
  40. data/test/session_bus_test_manual.rb +20 -0
  41. data/test/signal_test.rb +64 -0
  42. data/test/t1 +4 -0
  43. data/test/t2.rb +66 -0
  44. data/test/t3-ticket27.rb +18 -0
  45. data/test/t5-report-dbus-interface.rb +58 -0
  46. data/test/t6-loop.rb +82 -0
  47. data/test/test_env +13 -0
  48. data/test/test_server +39 -0
  49. data/test/variant_test.rb +66 -0
  50. metadata +117 -0
@@ -0,0 +1,310 @@
1
+ # dbus.rb - Module containing the low-level D-Bus implementation
2
+ #
3
+ # This file is part of the ruby-dbus project
4
+ # Copyright (C) 2007 Arnaud Cornet and Paul van Tilburg
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License, version 2.1 as published by the Free Software Foundation.
9
+ # See the file "COPYING" for the exact licensing terms.
10
+
11
+ # = D-Bus main module
12
+ #
13
+ # Module containing all the D-Bus modules and classes.
14
+ module DBus
15
+ # = InvalidDestinationName class
16
+ # Thrown when you try to send a message to /org/freedesktop/DBus/Local, that
17
+ # is reserved.
18
+ class InvalidDestinationName < Exception
19
+ end
20
+
21
+ # = D-Bus message class
22
+ #
23
+ # Class that holds any type of message that travels over the bus.
24
+ class Message
25
+ # The serial number of the message.
26
+ @@serial = 1
27
+ # Mutex that protects updates on the serial number.
28
+ @@serial_mutex = Mutex.new
29
+ # Type of a message (by specification).
30
+ MESSAGE_SIGNATURE = "yyyyuua(yv)"
31
+
32
+ # FIXME: following message type constants should be under Message::Type IMO
33
+ # well, yeah sure
34
+ #
35
+ # Invalid message type.
36
+ INVALID = 0
37
+ # Method call message type.
38
+ METHOD_CALL = 1
39
+ # Method call return value message type.
40
+ METHOD_RETURN = 2
41
+ # Error message type.
42
+ ERROR = 3
43
+ # Signal message type.
44
+ SIGNAL = 4
45
+
46
+ # Message flag signyfing that no reply is expected.
47
+ NO_REPLY_EXPECTED = 0x1
48
+ # Message flag signifying that no automatic start is required/must be
49
+ # performed.
50
+ NO_AUTO_START = 0x2
51
+
52
+ # The type of the message.
53
+ attr_reader :message_type
54
+ # The path of the object instance the message must be sent to/is sent from.
55
+ attr_accessor :path
56
+ # The interface of the object that must be used/was used.
57
+ attr_accessor :interface
58
+ # The interface member (method/signal name) of the object that must be
59
+ # used/was used.
60
+ attr_accessor :member
61
+ # The name of the error (in case of an error message type).
62
+ attr_accessor :error_name
63
+ # The destination connection of the object that must be used/was used.
64
+ attr_accessor :destination
65
+ # The sender of the message.
66
+ attr_accessor :sender
67
+ # The signature of the message contents.
68
+ attr_accessor :signature
69
+ # The serial number of the message this message is a reply for.
70
+ attr_accessor :reply_serial
71
+ # The protocol.
72
+ attr_reader :protocol
73
+ # The serial of the message.
74
+ attr_reader :serial
75
+ # The parameters of the message.
76
+ attr_reader :params
77
+
78
+ # Create a message with message type _mtype_ with default values and a
79
+ # unique serial number.
80
+ def initialize(mtype = INVALID)
81
+ @message_type = mtype
82
+
83
+ @flags = 0
84
+ @protocol = 1
85
+ @body_length = 0
86
+ @signature = String.new
87
+ @@serial_mutex.synchronize do
88
+ @serial = @@serial
89
+ @@serial += 1
90
+ end
91
+ @params = Array.new
92
+ @destination = nil
93
+ @interface = nil
94
+ @error_name = nil
95
+ @member = nil
96
+ @path = nil
97
+ @reply_serial = nil
98
+
99
+ if mtype == METHOD_RETURN
100
+ @flags = NO_REPLY_EXPECTED
101
+ end
102
+ end
103
+
104
+ # Create a regular reply to a message _m_.
105
+ def self.method_return(m)
106
+ MethodReturnMessage.new.reply_to(m)
107
+ end
108
+
109
+ # Create an error reply to a message _m_.
110
+ def self.error(m, error_name, description=nil)
111
+ ErrorMessage.new(error_name, description).reply_to(m)
112
+ end
113
+
114
+ # Mark this message as a reply to a another message _m_, taking
115
+ # the serial number of _m_ as reply serial and the sender of _m_ as
116
+ # destination.
117
+ def reply_to(m)
118
+ @reply_serial = m.serial
119
+ @destination = m.sender
120
+ self
121
+ end
122
+
123
+ # Add a parameter _val_ of type _type_ to the message.
124
+ def add_param(type, val)
125
+ type = type.chr if type.kind_of?(Fixnum)
126
+ @signature += type.to_s
127
+ @params << [type, val]
128
+ end
129
+
130
+ # FIXME: what are these? a message element constant enumeration?
131
+ # See method below, in a message, you have and array of optional parameters
132
+ # that come with an index, to determine their meaning. The values are in
133
+ # spec, more a definition than an enumeration.
134
+
135
+ PATH = 1
136
+ INTERFACE = 2
137
+ MEMBER = 3
138
+ ERROR_NAME = 4
139
+ REPLY_SERIAL = 5
140
+ DESTINATION = 6
141
+ SENDER = 7
142
+ SIGNATURE = 8
143
+
144
+ # Marshall the message with its current set parameters and return
145
+ # it in a packet form.
146
+ def marshall
147
+ if @path == "/org/freedesktop/DBus/Local"
148
+ raise InvalidDestinationName
149
+ end
150
+
151
+ params = PacketMarshaller.new
152
+ @params.each do |param|
153
+ params.append(param[0], param[1])
154
+ end
155
+ @body_length = params.packet.length
156
+
157
+ marshaller = PacketMarshaller.new
158
+ marshaller.append(Type::BYTE, HOST_END)
159
+ marshaller.append(Type::BYTE, @message_type)
160
+ marshaller.append(Type::BYTE, @flags)
161
+ marshaller.append(Type::BYTE, @protocol)
162
+ marshaller.append(Type::UINT32, @body_length)
163
+ marshaller.append(Type::UINT32, @serial)
164
+ marshaller.array(Type::Parser.new("y").parse[0]) do
165
+ if @path
166
+ marshaller.struct do
167
+ marshaller.append(Type::BYTE, PATH)
168
+ marshaller.append(Type::BYTE, 1)
169
+ marshaller.append_simple_string("o")
170
+ marshaller.append(Type::OBJECT_PATH, @path)
171
+ end
172
+ end
173
+ if @interface
174
+ marshaller.struct do
175
+ marshaller.append(Type::BYTE, INTERFACE)
176
+ marshaller.append(Type::BYTE, 1)
177
+ marshaller.append_simple_string("s")
178
+ marshaller.append(Type::STRING, @interface)
179
+ end
180
+ end
181
+ if @member
182
+ marshaller.struct do
183
+ marshaller.append(Type::BYTE, MEMBER)
184
+ marshaller.append(Type::BYTE, 1)
185
+ marshaller.append_simple_string("s")
186
+ marshaller.append(Type::STRING, @member)
187
+ end
188
+ end
189
+ if @error_name
190
+ marshaller.struct do
191
+ marshaller.append(Type::BYTE, ERROR_NAME)
192
+ marshaller.append(Type::BYTE, 1)
193
+ marshaller.append_simple_string("s")
194
+ marshaller.append(Type::STRING, @error_name)
195
+ end
196
+ end
197
+ if @reply_serial
198
+ marshaller.struct do
199
+ marshaller.append(Type::BYTE, REPLY_SERIAL)
200
+ marshaller.append(Type::BYTE, 1)
201
+ marshaller.append_simple_string("u")
202
+ marshaller.append(Type::UINT32, @reply_serial)
203
+ end
204
+ end
205
+ if @destination
206
+ marshaller.struct do
207
+ marshaller.append(Type::BYTE, DESTINATION)
208
+ marshaller.append(Type::BYTE, 1)
209
+ marshaller.append_simple_string("s")
210
+ marshaller.append(Type::STRING, @destination)
211
+ end
212
+ end
213
+ if @signature != ""
214
+ marshaller.struct do
215
+ marshaller.append(Type::BYTE, SIGNATURE)
216
+ marshaller.append(Type::BYTE, 1)
217
+ marshaller.append_simple_string("g")
218
+ marshaller.append(Type::SIGNATURE, @signature)
219
+ end
220
+ end
221
+ end
222
+ marshaller.align(8)
223
+ @params.each do |param|
224
+ marshaller.append(param[0], param[1])
225
+ end
226
+ marshaller.packet
227
+ end
228
+
229
+ # Unmarshall a packet contained in the buffer _buf_ and set the
230
+ # parameters of the message object according the data found in the
231
+ # buffer.
232
+ # Return the detected message and the index pointer of the buffer where
233
+ # the message data ended.
234
+ def unmarshall_buffer(buf)
235
+ buf = buf.dup
236
+ if buf[0] == ?l
237
+ endianness = LIL_END
238
+ else
239
+ endianness = BIG_END
240
+ end
241
+ pu = PacketUnmarshaller.new(buf, endianness)
242
+ mdata = pu.unmarshall(MESSAGE_SIGNATURE)
243
+ dummy, @message_type, @flags, @protocol, @body_length, @serial,
244
+ headers = mdata
245
+
246
+ headers.each do |struct|
247
+ case struct[0]
248
+ when PATH
249
+ @path = struct[1]
250
+ when INTERFACE
251
+ @interface = struct[1]
252
+ when MEMBER
253
+ @member = struct[1]
254
+ when ERROR_NAME
255
+ @error_name = struct[1]
256
+ when REPLY_SERIAL
257
+ @reply_serial = struct[1]
258
+ when DESTINATION
259
+ @destination = struct[1]
260
+ when SENDER
261
+ @sender = struct[1]
262
+ when SIGNATURE
263
+ @signature = struct[1]
264
+ end
265
+ end
266
+ pu.align(8)
267
+ if @body_length > 0 and @signature
268
+ @params = pu.unmarshall(@signature, @body_length)
269
+ end
270
+ [self, pu.idx]
271
+ end # def unmarshall_buf
272
+
273
+ # Unmarshall the data of a message found in the buffer _buf_ using
274
+ # Message#unmarshall_buf.
275
+ # Return the message.
276
+ def unmarshall(buf)
277
+ ret, size = unmarshall_buffer(buf)
278
+ ret
279
+ end
280
+ end # class Message
281
+
282
+ class MethodReturnMessage < Message
283
+ def initialize
284
+ super(METHOD_RETURN)
285
+ end
286
+ end
287
+
288
+ class ErrorMessage < Message
289
+ def initialize(error_name, description=nil)
290
+ super(ERROR)
291
+ @error_name = error_name
292
+ unless description.nil?
293
+ add_param(Type::STRING, description)
294
+ end
295
+ end
296
+
297
+ def self.from_exception(ex)
298
+ name = if ex.respond_to? :name
299
+ ex.name
300
+ else
301
+ "org.freedesktop.DBus.Error.Failed"
302
+ # ex.class.to_s # RuntimeError is not a valid name, has no dot
303
+ end
304
+ description = ex.message
305
+ msg = self.new(name, description)
306
+ msg.add_param(DBus.type("as"), ex.backtrace)
307
+ msg
308
+ end
309
+ end
310
+ end # module DBus
@@ -0,0 +1,222 @@
1
+ # dbus/type.rb - module containing low-level D-Bus data type information
2
+ #
3
+ # This file is part of the ruby-dbus project
4
+ # Copyright (C) 2007 Arnaud Cornet and Paul van Tilburg
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License, version 2.1 as published by the Free Software Foundation.
9
+ # See the file "COPYING" for the exact licensing terms.
10
+
11
+ module DBus
12
+
13
+ # = D-Bus type module
14
+ #
15
+ # This module containts the constants of the types specified in the D-Bus
16
+ # protocol.
17
+ module Type
18
+ # The types.
19
+ INVALID = 0
20
+ BYTE = ?y
21
+ BOOLEAN = ?b
22
+ INT16 = ?n
23
+ UINT16 = ?q
24
+ INT32 = ?i
25
+ UINT32 = ?u
26
+ INT64 = ?x
27
+ UINT64 = ?t
28
+ DOUBLE = ?d
29
+ STRUCT = ?r
30
+ ARRAY = ?a
31
+ VARIANT = ?v
32
+ OBJECT_PATH = ?o
33
+ STRING = ?s
34
+ SIGNATURE = ?g
35
+ DICT_ENTRY = ?e
36
+
37
+ # Mapping from type number to name.
38
+ TypeName = {
39
+ INVALID => "INVALID",
40
+ BYTE => "BYTE",
41
+ BOOLEAN => "BOOLEAN",
42
+ INT16 => "INT16",
43
+ UINT16 => "UINT16",
44
+ INT32 => "INT32",
45
+ UINT32 => "UINT32",
46
+ INT64 => "INT64",
47
+ UINT64 => "UINT64",
48
+ DOUBLE => "DOUBLE",
49
+ STRUCT => "STRUCT",
50
+ ARRAY => "ARRAY",
51
+ VARIANT => "VARIANT",
52
+ OBJECT_PATH => "OBJECT_PATH",
53
+ STRING => "STRING",
54
+ SIGNATURE => "SIGNATURE",
55
+ DICT_ENTRY => "DICT_ENTRY"
56
+ }
57
+
58
+ # Exception raised when an unknown/incorrect type is encountered.
59
+ class SignatureException < Exception
60
+ end
61
+
62
+ # = D-Bus type conversion class
63
+ #
64
+ # Helper class for representing a D-Bus type.
65
+ class Type
66
+ # Returns the signature type number.
67
+ attr_reader :sigtype
68
+ # Return contained member types.
69
+ attr_reader :members
70
+
71
+ # Create a new type instance for type number _sigtype_.
72
+ def initialize(sigtype)
73
+ if not TypeName.keys.member?(sigtype)
74
+ raise SignatureException, "Unknown key in signature: #{sigtype.chr}"
75
+ end
76
+ @sigtype = sigtype
77
+ @members = Array.new
78
+ end
79
+
80
+ # Return the required alignment for the type.
81
+ def alignment
82
+ {
83
+ BYTE => 1,
84
+ BOOLEAN => 4,
85
+ INT16 => 2,
86
+ UINT16 => 2,
87
+ INT32 => 4,
88
+ UINT32 => 4,
89
+ INT64 => 8,
90
+ UINT64 => 8,
91
+ STRUCT => 8,
92
+ DICT_ENTRY => 8,
93
+ DOUBLE => 8,
94
+ ARRAY => 4,
95
+ VARIANT => 1,
96
+ OBJECT_PATH => 4,
97
+ STRING => 4,
98
+ SIGNATURE => 1,
99
+ }[@sigtype]
100
+ end
101
+
102
+ # Return a string representation of the type according to the
103
+ # D-Bus specification.
104
+ def to_s
105
+ case @sigtype
106
+ when STRUCT
107
+ "(" + @members.collect { |t| t.to_s }.join + ")"
108
+ when ARRAY
109
+ "a" + child.to_s
110
+ when DICT_ENTRY
111
+ "{" + @members.collect { |t| t.to_s }.join + "}"
112
+ else
113
+ if not TypeName.keys.member?(@sigtype)
114
+ raise NotImplementedError
115
+ end
116
+ @sigtype.chr
117
+ end
118
+ end
119
+
120
+ # Add a new member type _a_.
121
+ def <<(a)
122
+ if not [STRUCT, ARRAY, DICT_ENTRY].member?(@sigtype)
123
+ raise SignatureException
124
+ end
125
+ raise SignatureException if @sigtype == ARRAY and @members.size > 0
126
+ if @sigtype == DICT_ENTRY
127
+ if @members.size == 2
128
+ raise SignatureException, "Dict entries have exactly two members"
129
+ end
130
+ if @members.size == 0
131
+ if [STRUCT, ARRAY, DICT_ENTRY].member?(a.sigtype)
132
+ raise SignatureException, "Dict entry keys must be basic types"
133
+ end
134
+ end
135
+ end
136
+ @members << a
137
+ end
138
+
139
+ # Return the first contained member type.
140
+ def child
141
+ @members[0]
142
+ end
143
+
144
+ def inspect
145
+ s = TypeName[@sigtype]
146
+ if [STRUCT, ARRAY].member?(@sigtype)
147
+ s += ": " + @members.inspect
148
+ end
149
+ s
150
+ end
151
+ end # class Type
152
+
153
+ # = D-Bus type parser class
154
+ #
155
+ # Helper class to parse a type signature in the protocol.
156
+ class Parser
157
+ # Create a new parser for the given _signature_.
158
+ def initialize(signature)
159
+ @signature = signature
160
+ @idx = 0
161
+ end
162
+
163
+ # Returns the next character from the signature.
164
+ def nextchar
165
+ c = @signature[@idx]
166
+ @idx += 1
167
+ c
168
+ end
169
+
170
+ # Parse one character _c_ of the signature.
171
+ def parse_one(c)
172
+ res = nil
173
+ case c
174
+ when ?a
175
+ res = Type.new(ARRAY)
176
+ child = parse_one(nextchar)
177
+ res << child
178
+ when ?(
179
+ res = Type.new(STRUCT)
180
+ while (c = nextchar) != nil and c != ?)
181
+ res << parse_one(c)
182
+ end
183
+ raise SignatureException, "Parse error in #{@signature}" if c == nil
184
+ when ?{
185
+ res = Type.new(DICT_ENTRY)
186
+ while (c = nextchar) != nil and c != ?}
187
+ res << parse_one(c)
188
+ end
189
+ raise SignatureException, "Parse error in #{@signature}" if c == nil
190
+ else
191
+ res = Type.new(c)
192
+ end
193
+ res
194
+ end
195
+
196
+ # Parse the entire signature, return a DBus::Type object.
197
+ def parse
198
+ @idx = 0
199
+ ret = Array.new
200
+ while (c = nextchar)
201
+ ret << parse_one(c)
202
+ end
203
+ ret
204
+ end
205
+ end # class Parser
206
+ end # module Type
207
+
208
+ # shortcuts
209
+
210
+ # Parse a String to a DBus::Type::Type
211
+ def type(string_type)
212
+ Type::Parser.new(string_type).parse[0]
213
+ end
214
+ module_function :type
215
+
216
+ # Make an explicit [Type, value] pair
217
+ def variant(string_type, value)
218
+ [type(string_type), value]
219
+ end
220
+ module_function :variant
221
+
222
+ end # module DBus