yong-ruby-dbus 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ set -e
3
+ # for the lazy typer
4
+ ruby -w -I ../../lib gdbus
@@ -0,0 +1,16 @@
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
+ poi.define_method("getDevices", "")
14
+ p poi.getDevices
15
+
16
+
@@ -0,0 +1,16 @@
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
+
@@ -0,0 +1,25 @@
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
+
@@ -0,0 +1,25 @@
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
+
@@ -0,0 +1,51 @@
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
+
@@ -0,0 +1,34 @@
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
+
@@ -0,0 +1,11 @@
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
+
@@ -0,0 +1,19 @@
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
+
data/lib/dbus.rb ADDED
@@ -0,0 +1,82 @@
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
+ require 'dbus/type'
12
+ require 'dbus/introspect'
13
+ require 'dbus/export'
14
+ require 'dbus/bus.rb'
15
+ require 'dbus/marshall'
16
+ require 'dbus/message'
17
+ require 'dbus/matchrule'
18
+ require 'dbus/auth'
19
+
20
+ require 'socket'
21
+ require 'thread'
22
+
23
+ # = D-Bus main module
24
+ #
25
+ # Module containing all the D-Bus modules and classes.
26
+ module DBus
27
+ # Default socket name for the system bus.
28
+ SystemSocketName = "unix:path=/var/run/dbus/system_bus_socket"
29
+
30
+ # Byte signifying big endianness.
31
+ BIG_END = ?B
32
+ # Byte signifying little endianness.
33
+ LIL_END = ?l
34
+
35
+ # Byte signifying the host's endianness.
36
+ HOST_END = if [0x01020304].pack("L").unpack("V")[0] == 0x01020304
37
+ LIL_END
38
+ else
39
+ BIG_END
40
+ end
41
+
42
+ # General exceptions.
43
+
44
+ # Exception raised when an invalid packet is encountered.
45
+ class InvalidPacketException < Exception
46
+ end
47
+
48
+ # Exception raised when there is a problem with a type (may be unknown or
49
+ # mismatch).
50
+ class TypeException < Exception
51
+ end
52
+
53
+ # Exception raised when an unmarshalled buffer is truncated and
54
+ # incomplete.
55
+ class IncompleteBufferException < Exception
56
+ end
57
+
58
+ # Exception raised when an interface is not implemented.
59
+ class InterfaceNotImplemented < Exception
60
+ end
61
+
62
+ # Exception raised when an method is not found in the interface.
63
+ class MethodNotInInterface < Exception
64
+ end
65
+
66
+ # Exception raised when a method has not been implemented (yet).
67
+ class MethodNotImplemented < Exception
68
+ end
69
+
70
+ # Exception raised when a method is invoked with invalid
71
+ # parameters (wrong number or type).
72
+ class InvalidParameters < Exception
73
+ end
74
+
75
+ # Exception raised when an invalid method name is used.
76
+ class InvalidMethodName < Exception
77
+ end
78
+
79
+ # Exception raised when invalid introspection data is parsed/used.
80
+ class InvalidIntrospectionData < Exception
81
+ end
82
+ end # module DBus
data/lib/dbus/auth.rb ADDED
@@ -0,0 +1,156 @@
1
+ # This file is part of the ruby-dbus project
2
+ # Copyright (C) 2007 Arnaud Cornet and Paul van Tilburg
3
+ #
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License, version 2.1 as published by the Free Software Foundation.
7
+ # See the file "COPYING" for the exact licensing terms.
8
+
9
+ module DBus
10
+ # Exception raised when authentication fails somehow.
11
+ class AuthenticationFailed < Exception
12
+ end
13
+
14
+ # = General class for authentication.
15
+ class Authenticator
16
+ # Returns the name of the authenticator.
17
+ def name
18
+ self.class.to_s.upcase.sub(/.*::/, "")
19
+ end
20
+ end
21
+
22
+ # = External authentication class
23
+ #
24
+ # Class for 'external' type authentication.
25
+ class External < Authenticator
26
+ # Performs the authentication.
27
+ def authenticate
28
+ # Take the user id (eg integer 1000) make a string out of it "1000", take
29
+ # each character and determin hex value "1" => 0x31, "0" => 0x30. You
30
+ # obtain for "1000" => 31303030 This is what the server is expecting.
31
+ # Why? I dunno. How did I come to that conclusion? by looking at rbus
32
+ # code. I have no idea how he found that out.
33
+ return Process.uid.to_s.split(//).collect { |a| "%x" % a[0] }.join
34
+ end
35
+ end
36
+
37
+ # Note: this following stuff is tested with External authenticator only!
38
+
39
+ # = Authentication client class.
40
+ #
41
+ # Class tha performs the actional authentication.
42
+ class Client
43
+ # Create a new authentication client.
44
+ def initialize(socket)
45
+ @socket = socket
46
+ @state = nil
47
+ @auth_list = [External]
48
+ end
49
+
50
+ # Start the authentication process.
51
+ def authenticate
52
+ @socket.write(0.chr)
53
+ next_authenticator
54
+ @state = :Starting
55
+ while @state != :Authenticated
56
+ r = next_state
57
+ return r if not r
58
+ end
59
+ true
60
+ end
61
+
62
+ ##########
63
+ private
64
+ ##########
65
+
66
+ # Send an authentication method _meth_ with arguments _args_ to the
67
+ # server.
68
+ def send(meth, *args)
69
+ o = ([meth] + args).join(" ")
70
+ @socket.write(o + "\r\n")
71
+ end
72
+
73
+ # Try authentication using the next authenticator.
74
+ def next_authenticator
75
+ raise AuthenticationFailed if @auth_list.size == 0
76
+ @authenticator = @auth_list.shift.new
77
+ send("AUTH", @authenticator.name, @authenticator.authenticate)
78
+ end
79
+
80
+
81
+ # Read data (a buffer) from the bus until CR LF is encountered.
82
+ # Return the buffer without the CR LF characters.
83
+ def next_msg
84
+ @socket.readline.chomp.split(" ")
85
+ end
86
+
87
+ # Try to reach the next state based on the current state.
88
+ def next_state
89
+ msg = next_msg
90
+ if @state == :Starting
91
+ case msg[0]
92
+ when "CONTINUE"
93
+ @state = :WaitingForData
94
+ when "OK"
95
+ @state = :WaitingForOk
96
+ end
97
+ end
98
+ case @state
99
+ when :WaitingForData
100
+ case msg[0]
101
+ when "DATA"
102
+ chall = msg[1]
103
+ resp, chall = @authenticator.data(chall)
104
+ case resp
105
+ when :AuthContinue
106
+ send("DATA", chall)
107
+ @state = :WaitingForData
108
+ when :AuthOk
109
+ send("DATA", chall)
110
+ @state = :WaitingForOk
111
+ when :AuthError
112
+ send("ERROR")
113
+ @state = :WaitingForData
114
+ end
115
+ when "REJECTED"
116
+ next_authenticator
117
+ @state = :WaitingForData
118
+ when "ERROR"
119
+ send("CANCEL")
120
+ @state = :WaitingForReject
121
+ when "OK"
122
+ send("BEGIN")
123
+ @state = :Authenticated
124
+ else
125
+ send("ERROR")
126
+ @state = :WaitingForData
127
+ end
128
+ when :WaitingForOk
129
+ case msg[0]
130
+ when "OK"
131
+ send("BEGIN")
132
+ @state = :Authenticated
133
+ when "REJECT"
134
+ next_authenticator
135
+ @state = :WaitingForData
136
+ when "DATA", "ERROR"
137
+ send("CANCEL")
138
+ @state = :WaitingForReject
139
+ else
140
+ send("ERROR")
141
+ @state = :WaitingForOk
142
+ end
143
+ when :WaitingForReject
144
+ case msg[0]
145
+ when "REJECT"
146
+ next_authenticator
147
+ @state = :WaitingForOk
148
+ else
149
+ @socket.close
150
+ return false
151
+ end
152
+ end
153
+ return true
154
+ end # def next_state
155
+ end # class Client
156
+ end # module D-Bus