Skip to content

Commit 3849061

Browse files
committed
PeerConnection: fix listening for signals (#44), with test and example
the fix is dirty, will move code to PeerConnection
1 parent 75d4451 commit 3849061

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

examples/no-bus/pulseaudio.rb

+20
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,23 @@ def peer_address
2828
obj = no_svc["/org/pulseaudio/core1"]
2929
ifc = obj["org.PulseAudio.Core1"]
3030
puts "PA version: #{ifc["Version"]}"
31+
32+
puts "Waiting for volume changes, try adjusting it. Ctrl-C to exit."
33+
34+
vol_ifc = "org.PulseAudio.Core1.Device"
35+
vol_member = "VolumeUpdated"
36+
# PA needs explicit enabling of signals
37+
ifc.ListenForSignal("#{vol_ifc}.#{vol_member}", [])
38+
39+
match_rule = DBus::MatchRule.new
40+
match_rule.interface = vol_ifc
41+
match_rule.member = vol_member
42+
conn.add_match(match_rule) do |msg|
43+
# a single argument that is an array
44+
volumes = msg.params[0]
45+
puts "VolumeUpdated: #{volumes.join(", ")}"
46+
end
47+
48+
loop = DBus::Main.new
49+
loop << conn
50+
loop.run

lib/dbus/bus.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def add_match(match_rule, &slot)
369369
# don't ask for the same match if we override it
370370
unless @signal_matchrules.key?(mrs)
371371
DBus.logger.debug "Asked for a new match"
372-
proxy.AddMatch(mrs)
372+
proxy.AddMatch(mrs) if @unique_name
373373
end
374374
@signal_matchrules[mrs] = slot
375375
end
@@ -383,7 +383,7 @@ def remove_match(match_rule)
383383

384384
# FIXME: if we do try, the Error.MatchRuleNotFound is *not* raised
385385
# and instead is reported as "no return code for nil"
386-
proxy.RemoveMatch(mrs)
386+
proxy.RemoveMatch(mrs) if @unique_name
387387
end
388388

389389
# @api private

spec/connection_spec.rb

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
require "dbus"
66

77
describe DBus::PeerConnection do
8+
let(:address) { ENV["DBUS_SESSION_BUS_ADDRESS"] }
9+
subject { described_class.new(address) }
10+
811
describe "#peer_service" do
912
it "returns a PeerService with a nil name" do
10-
address = ENV["DBUS_SESSION_BUS_ADDRESS"]
11-
pconn = described_class.new(address)
12-
svc = pconn.peer_service
13+
svc = subject.peer_service
1314
expect(svc).to be_a(DBus::ProxyService)
1415
expect(svc.name).to be_nil
1516
end
1617
end
18+
19+
describe "#add_match, #remove_match" do
20+
it "doesn't crash trying to call AddMatch, RemoveMatch" do
21+
mr = DBus::MatchRule.new
22+
mr.member = "StateUpdated"
23+
mr.interface = "org.PulseAudio.Core1.Device"
24+
handler = ->(_msg) {}
25+
26+
# Cheating a bit with the mocking:
27+
# a PulseAudio peer connection would error with
28+
# > DBus::Error: Method "AddMatch" with signature "s" on interface
29+
# > "org.freedesktop.DBus" doesn't exist
30+
# but here we do have a bus at the other end, which replies with
31+
# > DBus::Error: Client tried to send a message other than Hello without being registered
32+
# where "registering" is a libdbus-1 thing meaning "internal bookkeeping and send Hello"
33+
expect { subject.add_match(mr, &handler) }.to_not raise_error
34+
expect { subject.remove_match(mr) }.to_not raise_error
35+
end
36+
end
1737
end

0 commit comments

Comments
 (0)