Class: DBus::ProxyObjectInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/proxy_object_interface.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.



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

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.



17
18
19
# File 'lib/dbus/proxy_object_interface.rb', line 17

def methods
  @methods
end

#nameObject (readonly)

The name of the interface.



23
24
25
# File 'lib/dbus/proxy_object_interface.rb', line 23

def name
  @name
end

#objectObject (readonly)

The proxy object to which this interface belongs.



21
22
23
# File 'lib/dbus/proxy_object_interface.rb', line 21

def object
  @object
end

#signalsObject

The proxied signals contained in the interface.



19
20
21
# File 'lib/dbus/proxy_object_interface.rb', line 19

def signals
  @signals
end

Instance Method Details

#[](propname) ⇒ Object

Read a property.

Parameters:

  • propname (String)


114
115
116
117
118
119
120
121
122
# File 'lib/dbus/proxy_object_interface.rb', line 114

def [](propname)
  ret = self.object[PROPERTY_INTERFACE].Get(self.name, propname)
  # this method always returns the single property
  if @object.api.proxy_method_returns_array
    ret[0]
  else
    ret
  end
end

#[]=(propname, value) ⇒ Object

Write a property.

Parameters:

  • propname (String)
  • value (Object)


127
128
129
# File 'lib/dbus/proxy_object_interface.rb', line 127

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

#all_propertiesHash{String}

Read all properties at once, as a hash.

Returns:



133
134
135
136
137
138
139
140
141
# File 'lib/dbus/proxy_object_interface.rb', line 133

def all_properties
  ret = self.object[PROPERTY_INTERFACE].GetAll(self.name)
  # this method always returns the single property
  if @object.api.proxy_method_returns_array
    ret[0]
  else
    ret
  end
end

#define(m) ⇒ Object

Defines a signal or method based on the descriptor m.



79
80
81
82
83
84
85
# File 'lib/dbus/proxy_object_interface.rb', line 79

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.



88
89
90
91
92
# File 'lib/dbus/proxy_object_interface.rb', line 88

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.



38
39
40
41
42
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
71
# File 'lib/dbus/proxy_object_interface.rb', line 38

def define_method_from_descriptor(m)
  m.params.each do |fpar|
    par = fpar.type
    # This is the signature validity check
    Type::Parser.new(par).parse
  end

  singleton_class.class_eval do
    define_method m.name do |*args, &reply_handler|
      if m.params.size != args.size
        raise ArgumentError, "wrong number of arguments (#{args.size} for #{m.params.size})"
      end

      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
      m.params.each do |fpar|
        par = fpar.type
        msg.add_param(par, args.shift)
      end
      ret = @object.bus.send_sync_or_async(msg, &reply_handler)
      if ret.nil? || @object.api.proxy_method_returns_array
        ret
      else
        m.rets.size == 1 ? ret.first : ret
      end
    end
  end

  @methods[m.name] = m
end

#define_signal_from_descriptor(s) ⇒ Object

Defines a signal from the descriptor s.



74
75
76
# File 'lib/dbus/proxy_object_interface.rb', line 74

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

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

This method returns an undefined value.

Registers a handler (code block) for a signal with name arriving over the given bus. If no block is given, the signal is unregistered. Note that specifying bus is discouraged and the option is kept only for backward compatibility.



101
102
103
104
105
106
107
108
# File 'lib/dbus/proxy_object_interface.rb', line 101

def on_signal(bus = @object.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

#to_strObject

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



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

def to_str
  @name
end