Class: DBus::Object
- Inherits:
-
Object
- Object
- DBus::Object
- Defined in:
- lib/dbus/export.rb
Overview
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.
Defined Under Namespace
Classes: UndefinedInterface
Constant Summary collapse
- @@cur_intf =
nil- @@intfs_mutex =
Mutex.new
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
The path of the object.
-
#service ⇒ Object
writeonly
The service that the object is exported by.
Class Method Summary collapse
-
.dbus_interface(s) ⇒ Object
Select (and create) the interface that the following defined methods belong to.
-
.dbus_method(sym, protoype = "", &block) ⇒ Object
Defines an exportable method on the object with the given name sym, prototype and the code in a block.
-
.dbus_signal(sym, protoype = "") ⇒ Object
Defines a signal for the object with a given name sym and prototype.
Instance Method Summary collapse
-
#dispatch(msg) ⇒ Object
Dispatch a message msg.
-
#emit(intf, sig, *args) ⇒ Object
Emits a signal from the object with the given interface, signal sig and arguments args.
-
#implements(intf) ⇒ Object
State that the object implements the given intf.
-
#initialize(path) ⇒ Object
constructor
Create a new object with a given path.
Constructor Details
#initialize(path) ⇒ Object
Create a new object with a given path.
31 32 33 34 |
# File 'lib/dbus/export.rb', line 31 def initialize(path) @path = path @service = nil end |
Instance Attribute Details
#path ⇒ Object (readonly)
The path of the object.
21 22 23 |
# File 'lib/dbus/export.rb', line 21 def path @path end |
#service=(value) ⇒ Object (writeonly)
The service that the object is exported by.
25 26 27 |
# File 'lib/dbus/export.rb', line 25 def service=(value) @service = value end |
Class Method Details
.dbus_interface(s) ⇒ Object
Select (and create) the interface that the following defined methods belong to.
74 75 76 77 78 79 80 81 |
# File 'lib/dbus/export.rb', line 74 def self.dbus_interface(s) @@intfs_mutex.synchronize do @@cur_intf = Interface.new(s) self.intfs = (self.intfs || {}).merge({s => @@cur_intf}) yield @@cur_intf = nil end end |
.dbus_method(sym, protoype = "", &block) ⇒ Object
Defines an exportable method on the object with the given name sym, prototype and the code in a block.
92 93 94 95 96 |
# File 'lib/dbus/export.rb', line 92 def self.dbus_method(sym, protoype = "", &block) raise UndefinedInterface, sym if @@cur_intf.nil? @@cur_intf.define(Method.new(sym.to_s).from_prototype(protoype)) define_method(Object.make_method_name(@@cur_intf.name, sym.to_s), &block) end |
.dbus_signal(sym, protoype = "") ⇒ Object
Defines a signal for the object with a given name sym and prototype.
105 106 107 108 109 110 111 112 113 |
# File 'lib/dbus/export.rb', line 105 def self.dbus_signal(sym, protoype = "") raise UndefinedInterface, sym if @@cur_intf.nil? cur_intf = @@cur_intf signal = Signal.new(sym.to_s).from_prototype(protoype) cur_intf.define(Signal.new(sym.to_s).from_prototype(protoype)) define_method(sym.to_s) do |*args| emit(cur_intf, signal, *args) end end |
Instance Method Details
#dispatch(msg) ⇒ Object
Dispatch a message msg.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dbus/export.rb', line 43 def dispatch(msg) case msg. when Message::METHOD_CALL reply = nil begin if not self.intfs[msg.interface] raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"), "Interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist" end meth = self.intfs[msg.interface].methods[msg.member.to_sym] if not meth raise DBus.error("org.freedesktop.DBus.Error.UnknownMethod"), "Method \"#{msg.member}\" on interface \"#{msg.interface}\" of object \"#{msg.path}\" doesn't exist" end methname = Object.make_method_name(msg.interface, msg.member) retdata = method(methname).call(*msg.params) retdata = [*retdata] reply = Message.method_return(msg) meth.rets.zip(retdata).each do |rsig, rdata| reply.add_param(rsig.type, rdata) end rescue => ex reply = ErrorMessage.from_exception(ex).reply_to(msg) end @service.bus.send(reply.marshall) end end |
#emit(intf, sig, *args) ⇒ Object
Emits a signal from the object with the given interface, signal sig and arguments args.
100 101 102 |
# File 'lib/dbus/export.rb', line 100 def emit(intf, sig, *args) @service.bus.emit(@service, self, intf, sig, *args) end |
#implements(intf) ⇒ Object
State that the object implements the given intf.
37 38 39 40 |
# File 'lib/dbus/export.rb', line 37 def implements(intf) # use a setter self.intfs = (self.intfs || {}).merge({intf.name => intf}) end |