More code cleanups and refactoring

This commit is contained in:
Marco Pesenti Gritti 2006-05-11 21:56:23 -04:00
parent afb9b2c97f
commit 18f9a43716
7 changed files with 103 additions and 88 deletions

View File

@ -24,6 +24,13 @@ except ImportError:
import richtext import richtext
CHAT_SERVICE_TYPE = "_olpc_chat._tcp"
CHAT_SERVICE_PORT = 6100
GROUP_CHAT_SERVICE_TYPE = "_olpc_group_chat._udp"
GROUP_CHAT_SERVICE_ADDRESS = "224.0.0.221"
GROUP_CHAT_SERVICE_PORT = 6200
class Chat(activity.Activity): class Chat(activity.Activity):
def __init__(self, controller): def __init__(self, controller):
self._controller = controller self._controller = controller
@ -216,17 +223,24 @@ class GroupChat(Chat):
def _start(self): def _start(self):
self._group = LocalGroup() self._group = LocalGroup()
self._group.add_listener(self._on_group_event) self._group.add_presence_listener(self._on_group_event)
self._group.join() self._group.join()
owner_service = self._group.get_owner().get_service_name() name = self._group.get_owner().get_service_name()
self._buddy_reader = StreamReader(self._group, owner_service) service = Service(name, CHAT_SERVICE_TYPE, '', CHAT_SERVICE_PORT)
self._buddy_reader = StreamReader(self._group, service)
self._buddy_reader.set_listener(self._buddy_recv_message) self._buddy_reader.set_listener(self._buddy_recv_message)
service.register(self._group)
self._buddy_reader = StreamReader(self._group, "localgroup_multicast") service = Service(name, GROUP_CHAT_SERVICE_TYPE,
GROUP_CHAT_SERVICE_ADDRESS,
GROUP_CHAT_SERVICE_PORT, True)
self._group.add_service(service)
self._buddy_reader = StreamReader(self._group, service)
self._buddy_reader.set_listener(self.recv_message) self._buddy_reader.set_listener(self.recv_message)
self._stream_writer = StreamWriter(self._group, "localgroup_multicast") self._stream_writer = StreamWriter(self._group, service)
def _create_sidebar(self): def _create_sidebar(self):
vbox = gtk.VBox(False, 6) vbox = gtk.VBox(False, 6)
@ -311,7 +325,10 @@ class GroupChat(Chat):
chat.activity_connect_to_shell() chat.activity_connect_to_shell()
def _on_group_event(self, action, buddy): def _on_group_event(self, action, buddy):
if action == BUDDY_JOIN: if buddy.get_nick_name() == self._group.get_owner().get_nick_name():
# Do not show ourself in the buddy list
pass
elif action == BUDDY_JOIN:
aniter = self._buddy_list_model.append(None) aniter = self._buddy_list_model.append(None)
self._buddy_list_model.set(aniter, self._MODEL_COL_NICK, buddy.get_nick_name(), self._buddy_list_model.set(aniter, self._MODEL_COL_NICK, buddy.get_nick_name(),
self._MODEL_COL_ICON, None, self._MODEL_COL_BUDDY, buddy) self._MODEL_COL_ICON, None, self._MODEL_COL_BUDDY, buddy)

View File

