2006-06-18 07:05:40 +02:00
|
|
|
import logging
|
|
|
|
|
2006-06-15 05:24:11 +02:00
|
|
|
from sugar.chat.Chat import Chat
|
2006-06-15 17:18:33 +02:00
|
|
|
from sugar.presence.Service import Service
|
|
|
|
from sugar.p2p.Stream import Stream
|
2006-06-18 07:05:40 +02:00
|
|
|
from sugar.presence.PresenceService import PresenceService
|
2006-06-15 05:24:11 +02:00
|
|
|
|
|
|
|
class GroupChat(Chat):
|
2006-06-18 07:05:40 +02:00
|
|
|
SERVICE_TYPE = "_olpc_group_chat._udp"
|
|
|
|
SERVICE_PORT = 6200
|
|
|
|
|
|
|
|
def __init__(self, activity):
|
2006-06-15 22:51:30 +02:00
|
|
|
Chat.__init__(self)
|
2006-06-15 05:24:11 +02:00
|
|
|
self._chats = {}
|
2006-06-18 07:05:40 +02:00
|
|
|
self._activity = activity
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
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)
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
# 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())
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
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)
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
def publish(self):
|
|
|
|
service = self._pservice.share_activity(self._activity,
|
|
|
|
stype = GroupChat.SERVICE_TYPE, port = GroupChat.SERVICE_PORT)
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
def _setup_stream(self, service):
|
|
|
|
self._group_stream = Stream.new_from_service(service)
|
2006-06-15 05:24:11 +02:00
|
|
|
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)
|