Class: DBus::DBusCallable

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus.rb

Overview

Base class for objects that support D-BUS invocation messages

Direct Known Subclasses

Object, ObjectTree

Instance Method Summary collapse

Constructor Details

#initialize(connection, dbus_methods = []) ⇒ DBusCallable

Create a new DBusCallable instance on the specified connection. dbus_methods is an Array containing a list of symbols for the methods which may be invoked remotely.



214
215
216
217
# File 'lib/dbus.rb', line 214

def initialize(connection, dbus_methods=[])
  @connection = connection
  @dbus_methods = dbus_methods
end

Instance Method Details

#dispatch(name, args, source_message) ⇒ Object

Invoke the method name, with arguments args, and source invocation request message source_message, returning the reply message.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/dbus.rb', line 227

def dispatch(name, args, source_message)
  unless @dbus_methods.include?(name.to_sym)
    return new_error_reply(source_message, "Method '#{name}' not in allowed list")
  end
  unless self.respond_to?(name)
    return new_error_reply(source_message, "No such method '#{name}'")
  end
  ret = nil
  begin
    args = [source_message, *args]
    ret = self.send(name, *args)
  rescue
    return new_error_reply(source_message, $!.to_s)
  end
  reply = DBus::Binding::DBusMessage.new_method_return(source_message)
  iter = reply.get_iter
  iter.append(ret)
  reply
end

#dispatch_message(message) ⇒ Object

Process the method invocation message given in message. Returns the reply message.



221
222
223
# File 'lib/dbus.rb', line 221

def dispatch_message(message)
  dispatch(message.get_member, message.to_a, message)
end

#new_error_reply(message, error_message) ⇒ Object

Generate a new error reply from source message message, and error string error_message.



249
250
251
252
253
# File 'lib/dbus.rb', line 249

def new_error_reply(message, error_message)
  error_name = self.class.to_s.gsub(/::/, '.')
  error_name += '.ERROR'
  DBus::Binding::DBusMessage.new_error(message, error_name, error_message)
end

#on_message(connection, message) ⇒ Object

Called when a message arrives on the connection for this object



260
261
262
263
264
# File 'lib/dbus.rb', line 260

def on_message(connection, message)
  reply = dispatch_message(message)
  @connection.send(reply)
  HANDLER_RESULT_HANDLED
end

#on_unregister(connection) ⇒ Object

Called when this object is unregistered from the connection



256
257
# File 'lib/dbus.rb', line 256

def on_unregister(connection)
end