class DBus::Object
Exported object type
Exportable D-Bus object class¶ ↑
Objects that are going to be exported by a D-Bus service should inherit from this class. At the client side, use ProxyObject.
Attributes
The path of the object.
The service that the object is exported by.
Public Class Methods
Select (and create) the interface that the following defined methods belong to.
# File lib/dbus/export.rb 73 def self.dbus_interface(s) 74 @@intfs_mutex.synchronize do 75 @@cur_intf = intfs[s] 76 if !@@cur_intf 77 @@cur_intf = Interface.new(s) 78 # As this is a mutable class_attr, we cannot use 79 # self.intfs[s] = @@cur_intf # Hash#[]= 80 # as that would modify parent class attr in place. 81 # Using the setter lets a subclass have the new value 82 # while the superclass keeps the old one. 83 self.intfs = intfs.merge(s => @@cur_intf) 84 end 85 yield 86 @@cur_intf = nil 87 end 88 end
Defines an exportable method on the object with the given name sym, prototype and the code in a block.
# File lib/dbus/export.rb 99 def self.dbus_method(sym, protoype = "", &block) 100 raise UndefinedInterface, sym if @@cur_intf.nil? 101 @@cur_intf.define(Method.new(sym.to_s).from_prototype(protoype)) 102 define_method(Object.make_method_name(@@cur_intf.name, sym.to_s), &block) 103 end
Defines a signal for the object with a given name sym and prototype.
# File lib/dbus/export.rb 112 def self.dbus_signal(sym, protoype = "") 113 raise UndefinedInterface, sym if @@cur_intf.nil? 114 cur_intf = @@cur_intf 115 signal = Signal.new(sym.to_s).from_prototype(protoype) 116 cur_intf.define(Signal.new(sym.to_s).from_prototype(protoype)) 117 define_method(sym.to_s) do |*args| 118 emit(cur_intf, signal, *args) 119 end 120 end
Helper method that returns a method name generated from the interface name intfname and method name methname. @api private
# File lib/dbus/export.rb 127 def self.make_method_name(intfname, methname) 128 "#{intfname}%%#{methname}" 129 end
Create a new object with a given path. Use Service#export to export it.
# File lib/dbus/export.rb 35 def initialize(path) 36 @path = path 37 @service = nil 38 end
Public Instance Methods
Dispatch a message msg to call exported methods
# File lib/dbus/export.rb 41 def dispatch(msg) 42 case msg.message_type 43 when Message::METHOD_CALL 44 reply = nil 45 begin 46 if !intfs[msg.interface] 47 raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"), 48 "Interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist" 49 end 50 meth = intfs[msg.interface].methods[msg.member.to_sym] 51 if !meth 52 raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"), 53 "Method \"#{msg.member}\" on interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist" 54 end 55 methname = Object.make_method_name(msg.interface, msg.member) 56 retdata = method(methname).call(*msg.params) 57 retdata = [*retdata] 58 59 reply = Message.method_return(msg) 60 meth.rets.zip(retdata).each do |rsig, rdata| 61 reply.add_param(rsig.type, rdata) 62 end 63 rescue => ex 64 dbus_msg_exc = msg.annotate_exception(ex) 65 reply = ErrorMessage.from_exception(dbus_msg_exc).reply_to(msg) 66 end 67 @service.bus.message_queue.push(reply) 68 end 69 end
Emits a signal from the object with the given interface, signal sig and arguments args.
# File lib/dbus/export.rb 107 def emit(intf, sig, *args) 108 @service.bus.emit(@service, self, intf, sig, *args) 109 end