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.



32
33
34
35
36
37
38
# File 'lib/dbus/proxy_object_interface.rb', line 32

def initialize(object, name)
  @object = object
  @name = name
  @methods = {}
  @signals = {}
  @properties = {}
end

Instance Attribute Details

#methodsHash{String => DBus::Method} (readonly)

Returns:



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

def methods
  @methods
end

#nameString (readonly)

Returns The name of the interface.

Returns:

  • (String)

    The name of the interface.



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

def name
  @name
end

#objectProxyObject (readonly)

Returns The proxy object to which this interface belongs.

Returns:

  • (ProxyObject)

    The proxy object to which this interface belongs.



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

def object
  @object
end

#propertiesHash{Symbol => Property} (readonly)

Returns:



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

def properties
  @properties
end

#signalsHash{String => Signal} (readonly)

Returns:



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

def signals
  @signals
end

Instance Method Details

#[](propname) ⇒ Object

Read a property.

Parameters:

  • propname (String)


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

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

#[]=(property_name, value) ⇒ Object

Write a property.

Parameters:

  • property_name (String)
  • value (Object)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dbus/proxy_object_interface.rb', line 146

def []=(property_name, value)
  property = properties[property_name.to_sym]
  if !property
    raise DBus.error("org.freedesktop.DBus.Error.UnknownProperty"),
          "Property '#{name}.#{property_name}' (on object '#{object.path}') not found"
  end

  case value
  # accommodate former need to explicitly make a variant with the right type
  when Data::Variant
    variant = value
  else
    type = property.type
    typed_value = Data.make_typed(type, value)
    variant = Data::Variant.new(typed_value, member_type: type)
  end

  object[PROPERTY_INTERFACE].Set(name, property_name, variant)
end

#all_propertiesHash{String}

Read all properties at once, as a hash.

Returns:

  • (Hash{String})


168
169
170
171
172
173
174
175
176
# File 'lib/dbus/proxy_object_interface.rb', line 168

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

#define(ifc_el) ⇒ Object

Defines a signal or method based on the descriptor ifc_el.

Parameters:



95
96
97
98
99
100
101
102
103
104
# File 'lib/dbus/proxy_object_interface.rb', line 95

def define(ifc_el)
  case ifc_el
  when Method
    define_method_from_descriptor(ifc_el)
  when Signal
    define_signal_from_descriptor(ifc_el)
  when Property
    define_property_from_descriptor(ifc_el)
  end
end

#define_method(methodname, prototype) ⇒ Object

Defines a proxied method on the interface.



107
108
109
110
111
# File 'lib/dbus/proxy_object_interface.rb', line 107

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

#define_method_from_descriptor(method) ⇒ Object

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

Parameters:



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
72
73
74
75
76
77
78
79
80
# File 'lib/dbus/proxy_object_interface.rb', line 47

def define_method_from_descriptor(method)
  method.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 method.name do |*args, &reply_handler|
      if method.params.size != args.size
        raise ArgumentError, "wrong number of arguments (#{args.size} for #{method.params.size})"
      end

      msg = Message.new(Message::METHOD_CALL)
      msg.path = @object.path
      msg.interface = @name
      msg.destination = @object.destination
      msg.member = method.name
      msg.sender = @object.bus.unique_name
      method.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
        method.rets.size == 1 ? ret.first : ret
      end
    end
  end

  @methods[method.name] = method
end

#define_property_from_descriptor(prop) ⇒ Object

Parameters:



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

def define_property_from_descriptor(prop)
  @properties[prop.name] = prop
end

#define_signal_from_descriptor(sig) ⇒ Object

Defines a signal from the descriptor sig.

Parameters:



84
85
86
# File 'lib/dbus/proxy_object_interface.rb', line 84

def define_signal_from_descriptor(sig)
  @signals[sig.name] = sig
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.



120
121
122
123
124
125
126
127
# File 'lib/dbus/proxy_object_interface.rb', line 120

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).



41
42
43
# File 'lib/dbus/proxy_object_interface.rb', line 41

def to_str
  @name
end