From b4413fe3c25e1f709d261bd4db0935f98f378924 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 18 Jun 2006 01:05:40 -0400 Subject: [PATCH] Some work to make group chat work again --- sugar/browser/BrowserActivity.py | 2 +- sugar/chat/GroupChat.py | 60 +++++++++++++------------------ sugar/p2p/Stream.py | 3 +- sugar/presence/PresenceService.py | 1 + sugar/shell/shell.py | 3 +- 5 files changed, 30 insertions(+), 39 deletions(-) diff --git a/sugar/browser/BrowserActivity.py b/sugar/browser/BrowserActivity.py index 905b4eb4..f4bb2bd8 100644 --- a/sugar/browser/BrowserActivity.py +++ b/sugar/browser/BrowserActivity.py @@ -43,7 +43,6 @@ class BrowserActivity(activity.Activity): self._model = None def _service_appeared_cb(self, pservice, buddy, service): - print 'appeared ' + service.get_type() if service.get_type() == _BROWSER_ACTIVITY_TYPE: self._notif_service = service elif service.get_type() == LocalModel.SERVICE_TYPE: @@ -112,6 +111,7 @@ class BrowserActivity(activity.Activity): vbox.show() # FIXME remove, when we join the activity this will happen automatically + # (Once we have a global presence service) self._pservice.track_activity(self.get_id()) def get_embed(self): diff --git a/sugar/chat/GroupChat.py b/sugar/chat/GroupChat.py index 94200945..c77525af 100644 --- a/sugar/chat/GroupChat.py +++ b/sugar/chat/GroupChat.py @@ -1,53 +1,41 @@ +import logging + from sugar.chat.Chat import Chat from sugar.presence.Service import Service from sugar.p2p.Stream import Stream - -GROUP_CHAT_SERVICE_TYPE = "_olpc_group_chat._udp" -GROUP_CHAT_SERVICE_ADDRESS = "224.0.0.221" -GROUP_CHAT_SERVICE_PORT = 6200 +from sugar.presence.PresenceService import PresenceService class GroupChat(Chat): - def __init__(self): + SERVICE_TYPE = "_olpc_group_chat._udp" + SERVICE_PORT = 6200 + + def __init__(self, activity): Chat.__init__(self) self._chats = {} + self._activity = activity - def get_group(self): - return self._group + self._pservice = PresenceService.get_instance() + self._pservice.start() + self._pservice.connect('service-appeared', self._service_appeared_cb) + self._pservice.track_service_type(GroupChat.SERVICE_TYPE) - def new_buddy_writer(self, buddy): - service = buddy.get_service(Chat.SERVICE_TYPE) - return self._buddy_stream.new_writer(service) + # FIXME remove, when we join the activity this will happen automatically + # (Once we have a global presence service) + self._pservice.track_activity(activity.get_id()) - def _start(self): - name = self._group.get_owner().get_nick_name() + def _service_appeared_cb(self, pservice, buddy, service): + if service.get_type() == GroupChat.SERVICE_TYPE: + logging.debug('Group chat service appeared, setup the stream.') + self._setup_stream(service) - # Group controls the Stream for incoming messages for - # specific buddy chats - buddy_service = Service(name, Chat.SERVICE_TYPE, Chat.SERVICE_PORT) - self._buddy_stream = Stream.new_from_service(buddy_service, self._group) - self._buddy_stream.set_data_listener(getattr(self, "_buddy_recv_message")) - buddy_service.register(self._group) + def publish(self): + service = self._pservice.share_activity(self._activity, + stype = GroupChat.SERVICE_TYPE, port = GroupChat.SERVICE_PORT) - # Group chat Stream - group_service = Service(name, - GROUP_CHAT_SERVICE_TYPE, - GROUP_CHAT_SERVICE_PORT, - GROUP_CHAT_SERVICE_ADDRESS) - self._group.add_service(group_service) - - self._group_stream = Stream.new_from_service(group_service, self._group) + def _setup_stream(self, service): + self._group_stream = Stream.new_from_service(service) self._group_stream.set_data_listener(self._group_recv_message) self._stream_writer = self._group_stream.new_writer() def _group_recv_message(self, buddy, msg): self.recv_message(buddy, msg) - - def _buddy_recv_message(self, buddy, msg): - if not self._chats.has_key(buddy): -# chat = BuddyChat(self, buddy) -# self._chats[buddy] = chat -# chat.connect_to_shell() - pass - else: - chat = self._chats[buddy] - chat.recv_message(buddy, msg) diff --git a/sugar/p2p/Stream.py b/sugar/p2p/Stream.py index 45d61a50..af770a23 100644 --- a/sugar/p2p/Stream.py +++ b/sugar/p2p/Stream.py @@ -126,13 +126,14 @@ class UnicastStream(Stream): class MulticastStream(Stream): def __init__(self, service): Stream.__init__(self, service) + self._service = service self._internal_start_reader() def start_reader(self): return self._reader_port def _internal_start_reader(self): - if not service.get_address(): + if not self._service.get_address(): raise ValueError("service must have a valid address.") self._pipe = MostlyReliablePipe('', self._address, self._reader_port, self._recv_data_cb) diff --git a/sugar/presence/PresenceService.py b/sugar/presence/PresenceService.py index b76b49c2..ac3b6b7d 100644 --- a/sugar/presence/PresenceService.py +++ b/sugar/presence/PresenceService.py @@ -506,6 +506,7 @@ class PresenceService(gobject.GObject): def share_activity(self, activity, stype, properties={}, address=None, port=None): """Convenience function to share an activity with other buddies.""" uid = activity.get_id() + logging.debug('Sharing activity uid %s, stype %s' % (uid, stype)) owner_nick = self._owner.get_nick_name() real_stype = Service.compose_service_type(stype, uid) if address and type(address) != type(""): diff --git a/sugar/shell/shell.py b/sugar/shell/shell.py index 803d3a8d..630bde5c 100755 --- a/sugar/shell/shell.py +++ b/sugar/shell/shell.py @@ -80,7 +80,7 @@ class ActivityHost(dbus.service.Object): self._create_chat() def _create_chat(self): - self._group_chat = GroupChat() + self._group_chat = GroupChat(self) def get_chat(self): return self._group_chat @@ -95,6 +95,7 @@ class ActivityHost(dbus.service.Object): pass def publish(self): + self._group_chat.publish() self.peer_service.publish() def tab_close_button_clicked(self, button):