blob: 01b3b86ad1f9b17768fd4b5d9c2a4086971e6073 [file] [log] [blame]
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
import dbus
import dbus.bus
import dbus.service
import logging
import uuid
from autotest_lib.client.cros import dbus_util
from autotest_lib.client.cros.tendo import peerd_dbus_helper
from autotest_lib.client.cros.tendo.n_faced_peerd import dbus_property_exposer
from autotest_lib.client.cros.tendo.n_faced_peerd import peer
from autotest_lib.client.cros.tendo.n_faced_peerd import service
# A tuple of a bus name that sent us an ExposeService message, and an
# object responsible for watching for the death of that bus name's
# DBus connection.
SenderWatch = collections.namedtuple('SenderWatch', ['sender', 'watch'])
IGNORED_MONITORING_TOKEN_VALUE = 'This is a monitoring token.'
class InvalidMonitoringTokenException(Exception):
"""Self explanatory."""
class Manager(dbus_property_exposer.DBusPropertyExposer):
"""Represents an instance of org.chromium.peerd.Manager."""
def __init__(self, bus, ip_address, on_service_modified, unique_name,
"""Construct an instance of Manager.
@param bus: dbus.Bus object to export this object on.
@param ip_address: string IP address (e.g. '127.0.01').
@param on_service_modified: callback that takes a Manager instance and
a service ID. We'll call this whenever we Expose/Remove a
service via the DBus API.
@param unique_name: string DBus well known name to claim on DBus.
@param object_manager: an instance of ObjectManager.
super(Manager, self).__init__(bus,
self._bus = bus
self._object_manager = object_manager
self._peer_counter = 0
self._peers = dict()
self._ip_address = ip_address
self._on_service_modified = on_service_modified
# A map from service_ids to dbus.bus.NameOwnerWatch objects.
self._watches = dict()
self.self_peer = peer.Peer(self._bus,
# TODO(wiley) Expose monitored technologies property
if (self._bus.request_name(unique_name) !=
raise Exception('Failed to claim name %s' % unique_name)
def _on_name_owner_changed(self, service_id, owner):
"""Callback that removes a service when the owner disconnects from DBus.
@param service_id: string service_id of service to remove.
@param owner: dbus.String identifier of service owner.
owner = dbus_util.dbus2primitive(owner)
logging.debug('Name owner for service=%s changed to "%s".',
service_id, owner)
if not owner:
def close(self):
"""Release resources held by this object and child objects."""
# TODO(wiley) call close on self and remote peers.
raise NotImplementedError('Manager.close() does not work properly')
def add_remote_peer(self, remote_peer):
"""Add a remote peer to this object.
For any given face of NFacedPeerd, the other N - 1 faces are treated
as "remote peers" that we instantly discover changes on.
@param remote_peer: Peer object. Should be the |self_peer| of another
instance of Manager.
"""'Adding remote peer %s', remote_peer.uuid)
self._peer_counter += 1
peer_path = '%s%d' % (peerd_dbus_helper.PEER_PATH_PREFIX,
self._peers[remote_peer.uuid] = peer.Peer(
self._bus, peer_path, remote_peer.uuid, self._object_manager)
def on_remote_service_modified(self, remote_peer, service_id):
"""Cause this face to update its view of a remote peer.
@param remote_peer: Peer object. Should be the |self_peer| of another
instance of Manager.
@param service_id: string service ID of remote service that has changed.
Note that this service may have been removed.
local_peer = self._peers[remote_peer.uuid]
remote_service =
if remote_service is not None:'Exposing remote service: %s', service_id)
else:'Removing remote service: %s', service_id)
in_signature='sa{ss}a{sv}', out_signature='',
def ExposeService(self, service_id, service_info, options, sender=None):
"""Implementation of org.chromium.peerd.Manager.ExposeService().
@param service_id: see DBus API documentation.
@param service_info: see DBus API documentation.
@param options: see DBus API documentation.
@param sender: string DBus connection ID of caller. Must match
|sender_keyword| value in decorator.
if (service_id in self._watches and
sender != self._watches[service_id].sender):
logging.error('Asked to advertise a duplicate service by %s. '
'Expected %s.',
sender, self._watches[service_id].sender)
raise Exception('Will not advertise duplicate services.')'Exposing service %s', service_id)
port = options.get('mdns', dict()).get('port', 0)
service_id, service_info,
service.IpInfo(addr=self._ip_address, port=port))
if service_id not in self._watches:
cb = lambda owner: self._on_name_owner_changed(service_id, owner)
watch = dbus.bus.NameOwnerWatch(self._bus, sender, cb)
self._watches[service_id] = SenderWatch(sender, watch)
self._on_service_modified(self, service_id)
in_signature='s', out_signature='')
def RemoveExposedService(self, service_id):
"""Implementation of org.chromium.peerd.Manager.RemoveExposedService().
@param service_id: see DBus API documentation.
"""'Removing service %s', service_id)
self._watches.pop(service_id, None)
self._on_service_modified(self, service_id)
in_signature='asa{sv}', out_signature='s')
def StartMonitoring(self, technologies, options):
"""Fake implementation of org.chromium.peerd.Manager.StartMonitoring().
We do not monitor anything in NFacedPeerd.
@param technologies: See DBus API documentation.
@param options: See DBus API documentation.
in_signature='s', out_signature='')
def StopMonitoring(self, monitoring_token):
"""Fake implementation of org.chromium.peerd.Manager.StopMonitoring().
We do not monitor anything in NFacedPeerd
@param monitoring_token: See DBus API documentation.
if monitoring_token != IGNORED_MONITORING_TOKEN_VALUE:
raise InvalidMonitoringTokenException()
in_signature='', out_signature='s')
def Ping(self):
"""Implementation of org.chromium.peerd.Manager.Ping()."""
return 'Hello world!'