Skip to content

Instantly share code, notes, and snippets.

@hramrach
Created November 22, 2015 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hramrach/b91e705c39df79d8337d to your computer and use it in GitHub Desktop.
Save hramrach/b91e705c39df79d8337d to your computer and use it in GitHub Desktop.
require 'dbus'
class Exception
def pp
STDERR.puts self.inspect
self.backtrace.each{|l|
STDERR.puts " " + l
}
end
end
$bus = DBus::SessionBus.instance
$path = %w(net luon trac rubydbus testsrv)
$cli_path = %w(net luon trac rubydbus testcli)
$service = $path.join "."
$cli_service = $cli_path.join "."
class TestCli < DBus::Object
dbus_interface ($cli_path.join '.') do
dbus_method :RecvToken, "in server:o, in token:u" do |server,token|
begin
STDERR.puts "RecvToken #{server}, #{token}"
t = Thread.new {
Thread.stop
STDERR.puts "Query thread #{server}, #{token}"
loop {
sleep 1
begin
STDERR.puts "Data: #{@server.GetData @token}"
exit 0
rescue Exception
pp $!
end
}
}
@server = service.object server
@server.introspect
@server.default_iface = $path.join '.'
@token = token
@server.AckToken @token
t.wakeup
rescue Exception
pp $!
end
end
end
end
$object = TestCli.new ('/' + $cli_path.join('/'))
$service = $bus.service $service
$cli_service = $bus.request_service $cli_service
$cli_service.export $object
server = $service.object ('/' + $path.join('/'))
server.introspect
server.default_iface = $path.join '.'
server.Register $object.path
loop = DBus::Main.new
loop << $bus
loop.run
require 'dbus'
class Exception
def pp
STDERR.puts self.inspect
self.backtrace.each{|l|
STDERR.puts " " + l
}
end
end
$bus = DBus::SessionBus.instance
$path = %w(net luon trac rubydbus testsrv)
$cli_path = %w(net luon trac rubydbus testcli)
$service = $path.join "."
class TestSrv < DBus::Object
dbus_interface ($path.join('.')) do
dbus_method :Register, "in client:o" do |client|
begin
STDERR.puts "Register #{client}"
t = Thread.new {
Thread.stop
STDERR.puts "Token thread #{client}"
sleep 3 # fake some processing
begin
client = $service.object client
client.introspect
client.default_iface = $cli_path.join '.'
@token = Random.rand 65535
@client = client
@data = nil
STDERR.puts "Sending token #@token to #{client}"
client.RecvToken @token
rescue Exception
pp $!
end
}
t.run
rescue Exception
pp $!
end
end
dbus_method :AckToken, "in token:u" do |token|
STDERR.puts "AckToken #{token}"
raise RuntimeError.new "Invalid token!" unless token == @token
begin
t = Thread.new {
Thread.stop
STDERR.puts "Data thread #{token}"
sleep 5 # fake some processing
@data = Random.rand 65535
STDERR.puts "Generated data #@data"
}
t.run
rescue Exception
pp $!
end
end
dbus_method :GetData, "in token:u, out data:u" do |token|
STDERR.puts "GetData #{token}"
raise RuntimeError.new "Invalid token!" unless token == @token
raise RuntimeError.new "No data available!" unless @data
@data
end
end
end
$object = TestSrv.new ('/' + $path.join('/'))
$service = $bus.request_service $service
$service.export $object
loop = DBus::Main.new
loop << $bus
loop.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment