当前位置: 首页>>代码示例>>Python>>正文


Python Message.build方法代码示例

本文整理汇总了Python中message.Message.build方法的典型用法代码示例。如果您正苦于以下问题:Python Message.build方法的具体用法?Python Message.build怎么用?Python Message.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在message.Message的用法示例。


在下文中一共展示了Message.build方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _receive_message

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import build [as 别名]
    def _receive_message(self, msg):
        """ Handles a received COAP message.

            The state machine calls this function to process a received a CoAP message.
        """
        coap_log.info('Received CoAP message {0}'.format(str(msg)))

        # Check if any unimplemented option appears in the message.
        # If there is any critical unsupported message we should send a reset message.
        unsupported_options = [opt for opt in msg.coap_option if opt.option_number not in implemented_options]
        unsupported_critical_options = [opt for opt in unsupported_options if opt.option_number & 1]
        if len(unsupported_critical_options) > 0:
            for opt in unsupported_critical_options:
                coap_log.warning('Unsupported critical option {0}'.format(opt.option_number))
            coap_log.warning('Sending RESET for {0}'.format(str(msg)))
            reset_msg = Message(message_id=msg.message_id, message_type=MessageType.reset)
            self._socket.send(reset_msg.build())
            return

        has_observe_option = msg.has_observe_option()

        # If we got reset message in response to a request, then handle it accordingly
        if msg.type == MessageType.reset:
            self._remove_message(msg)
            coap_log.error('RESET handling is not yet implemented')
            return

        # If ACK is received, do the necessary processing for that first.
        if msg.type == MessageType.acknowledgment:
            self._receive_ack(msg)
            return

        assert msg.type in [MessageType.confirmable, MessageType.non_confirmable]

        # At this point this message can be a REQUEST or a separate response or a notify for  resource being observed.
        idx, req_msg = self._find_message(token=msg.token, message_id=None, state=MessageState.wait_for_response)
        if req_msg is None and not has_observe_option:
            # This is a new REQ
            coap_log.error('REQUEST not implemented yet {0}'.format(str(msg)))
            return

        # We got the response as separate message, first send ACK if needed.
        if msg.type == MessageType.confirmable:
            ack_msg = Message(message_id=msg.message_id, message_type=MessageType.acknowledgment, token=msg.token)
            self._socket.send(ack_msg.build())

        #if message has observe option set, the server will send updates, So copy the message into wait_for_updates queue
        if has_observe_option:
            if req_msg is None:
                idx, req_msg = self._find_message(token=msg.token, message_id=None, state=MessageState.wait_for_updates)

            if req_msg is None:
                coap_log.error('OBSERVE message received for but not observing token {0}'.format(msg.token))
                #TODO - send a reset message
                return
            self._receive_observe(req_msg, msg)
            return

        self._receive_response(req_msg, msg)
开发者ID:samueldotj,项目名称:pycoap,代码行数:61,代码来源:coap.py

示例2: _receive_ack

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import build [as 别名]
    def _receive_ack(self, msg):
        """Receives an ACK and transitions message's state based on that.
        """
        self._remove_message(msg)

        idx, parent_msg = self._find_message(token=None, message_id=msg.message_id, state=MessageState.wait_for_ack)
        if parent_msg is None:
            coap_log.warning('ACK received but no matching message found - Sending RESET')
            reset_msg = Message(message_id=msg.message_id, message_type=MessageType.reset)
            self._socket.send(reset_msg.build())
            return
        if parent_msg.type != MessageType.confirmable:
            coap_log.error('ACK received for NON-CONFIRMABLE message - Ignoring')
            return

        self._transition_message(parent_msg, MessageState.wait_for_response)

        if msg.class_code == 0 and msg.class_detail == 0:
            # if this is empty message send just for ACK we are already done.
            coap_log.debug('Separate ACK received')
            return

        #coap_log.debug('Piggybacked RESPONSE {0}'.format(str(msg)))
        if msg.has_observe_option():
            self._receive_observe(parent_msg, msg)
            return

        # This message has a piggybacked response, so receive it.
        self._receive_response(parent_msg, msg)
开发者ID:samueldotj,项目名称:pycoap,代码行数:31,代码来源:coap.py

示例3: stop_observe

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import build [as 别名]
    def stop_observe(self, uri_path):
        """ Stop Observing the specified CoAP url """
        obs_msg = None
        for msg in self.message_queues[MessageState.wait_for_updates]:
            if msg.url == uri_path:
                obs_msg = msg

        if obs_msg is None:
            return False

        coap_log.info('Cancelling observe - {0}'.format(uri_path))

        # send stop request(get without observe option) and wait for it.
        self._request(method_code=MethodCode.get, uri_path=uri_path, confirmable=True, options=None)
        # Also send a RESET message.
        # According to RFC the above message itself if enough but some servers are not respecting it.
        reset_msg = Message(message_id=obs_msg.message_id, message_type=MessageType.reset, token=obs_msg.token)
        self._socket.send(reset_msg.build())
        self._remove_message(obs_msg)

        return True
开发者ID:samueldotj,项目名称:pycoap,代码行数:23,代码来源:coap.py


注:本文中的message.Message.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。