2006-07-18 16:51:04 +02:00
|
|
|
import base64
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import gobject
|
2006-07-19 06:10:35 +02:00
|
|
|
import dbus, dbus.service
|
2006-09-11 15:18:57 +02:00
|
|
|
from sugar import env
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
|
2006-07-22 07:26:39 +02:00
|
|
|
BUDDY_DBUS_OBJECT_PATH = "/org/laptop/Presence/Buddies/"
|
2006-07-18 16:51:04 +02:00
|
|
|
BUDDY_DBUS_INTERFACE = "org.laptop.Presence.Buddy"
|
|
|
|
|
|
|
|
class NotFoundError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BuddyDBusHelper(dbus.service.Object):
|
|
|
|
def __init__(self, parent, bus_name, object_path):
|
|
|
|
self._parent = parent
|
|
|
|
self._bus_name = bus_name
|
|
|
|
self._object_path = object_path
|
|
|
|
dbus.service.Object.__init__(self, bus_name, self._object_path)
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-18 16:51:04 +02:00
|
|
|
def ServiceAppeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-18 16:51:04 +02:00
|
|
|
def ServiceDisappeared(self, object_path):
|
|
|
|
pass
|
|
|
|
|
2006-07-19 13:36:58 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
|
|
|
signature="")
|
2006-07-18 16:51:04 +02:00
|
|
|
def IconChanged(self):
|
|
|
|
pass
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-18 16:51:04 +02:00
|
|
|
def JoinedActivity(self, object_path):
|
|
|
|
pass
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
2006-07-19 13:36:58 +02:00
|
|
|
signature="o")
|
2006-07-18 16:51:04 +02:00
|
|
|
def LeftActivity(self, object_path):
|
|
|
|
pass
|
|
|
|
|
2006-09-08 05:30:22 +02:00
|
|
|
@dbus.service.signal(BUDDY_DBUS_INTERFACE,
|
|
|
|
signature="as")
|
|
|
|
def PropertyChanged(self, prop_list):
|
|
|
|
pass
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
@dbus.service.method(BUDDY_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="ay")
|
|
|
|
def getIcon(self):
|
|
|
|
icon = self._parent.get_icon()
|
|
|
|
if not icon:
|
|
|
|
return ""
|
|
|
|
return icon
|
|
|
|
|
|
|
|
@dbus.service.method(BUDDY_DBUS_INTERFACE,
|
2006-07-26 15:38:54 +02:00
|
|
|
in_signature="s", out_signature="o")
|
2006-07-18 16:51:04 +02:00
|
|
|
def getServiceOfType(self, stype):
|
|
|
|
service = self._parent.get_service_of_type(stype)
|
|
|
|
if not service:
|
|
|
|
raise NotFoundError("Not found")
|
2006-07-26 15:38:54 +02:00
|
|
|
return service.object_path()
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
@dbus.service.method(BUDDY_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def getJoinedActivities(self):
|
|
|
|
acts = []
|
2006-08-25 20:55:19 +02:00
|
|
|
for act in self._parent.get_joined_activities():
|
|
|
|
acts.append(act.object_path())
|
2006-07-18 16:51:04 +02:00
|
|
|
return acts
|
|
|
|
|
|
|
|
@dbus.service.method(BUDDY_DBUS_INTERFACE,
|
|
|
|
in_signature="", out_signature="a{sv}")
|
|
|
|
def getProperties(self):
|
|
|
|
props = {}
|
2006-07-26 02:04:15 +02:00
|
|
|
props['name'] = self._parent.get_name()
|
2006-09-08 05:30:22 +02:00
|
|
|
addr = self._parent.get_address()
|
|
|
|
if addr:
|
|
|
|
props['ip4_address'] = addr
|
2006-07-18 16:51:04 +02:00
|
|
|
props['owner'] = self._parent.is_owner()
|
2006-08-24 17:09:53 +02:00
|
|
|
color = self._parent.get_color()
|
|
|
|
if color:
|
|
|
|
props['color'] = self._parent.get_color()
|
2006-07-18 16:51:04 +02:00
|
|
|
return props
|
|
|
|
|
|
|
|
|
|
|
|
class Buddy(object):
|
|
|
|
"""Represents another person on the network and keeps track of the
|
|
|
|
activities and resources they make available for sharing."""
|
|
|
|
|
2006-09-06 16:16:49 +02:00
|
|
|
def __init__(self, bus_name, object_id, service):
|
2006-07-18 16:51:04 +02:00
|
|
|
if not bus_name:
|
|
|
|
raise ValueError("DBus bus name must be valid")
|
|
|
|
if not object_id or type(object_id) != type(1):
|
|
|
|
raise ValueError("object id must be a valid number")
|
2006-09-06 16:16:49 +02:00
|
|
|
# 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):
|
|
|
|
raise ValueError("service must be a valid service object")
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
self._services = {}
|
|
|
|
self._activities = {}
|
|
|
|
|
2006-09-06 16:16:49 +02:00
|
|
|
self._nick_name = None
|
|
|
|
self._address = None
|
|
|
|
if service is not None:
|
|
|
|
self._nick_name = service.get_name()
|
|
|
|
self._address = service.get_source_address()
|
2006-08-23 17:14:46 +02:00
|
|
|
self._color = None
|
2006-07-18 16:51:04 +02:00
|
|
|
self._valid = False
|
|
|
|
self._icon = None
|
|
|
|
self._icon_tries = 0
|
|
|
|
|
|
|
|
self._object_id = object_id
|
2006-07-22 07:26:39 +02:00
|
|
|
self._object_path = BUDDY_DBUS_OBJECT_PATH + str(self._object_id)
|
2006-07-18 16:51:04 +02:00
|
|
|
self._dbus_helper = BuddyDBusHelper(self, bus_name, self._object_path)
|
|
|
|
|
2006-09-06 16:16:49 +02:00
|
|
|
if service is not None:
|
|
|
|
self.add_service(service)
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
def object_path(self):
|
2006-07-19 06:10:35 +02:00
|
|
|
return dbus.ObjectPath(self._object_path)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
def _request_buddy_icon_cb(self, result_status, response, user_data):
|
|
|
|
"""Callback when icon request has completed."""
|
2006-07-19 06:10:35 +02:00
|
|
|
from sugar.p2p import network
|
2006-07-18 16:51:04 +02:00
|
|
|
icon = response
|
|
|
|
service = user_data
|
|
|
|
if result_status == network.RESULT_SUCCESS:
|
|
|
|
if icon and len(icon):
|
|
|
|
icon = base64.b64decode(icon)
|
2006-08-25 22:09:54 +02:00
|
|
|
logging.debug("Buddy icon for '%s' is size %d" % (self._nick_name, len(icon)))
|
2006-07-18 16:51:04 +02:00
|
|
|
self._set_icon(icon)
|
|
|
|
|
|
|
|
if (result_status == network.RESULT_FAILED or not icon) and self._icon_tries < 3:
|
|
|
|
self._icon_tries = self._icon_tries + 1
|
2006-08-25 22:09:54 +02:00
|
|
|
logging.debug("Failed to retrieve buddy icon for '%s' on try %d of %d" % (self._nick_name, \
|
|
|
|
self._icon_tries, 3))
|
2006-07-18 16:51:04 +02:00
|
|
|
gobject.timeout_add(1000, self._request_buddy_icon, service)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _request_buddy_icon(self, service):
|
|
|
|
"""Contact the buddy to retrieve the buddy icon."""
|
2006-07-19 06:10:35 +02:00
|
|
|
from sugar.p2p import Stream
|
2006-07-18 16:51:04 +02:00
|
|
|
buddy_stream = Stream.Stream.new_from_service(service, start_reader=False)
|
|
|
|
writer = buddy_stream.new_writer(service)
|
|
|
|
success = writer.custom_request("get_buddy_icon", self._request_buddy_icon_cb, service)
|
|
|
|
if not success:
|
|
|
|
del writer, buddy_stream
|
|
|
|
gobject.timeout_add(1000, self._request_buddy_icon, service)
|
|
|
|
return False
|
|
|
|
|
2006-09-04 13:32:31 +02:00
|
|
|
def _get_service_key(self, service):
|
|
|
|
return (service.get_type(), service.get_activity_id())
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
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:
|
2006-09-12 18:58:24 +02:00
|
|
|
logging.error("Service and buddy nick names doesn't match: " \
|
|
|
|
"%s %s" % (service.get_name(), self._nick_name))
|
2006-07-18 16:51:04 +02:00
|
|
|
return False
|
2006-09-12 18:58:24 +02:00
|
|
|
|
2006-07-26 07:14:09 +02:00
|
|
|
source_addr = service.get_source_address()
|
|
|
|
if source_addr != self._address:
|
2006-09-12 18:58:24 +02:00
|
|
|
logging.error("Service source and buddy address doesn't " \
|
|
|
|
"match: %s %s" % (source_addr, self._address))
|
|
|
|
return False
|
|
|
|
return self._internal_add_service(service)
|
|
|
|
|
|
|
|
def _internal_add_service(self, service):
|
2006-09-04 13:32:31 +02:00
|
|
|
service_key = self._get_service_key(service)
|
|
|
|
if service_key in self._services.keys():
|
2006-09-12 18:58:24 +02:00
|
|
|
logging.error("Service already known: %s %s" % (service_key[0],
|
|
|
|
service_key[1]))
|
2006-07-18 16:51:04 +02:00
|
|
|
return False
|
2006-09-12 18:58:24 +02:00
|
|
|
|
|
|
|
logging.debug("Buddy %s added service type %s id %s" % (self._nick_name,
|
|
|
|
service.get_type(), service.get_activity_id()))
|
2006-09-04 13:32:31 +02:00
|
|
|
self._services[service_key] = service
|
2006-07-19 06:10:35 +02:00
|
|
|
service.set_owner(self)
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-09-04 13:32:31 +02:00
|
|
|
if service.get_type() == PRESENCE_SERVICE_TYPE:
|
2006-07-18 16:51:04 +02:00
|
|
|
# A buddy isn't valid until its official presence
|
|
|
|
# service has been found and resolved
|
|
|
|
self._valid = True
|
|
|
|
print 'Requesting buddy icon %s' % self._nick_name
|
|
|
|
self._request_buddy_icon(service)
|
2006-08-23 17:14:46 +02:00
|
|
|
self._color = service.get_one_property('color')
|
2006-09-08 05:30:22 +02:00
|
|
|
if self._color:
|
|
|
|
self._dbus_helper.PropertyChanged(['color'])
|
2006-07-19 06:10:35 +02:00
|
|
|
|
|
|
|
if self._valid:
|
|
|
|
self._dbus_helper.ServiceAppeared(service.object_path())
|
2006-07-18 16:51:04 +02:00
|
|
|
return True
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def add_activity(self, activity):
|
|
|
|
actid = activity.get_id()
|
|
|
|
if activity in self._activities.values():
|
|
|
|
raise RuntimeError("Tried to add activity twice")
|
|
|
|
found = False
|
|
|
|
for serv in self._services.values():
|
|
|
|
if serv.get_activity_id() == activity.get_id():
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
raise RuntimeError("Tried to add activity for which we had no service")
|
|
|
|
self._activities[actid] = activity
|
2006-08-25 20:55:19 +02:00
|
|
|
if activity.is_valid():
|
|
|
|
self._dbus_helper.JoinedActivity(activity.object_path())
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
def remove_service(self, service):
|
|
|
|
"""Remove a service from a buddy; ie, the activity was closed
|
|
|
|
or the buddy went away."""
|
2006-07-26 07:14:09 +02:00
|
|
|
if service.get_source_address() != self._address:
|
2006-07-18 16:51:04 +02:00
|
|
|
return
|
|
|
|
if service.get_name() != self._nick_name:
|
|
|
|
return
|
2006-09-04 13:32:31 +02:00
|
|
|
service_key = self._get_service_key(service)
|
|
|
|
if self._services.has_key(service_key):
|
2006-07-18 16:51:04 +02:00
|
|
|
if self._valid:
|
2006-07-19 06:10:35 +02:00
|
|
|
self._dbus_helper.ServiceDisappeared(service.object_path())
|
2006-09-04 13:32:31 +02:00
|
|
|
del self._services[service_key]
|
2006-07-18 16:51:04 +02:00
|
|
|
|
2006-09-04 13:32:31 +02:00
|
|
|
if service.get_type() == PRESENCE_SERVICE_TYPE:
|
2006-07-18 16:51:04 +02:00
|
|
|
self._valid = False
|
|
|
|
|
2006-07-19 06:10:35 +02:00
|
|
|
def remove_activity(self, activity):
|
|
|
|
actid = activity.get_id()
|
|
|
|
if not self._activities.has_key(actid):
|
|
|
|
return
|
|
|
|
del self._activities[actid]
|
2006-08-25 20:55:19 +02:00
|
|
|
if activity.is_valid():
|
|
|
|
self._dbus_helper.LeftActivity(activity.object_path())
|
|
|
|
|
|
|
|
def get_joined_activities(self):
|
|
|
|
acts = []
|
|
|
|
for act in self._activities.values():
|
|
|
|
if act.is_valid():
|
|
|
|
acts.append(act)
|
|
|
|
return acts
|
2006-07-19 06:10:35 +02:00
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
def get_service_of_type(self, stype=None, activity=None):
|
|
|
|
"""Return a service of a certain type, or None if the buddy
|
|
|
|
doesn't provide that service."""
|
|
|
|
if not stype:
|
|
|
|
raise RuntimeError("Need to specify a service type.")
|
|
|
|
|
2006-08-25 20:55:19 +02:00
|
|
|
if activity and not activity.is_valid():
|
|
|
|
raise RuntimeError("Activity is not yet valid.")
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
if activity:
|
2006-09-04 14:30:44 +02:00
|
|
|
key = (stype, activity.get_id())
|
|
|
|
else:
|
|
|
|
key = (stype, None)
|
|
|
|
if self._services.has_key(key):
|
|
|
|
return self._services[key]
|
2006-07-18 16:51:04 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def is_valid(self):
|
|
|
|
"""Return whether the buddy is valid or not. A buddy is
|
|
|
|
not valid until its official presence service has been found
|
|
|
|
and successfully resolved."""
|
|
|
|
return self._valid
|
|
|
|
|
|
|
|
def get_icon(self):
|
|
|
|
"""Return the buddies icon, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
def get_address(self):
|
|
|
|
return self._address
|
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
def get_name(self):
|
2006-07-18 16:51:04 +02:00
|
|
|
return self._nick_name
|
|
|
|
|
2006-08-23 17:14:46 +02:00
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
2006-07-18 16:51:04 +02:00
|
|
|
def _set_icon(self, icon):
|
|
|
|
"""Can only set icon for other buddies. The Owner
|
|
|
|
takes care of setting it's own icon."""
|
|
|
|
if icon != self._icon:
|
|
|
|
self._icon = icon
|
|
|
|
self._dbus_helper.IconChanged()
|
|
|
|
|
|
|
|
def is_owner(self):
|
2006-09-06 16:16:49 +02:00
|
|
|
return False
|
2006-07-18 16:51:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Owner(Buddy):
|
|
|
|
"""Class representing the owner of the machine. This is the client
|
|
|
|
portion of the Owner, paired with the server portion in Owner.py."""
|
2006-09-12 18:36:24 +02:00
|
|
|
def __init__(self, ps, bus_name, object_id):
|
2006-09-06 16:16:49 +02:00
|
|
|
Buddy.__init__(self, bus_name, object_id, None)
|
2006-09-12 18:36:24 +02:00
|
|
|
self._nick_name = env.get_nick_name()
|
2006-09-11 15:18:57 +02:00
|
|
|
self._color = env.get_color()
|
2006-09-06 16:16:49 +02:00
|
|
|
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:
|
2006-09-12 18:58:24 +02:00
|
|
|
logging.error("Service and buddy nick names doesn't match: " \
|
|
|
|
"%s %s" % (service.get_name(), self._nick_name))
|
2006-09-06 16:16:49 +02:00
|
|
|
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()
|
2006-09-12 18:58:24 +02:00
|
|
|
if self._address is None and service.is_local():
|
|
|
|
self._address = source_addr
|
|
|
|
self._dbus_helper.PropertyChanged(['ip4_address'])
|
|
|
|
|
|
|
|
# The owner bypasses address checks and only cares if
|
|
|
|
# avahi says the service is a local service
|
|
|
|
if not service.is_local():
|
|
|
|
logging.error("Cannot add remote service to owner object.")
|
|
|
|
return False
|
2006-09-12 18:48:32 +02:00
|
|
|
|
2006-09-12 18:36:24 +02:00
|
|
|
logging.debug("Adding owner service %s.%s at %s:%d." % (service.get_name(),
|
|
|
|
service.get_type(), service.get_source_address(),
|
|
|
|
service.get_port()))
|
2006-09-12 18:58:24 +02:00
|
|
|
return self._internal_add_service(service)
|
2006-09-06 16:16:49 +02:00
|
|
|
|
|
|
|
def is_owner(self):
|
|
|
|
return True
|
2006-07-22 07:26:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
#################################################################
|
|
|
|
# Tests
|
|
|
|
#################################################################
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import Service
|
|
|
|
|
|
|
|
__objid_seq = 0
|
|
|
|
def _next_objid():
|
|
|
|
global __objid_seq
|
|
|
|
__objid_seq = __objid_seq + 1
|
|
|
|
return __objid_seq
|
|
|
|
|
|
|
|
|
|
|
|
class BuddyTestCase(unittest.TestCase):
|
|
|
|
_DEF_NAME = u"Tommy"
|
|
|
|
_DEF_STYPE = unicode(PRESENCE_SERVICE_TYPE)
|
|
|
|
_DEF_DOMAIN = u"local"
|
|
|
|
_DEF_ADDRESS = u"1.1.1.1"
|
|
|
|
_DEF_PORT = 1234
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self._bus = dbus.SessionBus()
|
|
|
|
self._bus_name = dbus.service.BusName('org.laptop.Presence', bus=self._bus)
|
|
|
|
unittest.TestCase.__init__(self, name)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
del self._bus_name
|
|
|
|
del self._bus
|
|
|
|
|
|
|
|
def _test_init_fail(self, service, fail_msg):
|
|
|
|
"""Test something we expect to fail."""
|
|
|
|
try:
|
|
|
|
objid = _next_objid()
|
|
|
|
buddy = Buddy(self._bus_name, objid, service, owner=False)
|
|
|
|
except ValueError, exc:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("expected a ValueError for %s." % fail_msg)
|
|
|
|
|
|
|
|
def testService(self):
|
|
|
|
service = None
|
|
|
|
self._test_init_fail(service, "invalid service")
|
|
|
|
|
|
|
|
def testGoodInit(self):
|
|
|
|
objid = _next_objid()
|
|
|
|
service = Service.Service(self._bus_name, objid, self._DEF_NAME, self._DEF_STYPE, self._DEF_DOMAIN,
|
|
|
|
self._DEF_ADDRESS, self._DEF_PORT)
|
|
|
|
objid = _next_objid()
|
|
|
|
buddy = Buddy(self._bus_name, objid, service)
|
2006-07-26 02:04:15 +02:00
|
|
|
assert buddy.get_name() == self._DEF_NAME, "buddy name wasn't correct after init."
|
2006-07-22 07:26:39 +02:00
|
|
|
assert buddy.get_address() == self._DEF_ADDRESS, "buddy address wasn't correct after init."
|
|
|
|
assert buddy.object_path() == BUDDY_DBUS_OBJECT_PATH + str(objid)
|
|
|
|
|
|
|
|
def addToSuite(suite):
|
|
|
|
suite.addTest(BuddyTestCase("testService"))
|
|
|
|
suite.addTest(BuddyTestCase("testGoodInit"))
|
|
|
|
addToSuite = staticmethod(addToSuite)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
suite = unittest.TestSuite()
|
|
|
|
BuddyTestCase.addToSuite(suite)
|
|
|
|
runner = unittest.TextTestRunner()
|
|
|
|
runner.run(suite)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|