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.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-20 03:17:58 +02:00
|
|
|
service = self._pservice.get_activity_service(activity, GroupChat.SERVICE_TYPE)
|
|
|
|
if service is not None:
|
2006-06-20 03:24:24 +02:00
|
|
|
self._service_appeared_cb(self._pservice, None, service)
|
2006-06-15 05:24:11 +02:00
|
|
|
|
2006-06-18 07:05:40 +02:00
|
|
|
def _service_appeared_cb(self, pservice, buddy, service):
|
2006-06-20 03:17:58 +02:00
|
|
|
if service.get_activity_uid() == self._activity.get_id():
|
|
|
|
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()
|
|
|
|
|
2006-06-18 09:00:23 +02:00
|
|
|
def _group_recv_message(self, address, msg):
|
|
|
|
pservice = PresenceService.get_instance()
|
2006-06-18 20:14:59 +02:00
|
|
|
[nick, msg] = self.deserialize_message(msg)
|
|
|
|
buddy = pservice.get_buddy_by_nick_name(nick)
|
2006-06-18 09:00:23 +02:00
|
|
|
if buddy:
|
|
|
|
self.recv_message(buddy, msg)
|
|
|
|
else:
|
2006-06-18 20:14:59 +02:00
|
|
|
logging.error('The buddy %s is not present.' % (nick))
|