Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/shell/Owner.py
blob: bd8771d3f23c5c4349971e32f34f1be9a1356ba6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import random
import base64

from sugar import env
from sugar.presence import Service
from sugar.presence import Buddy
from sugar.presence import PresenceService
from sugar.p2p import Stream

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

		# Our presence service
		port = random.randint(40000, 65000)
		properties = {}
		self._service = Service.Service(nick, Buddy.PRESENCE_SERVICE_TYPE,
			domain="", address=None, port=port, properties=properties)
		print "Owner '%s' using port %d" % (nick, port)

		self._icon_stream = Stream.Stream.new_from_service(self._service)
		self._icon_stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon")

		# Announce ourselves to the world
		self._pservice = PresenceService.PresenceService.get_instance()
		self._pservice.start()
		self._pservice.register_service(self._service)

	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