2006-06-21 23:49:37 -04:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from sugar.chat.GroupChat import GroupChat
|
|
|
|
|
|
|
|
|
|
class ActivityChat(GroupChat):
|
|
|
|
|
SERVICE_TYPE = "_olpc_activity_chat._udp"
|
|
|
|
|
|
|
|
|
|
def __init__(self, activity):
|
|
|
|
|
GroupChat.__init__(self)
|
2006-06-22 14:37:34 -04:00
|
|
|
self._chat_service = None
|
|
|
|
|
|
2006-06-21 23:49:37 -04:00
|
|
|
self._activity = activity
|
2006-07-24 10:57:02 -05:00
|
|
|
self._pservice.register_service_type(ActivityChat.SERVICE_TYPE)
|
2006-07-23 09:21:00 -05:00
|
|
|
self._pservice.connect('service-appeared', self._service_appeared_cb)
|
2006-06-22 14:37:34 -04:00
|
|
|
|
|
|
|
|
# Find an existing activity chat to latch onto
|
2006-07-24 11:13:07 -05:00
|
|
|
ps_activity = self._pservice.get_activity(activity.get_id())
|
|
|
|
|
if ps_activity is not None:
|
|
|
|
|
service = ps_activity.get_service_of_type(ActivityChat.SERVICE_TYPE)
|
|
|
|
|
if service is not None:
|
2006-07-25 19:04:15 -05:00
|
|
|
self._service_appeared_cb(self._pservice, service)
|
2006-06-21 23:49:37 -04:00
|
|
|
|
2006-07-25 19:04:15 -05:00
|
|
|
def _service_appeared_cb(self, pservice, service):
|
2006-06-22 14:37:34 -04:00
|
|
|
if service.get_activity_id() != self._activity.get_id():
|
|
|
|
|
return
|
|
|
|
|
if service.get_type() != ActivityChat.SERVICE_TYPE:
|
|
|
|
|
return
|
|
|
|
|
if self._chat_service:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
logging.debug('Activity chat service appeared, setup the stream.')
|
|
|
|
|
# Ok, there's an existing chat service that we copy
|
|
|
|
|
# parameters and such from
|
|
|
|
|
addr = service.get_address()
|
|
|
|
|
port = service.get_port()
|
|
|
|
|
self._chat_service = self._pservice.share_activity(self._activity,
|
|
|
|
|
stype=ActivityChat.SERVICE_TYPE, properties=None,
|
|
|
|
|
address=addr, port=port)
|
|
|
|
|
self._setup_stream(self._chat_service)
|
2006-06-21 23:49:37 -04:00
|
|
|
|
2006-07-25 17:17:05 -05:00
|
|
|
def share(self):
|
|
|
|
|
"""Only called when we share the activity this chat is tied to."""
|
2006-06-22 14:37:34 -04:00
|
|
|
self._chat_service = self._pservice.share_activity(self._activity,
|
|
|
|
|
stype=ActivityChat.SERVICE_TYPE)
|
2006-06-27 12:27:30 -04:00
|
|
|
self._setup_stream(self._chat_service)
|