# 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

path[R]

The path of the object.

service[W]

The service that the object is exported by.

### Public Class Methods

dbus_interface(s) { || ... } click to toggle source

Select (and create) the interface that the following defined methods belong to.

       # File lib/dbus/object.rb
    71 def self.dbus_interface(s)
    72   @@intfs_mutex.synchronize do
    73     @@cur_intf = intfs[s]
    74     if !@@cur_intf
    75       @@cur_intf = Interface.new(s)
    76       # As this is a mutable class_attr, we cannot use
    77       #   self.intfs[s] = @@cur_intf                      # Hash#[]=
    78       # as that would modify parent class attr in place.
    79       # Using the setter lets a subclass have the new value
    80       # while the superclass keeps the old one.
    81       self.intfs = intfs.merge(s => @@cur_intf)
    82     end
    83     yield
    84     @@cur_intf = nil
    85   end
    86 end

dbus_method(sym, protoype = "", &block) click to toggle source

Defines an exportable method on the object with the given name sym, prototype and the code in a block.

        # File lib/dbus/object.rb
     97 def self.dbus_method(sym, protoype = "", &block)
     98   raise UndefinedInterface, sym if @@cur_intf.nil?
     99   @@cur_intf.define(Method.new(sym.to_s).from_prototype(protoype))
    100   define_method(Object.make_method_name(@@cur_intf.name, sym.to_s), &block)
    101 end

dbus_signal(sym, protoype = "") click to toggle source

Defines a signal for the object with a given name sym and prototype.

        # File lib/dbus/object.rb
    110 def self.dbus_signal(sym, protoype = "")
    111   raise UndefinedInterface, sym if @@cur_intf.nil?
    112   cur_intf = @@cur_intf
    113   signal = Signal.new(sym.to_s).from_prototype(protoype)
    114   cur_intf.define(Signal.new(sym.to_s).from_prototype(protoype))
    115   define_method(sym.to_s) do |*args|
    116     emit(cur_intf, signal, *args)
    117   end
    118 end

make_method_name(intfname, methname) click to toggle source

Helper method that returns a method name generated from the interface name intfname and method name methname. @api private

        # File lib/dbus/object.rb
    125 def self.make_method_name(intfname, methname)
    126   "#{intfname}%%#{methname}"
    127 end

new(path) click to toggle source

Create a new object with a given path. Use `Service#export` to export it.

       # File lib/dbus/object.rb
    33 def initialize(path)
    34   @path = path
    35   @service = nil
    36 end

### Public Instance Methods

dispatch(msg) click to toggle source

Dispatch a message msg to call exported methods

       # File lib/dbus/object.rb
    39 def dispatch(msg)
    40   case msg.message_type
    41   when Message::METHOD_CALL
    42     reply = nil
    43     begin
    44       if !intfs[msg.interface]
    45         raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"),
    46               "Interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist"
    47       end
    48       meth = intfs[msg.interface].methods[msg.member.to_sym]
    49       if !meth
    50         raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"),
    51               "Method \"#{msg.member}\" on interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist"
    52       end
    53       methname = Object.make_method_name(msg.interface, msg.member)
    54       retdata = method(methname).call(*msg.params)
    55       retdata = [*retdata]
    56
    57       reply = Message.method_return(msg)
    58       meth.rets.zip(retdata).each do |rsig, rdata|
    59         reply.add_param(rsig.type, rdata)
    60       end
    61     rescue => ex
    62       dbus_msg_exc = msg.annotate_exception(ex)
    63       reply = ErrorMessage.from_exception(dbus_msg_exc).reply_to(msg)
    64     end
    65     @service.bus.message_queue.push(reply)
    66   end
    67 end

emit(intf, sig, *args) click to toggle source

Emits a signal from the object with the given interface, signal sig and arguments args.

        # File lib/dbus/object.rb
    105 def emit(intf, sig, *args)
    106   @service.bus.emit(@service, self, intf, sig, *args)
    107 end