Class: DBus::ProxyObjectInterface

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

Overview

D-Bus proxy object interface class

A class similar to the normal Interface used as a proxy for remote object interfaces.

Constant Summary collapse

PROPERTY_INTERFACE =
"org.freedesktop.DBus.Properties"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, name) ⇒ ProxyObjectInterface

Creates a new proxy interface for the given proxy object and the given name.



313
314
315
316
# File 'lib/dbus/introspect.rb', line 313

def initialize(object, name)
  @object, @name = object, name
  @methods, @signals = Hash.new, Hash.new
end

Instance Attribute Details

#methodsObject

The proxied methods contained in the interface.



303
304
305
# File 'lib/dbus/introspect.rb', line 303

def methods
  @methods
end

#nameObject (readonly)

The name of the interface.



309
310
311
# File 'lib/dbus/introspect.rb', line 309

def name
  @name
end

#objectObject (readonly)

The proxy object to which this interface belongs.



307
308
309
# File 'lib/dbus/introspect.rb', line 307

def object
  @object
end

#signalsObject

The proxied signals contained in the interface.



305
306
307
# File 'lib/dbus/introspect.rb', line 305

def signals
  @signals
end

Instance Method Details

#[](propname) ⇒ Object

Read a property.



429
430
431
# File 'lib/dbus/introspect.rb', line 429

def [](propname)
  self.object[PROPERTY_INTERFACE].Get(self.name, propname)[0]
end

#[]=(propname, value) ⇒ Object

Write a property.



434
435
436
# File 'lib/dbus/introspect.rb', line 434

def []=(propname, value)
  self.object[PROPERTY_INTERFACE].Set(self.name, propname, value)
end

#all_propertiesObject

Read all properties at once, as a hash.



439
440
441
# File 'lib/dbus/introspect.rb', line 439

def all_properties
  self.object[PROPERTY_INTERFACE].GetAll(self.name)[0]
end

#check_for_eval(s) ⇒ Object

FIXME

Raises:

  • (RuntimeError)


329
330
331
# File 'lib/dbus/introspect.rb', line 329

def check_for_eval(s)
  raise RuntimeError, "invalid internal data '#{s}'" if not s.to_s =~ /^[A-Za-z0-9_]*$/
end

#check_for_quoted_eval(s) ⇒ Object

FIXME

Raises:

  • (RuntimeError)


334
335
336
# File 'lib/dbus/introspect.rb', line 334

def check_for_quoted_eval(s)
  raise RuntimeError, "invalid internal data '#{s}'" if not s.to_s =~ /^[^"]+$/
end

#define(m) ⇒ Object

Defines a signal or method based on the descriptor m.



400
401
402
403
404
405
406
# File 'lib/dbus/introspect.rb', line 400

def define(m)
  if m.kind_of?(Method)
    define_method_from_descriptor(m)
  elsif m.kind_of?(Signal)
    define_signal_from_descriptor(m)
  end
end

#define_method(methodname, prototype) ⇒ Object

Defines a proxied method on the interface.



409
410
411
412
413
# File 'lib/dbus/introspect.rb', line 409

def define_method(methodname, prototype)
  m = Method.new(methodname)
  m.from_prototype(prototype)
  define(m)
end

#define_method_from_descriptor(m) ⇒ Object

Defines a method on the interface from the Method descriptor m.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/dbus/introspect.rb', line 339

def define_method_from_descriptor(m)
  check_for_eval(m.name)
  check_for_quoted_eval(@name)
  methdef = "def #{m.name}("
  methdef += (0..(m.params.size - 1)).to_a.collect { |n|
    "arg#{n}"
  }.join(", ")
  methdef += %{)
          msg = Message.new(Message::METHOD_CALL)
          msg.path = @object.path
          msg.interface = "#{@name}"
          msg.destination = @object.destination
          msg.member = "#{m.name}"
          msg.sender = @object.bus.unique_name
        }
  idx = 0
  m.params.each do |fpar|
    par = fpar.type
    check_for_quoted_eval(par)

    # This is the signature validity check
    Type::Parser.new(par).parse

    methdef += %{
      msg.add_param("#{par}", arg#{idx})
    }
    idx += 1
  end
  methdef += "
    ret = nil
    if block_given?
      @object.bus.on_return(msg) do |rmsg|
        if rmsg.is_a?(Error)
          yield(rmsg)
        else
          yield(rmsg, *rmsg.params)
        end
      end
      @object.bus.send(msg.marshall)
    else
      @object.bus.send_sync(msg) do |rmsg|
        if rmsg.is_a?(Error)
          raise rmsg
        else
          ret = rmsg.params
        end
      end
    end
    ret
  end
  "
  singleton_class.class_eval(methdef)
  @methods[m.name] = m
end

#define_signal_from_descriptor(s) ⇒ Object

Defines a signal from the descriptor s.



395
396
397
# File 'lib/dbus/introspect.rb', line 395

def define_signal_from_descriptor(s)
  @signals[s.name] = s
end

#on_signal(bus, name, &block) ⇒ Object

Registers a handler (code block) for a signal with name arriving over the given bus. If no block is given, the signal is unregistered.



417
418
419
420
421
422
423
424
# File 'lib/dbus/introspect.rb', line 417

def on_signal(bus, name, &block)
  mr = DBus::MatchRule.new.from_signal(self, name)
  if block.nil?
    bus.remove_match(mr)
  else
    bus.add_match(mr) { |msg| block.call(*msg.params) }
  end
end

#singleton_classObject

Returns the singleton class of the interface.



324
325
326
# File 'lib/dbus/introspect.rb', line 324

def singleton_class
  (class << self ; self ; end)
end

#to_strObject

Returns the string representation of the interface (the name).



319
320
321
# File 'lib/dbus/introspect.rb', line 319

def to_str
  @name
end