Skip to content

Instantly share code, notes, and snippets.

@hramrach
Created November 22, 2015 12:45
Show Gist options
  • Save hramrach/b91e705c39df79d8337d to your computer and use it in GitHub Desktop.
Save hramrach/b91e705c39df79d8337d to your computer and use it in GitHub Desktop.

Revisions

  1. hramrach created this gist Nov 22, 2015.
    70 changes: 70 additions & 0 deletions client.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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
    80 changes: 80 additions & 0 deletions server.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    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