Class: DBus::Object

Inherits:
Object
  • Object
show all
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

@@intfs =
Hash.new
@@cur_intf =
nil
@@intfs_mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object

Create a new object with a given path.



44
45
46
47
48
# File 'lib/dbus/export.rb', line 44

def initialize(path)
  @path = path
  @intfs = @@intfs.dup
  @service = nil
end

Instance Attribute Details

#intfsObject (readonly)

The interfaces that the object supports.



35
36
37
# File 'lib/dbus/export.rb', line 35

def intfs
  @intfs
end

#pathObject (readonly)

The path of the object.



33
34
35
# File 'lib/dbus/export.rb', line 33

def path
  @path
end

#service=(value) ⇒ Object (writeonly)

The service that the object is exported by.



37
38
39
# File 'lib/dbus/export.rb', line 37

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.



77
78
79
80
81
82
83
# File 'lib/dbus/export.rb', line 77

def self.dbus_interface(s)
  @@intfs_mutex.synchronize do
    @@cur_intf = @@intfs[s] = Interface.new(s)
    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.

Raises:



91
92
93
94
95
# File 'lib/dbus/export.rb', line 91

def self.dbus_method(sym, protoype = "", &block)
  raise UndefinedInterface 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.

Raises:



104
105
106
107
108
109
110
111
112
# File 'lib/dbus/export.rb', line 104

def self.dbus_signal(sym, protoype = "")
  raise UndefinedInterface 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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dbus/export.rb', line 56

def dispatch(msg)
  case msg.message_type
  when Message::METHOD_CALL
    if not @intfs[msg.interface]
      raise InterfaceNotInObject, msg.interface
    end
    meth = @intfs[msg.interface].methods[msg.member.to_sym]
    raise MethodNotInInterface if not meth
    methname = Object.make_method_name(msg.interface, msg.member)
    retdata = method(methname).call(*msg.params).to_a

    reply = Message.new.reply_to(msg)
    meth.rets.zip(retdata).each do |rsig, rdata|
      reply.add_param(rsig[1], rdata)
    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.



99
100
101
# File 'lib/dbus/export.rb', line 99

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.



51
52
53
# File 'lib/dbus/export.rb', line 51

def implements(intf)
  @intfs[intf.name] = intf
end