pangdudu-ruby-dbus 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +0,0 @@
1
- #!/bin/sh
2
- set -e
3
- # for the lazy typer
4
- ruby -w -I ../../lib gdbus
@@ -1,21 +0,0 @@
1
- #!/usr/bin/ruby
2
- #
3
- # Trivial network interface lister using NetworkManager.
4
- # NetworkManager does not support introspection, so the api is not that sexy.
5
-
6
- require 'dbus'
7
-
8
- bus = DBus::SystemBus.instance
9
-
10
- nm_service = bus.service("org.freedesktop.NetworkManager")
11
- nm_manager = nm_service.object("/org/freedesktop/NetworkManager")
12
- poi = DBus::ProxyObjectInterface.new(nm_manager, "org.freedesktop.NetworkManager")
13
- begin
14
- poi.define_method("getDevices", "") # NM 0.6
15
- p poi.getDevices
16
- rescue Exception
17
- poi.define_method("GetDevices", "") # NM 0.7
18
- p poi.GetDevices
19
- end
20
-
21
-
@@ -1,16 +0,0 @@
1
- #!/usr/bin/ruby
2
- #
3
- # Trivial network interface lister using NetworkManager.
4
- # NetworkManager does not support introspection, so the api is not that sexy.
5
-
6
- require 'dbus'
7
-
8
- bus = DBus::SessionBus.instance
9
-
10
- tracker_service = bus.service("org.freedesktop.Tracker")
11
- tracker_manager = tracker_service.object("/org/freedesktop/tracker")
12
- poi = DBus::ProxyObjectInterface.new(tracker_manager, "org.freedesktop.Tracker.Files")
13
- poi.define_method("GetMetadataForFilesInFolder", "in live_query_id:i, in uri:s, in fields:as, out values:aas")
14
- p poi.GetMetadataForFilesInFolder(-1, ENV['HOME'] + "/Desktop", ["File:Name", "File:Size"])
15
-
16
-
@@ -1,25 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'dbus'
4
- bus = DBus::SessionBus.instance
5
- # get a rb object
6
- proxy = bus.introspect("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Player")
7
- proxyi = proxy["org.gnome.Rhythmbox.Player"]
8
-
9
- # register for signals
10
-
11
- mr = DBus::MatchRule.new
12
- mr.type = "signal"
13
- mr.interface = "org.gnome.Rhythmbox.Player"
14
- mr.path = "/org/gnome/Rhythmbox/Player"
15
- bus.add_match(mr) do |msg, first_param|
16
- print msg.member + " "
17
- puts first_param
18
- end
19
-
20
- proxyi.playPause(true)
21
-
22
- main = DBus::Main.new
23
- main << bus
24
- main.run
25
-
@@ -1,25 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require "dbus"
4
-
5
- session_bus = DBus::SessionBus.instance
6
-
7
- ruby_srv = session_bus.service("org.ruby.service")
8
-
9
- # Get the object from this service
10
- player = ruby_srv.object("/org/ruby/MyInstance")
11
-
12
- # Introspect it
13
- puts player.introspect
14
- player.default_iface = "org.ruby.SampleInterface"
15
- player.test_variant(["s", "coucou"])
16
- player.on_signal("SomethingJustHappened") do |u, v|
17
- puts "SomethingJustHappened: #{u} #{v}"
18
- end
19
- player.hello("8=======D", "(_._)")
20
- p player["org.ruby.AnotherInterface"].Reverse("Hello world!")
21
-
22
- main = DBus::Main.new
23
- main << session_bus
24
- main.run
25
-
@@ -1,51 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'dbus'
4
- require 'thread'
5
- Thread.abort_on_exception = true
6
-
7
- class Test < DBus::Object
8
- # Create an interface aggregating all upcoming dbus_method defines.
9
- dbus_interface "org.ruby.SampleInterface" do
10
- dbus_method :hello, "in name:s, in name2:s" do |name, name2|
11
- puts "hello(#{name}, #{name2})"
12
- end
13
-
14
- dbus_method :test_variant, "in stuff:v" do |variant|
15
- p variant
16
- end
17
-
18
- dbus_signal :SomethingJustHappened, "toto:s, tutu:u"
19
- end
20
-
21
- dbus_interface "org.ruby.AnotherInterface" do
22
- dbus_method :ThatsALongMethodNameIThink do
23
- puts "ThatsALongMethodNameIThink"
24
- end
25
- dbus_method :Reverse, "in instr:s, out outstr:s" do |instr|
26
- outstr = instr.split(//).reverse.join
27
- puts "got: #{instr}, replying: #{outstr}"
28
- [outstr]
29
- end
30
- end
31
- end
32
-
33
- bus = DBus::SessionBus.instance
34
- service = bus.request_service("org.ruby.service")
35
- myobj = Test.new("/org/ruby/MyInstance")
36
- service.export(myobj)
37
-
38
- Thread.new do
39
- i = 0
40
- loop do
41
- # Signal emission
42
- myobj.SomethingJustHappened("hey", i += 1)
43
- sleep(0.5)
44
- end
45
- end
46
-
47
- puts "listening"
48
- main = DBus::Main.new
49
- main << bus
50
- main.run
51
-
@@ -1,34 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require "dbus"
4
-
5
- session_bus = DBus::SessionBus.instance
6
-
7
- # Get the Rhythmbox service
8
- rhythmbox = session_bus.service("org.gnome.Rhythmbox")
9
-
10
- # Get the object from this service
11
- player = rhythmbox.object("/org/gnome/Rhythmbox/Player")
12
-
13
- # Introspect it
14
- player.introspect
15
- if player.has_iface? "org.gnome.Rhythmbox.Player"
16
- puts "We have Rhythmbox Player interface"
17
- end
18
-
19
- player_with_iface = player["org.gnome.Rhythmbox.Player"]
20
- p player_with_iface.getPlayingUri
21
-
22
- # Maybe support default_iface=(iface_str) on an ProxyObject, so
23
- # that this is possible?
24
- player.default_iface = "org.gnome.Rhythmbox.Player"
25
- puts "default_iface test:"
26
- p player.getPlayingUri
27
- player.on_signal("elapsedChanged") do |u|
28
- puts "elapsedChanged: #{u}"
29
- end
30
-
31
- main = DBus::Main.new
32
- main << session_bus
33
- main.run
34
-
@@ -1,11 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'dbus'
4
-
5
- d = if ARGV.member?("--system")
6
- DBus::SystemBus.instance
7
- else
8
- DBus::SessionBus.instance
9
- end
10
- d.proxy.ListNames[0].each{ |n| puts "\t#{n}" }
11
-
@@ -1,19 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'dbus'
4
-
5
- if ARGV.size < 2
6
- puts "Usage:"
7
- puts "notify.rb \"title\" \"body\""
8
- exit
9
- end
10
-
11
- d = DBus::SessionBus.instance
12
- o = d.service("org.freedesktop.Notifications").object("/org/freedesktop/Notifications")
13
- o.introspect
14
-
15
- i = o["org.freedesktop.Notifications"]
16
-
17
- i.Notify('notify.rb', 0, 'info', ARGV[0], ARGV[1], [], {}, 2000) do |ret, param|
18
- end
19
-
@@ -1,468 +0,0 @@
1
- require 'dbus/bus'
2
- require 'dbus/auth'
3
-
4
- # = D-Bus main module
5
- #
6
- # Module containing all the D-Bus modules and classes.
7
- module DBus
8
- # D-Bus main connection class
9
- #
10
- # Main class that maintains a connection to a bus and can handle incoming
11
- # and outgoing messages.
12
- class Connection
13
- # The unique name (by specification) of the message.
14
- attr_reader :unique_name
15
- # The socket that is used to connect with the bus.
16
- attr_reader :socket
17
-
18
- # Create a new connection to the bus for a given connect _path_. _path_
19
- # format is described in the D-Bus specification:
20
- # http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
21
- # and is something like:
22
- # "transport1:key1=value1,key2=value2;transport2:key1=value1,key2=value2"
23
- # e.g. "unix:path=/tmp/dbus-test"
24
- #
25
- # Current implementation of ruby-dbus supports only a single server
26
- # address and only "unix:path=...,guid=..." and
27
- # "unix:abstract=...,guid=..." forms
28
- def initialize(path)
29
- @path = path
30
- @unique_name = nil
31
- @buffer = ""
32
- @method_call_replies = Hash.new
33
- @method_call_msgs = Hash.new
34
- @signal_matchrules = Array.new
35
- @proxy = nil
36
- # FIXME: can be TCP or any stream, let's do it.
37
- @socket = Socket.new(Socket::Constants::PF_UNIX,Socket::Constants::SOCK_STREAM, 0)
38
- @object_root = Node.new("/")
39
- end
40
-
41
- # Connect to the bus and initialize the connection.
42
- def connect
43
- connect_to_unix_abstract
44
- end
45
-
46
- # Connect to the bus and initialize the connection.
47
- def connect_to_unix_abstract
48
- parse_session_string
49
- if @transport == "unix" and @type == "abstract"
50
- if HOST_END == LIL_END
51
- sockaddr = "\1\0\0#{@unix_abstract}"
52
- else
53
- sockaddr = "\0\1\0#{@unix_abstract}"
54
- end
55
- elsif @transport == "unix" and @type == "path"
56
- sockaddr = Socket.pack_sockaddr_un(@unix)
57
- end
58
- @socket.connect(sockaddr)
59
- init_connection
60
- end
61
-
62
- # Send the buffer _buf_ to the bus using Connection#writel.
63
- def send(buf)
64
- @socket.write(buf)
65
- end
66
-
67
- # Tell a bus to register itself on the glib main loop
68
- def glibize
69
- puts "dbus/bus.rb-221: Entered glibize."
70
- require 'glib2'
71
- # Circumvent a ruby-glib bug
72
- @channels ||= Array.new
73
-
74
- gio = GLib::IOChannel.new(@socket.fileno)
75
- @channels << gio
76
- gio.add_watch(GLib::IOChannel::IN) do |c, ch|
77
- update_buffer
78
- messages.each do |msg|
79
- process(msg)
80
- end
81
- true
82
- end
83
- end
84
-
85
- # FIXME: describe the following names, flags and constants.
86
- # See DBus spec for definition
87
- NAME_FLAG_ALLOW_REPLACEMENT = 0x1
88
- NAME_FLAG_REPLACE_EXISTING = 0x2
89
- NAME_FLAG_DO_NOT_QUEUE = 0x4
90
-
91
- REQUEST_NAME_REPLY_PRIMARY_OWNER = 0x1
92
- REQUEST_NAME_REPLY_IN_QUEUE = 0x2
93
- REQUEST_NAME_REPLY_EXISTS = 0x3
94
- REQUEST_NAME_REPLY_ALREADY_OWNER = 0x4
95
-
96
- DBUSXMLINTRO = '<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
97
- "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
98
- <node>
99
- <interface name="org.freedesktop.DBus.Introspectable">
100
- <method name="Introspect">
101
- <arg name="data" direction="out" type="s"/>
102
- </method>
103
- </interface>
104
- <interface name="org.freedesktop.DBus">
105
- <method name="RequestName">
106
- <arg direction="in" type="s"/>
107
- <arg direction="in" type="u"/>
108
- <arg direction="out" type="u"/>
109
- </method>
110
- <method name="ReleaseName">
111
- <arg direction="in" type="s"/>
112
- <arg direction="out" type="u"/>
113
- </method>
114
- <method name="StartServiceByName">
115
- <arg direction="in" type="s"/>
116
- <arg direction="in" type="u"/>
117
- <arg direction="out" type="u"/>
118
- </method>
119
- <method name="Hello">
120
- <arg direction="out" type="s"/>
121
- </method>
122
- <method name="NameHasOwner">
123
- <arg direction="in" type="s"/>
124
- <arg direction="out" type="b"/>
125
- </method>
126
- <method name="ListNames">
127
- <arg direction="out" type="as"/>
128
- </method>
129
- <method name="ListActivatableNames">
130
- <arg direction="out" type="as"/>
131
- </method>
132
- <method name="AddMatch">
133
- <arg direction="in" type="s"/>
134
- </method>
135
- <method name="RemoveMatch">
136
- <arg direction="in" type="s"/>
137
- </method>
138
- <method name="GetNameOwner">
139
- <arg direction="in" type="s"/>
140
- <arg direction="out" type="s"/>
141
- </method>
142
- <method name="ListQueuedOwners">
143
- <arg direction="in" type="s"/>
144
- <arg direction="out" type="as"/>
145
- </method>
146
- <method name="GetConnectionUnixUser">
147
- <arg direction="in" type="s"/>
148
- <arg direction="out" type="u"/>
149
- </method>
150
- <method name="GetConnectionUnixProcessID">
151
- <arg direction="in" type="s"/>
152
- <arg direction="out" type="u"/>
153
- </method>
154
- <method name="GetConnectionSELinuxSecurityContext">
155
- <arg direction="in" type="s"/>
156
- <arg direction="out" type="ay"/>
157
- </method>
158
- <method name="ReloadConfig">
159
- </method>
160
- <signal name="NameOwnerChanged">
161
- <arg type="s"/>
162
- <arg type="s"/>
163
- <arg type="s"/>
164
- </signal>
165
- <signal name="NameLost">
166
- <arg type="s"/>
167
- </signal>
168
- <signal name="NameAcquired">
169
- <arg type="s"/>
170
- </signal>
171
- </interface>
172
- </node>
173
- '
174
-
175
- def introspect_data(dest, path)
176
- m = DBus::Message.new(DBus::Message::METHOD_CALL)
177
- m.path = path
178
- m.interface = "org.freedesktop.DBus.Introspectable"
179
- m.destination = dest
180
- m.member = "Introspect"
181
- m.sender = unique_name
182
- if not block_given?
183
- # introspect in synchronous !
184
- send_sync(m) do |rmsg|
185
- if rmsg.is_a?(Error)
186
- raise rmsg
187
- else
188
- return rmsg.params[0]
189
- end
190
- end
191
- else
192
- send(m.marshall)
193
- on_return(m) do |rmsg|
194
- if rmsg.is_a?(Error)
195
- yield rmsg
196
- else
197
- yield rmsg.params[0]
198
- end
199
- end
200
- end
201
- nil
202
- end
203
-
204
- # Issues a call to the org.freedesktop.DBus.Introspectable.Introspect method
205
- # _dest_ is the service and _path_ the object path you want to introspect
206
- # If a code block is given, the introspect call in asynchronous. If not
207
- # data is returned
208
- #
209
- # FIXME: link to ProxyObject data definition
210
- # The returned object is a ProxyObject that has methods you can call to
211
- # issue somme METHOD_CALL messages, and to setup to receive METHOD_RETURN
212
- def introspect(dest, path)
213
- if not block_given?
214
- # introspect in synchronous !
215
- data = introspect_data(dest, path)
216
- pof = DBus::ProxyObjectFactory.new(data, self, dest, path)
217
- return pof.build
218
- else
219
- introspect_data(dest, path) do |data|
220
- yield(DBus::ProxyObjectFactory.new(data, self, dest, path).build)
221
- end
222
- end
223
- end
224
-
225
- # Exception raised when a service name is requested that is not available.
226
- class NameRequestError < Exception
227
- end
228
-
229
- # Attempt to request a service _name_.
230
- def request_service(name)
231
- r = proxy.RequestName(name, NAME_FLAG_REPLACE_EXISTING)
232
- raise NameRequestError if r[0] != REQUEST_NAME_REPLY_PRIMARY_OWNER
233
- @service = Service.new(name, self)
234
- @service
235
- end
236
-
237
- # Set up a ProxyObject for the bus itself, since the bus is introspectable.
238
- # Returns the object.
239
- def proxy
240
- if @proxy == nil
241
- path = "/org/freedesktop/DBus"
242
- dest = "org.freedesktop.DBus"
243
- pof = DBus::ProxyObjectFactory.new(DBUSXMLINTRO, self, dest, path)
244
- @proxy = pof.build["org.freedesktop.DBus"]
245
- end
246
- @proxy
247
- end
248
-
249
- # Fill (append) the buffer from data that might be available on the
250
- # socket.
251
- def update_buffer
252
- @buffer += @socket.read_nonblock(MSG_BUF_SIZE)
253
- end
254
-
255
- # Get one message from the bus and remove it from the buffer.
256
- # Return the message.
257
- def pop_message
258
- ret = nil
259
- begin
260
- ret, size = Message.new.unmarshall_buffer(@buffer)
261
- @buffer.slice!(0, size)
262
- rescue IncompleteBufferException => e
263
- # fall through, let ret be null
264
- end
265
- ret
266
- end
267
-
268
- # Retrieve all the messages that are currently in the buffer.
269
- def messages
270
- ret = Array.new
271
- while msg = pop_message
272
- ret << msg
273
- end
274
- ret
275
- end
276
-
277
- # The buffer size for messages.
278
- MSG_BUF_SIZE = 4096
279
-
280
- # Update the buffer and retrieve all messages using Connection#messages.
281
- # Return the messages.
282
- def poll_messages
283
- ret = nil
284
- r, d, d = IO.select([@socket], nil, nil, 0)
285
- if r and r.size > 0
286
- update_buffer
287
- end
288
- messages
289
- end
290
-
291
- # Wait for a message to arrive. Return it once it is available.
292
- def wait_for_message
293
- ret = pop_message
294
- while ret == nil
295
- r, d, d = IO.select([@socket])
296
- if r and r[0] == @socket
297
- update_buffer
298
- ret = pop_message
299
- end
300
- end
301
- ret
302
- end
303
-
304
- # Send a message _m_ on to the bus. This is done synchronously, thus
305
- # the call will block until a reply message arrives.
306
- def send_sync(m, &retc) # :yields: reply/return message
307
- send(m.marshall)
308
- @method_call_msgs[m.serial] = m
309
- @method_call_replies[m.serial] = retc
310
-
311
- retm = wait_for_message
312
- process(retm)
313
- until [DBus::Message::ERROR,
314
- DBus::Message::METHOD_RETURN].include?(retm.message_type) and
315
- retm.reply_serial == m.serial
316
- retm = wait_for_message
317
- process(retm)
318
- end
319
- end
320
-
321
- # Specify a code block that has to be executed when a reply for
322
- # message _m_ is received.
323
- def on_return(m, &retc)
324
- # Have a better exception here
325
- if m.message_type != Message::METHOD_CALL
326
- raise "on_return should only get method_calls"
327
- end
328
- @method_call_msgs[m.serial] = m
329
- @method_call_replies[m.serial] = retc
330
- end
331
-
332
- # Asks bus to send us messages matching mr, and execute slot when
333
- # received
334
- def add_match(mr, &slot)
335
- # check this is a signal.
336
- @signal_matchrules << [mr, slot]
337
- self.proxy.AddMatch(mr.to_s)
338
- end
339
-
340
- # Process a message _m_ based on its type.
341
- # method call:: FIXME...
342
- # method call return value:: FIXME...
343
- # signal:: FIXME...
344
- # error:: FIXME...
345
- def process(m)
346
- case m.message_type
347
- when Message::ERROR, Message::METHOD_RETURN
348
- raise InvalidPacketException if m.reply_serial == nil
349
- mcs = @method_call_replies[m.reply_serial]
350
- if not mcs
351
- puts "no return code for #{mcs.inspect} (#{m.inspect})" if $DEBUG
352
- else
353
- if m.message_type == Message::ERROR
354
- mcs.call(Error.new(m))
355
- else
356
- mcs.call(m)
357
- end
358
- @method_call_replies.delete(m.reply_serial)
359
- @method_call_msgs.delete(m.reply_serial)
360
- end
361
- when DBus::Message::METHOD_CALL
362
- if m.path == "/org/freedesktop/DBus"
363
- puts "Got method call on /org/freedesktop/DBus" if $DEBUG
364
- end
365
- # handle introspectable as an exception:
366
- if m.interface == "org.freedesktop.DBus.Introspectable" and
367
- m.member == "Introspect"
368
- reply = Message.new(Message::METHOD_RETURN).reply_to(m)
369
- reply.sender = @unique_name
370
- node = @service.get_node(m.path)
371
- raise NotImplementedError if not node
372
- reply.sender = @unique_name
373
- reply.add_param(Type::STRING, @service.get_node(m.path).to_xml)
374
- send(reply.marshall)
375
- else
376
- node = @service.get_node(m.path)
377
- return if node.nil?
378
- obj = node.object
379
- return if obj.nil?
380
- obj.dispatch(m) if obj
381
- end
382
- when DBus::Message::SIGNAL
383
- @signal_matchrules.each do |elem|
384
- mr, slot = elem
385
- if mr.match(m)
386
- slot.call(m)
387
- return
388
- end
389
- end
390
- else
391
- puts "Unknown message type: #{m.message_type}" if $DEBUG
392
- end
393
- end
394
-
395
- # Retrieves the service with the given _name_.
396
- def service(name)
397
- # The service might not exist at this time so we cannot really check
398
- # anything
399
- Service.new(name, self)
400
- end
401
- alias :[] :service
402
-
403
- # Emit a signal event for the given _service_, object _obj_, interface
404
- # _intf_ and signal _sig_ with arguments _args_.
405
- def emit(service, obj, intf, sig, *args)
406
- m = Message.new(DBus::Message::SIGNAL)
407
- m.path = obj.path
408
- m.interface = intf.name
409
- m.member = sig.name
410
- m.sender = service.name
411
- i = 0
412
- sig.params.each do |par|
413
- m.add_param(par[1], args[i])
414
- i += 1
415
- end
416
- send(m.marshall)
417
- end
418
-
419
- ###########################################################################
420
- private
421
-
422
- # Send a hello messages to the bus to let it know we are here.
423
- def send_hello
424
- m = Message.new(DBus::Message::METHOD_CALL)
425
- m.path = "/org/freedesktop/DBus"
426
- m.destination = "org.freedesktop.DBus"
427
- m.interface = "org.freedesktop.DBus"
428
- m.member = "Hello"
429
- send_sync(m) do |rmsg|
430
- @unique_name = rmsg.destination
431
- puts "Got hello reply. Our unique_name is #{@unique_name}" if $DEBUG
432
- end
433
- end
434
-
435
- # Parse the session string (socket address).
436
- def parse_session_string
437
- path_parsed = /^([^:]*):([^;]*)$/.match(@path)
438
- @transport = path_parsed[1]
439
- adr = path_parsed[2]
440
- if @transport == "unix"
441
- adr.split(",").each do |eqstr|
442
- idx, val = eqstr.split("=")
443
- case idx
444
- when "path"
445
- @type = idx
446
- @unix = val
447
- when "abstract"
448
- @type = idx
449
- @unix_abstract = val
450
- when "guid"
451
- @guid = val
452
- end
453
- end
454
- end
455
- end
456
-
457
- # Initialize the connection to the bus.
458
- def init_connection
459
- @client = Client.new(@socket)
460
- @client.authenticate
461
- # TODO: code some real stuff here
462
- #writel("AUTH EXTERNAL 31303030")
463
- #s = readl
464
- # parse OK ?
465
- #writel("BEGIN")
466
- end
467
- end # class Connection
468
- end # module DBus