2006-06-20 09:19:33 +02:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import base64
|
|
|
|
|
2006-06-13 00:51:40 +02:00
|
|
|
from sugar import env
|
|
|
|
from sugar.p2p import Stream
|
2006-07-23 06:56:40 +02:00
|
|
|
from sugar.presence import PresenceService
|
|
|
|
|
|
|
|
PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
|
2006-06-16 19:19:35 +02:00
|
|
|
|
2006-06-13 00:51:40 +02:00
|
|
|
class ShellOwner(object):
|
|
|
|
"""Class representing the owner of this machine/instance. This class
|
|
|
|
runs in the shell and serves up the buddy icon and other stuff. It's the
|
|
|
|
server portion of the Owner, paired with the client portion in Buddy.py."""
|
|
|
|
def __init__(self):
|
|
|
|
nick = env.get_nick_name()
|
|
|
|
user_dir = env.get_user_dir()
|
|
|
|
if not os.path.exists(user_dir):
|
|
|
|
try:
|
|
|
|
os.makedirs(user_dir)
|
|
|
|
except OSError:
|
|
|
|
print "Could not create user directory."
|
|
|
|
|
|
|
|
self._icon = None
|
|
|
|
for fname in os.listdir(user_dir):
|
|
|
|
if not fname.startswith("buddy-icon."):
|
|
|
|
continue
|
|
|
|
fd = open(os.path.join(user_dir, fname), "r")
|
|
|
|
self._icon = fd.read()
|
|
|
|
fd.close()
|
|
|
|
break
|
|
|
|
|
2006-07-23 06:56:40 +02:00
|
|
|
# Create and announce our presence
|
|
|
|
self._pservice = PresenceService.PresenceService()
|
2006-07-23 16:21:00 +02:00
|
|
|
self._service = self._pservice.register_service(nick, PRESENCE_SERVICE_TYPE)
|
2006-07-23 06:57:18 +02:00
|
|
|
print "Owner '%s' using port %d" % (nick, self._service.get_port())
|
2006-07-23 06:56:40 +02:00
|
|
|
self._icon_stream = Stream.Stream.new_from_service(self._service)
|
|
|
|
self._icon_stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon")
|
2006-06-13 00:51:40 +02:00
|
|
|
|
|
|
|
def _handle_buddy_icon_request(self):
|
|
|
|
"""XMLRPC method, return the owner's icon encoded with base64."""
|
|
|
|
if self._icon:
|
|
|
|
return base64.b64encode(self._icon)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def set_icon(self, icon):
|
|
|
|
"""Can only set icon in constructor for now."""
|
|
|
|
pass
|
|
|
|
|