# class DBus::Service

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

### Attributes

bus[R]

The bus the service is running on.

name[R]

The service name.

root[R]

The service root (FIXME).

### Public Class Methods

new(name, bus) click to toggle source

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

       # File lib/dbus/bus.rb
    30 def initialize(name, bus)
    31   @name = BusName.new(name)
    32   @bus = bus
    33   @root = Node.new("/")
    34 end

### Public Instance Methods

[](path) click to toggle source

Retrieves an object at the given path. @return [ProxyObject]

       # File lib/dbus/bus.rb
    52 def [](path)
    53   object(path, api: ApiOptions::A1)
    54 end

exists?() click to toggle source

Determine whether the service name already exists.

       # File lib/dbus/bus.rb
    37 def exists?
    38   bus.proxy.ListNames[0].member?(@name)
    39 end

export(obj) click to toggle source

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

       # File lib/dbus/bus.rb
    71 def export(obj)
    72   obj.service = self
    73   get_node(obj.path, true).object = obj
    74 end

get_node(path, create = false) click to toggle source

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.

        # File lib/dbus/bus.rb
     94 def get_node(path, create = false)
     95   n = @root
     96   path.sub(%r{^/}, "").split("/").each do |elem|
     97     if !(n[elem])
     98       return nil if !create
     99       n[elem] = Node.new(elem)
    100     end
    101     n = n[elem]
    102   end
    103   if n.nil?
    104     DBus.logger.debug "Warning, unknown object #{path}"
    105   end
    106   n
    107 end

introspect() click to toggle source

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

       # File lib/dbus/bus.rb
    43 def introspect
    44   raise NotImplementedError if block_given?
    45
    46   rec_introspect(@root, "/")
    47   self
    48 end

object(path, api: ApiOptions::A0) click to toggle source

Retrieves an object at the given path whose methods always return an array. @return [ProxyObject]

       # File lib/dbus/bus.rb
    59 def object(path, api: ApiOptions::A0)
    60   node = get_node(path, _create = true)
    61   if node.object.nil? || node.object.api != api
    62     node.object = ProxyObject.new(
    63       @bus, @name, path,
    64       api: api
    65     )
    66   end
    67   node.object
    68 end

unexport(obj) click to toggle source

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

       # File lib/dbus/bus.rb
    79 def unexport(obj)
    80   raise ArgumentError, "DBus::Service#unexport() expects a DBus::Object argument" unless obj.is_a?(DBus::Object)
    81   return false unless obj.path
    82   last_path_separator_idx = obj.path.rindex("/")
    83   parent_path = obj.path[1..last_path_separator_idx - 1]
    84   node_name = obj.path[last_path_separator_idx + 1..-1]
    85
    86   parent_node = get_node(parent_path, false)
    87   return false unless parent_node
    88   obj.service = nil
    89   parent_node.delete(node_name).object
    90 end

### Private Instance Methods

rec_introspect(node, path) click to toggle source

Perform a recursive retrospection on the given current node on the given path.

        # File lib/dbus/bus.rb
    117 def rec_introspect(node, path)
    118   xml = bus.introspect_data(@name, path)
    119   intfs, subnodes = IntrospectXMLParser.new(xml).parse
    120   subnodes.each do |nodename|
    121     subnode = node[nodename] = Node.new(nodename)
    122     subpath = if path == "/"
    123                 "/" + nodename
    124               else
    125                 path + "/" + nodename
    126               end
    127     rec_introspect(subnode, subpath)
    128   end
    129   return if intfs.empty?
    130   node.object = ProxyObjectFactory.new(xml, @bus, @name, path).build
    131 end