Remove unused files
This commit is contained in:
parent
ce1d1c879f
commit
37d6c00fc3
@ -1,125 +0,0 @@
|
||||
import pwd
|
||||
import os
|
||||
|
||||
import pygtk
|
||||
pygtk.require('2.0')
|
||||
import gtk, gobject
|
||||
|
||||
from Service import Service
|
||||
from sugar import env
|
||||
|
||||
PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp"
|
||||
PRESENCE_SERVICE_PORT = 6000
|
||||
|
||||
__buddy_service_types = [PRESENCE_SERVICE_TYPE]
|
||||
|
||||
def recognize_buddy_service_type(stype):
|
||||
if stype not in __buddy_service_types:
|
||||
__buddy_service_types.append(stype)
|
||||
|
||||
def get_recognized_buddy_service_types():
|
||||
return __buddy_service_types[:]
|
||||
|
||||
class Buddy(gobject.GObject):
|
||||
__gsignals__ = {
|
||||
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
||||
([]))
|
||||
}
|
||||
|
||||
def __init__(self, service):
|
||||
gobject.GObject.__init__(self)
|
||||
self._services = {}
|
||||
self._services[service.get_type()] = service
|
||||
self._nick_name = service.get_name()
|
||||
self._address = service.get_address()
|
||||
self._icon = None
|
||||
|
||||
def get_icon_pixbuf(self):
|
||||
if self._icon:
|
||||
pbl = gtk.gdk.PixbufLoader()
|
||||
pbl.write(self._icon)
|
||||
pbl.close()
|
||||
return pbl.get_pixbuf()
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_icon(self):
|
||||
"""Return the buddies icon, if any."""
|
||||
return self._icon
|
||||
|
||||
def get_address(self):
|
||||
return self._address
|
||||
|
||||
def add_service(self, service):
|
||||
if service.get_name() != self._nick_name:
|
||||
return False
|
||||
if service.get_address() != self._address:
|
||||
return False
|
||||
if self._services.has_key(service.get_type()):
|
||||
return False
|
||||
self._services[service.get_type()] = service
|
||||
|
||||
def remove_service(self, stype):
|
||||
if self._services.has_key(stype):
|
||||
del self._services[stype]
|
||||
|
||||
def get_service(self, stype):
|
||||
if self._services.has_key(stype):
|
||||
return self._services[stype]
|
||||
return None
|
||||
|
||||
def get_nick_name(self):
|
||||
return self._nick_name
|
||||
|
||||
def set_icon(self, icon):
|
||||
"""Can only set icon for other buddies. The Owner
|
||||
takes care of setting it's own icon."""
|
||||
self._icon = icon
|
||||
self.emit('icon-changed')
|
||||
|
||||
|
||||
class Owner(Buddy):
|
||||
"""Class representing the owner of this machine/instance."""
|
||||
def __init__(self, group):
|
||||
self._group = group
|
||||
|
||||
nick = env.get_nick_name()
|
||||
if not nick:
|
||||
nick = pwd.getpwuid(os.getuid())[0]
|
||||
if not nick or not len(nick):
|
||||
nick = "n00b"
|
||||
|
||||
self._presence_service = Service(nick, PRESENCE_SERVICE_TYPE, PRESENCE_SERVICE_PORT)
|
||||
Buddy.__init__(self, self._presence_service)
|
||||
|
||||
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.'
|
||||
|
||||
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
|
||||
|
||||
def set_icon(self, icon):
|
||||
"""Can only set icon in constructor for now."""
|
||||
pass
|
||||
|
||||
def register(self):
|
||||
self._presence_service.register(self._group)
|
||||
|
||||
def notify_service_registered(self, service):
|
||||
"""New services registered in our group are automatically owned
|
||||
by us."""
|
||||
self._services[service.get_type()] = service
|
||||
|
||||
def add_service(self, service):
|
||||
"""Do nothing here, since all services we need to know about
|
||||
are registered with us by our group."""
|
||||
pass
|
@ -1,163 +0,0 @@
|
||||
import avahi
|
||||
|
||||
from Buddy import Buddy
|
||||
from Buddy import get_recognized_buddy_service_types
|
||||
from Buddy import Owner
|
||||
from Buddy import PRESENCE_SERVICE_TYPE
|
||||
from Service import Service
|
||||
from sugar.p2p.model.Store import Store
|
||||
import presence
|
||||
|
||||
_OLPC_SERVICE_TYPE_PREFIX = "_olpc"
|
||||
|
||||
class Group:
|
||||
_groups = {}
|
||||
|
||||
SERVICE_ADDED = "service_added"
|
||||
SERVICE_REMOVED = "service_removed"
|
||||
|
||||
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 = []
|
||||
self._store = Store(self)
|
||||
|
||||
Group._groups[group_id] = self
|
||||
|
||||
def get_store(self):
|
||||
return self._store
|
||||
|
||||
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:
|
||||
listener(Group.SERVICE_ADDED, service)
|
||||
|
||||
def _notify_service_removed(self, service_id):
|
||||
for listener in self._service_listeners:
|
||||
listener(Group.SERVICE_REMOVED, service_id)
|
||||
|
||||
def _notify_buddy_join(self, buddy):
|
||||
for listener in self._presence_listeners:
|
||||
listener(Group.BUDDY_JOIN, buddy)
|
||||
|
||||
def _notify_buddy_leave(self, buddy):
|
||||
for listener in self._presence_listeners:
|
||||
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()
|
||||
|
||||
self._owner = Owner(self)
|
||||
self._add_buddy(self._owner)
|
||||
|
||||
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)
|
||||
|
||||
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):
|
||||
if self._services.has_key((name, stype)):
|
||||
return self._services[(name, stype)]
|
||||
return None
|
||||
|
||||
def get_buddy(self, name):
|
||||
if self._buddies.has_key(name):
|
||||
return self._buddies[name]
|
||||
return None
|
||||
|
||||
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)
|
||||
|
||||
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:
|
||||
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))
|
||||
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)
|
||||
|
||||
for prop in avahi.txt_array_to_string_array(txt):
|
||||
(key, value) = prop.split('=')
|
||||
if key == 'group_address':
|
||||
service.set_group_address(value)
|
||||
|
||||
#print "ServiceResolved: name=%s, stype=%s, port=%s, address=%s" % (name, stype, port, address)
|
||||
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)
|
||||
elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX):
|
||||
# These services aren't associated with buddies
|
||||
self.add_service(service)
|
||||
|
@ -1,46 +0,0 @@
|
||||
import presence
|
||||
|
||||
class Service(object):
|
||||
def __init__(self, name, stype, port, group_address = None):
|
||||
self._name = name
|
||||
self._stype = stype
|
||||
self._port = int(port)
|
||||
self._address = ''
|
||||
self._group_address = group_address
|
||||
|
||||
def get_name(self):
|
||||
return self._name
|
||||
|
||||
def get_type(self):
|
||||
return self._stype
|
||||
|
||||
def get_port(self):
|
||||
return self._port
|
||||
|
||||
def set_port(self, port):
|
||||
self._port = port
|
||||
|
||||
def get_address(self):
|
||||
return self._address
|
||||
|
||||
def get_group_address(self):
|
||||
return self._group_address
|
||||
|
||||
def set_address(self, address):
|
||||
self._address = address
|
||||
|
||||
def set_group_address(self, group_address):
|
||||
self._group_address = group_address
|
||||
|
||||
def is_multicast(self):
|
||||
return self._group_address != None
|
||||
|
||||
def register(self, group):
|
||||
pannounce = presence.PresenceAnnounce()
|
||||
if self._group_address:
|
||||
pannounce.register_service(self._name, self._port, self._stype,
|
||||
group_address = self._group_address)
|
||||
else:
|
||||
pannounce.register_service(self._name, self._port, self._stype)
|
||||
# Notify the group that we're registering a new service
|
||||
group.notify_service_registered(self)
|
Loading…
Reference in New Issue
Block a user