2006-07-25 22:52:45 +02:00
|
|
|
import avahi, dbus, dbus.glib, gobject
|
2006-07-18 16:51:04 +02:00
|
|
|
import Buddy
|
|
|
|
import Service
|
2006-07-26 02:04:15 +02:00
|
|
|
import Activity
|
2006-07-18 16:51:04 +02:00
|
|
|
import random
|
|
|
|
import logging
|
2006-07-24 17:55:42 +02:00
|
|
|
from sugar import util
|
2006-10-09 15:11:15 +02:00
|
|
|
from sugar import env
|
2006-09-25 16:57:21 +02:00
|
|
|
import BuddyIconCache
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-09-06 16:16:49 +02:00
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
_SA_UNRESOLVED = 0
|
|
|
|
_SA_RESOLVE_PENDING = 1
|
|
|
|
_SA_RESOLVED = 2
|
2006-07-18 16:51:04 +02:00
|
|
|
class ServiceAdv(object):
|
|
|
|
"""Wrapper class to track services from Avahi."""
|
2006-09-12 18:36:24 +02:00
|
|
|
def __init__(self, interface, protocol, name, stype, domain, local):
|
2006-07-18 16:51:04 +02:00
|
|
|
self._interface = interface
|
|
|
|
self._protocol = protocol
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(name, unicode):
|
2006-07-18 16:51:04 +02:00
|
|
|
raise ValueError("service advertisement name must be unicode.")
|
|
|
|
self._name = name
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(stype, unicode):
|
2006-07-18 16:51:04 +02:00
|
|
|
raise ValueError("service advertisement type must be unicode.")
|
|
|
|
self._stype = stype
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(domain, unicode):
|
2006-07-18 16:51:04 +02:00
|
|
|
raise ValueError("service advertisement domain must be unicode.")
|
|
|
|
self._domain = domain
|
|
|
|
self._service = None
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(local, bool):
|
|
|
|
raise ValueError("local must be a bool.")
|
2006-09-12 18:36:24 +02:00
|
|
|
self._local = local
|
|
|
|
self._state = _SA_UNRESOLVED
|
2006-09-19 19:54:32 +02:00
|
|
|
self._resolver = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self._resolver:
|
|
|
|
del self._resolver
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
def interface(self):
|
|
|
|
return self._interface
|
|
|
|
def protocol(self):
|
|
|
|
return self._protocol
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
def stype(self):
|
|
|
|
return self._stype
|
|
|
|
def domain(self):
|
|
|
|
return self._domain
|
2006-09-12 18:36:24 +02:00
|
|
|
def is_local(self):
|
|
|
|
return self._local
|
2006-07-18 16:51:04 +02:00
|
|
|
def service(self):
|
|
|
|
return self._service
|
|
|
|
def set_service(self, service):
|
|
|
|
if not isinstance(service, Service.Service):
|
|
|
|
raise ValueError("must be a valid service.")
|
2006-09-19 19:54:32 +02:00
|
|
|
if service != self._service:
|
|
|
|
self._service = service
|
|
|
|
def resolver(self):
|
|
|
|
return self._resolver
|
|
|
|
def set_resolver(self, resolver):
|
|
|
|
if not isinstance(resolver, dbus.Interface):
|
|
|
|
raise ValueError("must be a valid dbus object")
|
|
|
|
self._resolver = resolver
|
2006-09-12 18:36:24 +02:00
|
|
|
def state(self):
|
|
|
|
return self._state
|
|
|
|
def set_state(self, state):
|
|
|
|
if state == _SA_RESOLVE_PENDING:
|
|
|
|
if self._state == _SA_RESOLVED:
|
|
|
|
raise ValueError("Can't reset to resolve pending from resolved.")
|
|
|
|
self._state = state
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
class RegisteredServiceType(object):
|
|
|
|
def __init__(self, stype):
|
|
|
|
self._stype = stype
|
|
|
|
self._refcount = 1
|
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
return self._stype
|
|
|
|
|
|
|
|
def ref(self):
|
|
|
|
self._refcount += 1
|
|
|
|
|
|
|
|
def unref(self):
|
|
|
|
self._refcount -= 1
|
|
|
|
return self._refcount
|
|
|
|
|
|
|
|
def _txt_to_dict(txt):
|
|
|
|
"""Convert an avahi-returned TXT record formatted
|
|
|
|
as nested arrays of integers (from dbus) into a dict
|
|
|
|
of key/value string pairs."""
|
|
|
|
prop_dict = {}
|
|
|
|
props = avahi.txt_array_to_string_array(txt)
|
|
|
|
for item in props:
|
|
|
|
key = value = None
|
|
|
|
if '=' not in item:
|
|
|
|
# No = means a boolean value of true
|
|
|
|
key = item
|
|
|
|
value = True
|
|
|
|
else:
|
|
|
|
(key, value) = item.split('=')
|
|
|
|
prop_dict[key] = value
|
|
|
|
return prop_dict
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
_PRESENCE_SERVICE = "org.laptop.Presence"
|
|
|
|
_PRESENCE_DBUS_INTERFACE = "org.laptop.Presence"
|
|
|
|
_PRESENCE_OBJECT_PATH = "/org/laptop/Presence"
|
|
|
|
|
2006-07-22 07:26:39 +02:00
|
|
|
class NotFoundError(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
class PresenceServiceDBusHelper(dbus.service.Object):
|
|
|
|
def __init__(self, parent, bus_name):
|
|
|
|
self._parent = parent
|
|
|
|
self._bus_name = bus_name
|
|
|
|
dbus.service.Object.__init__(self, bus_name, _PRESENCE_OBJECT_PATH)
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def BuddyAppeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def BuddyDisappeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def ServiceAppeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def ServiceDisappeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def ActivityAppeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-19 06:10:35 +02:00
|
|
|
def ActivityDisappeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
2006-07-22 07:26:39 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def getServices(self):
|
|
|
|
ret = []
|
2006-10-06 17:05:27 +02:00
|
|
|
for serv in self._parent.get_services():
|
2006-07-22 07:26:39 +02:00
|
|
|
ret.append(serv.object_path())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="ao")
|
|
|
|
def getServicesOfType(self, stype):
|
|
|
|
ret = []
|
2006-10-06 17:05:27 +02:00
|
|
|
for serv in self._parent.get_services_of_type(stype):
|
2006-07-22 07:26:39 +02:00
|
|
|
ret.append(serv.object_path())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def getActivities(self):
|
|
|
|
ret = []
|
2006-10-06 17:05:27 +02:00
|
|
|
for act in self._parent.get_activities():
|
2006-07-22 07:26:39 +02:00
|
|
|
ret.append(act.object_path())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="o")
|
|
|
|
def getActivity(self, actid):
|
|
|
|
act = self._parent.get_activity(actid)
|
|
|
|
if not act:
|
|
|
|
raise NotFoundError("Not found")
|
|
|
|
return act.object_path()
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def getBuddies(self):
|
|
|
|
ret = []
|
2006-10-06 17:05:27 +02:00
|
|
|
for buddy in self._parent.get_buddies():
|
2006-07-22 07:26:39 +02:00
|
|
|
ret.append(buddy.object_path())
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="o")
|
|
|
|
def getBuddyByName(self, name):
|
|
|
|
buddy = self._parent.get_buddy_by_name(name)
|
|
|
|
if not buddy:
|
|
|
|
raise NotFoundError("Not found")
|
|
|
|
return buddy.object_path()
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="o")
|
|
|
|
def getBuddyByAddress(self, addr):
|
|
|
|
buddy = self._parent.get_buddy_by_address(addr)
|
|
|
|
if not buddy:
|
|
|
|
raise NotFoundError("Not found")
|
|
|
|
return buddy.object_path()
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="o")
|
|
|
|
def getOwner(self):
|
|
|
|
owner = self._parent.get_owner()
|
|
|
|
if not owner:
|
|
|
|
raise NotFoundError("Not found")
|
|
|
|
return owner.object_path()
|
|
|
|
|
2006-07-26 07:14:09 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
2006-09-15 22:41:11 +02:00
|
|
|
in_signature="os", out_signature="o",
|
|
|
|
sender_keyword="sender")
|
|
|
|
def joinActivity(self, activity_op, stype, sender):
|
2006-07-26 07:14:09 +02:00
|
|
|
found_activity = None
|
|
|
|
acts = self._parent.get_activities()
|
|
|
|
for act in acts:
|
|
|
|
if act.object_path() == activity_op:
|
|
|
|
found_activity = act
|
|
|
|
break
|
|
|
|
if not found_activity:
|
|
|
|
raise NotFoundError("The activity %s was not found." % activity_op)
|
2006-09-15 22:41:11 +02:00
|
|
|
return self._parent.join_activity(found_activity, stype, sender)
|
2006-07-26 07:14:09 +02:00
|
|
|
|
2006-07-23 06:56:40 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
2006-09-15 22:41:11 +02:00
|
|
|
in_signature="ssa{ss}sis", out_signature="o",
|
|
|
|
sender_keyword="sender")
|
|
|
|
def shareActivity(self, activity_id, stype, properties, address, port,
|
|
|
|
domain, sender=None):
|
2006-07-26 07:14:09 +02:00
|
|
|
if not len(address):
|
|
|
|
address = None
|
2006-07-26 02:04:15 +02:00
|
|
|
service = self._parent.share_activity(activity_id, stype, properties, address,
|
2006-09-15 22:41:11 +02:00
|
|
|
port, domain, sender)
|
2006-07-23 06:56:40 +02:00
|
|
|
return service.object_path()
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
2006-09-15 22:41:11 +02:00
|
|
|
in_signature="ssa{ss}sis", out_signature="o",
|
|
|
|
sender_keyword="sender")
|
|
|
|
def registerService(self, name, stype, properties, address, port, domain,
|
|
|
|
sender=None):
|
2006-07-26 07:14:09 +02:00
|
|
|
if not len(address):
|
|
|
|
address = None
|
2006-07-26 02:04:15 +02:00
|
|
|
service = self._parent.register_service(name, stype, properties, address,
|
2006-09-15 22:41:11 +02:00
|
|
|
port, domain, sender)
|
2006-07-24 17:55:42 +02:00
|
|
|
return service.object_path()
|
|
|
|
|
2006-08-02 16:27:09 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
2006-09-15 22:41:11 +02:00
|
|
|
in_signature="o", out_signature="",
|
|
|
|
sender_keyword="sender")
|
|
|
|
def unregisterService(self, service_op, sender):
|
2006-08-02 16:27:09 +02:00
|
|
|
found_serv = None
|
2006-08-12 01:29:55 +02:00
|
|
|
services = self._parent.get_services()
|
2006-08-02 16:27:09 +02:00
|
|
|
for serv in services:
|
|
|
|
if serv.object_path() == service_op:
|
|
|
|
found_serv = serv
|
|
|
|
break
|
|
|
|
if not found_serv:
|
2006-10-06 17:05:27 +02:00
|
|
|
raise NotFoundError("The service %s was not found." % service_op)
|
2006-09-15 22:41:11 +02:00
|
|
|
return self._parent.unregister_service(found_serv, sender)
|
2006-08-02 16:27:09 +02:00
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="")
|
|
|
|
def registerServiceType(self, stype):
|
|
|
|
self._parent.register_service_type(stype)
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="")
|
|
|
|
def unregisterServiceType(self, stype):
|
|
|
|
self._parent.unregister_service_type(stype)
|
|
|
|
|
2006-08-13 00:28:38 +02:00
|
|
|
@dbus.service.method(_PRESENCE_DBUS_INTERFACE)
|
|
|
|
def start(self):
|
|
|
|
self._parent.start()
|
2006-07-23 16:21:00 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
class PresenceService(object):
|
2006-07-18 16:51:04 +02:00
|
|
|
def __init__(self):
|
|
|
|
# interface -> IP address: interfaces we've gotten events on so far
|
|
|
|
self._local_addrs = {}
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
self._next_object_id = 0
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
self._buddies = {} # nick -> Buddy
|
|
|
|
self._services = {} # (name, type) -> Service
|
|
|
|
self._activities = {} # activity id -> Activity
|
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
# Keep track of stuff we're already browsing
|
2006-07-18 16:51:04 +02:00
|
|
|
self._service_type_browsers = {}
|
|
|
|
self._service_browsers = {}
|
|
|
|
|
|
|
|
# Resolved service list
|
|
|
|
self._service_advs = []
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
# Service types we care about resolving
|
|
|
|
self._registered_service_types = []
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
# Set up the dbus service we provide
|
2006-07-23 06:56:40 +02:00
|
|
|
self._session_bus = dbus.SessionBus()
|
|
|
|
self._bus_name = dbus.service.BusName(_PRESENCE_SERVICE, bus=self._session_bus)
|
2006-07-19 06:10:35 +02:00
|
|
|
self._dbus_helper = PresenceServiceDBusHelper(self, self._bus_name)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-09-25 16:57:21 +02:00
|
|
|
self._icon_cache = BuddyIconCache.BuddyIconCache()
|
|
|
|
|
2006-09-06 16:16:49 +02:00
|
|
|
# Our owner object
|
2006-10-09 15:11:15 +02:00
|
|
|
if env.get_nick_name():
|
|
|
|
objid = self._get_next_object_id()
|
|
|
|
self._owner = Buddy.Owner(self, self._bus_name,
|
|
|
|
objid, self._icon_cache)
|
|
|
|
self._buddies[self._owner.get_name()] = self._owner
|
|
|
|
else:
|
|
|
|
self._owner = None
|
2006-09-06 16:16:49 +02:00
|
|
|
|
2006-08-16 18:09:43 +02:00
|
|
|
self._started = False
|
|
|
|
|
2006-08-13 00:28:38 +02:00
|
|
|
def start(self):
|
2006-08-16 18:09:43 +02:00
|
|
|
if self._started:
|
|
|
|
return
|
|
|
|
self._started = True
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
# Connect to Avahi for mDNS stuff
|
|
|
|
self._system_bus = dbus.SystemBus()
|
2006-07-19 13:36:58 +02:00
|
|
|
self._mdns_service = dbus.Interface(self._system_bus.get_object(avahi.DBUS_NAME,
|
2006-07-18 16:51:04 +02:00
|
|
|
avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
|
|
|
|
|
|
|
|
# Always browse .local
|
|
|
|
self._new_domain_cb(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
|
|
|
|
|
|
|
|
# Connect to Avahi and start looking for stuff
|
|
|
|
domain_browser = self._mdns_service.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC,
|
|
|
|
"", avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))
|
|
|
|
db = dbus.Interface(self._system_bus.get_object(avahi.DBUS_NAME, domain_browser), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
|
|
|
|
db.connect_to_signal('ItemNew', self._new_domain_cb_glue)
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def _get_next_object_id(self):
|
|
|
|
"""Increment and return the object ID counter."""
|
|
|
|
self._next_object_id = self._next_object_id + 1
|
|
|
|
return self._next_object_id
|
|
|
|
|
2006-07-22 07:26:39 +02:00
|
|
|
def get_services(self):
|
|
|
|
return self._services.values()
|
|
|
|
|
|
|
|
def get_services_of_type(self, stype):
|
|
|
|
ret = []
|
|
|
|
for serv in self._services.values():
|
|
|
|
if serv.get_type() == stype:
|
|
|
|
ret.append(serv)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def get_activities(self):
|
2006-08-25 20:55:19 +02:00
|
|
|
# Only return valid activities
|
|
|
|
ret = []
|
|
|
|
for act in self._activities.values():
|
|
|
|
if act.is_valid():
|
|
|
|
ret.append(act)
|
|
|
|
return ret
|
2006-07-22 07:26:39 +02:00
|
|
|
|
|
|
|
def get_activity(self, actid):
|
|
|
|
if self._activities.has_key(actid):
|
2006-08-25 20:55:19 +02:00
|
|
|
act = self._activities[actid]
|
|
|
|
if act.is_valid():
|
|
|
|
return act
|
2006-07-22 07:26:39 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_buddies(self):
|
2006-08-25 20:55:19 +02:00
|
|
|
buddies = []
|
|
|
|
for buddy in self._buddies.values():
|
|
|
|
if buddy.is_valid():
|
|
|
|
buddies.append(buddy)
|
|
|
|
return buddies
|
2006-07-22 07:26:39 +02:00
|
|
|
|
|
|
|
def get_buddy_by_name(self, name):
|
|
|
|
if self._buddies.has_key(name):
|
2006-08-25 20:55:19 +02:00
|
|
|
if self._buddies[name].is_valid():
|
|
|
|
return self._buddies[name]
|
2006-07-22 07:26:39 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_buddy_by_address(self, address):
|
|
|
|
for buddy in self._buddies.values():
|
2006-08-25 20:55:19 +02:00
|
|
|
if buddy.get_address() == address and buddy.is_valid():
|
2006-07-22 07:26:39 +02:00
|
|
|
return buddy
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_owner(self):
|
|
|
|
return self._owner
|
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
def _find_service_adv(self, interface=None, protocol=None, name=None,
|
|
|
|
stype=None, domain=None, local=None):
|
|
|
|
"""Search a list of service advertisements for ones matching
|
|
|
|
certain criteria."""
|
2006-07-18 16:51:04 +02:00
|
|
|
adv_list = []
|
|
|
|
for adv in self._service_advs:
|
|
|
|
if interface and adv.interface() != interface:
|
|
|
|
continue
|
|
|
|
if protocol and adv.protocol() != protocol:
|
|
|
|
continue
|
|
|
|
if name and adv.name() != name:
|
|
|
|
continue
|
|
|
|
if stype and adv.stype() != stype:
|
|
|
|
continue
|
|
|
|
if domain and adv.domain() != domain:
|
|
|
|
continue
|
2006-09-12 18:36:24 +02:00
|
|
|
if local is not None and adv.is_local() != local:
|
|
|
|
continue
|
2006-07-18 16:51:04 +02:00
|
|
|
adv_list.append(adv)
|
|
|
|
return adv_list
|
|
|
|
|
2006-09-16 00:37:54 +02:00
|
|
|
def _find_registered_service_type(self, stype):
|
|
|
|
for item in self._registered_service_types:
|
|
|
|
if item.get_type() == stype:
|
|
|
|
return item
|
|
|
|
return None
|
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
def _handle_new_service_for_buddy(self, service, local):
|
2006-07-19 06:10:35 +02:00
|
|
|
"""Deal with a new discovered service object."""
|
|
|
|
# Once a service is resolved, we match it up to an existing buddy,
|
|
|
|
# or create a new Buddy if this is the first service known about the buddy
|
|
|
|
buddy_was_valid = False
|
|
|
|
name = service.get_name()
|
|
|
|
buddy = None
|
|
|
|
try:
|
|
|
|
buddy = self._buddies[name]
|
|
|
|
buddy_was_valid = buddy.is_valid()
|
|
|
|
service_added = buddy.add_service(service)
|
|
|
|
if service_added:
|
|
|
|
self._dbus_helper.ServiceAppeared(service.object_path())
|
|
|
|
except KeyError:
|
2006-07-26 07:14:09 +02:00
|
|
|
source_addr = service.get_source_address()
|
2006-07-19 06:10:35 +02:00
|
|
|
objid = self._get_next_object_id()
|
2006-09-25 16:57:21 +02:00
|
|
|
buddy = Buddy.Buddy(self._bus_name, objid, service, self._icon_cache)
|
2006-07-19 06:10:35 +02:00
|
|
|
self._buddies[name] = buddy
|
|
|
|
self._dbus_helper.ServiceAppeared(service.object_path())
|
|
|
|
if not buddy_was_valid and buddy.is_valid():
|
|
|
|
self._dbus_helper.BuddyAppeared(buddy.object_path())
|
|
|
|
return buddy
|
|
|
|
|
|
|
|
def _handle_new_activity_service(self, service):
|
|
|
|
# If the serivce is an activity service, merge it into our activities list
|
|
|
|
actid = service.get_activity_id()
|
|
|
|
if not actid:
|
|
|
|
return
|
|
|
|
activity = None
|
2006-08-25 20:55:19 +02:00
|
|
|
was_valid = False
|
2006-07-19 06:10:35 +02:00
|
|
|
if not self._activities.has_key(actid):
|
|
|
|
objid = self._get_next_object_id()
|
|
|
|
activity = Activity.Activity(self._bus_name, objid, service)
|
|
|
|
self._activities[actid] = activity
|
|
|
|
else:
|
|
|
|
activity = self._activities[actid]
|
2006-08-25 20:55:19 +02:00
|
|
|
was_valid = activity.is_valid()
|
2006-07-19 06:10:35 +02:00
|
|
|
|
|
|
|
if activity:
|
|
|
|
activity.add_service(service)
|
|
|
|
|
2006-09-27 17:35:34 +02:00
|
|
|
# Add the activity to its buddy
|
|
|
|
# FIXME: use something other than name to attribute to buddy
|
|
|
|
try:
|
|
|
|
buddy = self._buddies[service.get_name()]
|
|
|
|
buddy.add_activity(activity)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2006-09-26 19:23:12 +02:00
|
|
|
|
2006-08-25 20:55:19 +02:00
|
|
|
if not was_valid and activity.is_valid():
|
|
|
|
self._dbus_helper.ActivityAppeared(activity.object_path())
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def _handle_remove_activity_service(self, service):
|
|
|
|
actid = service.get_activity_id()
|
|
|
|
if not actid:
|
|
|
|
return
|
|
|
|
if not self._activities.has_key(actid):
|
|
|
|
return
|
2006-09-26 19:23:12 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
activity = self._activities[actid]
|
2006-09-26 19:23:12 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
activity.remove_service(service)
|
|
|
|
if len(activity.get_services()) == 0:
|
2006-09-27 17:35:34 +02:00
|
|
|
# Remove the activity from its buddy
|
|
|
|
# FIXME: use something other than name to attribute to buddy
|
|
|
|
try:
|
|
|
|
buddy = self._buddies[service.get_name()]
|
|
|
|
buddy.remove_activity(activity)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
# Kill the activity
|
|
|
|
self._dbus_helper.ActivityDisappeared(activity.object_path())
|
|
|
|
del self._activities[actid]
|
|
|
|
|
2006-09-19 19:54:32 +02:00
|
|
|
def _service_resolved_cb(self, adv, interface, protocol, full_name,
|
|
|
|
stype, domain, host, aprotocol, address, port, txt, flags,
|
|
|
|
updated):
|
2006-09-12 18:36:24 +02:00
|
|
|
"""When the service discovery finally gets here, we've got enough
|
|
|
|
information about the service to assign it to a buddy."""
|
2006-09-25 03:30:28 +02:00
|
|
|
if updated == False:
|
|
|
|
logging.debug("Resolved service '%s' type '%s' domain '%s' to " \
|
|
|
|
" %s:%s" % (full_name, stype, domain, address, port))
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
if not adv in self._service_advs:
|
|
|
|
return False
|
|
|
|
if adv.state() != _SA_RESOLVED:
|
2006-07-19 06:10:35 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-22 07:26:39 +02:00
|
|
|
# See if we know about this service already
|
2006-09-19 19:54:32 +02:00
|
|
|
service = None
|
2006-07-22 07:26:39 +02:00
|
|
|
key = (full_name, stype)
|
2006-09-19 19:54:32 +02:00
|
|
|
props = _txt_to_dict(txt)
|
2006-07-22 07:26:39 +02:00
|
|
|
if not self._services.has_key(key):
|
|
|
|
objid = self._get_next_object_id()
|
|
|
|
service = Service.Service(self._bus_name, objid, name=full_name,
|
|
|
|
stype=stype, domain=domain, address=address, port=port,
|
2006-09-19 19:54:32 +02:00
|
|
|
properties=props, source_address=address)
|
2006-07-22 07:26:39 +02:00
|
|
|
self._services[key] = service
|
|
|
|
else:
|
2006-09-19 19:54:32 +02:00
|
|
|
# Already tracking this service; either:
|
|
|
|
# a) we were the one that shared it in the first place,
|
|
|
|
# and therefore the source address would not have
|
|
|
|
# been set yet
|
|
|
|
# b) the service has been updated
|
2006-07-22 07:26:39 +02:00
|
|
|
service = self._services[key]
|
2006-07-26 07:14:09 +02:00
|
|
|
if not service.get_source_address():
|
|
|
|
service.set_source_address(address)
|
2006-07-23 07:32:10 +02:00
|
|
|
if not service.get_address():
|
2006-07-26 07:14:09 +02:00
|
|
|
service.set_address(address)
|
2006-09-19 19:54:32 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
adv.set_service(service)
|
|
|
|
|
2006-09-19 19:54:32 +02:00
|
|
|
if service and updated:
|
|
|
|
service.set_properties(props, from_network=True)
|
|
|
|
return False
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
# Merge the service into our buddy and activity lists, if needed
|
2006-09-12 18:36:24 +02:00
|
|
|
buddy = self._handle_new_service_for_buddy(service, adv.is_local())
|
2006-07-19 06:10:35 +02:00
|
|
|
if buddy and service.get_activity_id():
|
|
|
|
self._handle_new_activity_service(service)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2006-09-19 19:54:32 +02:00
|
|
|
def _service_resolved_cb_glue(self, adv, interface, protocol, name,
|
2006-09-12 18:36:24 +02:00
|
|
|
stype, domain, host, aprotocol, address, port, txt, flags):
|
2006-09-19 19:54:32 +02:00
|
|
|
# Avahi doesn't flag updates to existing services, so we have
|
|
|
|
# to determine that here
|
|
|
|
updated = False
|
|
|
|
if adv.state() == _SA_RESOLVED:
|
|
|
|
updated = True
|
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
adv.set_state(_SA_RESOLVED)
|
2006-09-19 19:54:32 +02:00
|
|
|
gobject.idle_add(self._service_resolved_cb, adv, interface,
|
2006-09-12 18:36:24 +02:00
|
|
|
protocol, name, stype, domain, host, aprotocol, address,
|
2006-09-19 19:54:32 +02:00
|
|
|
port, txt, flags, updated)
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-09-19 19:54:32 +02:00
|
|
|
def _service_resolved_failure_cb(self, adv, err):
|
2006-09-12 18:36:24 +02:00
|
|
|
adv.set_state(_SA_UNRESOLVED)
|
|
|
|
logging.error("Error resolving service %s.%s: %s" % (adv.name(), adv.stype(), err))
|
2006-07-23 07:32:10 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def _resolve_service(self, adv):
|
|
|
|
"""Resolve and lookup a ZeroConf service to obtain its address and TXT records."""
|
|
|
|
# Ask avahi to resolve this particular service
|
2006-09-19 19:54:32 +02:00
|
|
|
path = self._mdns_service.ServiceResolverNew(dbus.Int32(adv.interface()),
|
|
|
|
dbus.Int32(adv.protocol()), adv.name(), adv.stype(), adv.domain(),
|
|
|
|
avahi.PROTO_UNSPEC, dbus.UInt32(0))
|
|
|
|
resolver = dbus.Interface(self._system_bus.get_object(avahi.DBUS_NAME, path),
|
|
|
|
avahi.DBUS_INTERFACE_SERVICE_RESOLVER)
|
|
|
|
resolver.connect_to_signal('Found', lambda *args: self._service_resolved_cb_glue(adv, *args))
|
|
|
|
resolver.connect_to_signal('Failure', lambda *args: self._service_resolved_failure_cb(adv, *args))
|
|
|
|
adv.set_resolver(resolver)
|
2006-07-19 06:10:35 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
def _service_appeared_cb(self, interface, protocol, full_name, stype, domain, flags):
|
2006-09-12 18:36:24 +02:00
|
|
|
local = flags & avahi.LOOKUP_RESULT_OUR_OWN > 0
|
2006-07-18 16:51:04 +02:00
|
|
|
adv_list = self._find_service_adv(interface=interface, protocol=protocol,
|
2006-09-12 18:36:24 +02:00
|
|
|
name=full_name, stype=stype, domain=domain, local=local)
|
2006-07-18 16:51:04 +02:00
|
|
|
adv = None
|
|
|
|
if not adv_list:
|
|
|
|
adv = ServiceAdv(interface=interface, protocol=protocol, name=full_name,
|
2006-09-12 18:36:24 +02:00
|
|
|
stype=stype, domain=domain, local=local)
|
2006-07-18 16:51:04 +02:00
|
|
|
self._service_advs.append(adv)
|
|
|
|
else:
|
|
|
|
adv = adv_list[0]
|
|
|
|
|
|
|
|
# Decompose service name if we can
|
2006-07-26 02:04:15 +02:00
|
|
|
(actid, buddy_name) = Service.decompose_service_name(full_name)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
# If we care about the service right now, resolve it
|
|
|
|
resolve = False
|
2006-09-16 00:37:54 +02:00
|
|
|
item = self._find_registered_service_type(stype)
|
|
|
|
if actid is not None or item is not None:
|
2006-07-18 16:51:04 +02:00
|
|
|
resolve = True
|
2006-09-12 18:36:24 +02:00
|
|
|
if resolve and adv.state() == _SA_UNRESOLVED:
|
|
|
|
logging.debug("Found '%s' (%d) of type '%s' in domain" \
|
|
|
|
" '%s' on %i.%i; will resolve." % (full_name, flags, stype,
|
|
|
|
domain, interface, protocol))
|
|
|
|
adv.set_state(_SA_RESOLVE_PENDING)
|
2006-07-18 16:51:04 +02:00
|
|
|
gobject.idle_add(self._resolve_service, adv)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _service_appeared_cb_glue(self, interface, protocol, name, stype, domain, flags):
|
|
|
|
gobject.idle_add(self._service_appeared_cb, interface, protocol, name, stype, domain, flags)
|
|
|
|
|
|
|
|
def _service_disappeared_cb(self, interface, protocol, full_name, stype, domain, flags):
|
2006-09-12 18:36:24 +02:00
|
|
|
local = flags & avahi.LOOKUP_RESULT_OUR_OWN > 0
|
2006-07-18 16:51:04 +02:00
|
|
|
# If it's an unresolved service, remove it from our unresolved list
|
|
|
|
adv_list = self._find_service_adv(interface=interface, protocol=protocol,
|
2006-09-12 18:36:24 +02:00
|
|
|
name=full_name, stype=stype, domain=domain, local=local)
|
2006-07-18 16:51:04 +02:00
|
|
|
if not adv_list:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Get the service object; if none, we have nothing left to do
|
|
|
|
adv = adv_list[0]
|
|
|
|
service = adv.service()
|
2006-09-12 18:36:24 +02:00
|
|
|
self._service_advs.remove(adv)
|
|
|
|
del adv
|
2006-07-18 16:51:04 +02:00
|
|
|
if not service:
|
|
|
|
return False
|
|
|
|
|
2006-09-12 20:04:35 +02:00
|
|
|
logging.debug("Service %s.%s in domain %s on %i.%i disappeared." % (full_name,
|
2006-09-12 18:36:24 +02:00
|
|
|
stype, domain, interface, protocol))
|
|
|
|
|
|
|
|
self._dbus_helper.ServiceDisappeared(service.object_path())
|
|
|
|
self._handle_remove_activity_service(service)
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
# Decompose service name if we can
|
2006-07-26 02:04:15 +02:00
|
|
|
(actid, buddy_name) = Service.decompose_service_name(full_name)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
# Remove the service from the buddy
|
|
|
|
try:
|
|
|
|
buddy = self._buddies[buddy_name]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
buddy.remove_service(service)
|
|
|
|
if not buddy.is_valid():
|
2006-07-19 06:10:35 +02:00
|
|
|
self._dbus_helper.BuddyDisappeared(buddy.object_path())
|
2006-07-18 16:51:04 +02:00
|
|
|
del self._buddies[buddy_name]
|
2006-07-22 07:26:39 +02:00
|
|
|
key = (service.get_full_name(), service.get_type())
|
|
|
|
del self._services[key]
|
2006-07-18 16:51:04 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _service_disappeared_cb_glue(self, interface, protocol, name, stype, domain, flags):
|
|
|
|
gobject.idle_add(self._service_disappeared_cb, interface, protocol, name, stype, domain, flags)
|
|
|
|
|
|
|
|
def _new_service_type_cb(self, interface, protocol, stype, domain, flags):
|
|
|
|
# Are we already browsing this domain for this type?
|
|
|
|
if self._service_browsers.has_key((interface, protocol, stype, domain)):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Start browsing for all services of this type in this domain
|
2006-09-27 22:47:23 +02:00
|
|
|
try:
|
|
|
|
s_browser = self._mdns_service.ServiceBrowserNew(interface,
|
|
|
|
protocol, stype, domain, dbus.UInt32(0))
|
|
|
|
browser_obj = dbus.Interface(self._system_bus.get_object(avahi.DBUS_NAME, s_browser),
|
|
|
|
avahi.DBUS_INTERFACE_SERVICE_BROWSER)
|
|
|
|
browser_obj.connect_to_signal('ItemNew', self._service_appeared_cb_glue)
|
|
|
|
browser_obj.connect_to_signal('ItemRemove', self._service_disappeared_cb_glue)
|
|
|
|
|
|
|
|
self._service_browsers[(interface, protocol, stype, domain)] = browser_obj
|
|
|
|
except dbus.DBusException:
|
|
|
|
logging.debug("Error browsing service type '%s'" % stype)
|
2006-07-18 16:51:04 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _new_service_type_cb_glue(self, interface, protocol, stype, domain, flags):
|
2006-09-27 22:47:23 +02:00
|
|
|
if len(stype) > 0:
|
|
|
|
gobject.idle_add(self._new_service_type_cb, interface, protocol,
|
|
|
|
stype, domain, flags)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
def _new_domain_cb(self, interface, protocol, domain, flags=0):
|
|
|
|
"""Callback from Avahi when a new domain has been found. Start
|
|
|
|
browsing the new domain."""
|
|
|
|
# Only use .local for now...
|
|
|
|
if domain != "local":
|
|
|
|
return
|
|
|
|
|
|
|
|
# Are we already browsing this domain?
|
|
|
|
if self._service_type_browsers.has_key((interface, protocol, domain)):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Start browsing this domain for the services its members offer
|
|
|
|
try:
|
|
|
|
st_browser = self._mdns_service.ServiceTypeBrowserNew(interface, protocol, domain, dbus.UInt32(0))
|
|
|
|
browser_obj = dbus.Interface(self._system_bus.get_object(avahi.DBUS_NAME, st_browser),
|
|
|
|
avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
|
|
|
|
except dbus.DBusException, exc:
|
|
|
|
str_exc = str(exc)
|
2006-08-09 21:49:50 +02:00
|
|
|
logging.error("got exception %s while attempting to browse domain %s on %i.%i" % (str_exc, domain, interface, protocol))
|
2006-07-18 16:51:04 +02:00
|
|
|
if str_exc.find("The name org.freedesktop.Avahi was not provided by any .service files") >= 0:
|
|
|
|
raise Exception("Avahi does not appear to be running. '%s'" % str_exc)
|
|
|
|
else:
|
|
|
|
raise exc
|
2006-09-12 18:36:24 +02:00
|
|
|
logging.debug("Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol))
|
2006-07-18 16:51:04 +02:00
|
|
|
browser_obj.connect_to_signal('ItemNew', self._new_service_type_cb_glue)
|
|
|
|
self._service_type_browsers[(interface, protocol, domain)] = browser_obj
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _new_domain_cb_glue(self, interface, protocol, domain, flags=0):
|
|
|
|
gobject.idle_add(self._new_domain_cb, interface, protocol, domain, flags)
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
def join_activity(self, activity, stype, sender):
|
2006-07-26 07:14:09 +02:00
|
|
|
services = activity.get_services_of_type(stype)
|
|
|
|
if not len(services):
|
2006-09-15 22:41:11 +02:00
|
|
|
raise NotFoundError("The service type %s was not present within " \
|
|
|
|
"the activity %s" % (stype, activity.object_path()))
|
2006-07-26 07:14:09 +02:00
|
|
|
act_service = services[0]
|
2006-08-25 20:55:19 +02:00
|
|
|
props = act_service.get_properties()
|
|
|
|
color = activity.get_color()
|
|
|
|
if color:
|
|
|
|
props['color'] = color
|
|
|
|
return self._share_activity(activity.get_id(), stype, properties,
|
2006-09-15 22:41:11 +02:00
|
|
|
act_service.get_address(), act_service.get_port(),
|
|
|
|
act_service.get_domain(), sender)
|
2006-07-26 07:14:09 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
def share_activity(self, activity_id, stype, properties=None, address=None,
|
|
|
|
port=-1, domain=u"local", sender=None):
|
2006-07-24 17:55:42 +02:00
|
|
|
"""Convenience function to share an activity with other buddies."""
|
|
|
|
if not util.validate_activity_id(activity_id):
|
|
|
|
raise ValueError("invalid activity id")
|
2006-07-26 02:04:15 +02:00
|
|
|
owner_nick = self._owner.get_name()
|
|
|
|
real_name = Service.compose_service_name(owner_nick, activity_id)
|
2006-10-06 16:46:17 +02:00
|
|
|
if address and not isinstance(address, unicode):
|
2006-07-24 17:55:42 +02:00
|
|
|
raise ValueError("address must be a unicode string.")
|
2006-07-26 18:00:28 +02:00
|
|
|
if address == None and stype.endswith('_udp'):
|
2006-07-24 17:55:42 +02:00
|
|
|
# Use random currently unassigned multicast address
|
2006-07-26 07:14:09 +02:00
|
|
|
address = u"232.%d.%d.%d" % (random.randint(0, 254), random.randint(1, 254),
|
2006-07-24 17:55:42 +02:00
|
|
|
random.randint(1, 254))
|
2006-07-26 14:05:43 +02:00
|
|
|
properties['address'] = address
|
|
|
|
properties['port'] = port
|
2006-10-03 23:06:48 +02:00
|
|
|
if port and port != -1 and (not isinstance(port, int) or port <= 1024 or port >= 65535):
|
2006-07-24 17:55:42 +02:00
|
|
|
raise ValueError("port must be a number between 1024 and 65535")
|
2006-08-25 20:55:19 +02:00
|
|
|
|
|
|
|
color = self._owner.get_color()
|
|
|
|
if color:
|
|
|
|
properties['color'] = color
|
2006-07-24 17:55:42 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
logging.debug('Share activity %s, type %s, address %s, port %d, " \
|
|
|
|
"properties %s' % (activity_id, stype, address, port,
|
|
|
|
properties))
|
|
|
|
return self.register_service(real_name, stype, properties, address,
|
|
|
|
port, domain, sender)
|
2006-07-24 17:55:42 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
def register_service(self, name, stype, properties={}, address=None,
|
|
|
|
port=-1, domain=u"local", sender=None):
|
2006-07-22 07:26:39 +02:00
|
|
|
"""Register a new service, advertising it to other Buddies on the network."""
|
2006-09-19 19:54:32 +02:00
|
|
|
# Refuse to register if we can't get the dbus connection this request
|
|
|
|
# came from for some reason
|
|
|
|
if not sender:
|
|
|
|
raise RuntimeError("Service registration request must have a sender.")
|
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
(actid, person_name) = Service.decompose_service_name(name)
|
|
|
|
if self.get_owner() and person_name != self.get_owner().get_name():
|
2006-09-15 22:41:11 +02:00
|
|
|
raise RuntimeError("Tried to register a service that didn't have" \
|
|
|
|
" Owner nick as the service name!")
|
2006-07-23 07:32:10 +02:00
|
|
|
if not domain or not len(domain):
|
|
|
|
domain = u"local"
|
2006-07-26 07:14:09 +02:00
|
|
|
if not port or port == -1:
|
|
|
|
port = random.randint(4000, 65000)
|
2006-07-22 07:26:39 +02:00
|
|
|
|
2006-09-19 19:54:32 +02:00
|
|
|
objid = self._get_next_object_id()
|
|
|
|
service = Service.Service(self._bus_name, objid, name=name,
|
|
|
|
stype=stype, domain=domain, address=address, port=port,
|
|
|
|
properties=properties, source_address=None,
|
|
|
|
local_publisher=sender)
|
|
|
|
self._services[(name, stype)] = service
|
2006-07-23 07:32:10 +02:00
|
|
|
self.register_service_type(stype)
|
2006-09-19 19:54:32 +02:00
|
|
|
service.register(self._system_bus, self._mdns_service)
|
2006-07-23 06:56:40 +02:00
|
|
|
return service
|
2006-07-22 07:26:39 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
def unregister_service(self, service, sender=None):
|
2006-09-19 19:54:32 +02:00
|
|
|
service.unregister(sender)
|
2006-08-02 16:27:09 +02:00
|
|
|
|
2006-07-22 07:26:39 +02:00
|
|
|
def register_service_type(self, stype):
|
|
|
|
"""Requests that the Presence service look for and recognize
|
|
|
|
a certain mDNS service types."""
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(stype, unicode):
|
2006-07-22 07:26:39 +02:00
|
|
|
raise ValueError("service type must be a unicode string.")
|
2006-09-15 22:41:11 +02:00
|
|
|
|
|
|
|
# If we've already registered it as a service type, ref it and return
|
2006-09-16 00:37:54 +02:00
|
|
|
item = self._find_registered_service_type(stype)
|
|
|
|
if item is not None:
|
|
|
|
item.ref()
|
|
|
|
return
|
2006-09-15 22:41:11 +02:00
|
|
|
|
|
|
|
# Otherwise track this type now
|
|
|
|
obj = RegisteredServiceType(stype)
|
|
|
|
self._registered_service_types.append(obj)
|
2006-07-22 07:26:39 +02:00
|
|
|
|
|
|
|
# Find unresolved services that match the service type
|
|
|
|
# we're now interested in, and resolve them
|
|
|
|
resolv_list = []
|
|
|
|
|
|
|
|
# Find services of this type
|
|
|
|
resolv_list = self._find_service_adv(stype=stype)
|
|
|
|
# Request resolution for them if they aren't in-process already
|
|
|
|
for adv in resolv_list:
|
2006-09-12 20:22:06 +02:00
|
|
|
if adv.state() == _SA_UNRESOLVED:
|
|
|
|
adv.set_state(_SA_RESOLVE_PENDING)
|
2006-07-22 07:26:39 +02:00
|
|
|
gobject.idle_add(self._resolve_service, adv)
|
|
|
|
|
|
|
|
def unregister_service_type(self, stype):
|
|
|
|
"""Stop tracking a certain mDNS service."""
|
2006-10-03 23:06:48 +02:00
|
|
|
if not isinstance(stype, unicode):
|
2006-07-22 07:26:39 +02:00
|
|
|
raise ValueError("service type must be a unicode string.")
|
2006-09-16 00:37:54 +02:00
|
|
|
|
2006-09-15 22:41:11 +02:00
|
|
|
# if it was found, unref it and possibly remove it
|
2006-09-16 00:37:54 +02:00
|
|
|
item = self._find_registered_service_type(stype)
|
|
|
|
if not item:
|
|
|
|
return
|
|
|
|
if item.unref() <= 0:
|
|
|
|
self._registered_service_types.remove(item)
|
|
|
|
del item
|
2006-07-22 07:26:39 +02:00
|
|
|
|
2006-08-17 05:06:13 +02:00
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def main():
|
2006-08-17 05:06:13 +02:00
|
|
|
from sugar import TracebackUtils
|
2006-07-24 18:45:53 +02:00
|
|
|
loop = gobject.MainLoop()
|
2006-07-19 06:10:35 +02:00
|
|
|
ps = PresenceService()
|
2006-08-17 05:06:13 +02:00
|
|
|
tbh = TracebackUtils.TracebackHelper()
|
2006-08-12 01:29:55 +02:00
|
|
|
try:
|
|
|
|
loop.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print 'Ctrl+C pressed, exiting...'
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-08-17 05:06:13 +02:00
|
|
|
del tbh
|
|
|
|
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|