blob: aee047d941d64c26cf822937a6a422c207713a1e [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 dbus
import dbus.service
import logging
import time
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 service
class Peer(dbus_property_exposer.DBusPropertyExposer):
"""Represents local and remote peers."""
def __init__(self, bus, path, peer_id, object_manager, is_self=False):
"""Construct a org.chromium.peerd.Peer DBus object.
@param bus: dbus.Bus object to export this object on.
@param path: string object path to export this object at.
@param peer_id: string peerd peer ID; a UUID.
@param is_self: True iff this object will servce as a self peer.
@param object_manager: an instance of ObjectManager.
super(Peer, self).__init__(
bus, path, peerd_dbus_helper.DBUS_INTERFACE_PEER)
# Fill in the initial values for our properties.
self.uuid = peer_id
self._is_self = is_self
# Register properties with the property exposer.
# Claim our interace with the object manager.
self._object_manager = object_manager
self._path = path
path, peerd_dbus_helper.DBUS_INTERFACE_PEER,
# We need to keep a good bit of stuff around because we're responsible
# for creating child service objects.
self._bus = bus = dict()
self._services_counter = 0
def _get_dbus_id(self):
"""Getter for PEER_PROPERTY_ID.
@return dbus.String containing our peer ID.
return dbus.String(self.uuid)
def _get_dbus_last_seen(self):
@return dbus.UInt64 containing the last time this peer was seen
in milliseconds since the Unix epoc.
return dbus.UInt64(int(1000 * self._last_seen_seconds))
def _update_last_seen(self):
"""Updates our last seen value.
This would be a simple call to time.time(), except that peerd
has to report a last seen time of 0 for the peer object representing
if self._is_self:
self._last_seen_seconds = 0
self._last_seen_seconds = time.time()
def close(self):
"""Releases interfaces claimed over DBus."""
# TODO(wiley) call close on child services.
raise NotImplementedError('Peer.close() does not work properly')
def update_service(self, service_id, service_info, ip_info):
"""Update a service associated with this peer.
@param service_id: string peerd service ID.
@param service_info: dictionary of string,string items comprising
the metadata for the service.
@param ip_info: an instance of IpInfo defined in
if service_id in[service_id].update(service_info, ip_info)
self._services_counter += 1
service_path = '%s/services/%d' % (self._path,
self._services_counter)[service_id] = service.Service(
self._bus, service_path, self.uuid, service_id,
service_info, ip_info, self._object_manager)'service=%s has info %r.', service_id, service_info)
def remove_service(self, service_id):
"""Remove a service associated with this peer.
@param service_id: string peerd service ID.
removed_service =, None)
if removed_service is not None: