class DBus::ObjectServer

The part of a {Connection} that can export {DBus::Object}s to provide services to clients.

Note that an ObjectServer does not have a name. Typically a {Connection} has one well known name, but can have none or more.

Formerly this class was intermixed with {ProxyService} as Service.

@example Usage

bus = DBus.session_bus
obj = DBus::Object.new("/path") # a subclass more likely
bus.object_server.export(obj)
bus.request_name("org.example.Test")

Attributes

connection[R]

@return [Connection] The connection we’re using.

Public Class Methods

new(connection) click to toggle source
Calls superclass method DBus::NodeTree::new
   # File lib/dbus/object_server.rb
32 def initialize(connection)
33   @connection = connection
34   super()
35 end
path_of(obj_or_path) click to toggle source

@param obj_or_path [DBus::Object,ObjectPath,String] an object or a valid object path @return [ObjectPath] @api private

    # File lib/dbus/object_server.rb
118 def self.path_of(obj_or_path)
119   case obj_or_path
120   when ObjectPath
121     obj_or_path
122   when String
123     ObjectPath.new(obj_or_path)
124   when DBus::Object
125     obj_or_path.path
126   else
127     raise ArgumentError, "Expecting a DBus::Object argument or DBus::ObjectPath or String which parses as one"
128   end
129 end

Public Instance Methods

[](path)
Alias for: object
descendants_for(path) click to toggle source

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/object_server.rb
108 def descendants_for(path)
109   node = get_node(path, create: false)
110   raise ArgumentError, "Object path #{path} doesn't exist" if node.nil?
111 
112   node.descendant_objects
113 end
export(obj) click to toggle source

Export an object @param obj [DBus::Object] @raise RuntimeError if there’s already an exported object at the same path

   # File lib/dbus/object_server.rb
49 def export(obj)
50   node = get_node(obj.path, create: true)
51   raise "At #{obj.path} there is already an object #{node.object.inspect}" if node.object
52 
53   node.object = obj
54 
55   obj.object_server = self
56   object_manager_for(obj)&.object_added(obj)
57 end
object(path) click to toggle source

Retrieves an object at the given path @param path [ObjectPath] @return [DBus::Object,nil]

   # File lib/dbus/object_server.rb
40 def object(path)
41   node = get_node(path, create: false)
42   node&.object
43 end
Also aliased as: []
object_manager_for(object) click to toggle source

Find the (closest) parent of object implementing the ObjectManager interface, or nil @return [DBus::Object,nil]

    # File lib/dbus/object_server.rb
 95 def object_manager_for(object)
 96   path = object.path
 97   node_chain = get_node_chain(path)
 98   om_node = node_chain.reverse_each.find do |node|
 99     node.object&.is_a? DBus::ObjectManager
100   end
101   om_node&.object
102 end
unexport(obj_or_path) click to toggle source

Undo exporting an object obj_or_path. Raises ArgumentError if it is not a DBus::Object. Returns the object, or false if obj was not exported. @param obj_or_path [DBus::Object,ObjectPath,String] an object or a valid object path

   # File lib/dbus/object_server.rb
63 def unexport(obj_or_path)
64   path = self.class.path_of(obj_or_path)
65   parent_path, _separator, node_name = path.rpartition("/")
66 
67   parent_node = get_node(parent_path, create: false)
68   return false unless parent_node
69 
70   node = if node_name == "" # path == "/"
71            parent_node
72          else
73            parent_node[node_name]
74          end
75   obj = node&.object
76   raise ArgumentError, "Cannot unexport, no object at #{path}" unless obj
77 
78   object_manager_for(obj)&.object_removed(obj)
79   obj.object_server = nil
80   node.object = nil
81 
82   # node can be deleted if
83   # - it has no children
84   # - it is not root
85   if node.empty? && !node.equal?(parent_node)
86     parent_node.delete(node_name)
87   end
88 
89   obj
90 end

Private Instance Methods

get_node_chain(path) click to toggle source

@param path [ObjectPath] a path that must exist @return [Array<Node>] nodes from the root to the leaf

    # File lib/dbus/object_server.rb
139 def get_node_chain(path)
140   n = @root
141   result = [n]
142   path.sub(%r{^/}, "").split("/").each do |elem|
143     n = n[elem]
144     result.push(n)
145   end
146   result
147 end