Class: DBus::ObjectServer

Inherits:
NodeTree show all
Defined in:
lib/dbus/object_server.rb

Overview

The part of a Connection that can export Objects 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.

Examples:

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

Instance Attribute Summary collapse

Attributes inherited from NodeTree

#root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeTree

#get_node

Constructor Details

#initialize(connection) ⇒ ObjectServer

Returns a new instance of ObjectServer.



32
33
34
35
# File 'lib/dbus/object_server.rb', line 32

def initialize(connection)
  @connection = connection
  super()
end

Instance Attribute Details

#connectionConnection (readonly)

Returns The connection we’re using.

Returns:



30
31
32
# File 'lib/dbus/object_server.rb', line 30

def connection
  @connection
end

Class Method Details

.path_of(obj_or_path) ⇒ ObjectPath

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dbus/object_server.rb', line 118

def self.path_of(obj_or_path)
  case obj_or_path
  when ObjectPath
    obj_or_path
  when String
    ObjectPath.new(obj_or_path)
  when DBus::Object
    obj_or_path.path
  else
    raise ArgumentError, "Expecting a DBus::Object argument or DBus::ObjectPath or String which parses as one"
  end
end

Instance Method Details

#descendants_for(path) ⇒ Array<DBus::Object>

All objects (not paths) under this path (except itself).

Parameters:

Returns:

Raises:

  • ArgumentError if the path does not exist



108
109
110
111
112
113
# File 'lib/dbus/object_server.rb', line 108

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

#export(obj) ⇒ Object

Export an object

Parameters:

Raises:

  • RuntimeError if there’s already an exported object at the same path



49
50
51
52
53
54
55
56
57
# File 'lib/dbus/object_server.rb', line 49

def export(obj)
  node = get_node(obj.path, create: true)
  raise "At #{obj.path} there is already an object #{node.object.inspect}" if node.object

  node.object = obj

  obj.object_server = self
  object_manager_for(obj)&.object_added(obj)
end

#object(path) ⇒ DBus::Object? Also known as: []

Retrieves an object at the given path

Parameters:

Returns:



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

def object(path)
  node = get_node(path, create: false)
  node&.object
end

#object_manager_for(object) ⇒ DBus::Object?

Find the (closest) parent of object implementing the ObjectManager interface, or nil

Returns:



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

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

#unexport(obj_or_path) ⇒ Object

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.

Parameters:

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dbus/object_server.rb', line 63

def unexport(obj_or_path)
  path = self.class.path_of(obj_or_path)
  parent_path, _separator, node_name = path.rpartition("/")

  parent_node = get_node(parent_path, create: false)
  return false unless parent_node

  node = if node_name == "" # path == "/"
           parent_node
         else
           parent_node[node_name]
         end
  obj = node&.object
  raise ArgumentError, "Cannot unexport, no object at #{path}" unless obj

  object_manager_for(obj)&.object_removed(obj)
  obj.object_server = nil
  node.object = nil

  # node can be deleted if
  # - it has no children
  # - it is not root
  if node.empty? && !node.equal?(parent_node)
    parent_node.delete(node_name)
  end

  obj
end