@ -2,22 +2,15 @@ import pwd
import os import os
from Service import * from Service import *
import presence
BUDDY_SERVICE_TYPE = "_olpc_buddy._tcp" PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp"
BUDDY_SERVICE_PORT = 666 PRESENCE_SERVICE_PORT = 6000
GROUP_SERVICE_TYPE = "_olpc_buddy._udp"
GROUP_SERVICE_PORT = 6666
class Buddy: class Buddy:
def __init__(self, service, nick_name): def __init__(self, service, nick_name):
self._service = service self._service = service
self._nick_name = nick_name self._nick_name = nick_name
def get_service(self):
return self._service
def get_service_name(self): def get_service_name(self):
return self._service.get_name() return self._service.get_name()
@ -25,19 +18,17 @@ class Buddy:
return self._nick_name return self._nick_name
class Owner(Buddy): class Owner(Buddy):
def __init__(self): def __init__(self, group):
ent = pwd.getpwuid(os.getuid()) self._group = group
nick = ent[0]
nick = pwd.getpwuid(os.getuid())[0]
if not nick or not len(nick): if not nick or not len(nick):
nick = "n00b" nick = "n00b"
service = Service(nick, '', '', GROUP_SERVICE_PORT) service = Service(nick, PRESENCE_SERVICE_TYPE,
'', PRESENCE_SERVICE_PORT)
Buddy.__init__(self, service, nick) Buddy.__init__(self, service, nick)
def register(self): def register(self):
pannounce = presence.PresenceAnnounce() self._service.register(self._group)
pannounce.register_service(self._nick_name,
BUDDY_SERVICE_PORT,
BUDDY_SERVICE_TYPE,
nickname = self._nick_name)

View File

@ -4,26 +4,41 @@ import presence
from Buddy import * from Buddy import *
from Service import * from Service import *
BUDDY_JOIN = "join" SERVICE_ADDED = "service_added"
BUDDY_LEAVE = "leave" SERVICE_REMOVED = "service_removed"
BUDDY_JOIN = "buddy_join"
BUDDY_LEAVE = "buddy_leave"
class Group: class Group:
def __init__(self): def __init__(self):
self._listeners = [] self._service_listeners = []
self._presence_listeners = []
def join(self, buddy): def join(self, buddy):
pass pass
def add_listener(self, group_listener): def add_service_listener(self, listener):
self._listeners.append(group_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(SERVICE_ADDED, buddy)
def _notify_service_removed(self, service):
for listener in self._service_listeners:
listener(SERVICE_REMOVED,buddy)
def _notify_buddy_join(self, buddy): def _notify_buddy_join(self, buddy):
for listener in self._listeners: for listener in self._presence_listeners:
listener(BUDDY_JOIN, buddy) listener(BUDDY_JOIN, buddy)
def _notify_buddy_leave(self, buddy): def _notify_buddy_leave(self, buddy):
for listener in self._listeners: for listener in self._presence_listeners:
listener(BUDDY_LEAVE,buddy) listener(BUDDY_LEAVE, buddy)
class LocalGroup(Group): class LocalGroup(Group):
def __init__(self): def __init__(self):
@ -39,58 +54,49 @@ class LocalGroup(Group):
def get_owner(self): def get_owner(self):
return self._owner return self._owner
def add_service(self, service):
sid = (service.get_name(), service.get_type())
self._services[sid] = service
self._notify_service_added(service)
def remove_service(self, sid):
self._notify_service_removed(service)
del self._services[sid]
def join(self): def join(self):
self._owner = Owner() self._owner = Owner(self)
self._owner.register() self._owner.register()
def get_service_from_name(self, name): def get_service(self, name, stype):
if name == 'localgroup_multicast': return self._services[(name, stype)]
return Service('localgroup_multicast', '', '224.0.0.221', 6666, True)
elif name == self._owner.get_service().get_name():
return self._owner.get_service()
else:
return self._services[name]
def get_buddy_from_address(self, address): def get_buddy(self, name):
for buddy in self._buddies.values(): return self._buddy[name]
if buddy.get_service().get_address() == address:
return buddy def _add_buddy(self, buddy):
return None 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, buddy):
self._notify_buddy_leave(buddy)
del self._buddies[buddy.get_nick_name()]
def _on_service_change(self, action, interface, protocol, name, stype, domain, flags): def _on_service_change(self, action, interface, protocol, name, stype, domain, flags):
if action == presence.ACTION_SERVICE_NEW: if action == presence.ACTION_SERVICE_NEW:
self._pdiscovery.resolve_service(interface, protocol, name, stype, domain, self._pdiscovery.resolve_service(interface, protocol, name, stype, domain,
self._on_service_resolved) self._on_service_resolved)
elif action == presence.ACTION_SERVICE_REMOVED: elif action == presence.ACTION_SERVICE_REMOVED:
del self._services[name] if stype == PRESENCE_SERVICE_TYPE:
self._remove_buddy(name) self._remove_buddy(name)
elif stype.startswith("_olpc"):
self.remove_service((name, stype))
def _on_service_resolved(self, interface, protocol, name, stype, domain, def _on_service_resolved(self, interface, protocol, name, stype, domain,
host, aprotocol, address, port, txt, flags): host, aprotocol, address, port, txt, flags):
if name != self._owner.get_service().get_name():
service = Service(name, host, address, port) service = Service(name, host, address, port)
self._services[name] = service if stype == PRESENCE_SERVICE_TYPE:
if stype == BUDDY_SERVICE_TYPE: self._add_buddy(Buddy(service, name))
data = self._pair_to_dict(avahi.txt_array_to_string_array(txt)) elif stype.startswith("_olpc"):
self._add_buddy(service, data) self.add_service(service)
def _add_buddy(self, service, data):
name = service.get_name()
if not self._buddies.has_key(name):
buddy = Buddy(service, data['nickname'])
self._buddies[name] = buddy
self._notify_buddy_join(buddy)
def _remove_buddy(self, name):
self._notify_buddy_leave(self._buddies[name])
del self._buddies[name]
def _pair_to_dict(self, l):
res = {}
for el in l:
tmp = el.split('=', 1)
if len(tmp) > 1:
res[tmp[0]] = tmp[1]
else:
res[tmp[0]] = ''
return res

