Create placeholder Owner object when PS starts, fill in services later

This commit is contained in:
Dan Williams 2006-09-06 10:16:49 -04:00
parent f40485aad4
commit ef254ce128
2 changed files with 47 additions and 21 deletions

View File

@ -85,29 +85,35 @@ class Buddy(object):
"""Represents another person on the network and keeps track of the """Represents another person on the network and keeps track of the
activities and resources they make available for sharing.""" activities and resources they make available for sharing."""
def __init__(self, bus_name, object_id, service, owner=False): def __init__(self, bus_name, object_id, service):
if not bus_name: if not bus_name:
raise ValueError("DBus bus name must be valid") raise ValueError("DBus bus name must be valid")
if not object_id or type(object_id) != type(1): if not object_id or type(object_id) != type(1):
raise ValueError("object id must be a valid number") raise ValueError("object id must be a valid number")
# Normal Buddy objects must be created with a valid service,
# owner objects do not
if not isinstance(self, Owner):
if not isinstance(service, Service.Service): if not isinstance(service, Service.Service):
raise ValueError("service must be a valid service object") raise ValueError("service must be a valid service object")
self._services = {} self._services = {}
self._activities = {} self._activities = {}
self._nick_name = None
self._address = None
if service is not None:
self._nick_name = service.get_name() self._nick_name = service.get_name()
self._address = service.get_source_address() self._address = service.get_source_address()
self._color = None self._color = None
self._valid = False self._valid = False
self._icon = None self._icon = None
self._icon_tries = 0 self._icon_tries = 0
self._owner = owner
self._object_id = object_id self._object_id = object_id
self._object_path = BUDDY_DBUS_OBJECT_PATH + str(self._object_id) self._object_path = BUDDY_DBUS_OBJECT_PATH + str(self._object_id)
self._dbus_helper = BuddyDBusHelper(self, bus_name, self._object_path) self._dbus_helper = BuddyDBusHelper(self, bus_name, self._object_path)
if service is not None:
self.add_service(service) self.add_service(service)
def object_path(self): def object_path(self):
@ -263,14 +269,33 @@ class Buddy(object):
self._dbus_helper.IconChanged() self._dbus_helper.IconChanged()
def is_owner(self): def is_owner(self):
return self._owner return False
class Owner(Buddy): class Owner(Buddy):
"""Class representing the owner of the machine. This is the client """Class representing the owner of the machine. This is the client
portion of the Owner, paired with the server portion in Owner.py.""" portion of the Owner, paired with the server portion in Owner.py."""
def __init__(self, bus_name, object_id, service): def __init__(self, ps, bus_name, object_id, nick):
Buddy.__init__(self, bus_name, object_id, service, owner=True) Buddy.__init__(self, bus_name, object_id, None)
self._nick_name = nick
self._ps = ps
def add_service(self, service):
"""Adds a new service to this buddy's service list, returning
True if the service was successfully added, and False if it was not."""
if service.get_name() != self._nick_name:
return False
# The Owner initially doesn't have an address, so the first
# service added to the Owner determines the owner's address
source_addr = service.get_source_address()
if self._address is None:
if source_addr in self._ps.is_local_ip_address(source_addr):
self._address = source_addr
return Buddy.add_service(self, service)
def is_owner(self):
return True
################################################################# #################################################################

View File

@ -7,6 +7,7 @@ import logging
from sugar import env from sugar import env
from sugar import util from sugar import util
def _get_local_ip_address(ifname): def _get_local_ip_address(ifname):
"""Call Linux specific bits to retrieve our own IP address.""" """Call Linux specific bits to retrieve our own IP address."""
import socket import socket
@ -241,9 +242,6 @@ class PresenceService(object):
self._next_object_id = 0 self._next_object_id = 0
# Our owner object
self._owner = None
self._buddies = {} # nick -> Buddy self._buddies = {} # nick -> Buddy
self._services = {} # (name, type) -> Service self._services = {} # (name, type) -> Service
self._activities = {} # activity id -> Activity self._activities = {} # activity id -> Activity
@ -264,6 +262,11 @@ class PresenceService(object):
self._bus_name = dbus.service.BusName(_PRESENCE_SERVICE, bus=self._session_bus) self._bus_name = dbus.service.BusName(_PRESENCE_SERVICE, bus=self._session_bus)
self._dbus_helper = PresenceServiceDBusHelper(self, self._bus_name) self._dbus_helper = PresenceServiceDBusHelper(self, self._bus_name)
# Our owner object
owner_nick = env.get_nick_name()
objid = self._get_next_object_id()
self._owner = Buddy.Owner(self, self._bus_name, objid, owner_nick)
self._started = False self._started = False
def start(self): def start(self):
@ -337,6 +340,11 @@ class PresenceService(object):
def get_owner(self): def get_owner(self):
return self._owner return self._owner
def is_local_ip_address(self, address):
if address in self._local_addrs.values():
return True
return False
def _find_service_adv(self, interface=None, protocol=None, name=None, stype=None, domain=None): def _find_service_adv(self, interface=None, protocol=None, name=None, stype=None, domain=None):
"""Search a list of service advertisements for ones matching certain criteria.""" """Search a list of service advertisements for ones matching certain criteria."""
adv_list = [] adv_list = []
@ -368,15 +376,8 @@ class PresenceService(object):
if service_added: if service_added:
self._dbus_helper.ServiceAppeared(service.object_path()) self._dbus_helper.ServiceAppeared(service.object_path())
except KeyError: except KeyError:
# Should this service mark the owner?
owner_nick = env.get_nick_name()
source_addr = service.get_source_address() source_addr = service.get_source_address()
objid = self._get_next_object_id() objid = self._get_next_object_id()
if name == owner_nick and source_addr in self._local_addrs.values():
buddy = Buddy.Owner(self._bus_name, objid, service)
self._owner = buddy
logging.debug("Owner is '%s'." % name)
else:
buddy = Buddy.Buddy(self._bus_name, objid, service) buddy = Buddy.Buddy(self._bus_name, objid, service)
self._buddies[name] = buddy self._buddies[name] = buddy
self._dbus_helper.ServiceAppeared(service.object_path()) self._dbus_helper.ServiceAppeared(service.object_path())