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. At the client side, use ProxyObject.

Defined Under Namespace

Classes: UndefinedInterface

Constant Summary collapse

@@intfs_hash =

The interfaces that the object supports. intfs: Hash: String => Interface @@intfs_hash simulates a class attribute by being a hash Class => intfs

{DBus::Object => nil}
@@cur_intf =

Interface

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. Use Service#export to export it.



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

def initialize(path)
  @path = path
  @service = nil
end

Instance Attribute Details

#pathObject (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.



27
28
29
# File 'lib/dbus/export.rb', line 27

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.



116
117
118
119
120
121
122
123
124
125
# File 'lib/dbus/export.rb', line 116

def self.dbus_interface(s)
  @@intfs_mutex.synchronize do
    unless @@cur_intf = (self.intfs && self.intfs[s])
      @@cur_intf = Interface.new(s)
      self.intfs = (self.intfs || {}).merge({s => @@cur_intf})
    end
    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:



136
137
138
139
140
# File 'lib/dbus/export.rb', line 136

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.

Raises:



149
150
151
152
153
154
155
156
157
# File 'lib/dbus/export.rb', line 149

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

.intfsObject



39
40
41
42
43
44
45
# File 'lib/dbus/export.rb', line 39

def self.intfs
  if self.equal? DBus::Object
    @@intfs_hash[DBus::Object]
  else
    @@intfs_hash[self] || self.superclass.intfs
  end
end

.intfs=(param) ⇒ Object



47
48
49
# File 'lib/dbus/export.rb', line 47

def self.intfs= param
  @@intfs_hash[self] = param
end

Instance Method Details

#dispatch(msg) ⇒ Object

Dispatch a message msg to call exported methods



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

def dispatch(msg)
  if @service.threaded?
    Thread.new { process_dispatch(msg)}
  else
    process_dispatch(msg)
  end
end

#emit(intf, sig, *args) ⇒ Object

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



144
145
146
# File 'lib/dbus/export.rb', line 144

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.



70
71
72
73
# File 'lib/dbus/export.rb', line 70

def implements(intf)
  # use a setter
  self.intfs = (self.intfs || {}).merge({intf.name => intf})
end

#intfsObject



51
52
53
54
55
56
57
58
# File 'lib/dbus/export.rb', line 51

def intfs
  #check if singleton class is defined
  if self.singleton_methods.empty?
    self.class.intfs
  else
    self.singleton_class.intfs
  end
end

#intfs=(param) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/dbus/export.rb', line 60

def intfs= param
  #check if singleton class is defined
  if self.singleton_methods.empty?
    self.class.intfs = param
  else
    self.singleton_class.intfs = param
  end
end

#process_dispatch(msg) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dbus/export.rb', line 84

def process_dispatch(msg)
  case msg.message_type
  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