From 6d7940949adff699c609b0d11c9a7959087cfab8 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 17 Jun 2006 22:06:20 -0400 Subject: [PATCH] Empty addresses are valid, meaning the buddy own address. For group (multicast) services publisher_address != service_address, introduce the distinction in the API and in the avahi announcement. --- sugar/presence/Buddy.py | 4 +++- sugar/presence/PresenceService.py | 10 ++++++++-- sugar/presence/Service.py | 13 +++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sugar/presence/Buddy.py b/sugar/presence/Buddy.py index dd25d709..b55230b8 100644 --- a/sugar/presence/Buddy.py +++ b/sugar/presence/Buddy.py @@ -1,4 +1,5 @@ import base64 +import logging import pygtk pygtk.require('2.0') @@ -65,7 +66,8 @@ class Buddy(gobject.GObject): True if the service was successfully added, and False if it was not.""" if service.get_name() != self._nick_name: return False - if service.get_address() != self._address: + if service.get_publisher_address() != self._address: + logging.error('Service publisher and buddy address doesnt match: %s %s' % (service.get_publisher_address(), self._address)) return False if service.get_type() in self._services.keys(): return False diff --git a/sugar/presence/PresenceService.py b/sugar/presence/PresenceService.py index 739f6424..b76b49c2 100644 --- a/sugar/presence/PresenceService.py +++ b/sugar/presence/PresenceService.py @@ -322,6 +322,9 @@ class PresenceService(gobject.GObject): if resolve and not adv in self._resolve_queue: self._resolve_queue.append(adv) gobject.idle_add(self._resolve_service, adv) + else: + logging.debug("Do not resolve service '%s' of type '%s', we don't care about it." % (name, full_stype)) + return False def _service_appeared_cb_glue(self, interface, protocol, name, stype, domain, flags): @@ -507,17 +510,17 @@ class PresenceService(gobject.GObject): real_stype = Service.compose_service_type(stype, uid) if address and type(address) != type(""): raise ValueError("address must be a valid string.") - if not address: + if address == None: # Use random currently unassigned multicast address address = "232.%d.%d.%d" % (random.randint(0, 254), random.randint(1, 254), random.randint(1, 254)) - if port and (type(port) != type(1) or port <= 1024 or port >= 65535): raise ValueError("port must be a number between 1024 and 65535") if not port: # random port # port = random.randint(5000, 65535) + logging.debug('Share activity %s, address %s, port %d' % (stype, address, port)) service = Service.Service(name=owner_nick, stype=real_stype, domain="local", address=address, port=port, properties=properties) # Publish it to the world @@ -536,6 +539,7 @@ class PresenceService(gobject.GObject): raise ValueError("invalid service port.") rs_props = service.get_properties() rs_domain = service.get_domain() + rs_address = service.get_address() if not rs_domain or not len(rs_domain): rs_domain = "" logging.debug("registered service name '%s' type '%s' on port %d with args %s" % (rs_name, rs_stype, rs_port, rs_props)) @@ -543,6 +547,8 @@ class PresenceService(gobject.GObject): try: group = dbus.Interface(self._bus.get_object(avahi.DBUS_NAME, self._server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) info = ["%s=%s" % (k, v) for k, v in rs_props.items()] + if rs_address and len(rs_address): + info.append("address=%s" % (rs_address)) group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, 0, rs_name, rs_stype, rs_domain, "", # let Avahi figure the 'host' out dbus.UInt16(rs_port), info,) diff --git a/sugar/presence/Service.py b/sugar/presence/Service.py index 4740f785..c97830f4 100644 --- a/sugar/presence/Service.py +++ b/sugar/presence/Service.py @@ -92,12 +92,16 @@ class Service(object): self._full_stype = full_stype self._activity_stype = short_stype self._domain = domain - self._address = None - self.set_address(address) self._port = -1 self.set_port(port) self._properties = {} self.set_properties(properties) + self._publisher_address = address + self._address = None + if self._properties.has_key('address'): + self.set_address(self._properties['address']) + else: + self.set_address(address) # Ensure that an ActivityUID tag, if given, matches # what we expect from the service type @@ -166,6 +170,9 @@ class Service(object): raise ValueError("must specify a valid port number.") self._port = port + def get_publisher_address(self): + return self._publisher_address + def get_address(self): return self._address @@ -173,8 +180,6 @@ class Service(object): if address is not None: if type(address) != type("") and type(address) != type(u""): raise ValueError("must specify a valid address.") - if not len(address): - raise ValueError("must specify a valid address.") if address and type(address) == type(u""): address = address.encode() self._address = address