sugar-toolkit-gtk3/sugar/p2p/Group.py

164 lines
4.6 KiB
Python
Raw Normal View History

2006-05-15 22:28:09 +02:00
import avahi
2006-05-15 20:48:08 +02:00
from Buddy import Buddy
2006-05-22 08:11:39 +02:00
from Buddy import get_recognized_buddy_service_types
2006-05-15 20:48:08 +02:00
from Buddy import Owner
from Buddy import PRESENCE_SERVICE_TYPE
from Service import Service
from sugar.p2p.model.Store import Store
2006-05-13 07:28:51 +02:00
import presence
2006-05-22 04:21:42 +02:00
_OLPC_SERVICE_TYPE_PREFIX = "_olpc"
2006-05-15 20:48:08 +02:00
class Group:
_groups = {}
2006-05-15 20:48:08 +02:00
SERVICE_ADDED = "service_added"
SERVICE_REMOVED = "service_removed"
2006-05-15 20:48:08 +02:00
BUDDY_JOIN = "buddy_join"
BUDDY_LEAVE = "buddy_leave"
def get_from_id(group_id):
if group_id == 'local' and not Group._groups.has_key(group_id):
return LocalGroup()
else:
group = Group._groups[group_id]
get_from_id = staticmethod(get_from_id)
def __init__(self, group_id):
self._group_id = group_id
self._service_listeners = []
self._presence_listeners = []
2006-05-15 20:48:08 +02:00
self._store = Store(self)
Group._groups[group_id] = self
2006-05-15 20:48:08 +02:00
def get_store(self):
return self._store
2006-05-15 20:48:08 +02:00
def join(self):
pass
def add_service_listener(self, listener):
self._service_listeners.append(listener)
def add_presence_listener(self, listener):
self._presence_listeners.append(listener)
def _notify_service_added(self, service):
for listener in self._service_listeners:
2006-05-15 20:48:08 +02:00
listener(Group.SERVICE_ADDED, service)
2006-05-15 20:48:08 +02:00
def _notify_service_removed(self, service_id):
for listener in self._service_listeners:
2006-05-15 20:48:08 +02:00
listener(Group.SERVICE_REMOVED, service_id)
def _notify_buddy_join(self, buddy):
for listener in self._presence_listeners:
2006-05-15 20:48:08 +02:00
listener(Group.BUDDY_JOIN, buddy)
def _notify_buddy_leave(self, buddy):
for listener in self._presence_listeners:
2006-05-15 20:48:08 +02:00
listener(Group.BUDDY_LEAVE, buddy)
class LocalGroup(Group):
def __init__(self):
Group.__init__(self, 'local')
self._services = {}
self._buddies = {}
self._pdiscovery = presence.PresenceDiscovery()
self._pdiscovery.add_service_listener(self._on_service_change)
self._pdiscovery.start()
2006-05-18 22:35:12 +02:00
self._owner = Owner(self)
2006-05-22 08:10:30 +02:00
self._add_buddy(self._owner)
2006-05-18 22:35:12 +02:00
def get_owner(self):
return self._owner
def add_service(self, service):
sid = (service.get_name(), service.get_type())
if not self._services.has_key(sid):
self._services[sid] = service
self._notify_service_added(service)
2006-05-15 20:48:08 +02:00
def remove_service(self, service_id):
if self._services.has_key(service_id):
self._notify_service_removed(service_id)
del self._services[service_id]
def join(self):
self._owner.register()
def get_service(self, name, stype):
2006-05-15 20:48:08 +02:00
if self._services.has_key((name, stype)):
return self._services[(name, stype)]
2006-05-22 04:21:42 +02:00
return None
def get_buddy(self, name):
2006-05-22 04:21:42 +02:00
if self._buddies.has_key(name):
return self._buddies[name]
return None
2006-05-22 22:36:02 +02:00
def notify_service_registered(self, service):
"""A new service is automatically owned by the owner of this group."""
self._owner.notify_service_registered(service)
def _add_buddy(self, buddy):
bid = buddy.get_nick_name()
if not self._buddies.has_key(bid):
self._buddies[bid] = buddy
self._notify_buddy_join(buddy)
2006-05-19 20:44:42 +02:00
def _remove_buddy(self, bid):
if not self._buddies.has_key(bid):
return
buddy = self._buddies[bid]
self._notify_buddy_leave(buddy)
del self._buddies[buddy.get_nick_name()]
def _on_service_change(self, action, interface, protocol, name, stype, domain, flags):
if action == presence.ACTION_SERVICE_NEW:
self._pdiscovery.resolve_service(interface, protocol, name, stype, domain,
self._on_service_resolved)
elif action == presence.ACTION_SERVICE_REMOVED:
2006-05-22 08:11:39 +02:00
if stype in get_recognized_buddy_service_types():
buddy = self.get_buddy(name)
if buddy:
buddy.remove_service(stype)
# Removal of the presence service removes the buddy too
if stype == PRESENCE_SERVICE_TYPE:
self._remove_buddy(name)
self.remove_service((name, stype))
2006-05-22 04:21:42 +02:00
elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX):
self.remove_service((name, stype))
def _on_service_resolved(self, interface, protocol, name, stype, domain,
host, aprotocol, address, port, txt, flags):
service = Service(name, stype, port)
service.set_address(address)
2006-05-15 22:28:09 +02:00
for prop in avahi.txt_array_to_string_array(txt):
(key, value) = prop.split('=')
2006-05-15 23:16:55 +02:00
if key == 'group_address':
2006-05-22 08:11:39 +02:00
service.set_group_address(value)
2006-05-22 22:36:02 +02:00
#print "ServiceResolved: name=%s, stype=%s, port=%s, address=%s" % (name, stype, port, address)
2006-05-22 08:11:39 +02:00
if stype in get_recognized_buddy_service_types():
# Service recognized as Buddy services either create a new
# buddy if one doesn't exist yet, or get added to the existing
# buddy
buddy = self.get_buddy(name)
if buddy:
buddy.add_service(service)
else:
self._add_buddy(Buddy(service))
self.add_service(service)
2006-05-22 04:21:42 +02:00
elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX):
2006-05-22 08:11:39 +02:00
# These services aren't associated with buddies
2006-05-15 20:48:08 +02:00
self.add_service(service)
2006-05-22 08:11:39 +02:00