More code cleanups and refactoring
This commit is contained in:
parent
afb9b2c97f
commit
18f9a43716
29
chat/chat.py
29
chat/chat.py
@ -24,6 +24,13 @@ except ImportError:
|
||||
|
||||
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):
|
||||
def __init__(self, controller):
|
||||
self._controller = controller
|
||||
@ -216,17 +223,24 @@ class GroupChat(Chat):
|
||||
|
||||
def _start(self):
|
||||
self._group = LocalGroup()
|
||||
self._group.add_listener(self._on_group_event)
|
||||
self._group.add_presence_listener(self._on_group_event)
|
||||
self._group.join()
|
||||
|
||||
owner_service = self._group.get_owner().get_service_name()
|
||||
self._buddy_reader = StreamReader(self._group, owner_service)
|
||||
name = self._group.get_owner().get_service_name()
|
||||
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)
|
||||
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._stream_writer = StreamWriter(self._group, "localgroup_multicast")
|
||||
self._stream_writer = StreamWriter(self._group, service)
|
||||
|
||||
def _create_sidebar(self):
|
||||
vbox = gtk.VBox(False, 6)
|
||||
@ -311,7 +325,10 @@ class GroupChat(Chat):
|
||||
chat.activity_connect_to_shell()
|
||||
|
||||
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)
|
||||
self._buddy_list_model.set(aniter, self._MODEL_COL_NICK, buddy.get_nick_name(),
|
||||
self._MODEL_COL_ICON, None, self._MODEL_COL_BUDDY, buddy)
|
||||
|
27
p2p/Buddy.py
27
p2p/Buddy.py
@ -2,22 +2,15 @@ import pwd
|
||||
import os
|
||||
|
||||
from Service import *
|
||||
import presence
|
||||
|
||||
BUDDY_SERVICE_TYPE = "_olpc_buddy._tcp"
|
||||
BUDDY_SERVICE_PORT = 666
|
||||
|
||||
GROUP_SERVICE_TYPE = "_olpc_buddy._udp"
|
||||
GROUP_SERVICE_PORT = 6666
|
||||
PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp"
|
||||
PRESENCE_SERVICE_PORT = 6000
|
||||
|
||||
class Buddy:
|
||||
def __init__(self, service, nick_name):
|
||||
self._service = service
|
||||
self._nick_name = nick_name
|
||||
|
||||
def get_service(self):
|
||||
return self._service
|
||||
|
||||
def get_service_name(self):
|
||||
return self._service.get_name()
|
||||
|
||||
@ -25,19 +18,17 @@ class Buddy:
|
||||
return self._nick_name
|
||||
|
||||
class Owner(Buddy):
|
||||
def __init__(self):
|
||||
ent = pwd.getpwuid(os.getuid())
|
||||
nick = ent[0]
|
||||
def __init__(self, group):
|
||||
self._group = group
|
||||
|
||||
nick = pwd.getpwuid(os.getuid())[0]
|
||||
if not nick or not len(nick):
|
||||
nick = "n00b"
|
||||
|
||||
service = Service(nick, '', '', GROUP_SERVICE_PORT)
|
||||
service = Service(nick, PRESENCE_SERVICE_TYPE,
|
||||
'', PRESENCE_SERVICE_PORT)
|
||||
|
||||
Buddy.__init__(self, service, nick)
|
||||
|
||||
def register(self):
|
||||
pannounce = presence.PresenceAnnounce()
|
||||
pannounce.register_service(self._nick_name,
|
||||
BUDDY_SERVICE_PORT,
|
||||
BUDDY_SERVICE_TYPE,
|
||||
nickname = self._nick_name)
|
||||
self._service.register(self._group)
|
||||
|
102
p2p/Group.py
102
p2p/Group.py
@ -4,26 +4,41 @@ import presence
|
||||
from Buddy import *
|
||||
from Service import *
|
||||
|
||||
BUDDY_JOIN = "join"
|
||||
BUDDY_LEAVE = "leave"
|
||||
SERVICE_ADDED = "service_added"
|
||||
SERVICE_REMOVED = "service_removed"
|
||||
|
||||
BUDDY_JOIN = "buddy_join"
|
||||
BUDDY_LEAVE = "buddy_leave"
|
||||
|
||||
class Group:
|
||||
def __init__(self):
|
||||
self._listeners = []
|
||||
self._service_listeners = []
|
||||
self._presence_listeners = []
|
||||
|
||||
def join(self, buddy):
|
||||
pass
|
||||
|
||||
def add_listener(self, group_listener):
|
||||
self._listeners.append(group_listener)
|
||||
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(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):
|
||||
for listener in self._listeners:
|
||||
for listener in self._presence_listeners:
|
||||
listener(BUDDY_JOIN, buddy)
|
||||
|
||||
def _notify_buddy_leave(self, buddy):
|
||||
for listener in self._listeners:
|
||||
listener(BUDDY_LEAVE,buddy)
|
||||
for listener in self._presence_listeners:
|
||||
listener(BUDDY_LEAVE, buddy)
|
||||
|
||||
class LocalGroup(Group):
|
||||
def __init__(self):
|
||||
@ -39,58 +54,49 @@ class LocalGroup(Group):
|
||||
def get_owner(self):
|
||||
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):
|
||||
self._owner = Owner()
|
||||
self._owner = Owner(self)
|
||||
self._owner.register()
|
||||
|
||||
def get_service_from_name(self, name):
|
||||
if name == 'localgroup_multicast':
|
||||
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_service(self, name, stype):
|
||||
return self._services[(name, stype)]
|
||||
|
||||
def get_buddy_from_address(self, address):
|
||||
for buddy in self._buddies.values():
|
||||
if buddy.get_service().get_address() == address:
|
||||
return buddy
|
||||
return None
|
||||
def get_buddy(self, name):
|
||||
return self._buddy[name]
|
||||
|
||||
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, 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):
|
||||
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:
|
||||
del self._services[name]
|
||||
if stype == PRESENCE_SERVICE_TYPE:
|
||||
self._remove_buddy(name)
|
||||
elif stype.startswith("_olpc"):
|
||||
self.remove_service((name, stype))
|
||||
|
||||
def _on_service_resolved(self, interface, protocol, name, stype, domain,
|
||||
host, aprotocol, address, port, txt, flags):
|
||||
if name != self._owner.get_service().get_name():
|
||||
service = Service(name, host, address, port)
|
||||
self._services[name] = service
|
||||
if stype == BUDDY_SERVICE_TYPE:
|
||||
data = self._pair_to_dict(avahi.txt_array_to_string_array(txt))
|
||||
self._add_buddy(service, data)
|
||||
|
||||
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
|
||||
if stype == PRESENCE_SERVICE_TYPE:
|
||||
self._add_buddy(Buddy(service, name))
|
||||
elif stype.startswith("_olpc"):
|
||||
self.add_service(service)
|
||||
|
@ -1,7 +1,9 @@
|
||||
import presence
|
||||
|
||||
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._host = host
|
||||
self._stype = stype
|
||||
self._address = str(address)
|
||||
self._port = int(port)
|
||||
self._multicast = multicast
|
||||
@ -9,8 +11,8 @@ class Service(object):
|
||||
def get_name(self):
|
||||
return self._name
|
||||
|
||||
def get_host(self):
|
||||
return self._host
|
||||
def get_type(self):
|
||||
return self._stype
|
||||
|
||||
def get_address(self):
|
||||
return self._address
|
||||
@ -20,3 +22,7 @@ class Service(object):
|
||||
|
||||
def is_multicast(self):
|
||||
return self._multicast
|
||||
|
||||
def register(self, group):
|
||||
pannounce = presence.PresenceAnnounce()
|
||||
pannounce.register_service(self._name, self._port, self._stype)
|
||||
|
@ -10,11 +10,10 @@ class StreamReaderRequestHandler(object):
|
||||
return True
|
||||
|
||||
class StreamReader:
|
||||
def __init__(self, group, service_name):
|
||||
def __init__(self, group, service):
|
||||
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():
|
||||
self._setup_multicast()
|
||||
else:
|
||||
|
@ -5,10 +5,9 @@ import socket
|
||||
import network
|
||||
|
||||
class StreamWriter:
|
||||
def __init__(self, group, service_name):
|
||||
def __init__(self, group, service):
|
||||
self._group = group
|
||||
self._service_name = service_name
|
||||
self._service = group.get_service_from_name(service_name)
|
||||
self._service = service
|
||||
self._address = self._service.get_address()
|
||||
self._port = self._service.get_port()
|
||||
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
import avahi, dbus, dbus.glib
|
||||
|
||||
OLPC_CHAT_SERVICE = "_olpc_chat._udp"
|
||||
|
||||
ACTION_SERVICE_NEW = 'new'
|
||||
ACTION_SERVICE_REMOVED = 'removed'
|
||||
|
||||
@ -81,7 +79,6 @@ class PresenceAnnounce(object):
|
||||
|
||||
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)
|
||||
|
||||
if rs_name is None:
|
||||
if self._hostname is None:
|
||||
self._hostname = "%s:%s" % (self.server.GetHostName(), rs_port)
|
||||
|
Loading…
Reference in New Issue
Block a user