class DBus::Service
This represents a remote service. It should not be instantiated directly Use {Connection#service}
Attributes
The bus the service is running on.
The service name.
The service root (FIXME).
Public Class Methods
Create a new service with a given name on a given bus.
# File lib/dbus/bus.rb, line 31 def initialize(name, bus) @name = BusName.new(name) @bus = bus @root = Node.new("/") end
Public Instance Methods
Retrieves an object at the given path. @param path [ObjectPath] @return [ProxyObject]
# File lib/dbus/bus.rb, line 54 def [](path) object(path, api: ApiOptions::A1) end
All objects (not paths) under this path (except itself). @param path [ObjectPath] @return [Array<DBus::Object>] @raise ArgumentError if the path does not exist
# File lib/dbus/bus.rb, line 136 def descendants_for(path) node = get_node(path, create: false) raise ArgumentError, "Object path #{path} doesn't exist" if node.nil? node.descendant_objects end
Determine whether the service name already exists.
# File lib/dbus/bus.rb, line 38 def exists? bus.proxy.ListNames[0].member?(@name) end
Export an object @param obj [DBus::Object]
# File lib/dbus/bus.rb, line 76 def export(obj) obj.service = self get_node(obj.path, create: true).object = obj object_manager_for(obj)&.object_added(obj) end
Get the object node corresponding to the given path. @param path [ObjectPath] @param create [Boolean] if true, the the {Node}s in the path are created
if they do not already exist.
@return [Node,nil]
# File lib/dbus/bus.rb, line 107 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 n end
Perform an introspection on all the objects on the service (starting recursively from the root).
# File lib/dbus/bus.rb, line 44 def introspect raise NotImplementedError if block_given? rec_introspect(@root, "/") self end
Retrieves an object at the given path whose methods always return an array. @param path [ObjectPath] @param api [ApiOptions] @return [ProxyObject]
# File lib/dbus/bus.rb, line 63 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
Find the (closest) parent of object implementing the ObjectManager
interface, or nil @return [DBus::Object,nil]
# File lib/dbus/bus.rb, line 123 def object_manager_for(object) path = object.path node_chain = get_node_chain(path) om_node = node_chain.reverse_each.find do |node| node.object&.is_a? DBus::ObjectManager end om_node&.object end
Undo exporting an object obj. Raises ArgumentError if it is not a DBus::Object
. Returns the object, or false if obj was not exported. @param obj [DBus::Object]
# File lib/dbus/bus.rb, line 86 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, create: false) return false unless parent_node object_manager_for(obj)&.object_removed(obj) obj.service = nil parent_node.delete(node_name).object end
Private Instance Methods
@raise ArgumentError if the path does not exist
# File lib/dbus/bus.rb, line 150 def get_node_chain(path) n = @root result = [n] path.sub(%r{^/}, "").split("/").each do |elem| n = n[elem] raise ArgumentError, "Object path #{path} doesn't exist" if n.nil? result.push(n) end result end
Perform a recursive retrospection on the given current node on the given path.
# File lib/dbus/bus.rb, line 164 def rec_introspect(node, path) xml = bus.introspect_data(@name, path) intfs, subnodes = IntrospectXMLParser.new(xml).parse subnodes.each do |nodename| subnode = node[nodename] = Node.new(nodename) subpath = if path == "/" "/#{nodename}" else "#{path}/#{nodename}" end rec_introspect(subnode, subpath) end return if intfs.empty? node.object = ProxyObjectFactory.new(xml, @bus, @name, path).build end