コード例 #1
0
    def _message_queue_handler(self):
        notification_center = NotificationCenter()
        try:
            while True:
                message = self.message_queue.wait()
                if self.msrp_session is None:
                    if message.notify_progress:
                        data = NotificationData(message_id=message.id,
                                                message=None,
                                                code=0,
                                                reason='Stream ended')
                        notification_center.post_notification(
                            'ChatStreamDidNotDeliverMessage',
                            sender=self,
                            data=data)
                    break

                try:
                    if isinstance(message.content, str):
                        message.content = message.content.encode('utf8')
                        charset = 'utf8'
                    else:
                        charset = None

                    if not isinstance(message, QueuedOTRInternalMessage):
                        try:
                            message.content = self.encryption.otr_session.handle_output(
                                message.content, message.content_type)
                        except OTRError as e:
                            raise ChatStreamError(str(e))

                    message.sender = message.sender or self.local_identity
                    message.recipients = message.recipients or [
                        self.remote_identity
                    ]

                    # check if we MUST use CPIM
                    need_cpim = (message.sender != self.local_identity or
                                 message.recipients != [self.remote_identity]
                                 or message.courtesy_recipients
                                 or message.subject or message.timestamp
                                 or message.required
                                 or message.additional_headers)

                    if need_cpim or not contains_mime_type(
                            self.remote_accept_types, message.content_type):
                        if not contains_mime_type(
                                self.remote_accept_wrapped_types,
                                message.content_type):
                            raise ChatStreamError(
                                'Unsupported content_type for outgoing message: %r'
                                % message.content_type)
                        if not self.cpim_enabled:
                            raise ChatStreamError(
                                'Additional message meta-data cannot be sent, because the CPIM wrapper is not used'
                            )
                        if not self.private_messages_allowed and message.recipients != [
                                self.remote_identity
                        ]:
                            raise ChatStreamError(
                                'The remote end does not support private messages'
                            )
                        if message.timestamp is None:
                            message.timestamp = ISOTimestamp.now()
                        payload = CPIMPayload(charset=charset,
                                              **{
                                                  name: getattr(message, name)
                                                  for name in Message.__slots__
                                              })
                    elif self.prefer_cpim and self.cpim_enabled and contains_mime_type(
                            self.remote_accept_wrapped_types,
                            message.content_type):
                        if message.timestamp is None:
                            message.timestamp = ISOTimestamp.now()
                        payload = CPIMPayload(charset=charset,
                                              **{
                                                  name: getattr(message, name)
                                                  for name in Message.__slots__
                                              })
                    else:
                        payload = SimplePayload(message.content,
                                                message.content_type, charset)
                except ChatStreamError as e:
                    if message.notify_progress:
                        data = NotificationData(message_id=message.id,
                                                message=None,
                                                code=0,
                                                reason=e.args[0])
                        notification_center.post_notification(
                            'ChatStreamDidNotDeliverMessage',
                            sender=self,
                            data=data)
                    continue
                else:
                    content, content_type = payload.encode()

                message_id = message.id
                notify_progress = message.notify_progress
                report = 'yes' if notify_progress else 'no'

                chunk = self.msrp_session.make_message(
                    content, content_type=content_type, message_id=message_id)
                chunk.add_header(FailureReportHeader(report))
                chunk.add_header(SuccessReportHeader(report))

                try:
                    self.msrp_session.send_chunk(
                        chunk,
                        response_cb=partial(self._on_transaction_response,
                                            message_id))
                except Exception as e:
                    if notify_progress:
                        data = NotificationData(message_id=message_id,
                                                message=None,
                                                code=0,
                                                reason=str(e))
                        notification_center.post_notification(
                            'ChatStreamDidNotDeliverMessage',
                            sender=self,
                            data=data)
                except ProcExit:
                    if notify_progress:
                        data = NotificationData(message_id=message_id,
                                                message=None,
                                                code=0,
                                                reason='Stream ended')
                        notification_center.post_notification(
                            'ChatStreamDidNotDeliverMessage',
                            sender=self,
                            data=data)
                    raise
                else:
                    if notify_progress:
                        self.sent_messages.add(message_id)
                        notification_center.post_notification(
                            'ChatStreamDidSendMessage',
                            sender=self,
                            data=NotificationData(message=chunk))
        finally:
            self.message_queue_thread = None
            while self.sent_messages:
                message_id = self.sent_messages.pop()
                data = NotificationData(message_id=message_id,
                                        message=None,
                                        code=0,
                                        reason='Stream ended')
                notification_center.post_notification(
                    'ChatStreamDidNotDeliverMessage', sender=self, data=data)
            message_queue, self.message_queue = self.message_queue, queue()
            while message_queue:
                message = message_queue.wait()
                if message.notify_progress:
                    data = NotificationData(message_id=message.id,
                                            message=None,
                                            code=0,
                                            reason='Stream ended')
                    notification_center.post_notification(
                        'ChatStreamDidNotDeliverMessage',
                        sender=self,
                        data=data)
コード例 #2
0
                        payload = SimplePayload(message.content, message.content_type, charset)
                except ChatStreamError, e:
                    if message.notify_progress:
                        data = NotificationData(message_id=message.id, message=None, code=0, reason=e.args[0])
                        notification_center.post_notification('ChatStreamDidNotDeliverMessage', sender=self, data=data)
                    continue
                else:
                    content, content_type = payload.encode()

                message_id = message.id
                notify_progress = message.notify_progress
                report = 'yes' if notify_progress else 'no'

                chunk = self.msrp_session.make_message(content, content_type=content_type, message_id=message_id)
                chunk.add_header(FailureReportHeader(report))
                chunk.add_header(SuccessReportHeader(report))

                try:
                    self.msrp_session.send_chunk(chunk, response_cb=partial(self._on_transaction_response, message_id))
                except Exception, e:
                    if notify_progress:
                        data = NotificationData(message_id=message_id, message=None, code=0, reason=str(e))
                        notification_center.post_notification('ChatStreamDidNotDeliverMessage', sender=self, data=data)
                except ProcExit:
                    if notify_progress:
                        data = NotificationData(message_id=message_id, message=None, code=0, reason='Stream ended')
                        notification_center.post_notification('ChatStreamDidNotDeliverMessage', sender=self, data=data)
                    raise
                else:
                    if notify_progress:
                        self.sent_messages.add(message_id)