View File

@ -1,7 +1,9 @@
import presence
class Service(object): class Service(object):
def __init__(self, name, host, address, port, multicast=False): def __init__(self, name, stype, address, port, multicast=False):
self._name = name self._name = name
self._host = host self._stype = stype
self._address = str(address) self._address = str(address)
self._port = int(port) self._port = int(port)
self._multicast = multicast self._multicast = multicast
@ -9,8 +11,8 @@ class Service(object):
def get_name(self): def get_name(self):
return self._name return self._name
def get_host(self): def get_type(self):
return self._host return self._stype
def get_address(self): def get_address(self):
return self._address return self._address
@ -20,3 +22,7 @@ class Service(object):
def is_multicast(self): def is_multicast(self):
return self._multicast return self._multicast
def register(self, group):
pannounce = presence.PresenceAnnounce()
pannounce.register_service(self._name, self._port, self._stype)

View File

@ -10,11 +10,10 @@ class StreamReaderRequestHandler(object):
return True return True
class StreamReader: class StreamReader:
def __init__(self, group, service_name): def __init__(self, group, service):
self._group = group self._group = group
self._service_name = service_name self._service = service
self._service = group.get_service_from_name(service_name)
if self._service.is_multicast(): if self._service.is_multicast():
self._setup_multicast() self._setup_multicast()
else: else:

View File

@ -5,10 +5,9 @@ import socket
import network import network
class StreamWriter: class StreamWriter:
def __init__(self, group, service_name): def __init__(self, group, service):
self._group = group self._group = group
self._service_name = service_name self._service = service
self._service = group.get_service_from_name(service_name)
self._address = self._service.get_address() self._address = self._service.get_address()
self._port = self._service.get_port() self._port = self._service.get_port()

View File

@ -2,8 +2,6 @@
import avahi, dbus, dbus.glib import avahi, dbus, dbus.glib
OLPC_CHAT_SERVICE = "_olpc_chat._udp"
ACTION_SERVICE_NEW = 'new' ACTION_SERVICE_NEW = 'new'
ACTION_SERVICE_REMOVED = 'removed' ACTION_SERVICE_REMOVED = 'removed'
@ -81,7 +79,6 @@ class PresenceAnnounce(object):
def register_service(self, rs_name, rs_port, rs_service, **kwargs): def register_service(self, rs_name, rs_port, rs_service, **kwargs):
g = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) g = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
if rs_name is None: if rs_name is None:
if self._hostname is None: if self._hostname is None:
self._hostname = "%s:%s" % (self.server.GetHostName(), rs_port) self._hostname = "%s:%s" % (self.server.GetHostName(), rs_port)