Class: DBus::Service

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

Overview

This represents a remote service. It should not be instantiated directly Use Bus#service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bus) ⇒ Service

Create a new service with a given name on a given bus.



30
31
32
33
34
# File 'lib/dbus/bus.rb', line 30

def initialize(name, bus)
  @name = BusName.new(name)
  @bus = bus
  @root = Node.new("/")
end

Instance Attribute Details

#busObject (readonly)

The bus the service is running on.



25
26
27
# File 'lib/dbus/bus.rb', line 25

def bus
  @bus
end

#nameObject (readonly)

The service name.



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

def name
  @name
end

#rootObject (readonly)

The service root (FIXME).



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

def root
  @root
end

Instance Method Details

#[](path) ⇒ ProxyObject

Retrieves an object at the given path.

Returns:



52
53
54
# File 'lib/dbus/bus.rb', line 52

def [](path)
  object(path, api: ApiOptions::A1)
end

#exists?Boolean

Determine whether the service name already exists.

Returns:

  • (Boolean)


37
38
39
# File 'lib/dbus/bus.rb', line 37

def exists?
  bus.proxy.ListNames[0].member?(@name)
end

#export(obj) ⇒ Object

Export an object obj (an DBus::Object subclass instance).



71
72
73
74
# File 'lib/dbus/bus.rb', line 71

def export(obj)
  obj.service = self
  get_node(obj.path, true).object = obj
end

#get_node(path, create = false) ⇒ Object

Get the object node corresponding to the given path. if create is true, the the nodes in the path are created if they do not already exist.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dbus/bus.rb', line 94

def get_node(path, create = false)
  n = @root
  path.sub(%r{^/}, "").split("/").each do |elem|
    if !(n[elem])
      return nil if !create
      n[elem] = Node.new(elem)
    end
    n = n[elem]
  end
  if n.nil?
    DBus.logger.debug "Warning, unknown object #{path}"
  end
  n
end

#introspectObject

Perform an introspection on all the objects on the service (starting recursively from the root).

Raises:

  • (NotImplementedError)


43
44
45
46
47
48
# File 'lib/dbus/bus.rb', line 43

def introspect
  raise NotImplementedError if block_given?

  rec_introspect(@root, "/")
  self
end

#object(path, api: ApiOptions::A0) ⇒ ProxyObject

Retrieves an object at the given path whose methods always return an array.

Returns:



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

def object(path, api: ApiOptions::A0)
  node = get_node(path, _create = true)
  if node.object.nil? || node.object.api != api
    node.object = ProxyObject.new(
      @bus, @name, path,
      api: api
    )
  end
  node.object
end

#unexport(obj) ⇒ Object

Undo exporting an object obj. Raises ArgumentError if it is not a DBus::Object. Returns the object, or false if obj was not exported.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dbus/bus.rb', line 79

def unexport(obj)
  raise ArgumentError, "DBus::Service#unexport() expects a DBus::Object argument" unless obj.is_a?(DBus::Object)
  return false unless obj.path
  last_path_separator_idx = obj.path.rindex("/")
  parent_path = obj.path[1..last_path_separator_idx - 1]
  node_name = obj.path[last_path_separator_idx + 1..-1]

  parent_node = get_node(parent_path, false)
  return false unless parent_node
  obj.service = nil
  parent_node.delete(node_name).object
end