コード例 #1
0
    def _test_modular(self, lst):
        serializer = Serializer()
        for t in lst:
            message, expected = t
            send_ack = False
            if message is not None:
                datagram = serializer.serialize(message)
                self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
            else:
                send_ack = True

            datagram, source = self.tr.written.pop(0)
            host, port = source
            message = serializer.deserialize(datagram, host, port)
            self.assertEqual(message.type, expected.type)
            if not send_ack:
                self.assertEqual(message.mid, expected.mid)
            self.assertEqual(message.code, expected.code)
            self.assertEqual(message.source, source)
            self.assertEqual(message.token, expected.token)
            self.assertEqual(message.payload, expected.payload)
            self.assertEqual(message.options, expected.options)
            if send_ack:
                message = Message.new_ack(message)
                datagram = serializer.serialize(message)
                self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))

        self.tr.written = []
コード例 #2
0
ファイル: plugtest.py プロジェクト: Erguotou/CoAPthon
 def _test_plugtest(self, message_list):  # pragma: no cover
     serializer = Serializer()
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     for message, expected in message_list:
         if message is not None:
             datagram = serializer.serialize(message)
             sock.sendto(datagram, message.destination)
         if expected is not None:
             datagram, source = sock.recvfrom(4096)
             received_message = serializer.deserialize(datagram, source)
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, source)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload, expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options, expected.options)
                 for o in expected.options:
                     option_value = getattr(expected, o.name.lower().replace("-", "_"))
                     option_value_rec = getattr(received_message, o.name.lower().replace("-", "_"))
                     self.assertEqual(option_value, option_value_rec)
     sock.close()
コード例 #3
0
ファイル: coap_protocol.py プロジェクト: johanzander/CoAPthon
    def datagramReceived(self, datagram, host):
        serializer = Serializer()
        host, port = host
        message = serializer.deserialize(datagram, host, port)
        print "Message received from " + host + ":" + str(port)
        print "----------------------------------------"
        print message
        print "----------------------------------------"
        if isinstance(message, Response):
            self.handle_response(message)
        elif isinstance(message, Request):
            log.err("Received request")
        else:
            self.handle_message(message)

        key = hash(str(host) + str(port) + str(message.mid))
        if message.type == defines.inv_types["ACK"] and message.code == defines.inv_codes["EMPTY"] \
           and key in self.sent.keys():
            # Separate Response
            print "Separate Response"
        else:
            function, args, kwargs, client_callback = self.get_operation()
            key = hash(str(host) + str(port) + str(message.token))
            if function is None and len(self.relation) == 0:
                if not self._forward:
                    reactor.stop()
            elif key in self.relation:
                response, timestamp, client_callback = self.relation.get(key)
                self.handle_notification(message, client_callback)
            else:
                function(client_callback, *args, **kwargs)
コード例 #4
0
    def send_datagram(self, message):
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        message = serializer.serialize(message)

        self._socket.sendto(message, (host, port))
コード例 #5
0
ファイル: coap.py プロジェクト: rfjakob/CoAPthon
    def send_datagram(self, message):
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        message = serializer.serialize(message)

        self._socket.sendto(message, (host, port))
コード例 #6
0
ファイル: coap.py プロジェクト: Tanganelli/CoAPthon
    def receive_datagram(self):
        """
        Receive datagram from the UDP socket and invoke the callback function.
        """
        logger.debug("Start receiver Thread")
        while not self.stopped.isSet():
            self._socket.settimeout(0.1)
            try:
                datagram, addr = self._socket.recvfrom(1152)
            except socket.timeout:  # pragma: no cover
                continue
            except Exception as e:  # pragma: no cover
                if self._cb_ignore_read_exception is not None and callable(self._cb_ignore_read_exception):
                    if self._cb_ignore_read_exception(e, self):
                        continue
                return
            else:  # pragma: no cover
                if len(datagram) == 0:
                    logger.debug("Exiting receiver Thread due to orderly shutdown on server end")
                    return

            serializer = Serializer()

            try:
                host, port = addr
            except ValueError:
                host, port, tmp1, tmp2 = addr

            source = (host, port)

            message = serializer.deserialize(datagram, source)

            if isinstance(message, Response):
                logger.debug("receive_datagram - " + str(message))
                transaction, send_ack = self._messageLayer.receive_response(message)
                if transaction is None:  # pragma: no cover
                    continue
                self._wait_for_retransmit_thread(transaction)
                if send_ack:
                    self._send_ack(transaction)
                self._blockLayer.receive_response(transaction)
                if transaction.block_transfer:
                    self._send_block_request(transaction)
                    continue
                elif transaction is None:  # pragma: no cover
                    self._send_rst(transaction)
                    return
                self._observeLayer.receive_response(transaction)
                if transaction.notification:  # pragma: no cover
                    ack = Message()
                    ack.type = defines.Types['ACK']
                    ack = self._messageLayer.send_empty(transaction, transaction.response, ack)
                    self.send_datagram(ack)
                    self._callback(transaction.response)
                else:
                    self._callback(transaction.response)
            elif isinstance(message, Message):
                self._messageLayer.receive_empty(message)

        logger.debug("Exiting receiver Thread due to request")
コード例 #7
0
    def _test_separate(self, message, notification):
        serializer = Serializer()
        datagram = serializer.serialize(message)
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(datagram, self.server_address)

        datagram, source = sock.recvfrom(4096)
        host, port = source
        message = serializer.deserialize(datagram, host, port)

        self.assertEqual(message.type, defines.inv_types["ACK"])
        self.assertEqual(message.code, None)
        self.assertEqual(message.mid, self.current_mid - 1)
        self.assertEqual(message.source, source)

        datagram, source = sock.recvfrom(4096)
        host, port = source
        message = serializer.deserialize(datagram, host, port)

        self.assertEqual(message.type, notification.type)
        self.assertEqual(message.code, notification.code)
        self.assertEqual(message.source, source)
        self.assertEqual(message.token, notification.token)
        self.assertEqual(message.payload, notification.payload)
        self.assertEqual(message.options, notification.options)

        message = Message.new_ack(message)
        datagram = serializer.serialize(message)
        sock.sendto(datagram, self.server_address)
        sock.close()
コード例 #8
0
    def _test_modular(self, lst):
        serializer = Serializer()
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        for t in lst:
            message, expected = t
            send_ack = False
            if message is not None:
                datagram = serializer.serialize(message)
                sock.sendto(datagram, self.server_address)
            else:
                send_ack = True

            datagram, source = sock.recvfrom(4096)
            host, port = source
            message = serializer.deserialize(datagram, host, port)
            self.assertEqual(message.type, expected.type)
            if not send_ack:
                self.assertEqual(message.mid, expected.mid)
            self.assertEqual(message.code, expected.code)
            self.assertEqual(message.source, source)
            self.assertEqual(message.token, expected.token)
            self.assertEqual(message.payload, expected.payload)
            self.assertEqual(message.options, expected.options)
            if send_ack:
                message = Message.new_ack(message)
                datagram = serializer.serialize(message)
                sock.sendto(datagram, self.server_address)

        sock.close()
コード例 #9
0
    def _test_separate(self, message, notification):
        serializer = Serializer()
        datagram = serializer.serialize(message)
        self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))

        datagram, source = self.tr.written[0]
        host, port = source
        message = serializer.deserialize(datagram, host, port)

        self.assertEqual(message.type, defines.inv_types["ACK"])
        self.assertEqual(message.code, None)
        self.assertEqual(message.mid, self.current_mid + 4)
        self.assertEqual(message.source, source)

        datagram, source = self.tr.written[1]
        host, port = source
        message = serializer.deserialize(datagram, host, port)

        self.assertEqual(message.type, notification.type)
        self.assertEqual(message.code, notification.code)
        self.assertEqual(message.source, source)
        self.assertEqual(message.token, notification.token)
        self.assertEqual(message.payload, notification.payload)
        self.assertEqual(message.options, notification.options)

        self.tr.written = []

        message = Message.new_ack(message)
        datagram = serializer.serialize(message)
        self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
        self.tr.written = []
コード例 #10
0
    def send(self, request, endpoint, resend=False):

        self._endpoint = endpoint
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._receiver_thread = threading.Thread(target=self.datagram_received)
        self._receiver_thread.start()
        if not resend:
            if request.mid is None:
                request.mid = self._currentMID
                self._currentMID += 1
            key = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(request.mid))
            key_token = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(request.token))
            self.sent[key] = (request, time.time())
            self.sent[key_token] = request
        if request.type is None:
            request.type = defines.inv_types["CON"]
        serializer = Serializer()
        request.destination = self._endpoint
        host, port = request.destination
        print "Message sent to " + host + ":" + str(port)
        print "----------------------------------------"
        print request
        print "----------------------------------------"
        datagram = serializer.serialize(request)
        log.msg("Send datagram")
        self._socket.sendto(datagram, self._endpoint)
コード例 #11
0
ファイル: coap.py プロジェクト: rgerganov/CoAPthon3
    def send_datagram(self, message):
        """
        Send a message over the UDP socket.

        :param message: the message to send
        """
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        raw_message = serializer.serialize(message)

        try:
            self._socket.sendto(raw_message, (host, port))
        except Exception as e:
            if self._cb_ignore_write_exception is not None and isinstance(
                    self._cb_ignore_write_exception, collections.Callable):
                if not self._cb_ignore_write_exception(e, self):
                    raise

        # if you're explicitly setting that you don't want a response, don't wait for it
        # https://tools.ietf.org/html/rfc7967#section-2.1
        for opt in message.options:
            if opt.number == defines.OptionRegistry.NO_RESPONSE.number:
                if opt.value == 26:
                    return

        if self._receiver_thread is None or not self._receiver_thread.is_alive(
        ):
            self._receiver_thread = threading.Thread(
                target=self.receive_datagram)
            self._receiver_thread.daemon = True
            self._receiver_thread.start()
コード例 #12
0
ファイル: coap.py プロジェクト: zhsomin/CoAPthon3
    def receive_datagram(self):
        """
        Receive datagram from the UDP socket and invoke the callback function.
        """
        logger.debug("Start receiver Thread")
        while not self.stopped.isSet():
            self._socket.settimeout(0.1)
            try:
                datagram, addr = self._socket.recvfrom(1500)
            except socket.timeout:  # pragma: no cover
                continue
            except Exception as e:  # pragma: no cover
                if self._cb_ignore_read_exception is not None and isinstance(self._cb_ignore_read_exception, collections.Callable):
                    if self._cb_ignore_read_exception(e, self):
                        continue
                return
            else:  # pragma: no cover
                if len(datagram) == 0:
                    logger.debug("Exiting receiver Thread due to orderly shutdown on server end")
                    return

            serializer = Serializer()

            try:
                host, port = addr
            except ValueError:
                host, port, tmp1, tmp2 = addr

            source = (host, port)

            message = serializer.deserialize(datagram, source)

            if isinstance(message, Response):
                logger.debug("receive_datagram - " + str(message))
                transaction, send_ack = self._messageLayer.receive_response(message)
                if transaction is None:  # pragma: no cover
                    continue
                self._wait_for_retransmit_thread(transaction)
                if send_ack:
                    self._send_ack(transaction)
                self._blockLayer.receive_response(transaction)
                if transaction.block_transfer:
                    self._send_block_request(transaction)
                    continue
                elif transaction is None:  # pragma: no cover
                    self._send_rst(transaction)
                    return
                self._observeLayer.receive_response(transaction)
                if transaction.notification:  # pragma: no cover
                    ack = Message()
                    ack.type = defines.Types['ACK']
                    ack = self._messageLayer.send_empty(transaction, transaction.response, ack)
                    self.send_datagram(ack)
                    self._callback(transaction.response)
                else:
                    self._callback(transaction.response)
            elif isinstance(message, Message):
                self._messageLayer.receive_empty(message)

        logger.debug("Exiting receiver Thread due to request")
コード例 #13
0
ファイル: plugtest.py プロジェクト: Cereal84/CoAPthon
    def _test_plugtest(self, message, expected):
        serializer = Serializer()
        datagram = serializer.serialize(message)
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(datagram, message.destination)

        datagram, source = sock.recvfrom(4096)
        host, port = source
        message = serializer.deserialize(datagram, host, port)
        if expected.type is not None:
            self.assertEqual(message.type, expected.type)
        if expected.mid is not None:
            self.assertEqual(message.mid, expected.mid)

        self.assertEqual(message.code, expected.code)
        if expected.source is not None:
            self.assertEqual(message.source, source)
        if expected.token is not None:
            self.assertEqual(message.token, expected.token)
        if expected.payload is not None:
            self.assertEqual(message.payload, expected.payload)
        if expected.options is not None:
            self.assertEqual(message.options, expected.options)

        sock.close()
コード例 #14
0
ファイル: tests.py プロジェクト: thejdeep/Testing
    def _test_plugtest(self, message_list):
        serializer = Serializer()
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        for message, expected in message_list:
            if message is not None:
                datagram = serializer.serialize(message)
                sock.sendto(datagram, message.destination)
            if expected is not None:
                datagram, source = sock.recvfrom(4096)
                received_message = serializer.deserialize(datagram, source)
                if expected.type is not None:
                    self.assertEqual(received_message.type, expected.type)
                if expected.mid is not None:
                    self.assertEqual(received_message.mid, expected.mid)
                self.assertEqual(received_message.code, expected.code)
                if expected.source is not None:
                    self.assertEqual(received_message.source, source)
                if expected.token is not None:
                    self.assertEqual(received_message.token, expected.token)
                if expected.payload is not None:
                    self.assertEqual(received_message.payload,
                                     expected.payload)
                if expected.options is not None:
                    self.assertEqual(received_message.options,
                                     expected.options)

        sock.close()
コード例 #15
0
 def _test_plugtest(self, message_list):  # pragma: no cover
     serializer = Serializer()
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     for message, expected in message_list:
         if message is not None:
             datagram = serializer.serialize(message)
             sock.sendto(datagram, message.destination)
         if expected is not None:
             datagram, source = sock.recvfrom(4096)
             received_message = serializer.deserialize(datagram, source)
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, source)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.payload is not None:
                 self.assertEqual(received_message.payload,
                                  expected.payload)
             if expected.options is not None:
                 self.assertEqual(received_message.options,
                                  expected.options)
                 for o in expected.options:
                     option_value = getattr(
                         expected,
                         o.name.lower().replace("-", "_"))
                     option_value_rec = getattr(
                         received_message,
                         o.name.lower().replace("-", "_"))
                     self.assertEqual(option_value, option_value_rec)
     sock.close()
コード例 #16
0
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")
            rst = Message()
            rst.destination = client_address
            rst.type = Types["RST"]
            rst.code = message
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))

        if isinstance(message, Request):
            if not message.proxy_uri or message.uri_path != "coap2http":
                logger.error("receive_datagram - BAD REQUEST")
                rst = Message()
                rst.destination = client_address
                rst.type = Types["RST"]
                rst.code = Codes.BAD_REQUEST.number
                rst.mid = message.mid
                rst.token = message.token
                self.send_datagram(rst)
                return
            # Execute HTTP/HTTPS request
            http_response = CoAP_HTTP.execute_http_request(
                message.code, message.proxy_uri, message.payload)
            # HTTP response to CoAP response conversion
            coap_response = CoAP_HTTP.to_coap_response(http_response,
                                                       message.code,
                                                       client_address,
                                                       message.mid,
                                                       message.token)
            # Send datagram and return
            self.send_datagram(coap_response)
            return

        elif isinstance(message, Message):
            logger.error("Received message from %s", message.source)

        else:  # is Response
            logger.error("Received response from %s", message.source)
コード例 #17
0
ファイル: coap.py プロジェクト: LinuxIoT/IoT-Gateway
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer._current_mid
                    self._messageLayer._current_mid += 1 % 65535
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug("message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        return
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug("message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        return
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request, args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(message, transaction)
                            self._observeLayer.receive_empty(message, transaction)

            except RuntimeError:
                print "Exception with Executor"
        self._socket.close()
コード例 #18
0
    def receive_datagram(self):
        """
        Receive datagram from the UDP socket and invoke the callback function.
        """
        logger.debug("Start receiver Thread")
        while not self.stopped.isSet():
            self._socket.settimeout(1)
            try:
                datagram, addr = self._socket.recvfrom(1152)
            except socket.timeout:  # pragma: no cover
                continue
            except socket.error:  # pragma: no cover
                return
            else:  # pragma: no cover
                if len(datagram) == 0:
                    logger.debug("orderly shutdown on server end")
                    return

            serializer = Serializer()

            try:
                host, port = addr
            except ValueError:
                host, port, tmp1, tmp2 = addr

            source = (host, port)

            message = serializer.deserialize(datagram, source)

            if isinstance(message, Response):
                transaction, send_ack = self._messageLayer.receive_response(
                    message)
                if transaction is None:  # pragma: no cover
                    continue
                if send_ack:
                    self._send_ack(transaction)
                self._blockLayer.receive_response(transaction)
                if transaction.block_transfer:
                    transaction = self._messageLayer.send_request(
                        transaction.request)
                    self.send_datagram(transaction.request)
                    continue
                elif transaction is None:  # pragma: no cover
                    self._send_rst(transaction)
                    return
                self._observeLayer.receive_response(transaction)
                if transaction.notification:  # pragma: no cover
                    ack = Message()
                    ack.type = defines.Types['ACK']
                    ack = self._messageLayer.send_empty(
                        transaction, transaction.response, ack)
                    self.send_datagram(ack)
                    self._callback(transaction.response)
                else:
                    self._callback(transaction.response)
            elif isinstance(message, Message):
                self._messageLayer.receive_empty(message)
コード例 #19
0
ファイル: sensor-coap.py プロジェクト: zyp-rgb/NGINX-Demos
def coap_encode(payload):
    message = Message()
    message.type = defines.Types['CON']
    message.token = 4321
    message.mid = 2
    message.options = None
    message.payload = str(payload)
    serializer = Serializer()
    messagestring = serializer.serialize(message)
    return messagestring
コード例 #20
0
ファイル: sensor-coap.py プロジェクト: Omosofe/NGINX-Demos
def coap_encode(payload):
    message = Message()
    message.type = defines.Types['CON']
    message.token = 4321
    message.mid = 2
    message.options = None
    message.payload = str(payload)
    serializer = Serializer()
    messagestring = serializer.serialize(message)
    return messagestring
コード例 #21
0
ファイル: tests.py プロジェクト: Tanganelli/CoAPthon
    def test_etag_deserialize(self):
        req = Request()
        req.type = defines.Types["CON"]
        req._mid = 1
        req.etag = bytearray([0xC5])

        serializer = Serializer()
        serialized = serializer.serialize(req)
        received_message = serializer.deserialize(serialized, ("127.0.0.1", 5683))

        self.assertEqual(req.etag, received_message.etag)
コード例 #22
0
ファイル: tests.py プロジェクト: Python3pkg/CoAPthon
    def test_etag_deserialize(self):
        req = Request()
        req.type = defines.Types["CON"]
        req._mid = 1
        req.etag = bytearray([0xc5])

        serializer = Serializer()
        serialized = serializer.serialize(req)
        received_message = serializer.deserialize(serialized, ("127.0.0.1", 5683))

        self.assertEqual(req.etag, received_message.etag)
コード例 #23
0
ファイル: coap_protocol.py プロジェクト: johanzander/CoAPthon
 def send(self, message):
     serializer = Serializer()
     message.destination = self.server
     host, port = message.destination
     print "Message sent to " + host + ":" + str(port)
     print "----------------------------------------"
     print message
     print "----------------------------------------"
     datagram = serializer.serialize(message)
     log.msg("Send datagram")
     self.transport.write(datagram, self.server)
コード例 #24
0
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")
            rst = Message()
            rst.destination = client_address
            rst.type = Types["RST"]
            rst.code = message
            rst.mid = message.mid
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))

        if isinstance(message, Request):
            if not message.proxy_uri or message.uri_path != "coap2http":
                logger.error("receive_datagram - BAD REQUEST")
                rst = Message()
                rst.destination = client_address
                rst.type = Types["RST"]
                rst.code = Codes.BAD_REQUEST.number
                rst.mid = message.mid
                self.send_datagram(rst)
                return
            # Execute HTTP/HTTPS request
            http_response = CoAP_HTTP.execute_http_request(message.code, message.proxy_uri, message.payload)
            # HTTP response to CoAP response conversion
            coap_response = CoAP_HTTP.to_coap_response(http_response, message.code, client_address, message.mid)
            # Send datagram and return
            self.send_datagram(coap_response)
            return

        elif isinstance(message, Message):
            logger.error("Received message from %s", message.source)

        else:  # is Response
            logger.error("Received response from %s", message.source)
コード例 #25
0
ファイル: xbee_coap.py プロジェクト: mhagdorn/CoAPXBee
    def receive_datagram(self):
        """
        Receive datagram from the UDP socket and invoke the callback function.
        """

        logger.debug("Start receiver Thread")
        while not self.stopped.isSet():
            try:
                xbee_message = self.xbee.read_data(timeout=0.1)
            except TimeoutException:
                continue
            except Exception as e:
                if self._cb_ignore_read_exception is not None and isinstance(
                        self._cb_ignore_read_exception, collections.Callable):
                    if self._cb_ignore_read_exception(e, self):
                        continue
                return

            serializer = Serializer()
            message = serializer.deserialize(bytes(xbee_message.data),
                                             self._server)

            if isinstance(message, Response):
                logger.debug("receive_datagram - " + str(message))
                transaction, send_ack = self._messageLayer.receive_response(
                    message)
                if transaction is None:  # pragma: no cover
                    continue
                self._wait_for_retransmit_thread(transaction)
                if send_ack:
                    self._send_ack(transaction)
                self._blockLayer.receive_response(transaction)
                if transaction.block_transfer:
                    self._send_block_request(transaction)
                    continue
                elif transaction is None:  # pragma: no cover
                    self._send_rst(transaction)
                    return
                self._observeLayer.receive_response(transaction)
                if transaction.notification:  # pragma: no cover
                    ack = Message()
                    ack.type = defines.Types['ACK']
                    ack = self._messageLayer.send_empty(
                        transaction, transaction.response, ack)
                    self.send_datagram(ack)
                    self._callback(transaction.response)
                else:
                    self._callback(transaction.response)
            elif isinstance(message, Message):
                self._messageLayer.receive_empty(message)

        logger.debug("Exiting receiver Thread due to request")
コード例 #26
0
ファイル: coap.py プロジェクト: luigiDB/CoAPthon
    def send_datagram(self, message):
        """

        :type message: Message
        :param message:
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.debug("send_datagram - " + str(message))
            serializer = Serializer()
            message = serializer.serialize(message)

            self._socket.sendto(message, (host, port))
コード例 #27
0
ファイル: coap.py プロジェクト: samken600/3YP
    def send_datagram(self, message):
        """
        Send a message through the udp socket.

        :type message: Message
        :param message: the message to send
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.info("send_datagram - " + str(message))
            serializer = Serializer()
            message = serializer.serialize(message)
            self._socket.sendto(message, (host, port))
コード例 #28
0
ファイル: coap.py プロジェクト: LinuxIoT/IoT-Gateway
    def send_datagram(self, message):
        """

        :type message: Message
        :param message:
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.debug("send_datagram - " + str(message))
            serializer = Serializer()
            message = serializer.serialize(message)

            self._socket.sendto(message, (host, port))
コード例 #29
0
ファイル: coap.py プロジェクト: rfjakob/CoAPthon
    def receive_datagram(self):
        logger.debug("Start receiver Thread")
        while not self.stopped.isSet():
            self._socket.settimeout(1)
            try:
                datagram, addr = self._socket.recvfrom(1152)
            except socket.timeout:  # pragma: no cover
                continue
            except socket.error:  # pragma: no cover
                return
            else:  # pragma: no cover
                if len(datagram) == 0:
                    print 'orderly shutdown on server end'
                    return

            serializer = Serializer()

            try:
                host, port = addr
            except ValueError:
                host, port, tmp1, tmp2 = addr

            source = (host, port)

            message = serializer.deserialize(datagram, source)

            if isinstance(message, Response):
                transaction, send_ack = self._messageLayer.receive_response(message)
                if transaction is None:  # pragma: no cover
                    continue
                if send_ack:
                    self._send_ack(transaction)
                self._blockLayer.receive_response(transaction)
                if transaction.block_transfer:
                    transaction = self._messageLayer.send_request(transaction.request)
                    self.send_datagram(transaction.request)
                    continue
                elif transaction is None:  # pragma: no cover
                    self._send_rst(transaction)
                    return
                self._observeLayer.receive_response(transaction)
                if transaction.notification:  # pragma: no cover
                    ack = Message()
                    ack.type = defines.Types['ACK']
                    ack = self._messageLayer.send_empty(transaction, transaction.response, ack)
                    self.send_datagram(ack)
                    self._callback(transaction.response)
                else:
                    self._callback(transaction.response)
            elif isinstance(message, Message):
                self._messageLayer.receive_empty(message)
コード例 #30
0
ファイル: coap_protocol.py プロジェクト: johanzander/CoAPthon
    def send(self, message, host, port):
        """
        Send the message

        :param message: the message to send
        :param host: destination host
        :param port: destination port
        """
        print "Message send to " + host + ":" + str(port)
        print "----------------------------------------"
        print message
        print "----------------------------------------"
        serializer = Serializer()
        message = serializer.serialize(message)
        self.transport.write(message, (host, port))
コード例 #31
0
    def send_datagram(self, message):
        """
        Send a message over the UDP socket.

        :param message: the message to send
        """
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        message = serializer.serialize(message)

        self._socket.sendto(message, (host, port))

        if not self._receiver_thread.isAlive():
            self._receiver_thread.start()
コード例 #32
0
    def datagramReceived(self, data, addr):
        """
        Handler for received UDP datagram.

        :param data: the UDP datagram
        :param host: source host
        :param port: source port
        """
        try:
            host, port = addr
        except ValueError:
            host, port, tmp1, tmp2 = addr
        log.msg("Datagram received from " + str(host) + ":" + str(port))
        serializer = Serializer()
        message = serializer.deserialize(data, host, port)
        print "Message received from " + host + ":" + str(port)
        print "----------------------------------------"
        print message
        print "----------------------------------------"
        if isinstance(message, Request):
            log.msg("Received request")
            ret = self.request_layer.handle_request(message)
            if isinstance(ret, Request):
                response = self.request_layer.process(ret)
            else:
                response = ret
            self.schedule_retrasmission(message, response, None)
            log.msg("Send Response")
            self.send(response, host, port)
        elif isinstance(message, Response):
            log.err("Received response")
            rst = Message.new_rst(message)
            rst = self.message_layer.matcher_response(rst)
            log.msg("Send RST")
            self.send(rst, host, port)
        elif isinstance(message, tuple):
            message, error = message
            response = Response()
            response.destination = (host, port)
            response.code = defines.responses[error]
            response = self.reliability_response(message, response)
            response = self.message_layer.matcher_response(response)
            log.msg("Send Error")
            self.send(response, host, port)
        elif message is not None:
            # ACK or RST
            log.msg("Received ACK or RST")
            self.message_layer.handle_message(message)
コード例 #33
0
ファイル: coap_protocol.py プロジェクト: Cereal84/CoAPthon
    def send(self, message, host, port):
        """
        Send the message

        :param message: the message to send
        :param host: destination host
        :param port: destination port
        """
        # print "Message send to " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"
        serializer = Serializer()
        message = serializer.serialize(message)

        self._socket.sendto(message, (host, port))
コード例 #34
0
ファイル: coap.py プロジェクト: Tanganelli/CoAPthon
    def send_datagram(self, message):
        """
        Send a message through the udp socket.

        :type message: Message
        :param message: the message to send
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.debug("send_datagram - " + str(message))
            serializer = Serializer()
            message = serializer.serialize(message)
            if self.multicast:
                self._unicast_socket.sendto(message, (host, port))
            else:
                self._socket.sendto(message, (host, port))
コード例 #35
0
    def _test(self, message, expected):
        serializer = Serializer()
        datagram = serializer.serialize(message)
        self.proto.datagramReceived(datagram, ("127.0.0.1", 5600))
        datagram, source = self.tr.written[-1]
        host, port = source
        message = serializer.deserialize(datagram, host, port)
        self.assertEqual(message.type, expected.type)
        self.assertEqual(message.mid, expected.mid)
        self.assertEqual(message.code, expected.code)
        self.assertEqual(message.source, source)
        self.assertEqual(message.token, expected.token)
        self.assertEqual(message.payload, expected.payload)
        self.assertEqual(message.options, expected.options)

        self.tr.written = []
コード例 #36
0
ファイル: coap_protocol.py プロジェクト: Cereal84/CoAPthon
    def finish_request(self, args):
        """
        Handler for received UDP datagram.

        :param args: (data, (client_ip, client_port)
        """
        data, client_address = args
        host = client_address[0]
        port = client_address[1]

        # logging.log(logging.INFO, "Datagram received from " + str(host) + ":" + str(port))
        serializer = Serializer()
        message = serializer.deserialize(data, host, port)
        # print "Message received from " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"
        if isinstance(message, Request):
            # log.msg("Received request")
            ret = self.request_layer.handle_request(message)
            if isinstance(ret, Request):
                response = self.request_layer.process(ret)
            else:
                response = ret
            self.schedule_retrasmission(message, response, None)
            # log.msg("Send Response")
            return response, host, port
        elif isinstance(message, Response):
            # log.err("Received response")
            rst = Message.new_rst(message)
            rst = self.message_layer.matcher_response(rst)
            # log.msg("Send RST")
            return rst, host, port
        elif isinstance(message, tuple):
            message, error = message
            response = Response()
            response.destination = (host, port)
            response.code = defines.responses[error]
            response = self.message_layer.reliability_response(message, response)
            response = self.message_layer.matcher_response(response)
            # log.msg("Send Error")
            return response, host, port
        elif message is not None:
            # ACK or RST
            # log.msg("Received ACK or RST")
            self.message_layer.handle_message(message)
            return None
コード例 #37
0
ファイル: observe.py プロジェクト: DavideFoti/CoAPthon
    def send_notification(self, t):
        """
        Sends a notification message.

        :param t: (the resource, request, the notification message)
        """
        assert isinstance(t, tuple)
        resource, request, notification_message = t
        host, port = notification_message.destination
        serializer = Serializer()
        self._parent.schedule_retrasmission(request, notification_message, resource)
        print "Notification Message send to " + host + ":" + str(port)
        print "----------------------------------------"
        print notification_message
        print "----------------------------------------"
        notification_message = serializer.serialize(notification_message)
        self._parent.transport.write(notification_message, (host, port))
コード例 #38
0
ファイル: coap.py プロジェクト: federicorossifr/CoAPthon
    def send_datagram(self, message):
        """
        Send a message through the udp socket.

        :type message: Message
        :param message: the message to send
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.debug("send_datagram - " + str(message))
            serializer = Serializer()
            if (message.mid is None):
                message.mid = self._messageLayer.fetch_mid()
            message = serializer.serialize(message)
            if self.multicast:
                self._unicast_socket.sendto(message, (host, port))
            else:
                self._socket.sendto(message, (host, port))
コード例 #39
0
 def _test_check(self, message_list, timeout=0):
     serializer = Serializer()
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     for message, expected in message_list:
         if message is not None:
             datagram = serializer.serialize(message)
             sleep(timeout)
             sock.sendto(datagram, message.destination)
         if expected is not None:
             datagram, source = sock.recvfrom(4096)
             received_message = serializer.deserialize(datagram, source)
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, source)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.content_type is not None:
                 self.assertEqual(received_message.content_type,
                                  expected.content_type)
             if expected.payload is not None:
                 expected_list = self.parse_core_link_format(
                     expected.payload)
                 received_list = self.parse_core_link_format(
                     received_message.payload)
                 all_list = []
                 for expected_elem in expected_list:
                     for received_elem in received_list:
                         if expected_elem['path'] == received_elem['path']:
                             all_list_elem = (expected_elem, received_elem)
                             all_list.append(all_list_elem)
                 for data in all_list:
                     for k in data[1]:
                         self.assertIn(k, data[0])
                         if (k != "lt") and (k in data[0]):
                             self.assertEqual(data[0][k], data[1][k])
             else:
                 self.assertEqual(expected.payload,
                                  received_message.payload)
     sock.close()
コード例 #40
0
 def _test_check(self, message_list, timeout=0):
     serializer = Serializer()
     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     for message, expected in message_list:
         if message is not None:
             datagram = serializer.serialize(message)
             sleep(timeout)
             sock.sendto(datagram, message.destination)
         if expected is not None:
             datagram, source = sock.recvfrom(4096)
             received_message = serializer.deserialize(datagram, source)
             if expected.type is not None:
                 self.assertEqual(received_message.type, expected.type)
             if expected.mid is not None:
                 self.assertEqual(received_message.mid, expected.mid)
             self.assertEqual(received_message.code, expected.code)
             if expected.source is not None:
                 self.assertEqual(received_message.source, source)
             if expected.token is not None:
                 self.assertEqual(received_message.token, expected.token)
             if expected.content_type is not None:
                 self.assertEqual(received_message.content_type, expected.content_type)
             if expected.payload is not None:
                 expected_list = self.parse_core_link_format(expected.payload)
                 received_list = self.parse_core_link_format(received_message.payload)
                 self.assertEqual(len(expected_list), len(received_list))
                 all_list = []
                 for expected_elem in expected_list:
                     for received_elem in received_list:
                         if expected_elem['path'] == received_elem['path']:
                             all_list_elem = (expected_elem, received_elem)
                             all_list.append(all_list_elem)
                 self.assertEqual(len(expected_list), len(all_list))
                 for data in all_list:
                     for k in data[1]:
                         self.assertIn(k, data[0])
                         if (k != "lt") and (k in data[0]):
                             self.assertEqual(data[0][k], data[1][k])
             else:
                 self.assertEqual(expected.payload, received_message.payload)
     sock.close()
コード例 #41
0
ファイル: coap.py プロジェクト: tadodotcom/CoAPthon
    def send_datagram(self, message):
        """
        Send a message through the udp socket.

        :type message: Message
        :param message: the message to send
        """
        if not self.stopped.isSet():
            host, port = message.destination
            logger.debug("send_datagram - " + str(message))
            serializer = Serializer()
            try:
                message = serializer.serialize(message)
            except Exception as e:
                logger.error("Exception received in send_datagram")
                logger.exception(traceback.format_exc())

            if self.multicast:
                self._unicast_socket.sendto(message, (host, port))
            else:
                self._socket.sendto(message, (host, port))
コード例 #42
0
ファイル: coap.py プロジェクト: Tanganelli/CoAPthon
    def send_datagram(self, message):
        """
        Send a message over the UDP socket.

        :param message: the message to send
        """
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        raw_message = serializer.serialize(message)

        try:
            self._socket.sendto(raw_message, (host, port))
        except Exception as e:
            if self._cb_ignore_write_exception is not None and callable(self._cb_ignore_write_exception):
                if not self._cb_ignore_write_exception(e, self):
                    raise

        if self._receiver_thread is None or not self._receiver_thread.isAlive():
            self._receiver_thread = threading.Thread(target=self.receive_datagram)
            self._receiver_thread.start()
コード例 #43
0
    def _test(self, message, expected):
        serializer = Serializer()
        datagram = serializer.serialize(message)
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(datagram, message.destination)
        host, port = message.destination
        # print "Message sent to " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"

        datagram, source = sock.recvfrom(4096)
        host, port = source
        message = serializer.deserialize(datagram, host, port)
        self.assertEqual(message.type, expected.type)
        self.assertEqual(message.mid, expected.mid)
        self.assertEqual(message.code, expected.code)
        self.assertEqual(message.source, source)
        self.assertEqual(message.token, expected.token)
        self.assertEqual(message.payload, expected.payload)
        self.assertEqual(message.options, expected.options)
コード例 #44
0
ファイル: coap.py プロジェクト: wrauner/afl-fuzzing-training
    def send_datagram(self, message):
        """
        Send a message over the UDP socket.

        :param message: the message to send
        """
        host, port = message.destination
        logger.debug("send_datagram - " + str(message))
        serializer = Serializer()
        raw_message = serializer.serialize(message)

        try:
            self._socket.sendto(raw_message, (host, port))
        except Exception as e:
            if self._cb_ignore_write_exception is not None and callable(self._cb_ignore_write_exception):
                if not self._cb_ignore_write_exception(e, self):
                    raise

        if self._receiver_thread is None or not self._receiver_thread.isAlive():
            self._receiver_thread = threading.Thread(target=self.receive_datagram)
            self._receiver_thread.start()
コード例 #45
0
    def finish_request(self, request, client_address):
       
        
        host = client_address[0]
        port = client_address[1]
        data = request[0]
        self.socket = request[1]
 
        # log.msg("Datagram received from " + str(host) + ":" + str(port))
        serializer = Serializer()
        message = serializer.deserialize(data, host, port)
        # print "Message received from " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"
        if isinstance(message, Request):
            # log.msg("Received request")
            ret = self.request_layer.handle_request(message)
            if isinstance(ret, Request):
                self.forward_request(ret)
        elif isinstance(message, Response):
            # log.err("Received response")
            rst = Message.new_rst(message)
            rst = self.message_layer.matcher_response(rst)
            # log.msg("Send RST")
            self.send(rst, host, port)
        elif isinstance(message, tuple):
            message, error = message
            response = Response()
            response.destination = (host, port)
            response.code = defines.responses[error]
            response = self.message_layer.reliability_response(message, response)
            response = self.message_layer.matcher_response(response)
            # log.msg("Send Error")
            self.send(response, host, port)
        elif message is not None:
            # ACK or RST
            # log.msg("Received ACK or RST")
            self.message_layer.handle_message(message)
コード例 #46
0
ファイル: coap.py プロジェクト: tadodotcom/CoAPthon
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            except Exception as e:
                if self._cb_ignore_listen_exception is not None and callable(self._cb_ignore_listen_exception):
                    if self._cb_ignore_listen_exception(e, self):
                        continue
                raise
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer.fetch_mid()
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug("message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug("message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request, args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(message, transaction)
                            self._observeLayer.receive_empty(message, transaction)
            except Exception as e:
                logger.error("Exception received in listen")
                logger.exception(traceback.format_exc())
                continue
            except RuntimeError:
                logger.exception("Exception with Executor")
        try:
            # Python does not close the OS FD on socket.close()
            # Ensure OS socket is closed with shutdown to prevent FD leak
            self._socket.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
        self._socket.close()
コード例 #47
0
ファイル: coap.py プロジェクト: luigiDB/CoAPthon
    def receive_datagram(self, args):
        """
        Receive datagram from the udp socket.

        :rtype : Message
        """
        data, client_address = args

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")

            rst = Message()
            rst.destination = client_address
            rst.type = defines.Types["RST"]
            rst.code = message
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))
        if isinstance(message, Request):

            transaction = self._messageLayer.receive_request(message)

            if transaction.request.duplicated and transaction.completed:
                logger.debug("message duplicated,transaction completed")
                transaction = self._observeLayer.send_response(transaction)
                transaction = self._blockLayer.send_response(transaction)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return
            elif transaction.request.duplicated and not transaction.completed:
                logger.debug("message duplicated,transaction NOT completed")
                self._send_ack(transaction)
                return

            transaction.separate_timer = self._start_separate_timer(
                transaction)

            transaction = self._blockLayer.receive_request(transaction)

            if transaction.block_transfer:
                self._stop_separate_timer(transaction.separate_timer)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return

            transaction = self._observeLayer.receive_request(transaction)

            transaction = self._forwardLayer.receive_request_reverse(
                transaction)

            transaction = self._observeLayer.send_response(transaction)

            transaction = self._blockLayer.send_response(transaction)

            self._stop_separate_timer(transaction.separate_timer)

            transaction = self._messageLayer.send_response(transaction)

            if transaction.response is not None:
                if transaction.response.type == defines.Types["CON"]:
                    self._start_retrasmission(transaction,
                                              transaction.response)
                self.send_datagram(transaction.response)

        elif isinstance(message, Message):
            transaction = self._messageLayer.receive_empty(message)
            if transaction is not None:
                transaction = self._blockLayer.receive_empty(
                    message, transaction)
                self._observeLayer.receive_empty(message, transaction)

        else:  # pragma: no cover
            logger.error("Received response from %s", message.source)
コード例 #48
0
def main():  # pragma: no cover
    ip = "192.168.1.21"
    port = defines.COAP_DEFAULT_PORT
    multicast = False
    server = CoAPServer(ip, port, multicast)
    server.notify('/basic')
    try:
        # server.listen()
        while not server.stopped.isSet():
            data, client_address = server._socket.recvfrom(4096)
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    print("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = server._messageLayer.fetch_mid()
                    server.send_datagram(rst)
                    continue

                # print("receive_datagram " + str(message.payload))
                database().insert_value('CoAP', datetime.datetime.now(),
                                        str(message.payload))

                if isinstance(message, Request):
                    transaction = server._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        print("message duplicated, transaction completed")
                        if transaction.response is not None:
                            server.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        print("message duplicated, transaction NOT completed")
                        server._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=server.receive_request,
                                         args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    print("Received response from %s", message.source)

                else:  # is Message
                    transaction = server._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            server._blockLayer.receive_empty(
                                message, transaction)
                            server._observeLayer.receive_empty(
                                message, transaction)

            except RuntimeError:
                print("Exception with Executor")
        server._socket.close()
    except KeyboardInterrupt:
        print("Server Shutdown")
        server.close()
        print("Exiting...")
コード例 #49
0
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")

            rst = Message()
            rst.destination = client_address
            rst.type = defines.Types["RST"]
            rst.code = message
            self.send_datagram(rst)
            return
        logger.info("receive_datagram - " + str(message))
        if isinstance(message, Request):

            transaction = self._messageLayer.receive_request(message)

            if transaction.request.duplicated and transaction.completed:
                logger.debug("message duplicated, transaction completed")
                transaction = self._observeLayer.send_response(transaction)
                transaction = self._blockLayer.send_response(transaction)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return
            elif transaction.request.duplicated and not transaction.completed:
                logger.debug("message duplicated, transaction NOT completed")
                self._send_ack(transaction)
                return

            transaction.separate_timer = self._start_separate_timer(
                transaction)

            transaction = self._blockLayer.receive_request(transaction)

            if transaction.block_transfer:
                self._stop_separate_timer(transaction.separate_timer)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return

            transaction = self._observeLayer.receive_request(transaction)
            """
            call to the cache layer to check if there's a cached response for the request
            if not, call the forward layer
            """
            if self._cacheLayer is not None:
                transaction = self._cacheLayer.receive_request(transaction)

                if transaction.cacheHit is False:
                    logging.debug(transaction.request)
                    transaction = self._forwardLayer.receive_request(
                        transaction)
                    logging.debug(transaction.response)

                transaction = self._observeLayer.send_response(transaction)

                transaction = self._blockLayer.send_response(transaction)

                transaction = self._cacheLayer.send_response(transaction)
            else:
                transaction = self._forwardLayer.receive_request(transaction)

                transaction = self._observeLayer.send_response(transaction)

                transaction = self._blockLayer.send_response(transaction)

            self._stop_separate_timer(transaction.separate_timer)

            transaction = self._messageLayer.send_response(transaction)

            if transaction.response is not None:
                if transaction.response.type == defines.Types["CON"]:
                    self._start_retransmission(transaction,
                                               transaction.response)
                self.send_datagram(transaction.response)

        elif isinstance(message, Message):
            transaction = self._messageLayer.receive_empty(message)
            if transaction is not None:
                transaction = self._blockLayer.receive_empty(
                    message, transaction)
                self._observeLayer.receive_empty(message, transaction)

        else:  # is Response
            logger.error("Received response from %s", message.source)
コード例 #50
0
# -*- coding: utf-8 -*-
import random
import socket
import threading
import unittest
from coapthon.messages.response import Response
from coapthon.messages.request import Request
from coapthon import defines
from coapthon.serializer import Serializer
from plugtest_coapserver import CoAPServerPlugTest
import sys
from coapthon.server.coap import CoAP
import afl

afl.init()
content = open(sys.argv[1], 'r')
data = content.read()

try:
    serializer = Serializer()
    message = serializer.deserialize(data, ("127.0.0.1", 5683))
    if isinstance(message, int):
        print("receive_datagram - BAD REQUEST")
    print("receive_datagram - " + str(message))

except RuntimeError:
    logger.exception("Exception with Executor")
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise
コード例 #51
0
ファイル: coap.py プロジェクト: WAvdBeek/CoAPthon3
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        empty = []
        while not self.stopped.isSet():
            try:
                data = None
                client_address = None
                readable, writable, exceptional = select.select(self._socketlist, empty, empty, timeout)
                for s in readable:
                    data, client_address = s.recvfrom(4096)
                    if len(client_address) > 2:
                        client_address = (client_address[0], client_address[1])
                        print ("listen : received ", client_address)
            except socket.timeout:
                print (".")
                continue
            except Exception as e:
                if self._cb_ignore_listen_exception is not None and isinstance(self._cb_ignore_listen_exception, collections.Callable):
                    if self._cb_ignore_listen_exception(e, self):
                        continue
                raise
            try:
                serializer = Serializer()
                if data is not None:
                    message = serializer.deserialize(data, client_address)
                
                    if isinstance(message, int):
                        logger.error("receive_datagram - BAD REQUEST")

                        rst = Message()
                        rst.destination = client_address
                        rst.type = defines.Types["RST"]
                        rst.code = message
                        rst.mid = self._messageLayer.fetch_mid()
                        self.send_datagram(rst)
                        continue

                    logger.debug("receive_datagram - " + str(message))
                    if isinstance(message, Request):
                        transaction = self._messageLayer.receive_request(message)
                        if transaction.request.duplicated and transaction.completed:
                            logger.debug("message duplicated, transaction completed")
                            if transaction.response is not None:
                                self.send_datagram(transaction.response)
                            continue
                        elif transaction.request.duplicated and not transaction.completed:
                            logger.debug("message duplicated, transaction NOT completed")
                            self._send_ack(transaction)
                            continue
                        args = (transaction, )
                        t = threading.Thread(target=self.receive_request, args=args)
                        t.start()
                    # self.receive_datagram(data, client_address)
                    elif isinstance(message, Response):
                        logger.error("Received response from %s", message.source)

                    else:  # is Message
                        transaction = self._messageLayer.receive_empty(message)
                        if transaction is not None:
                            with transaction:
                                self._blockLayer.receive_empty(message, transaction)
                                self._observeLayer.receive_empty(message, transaction)

            except RuntimeError:
                logger.exception("Exception with Executor")
        # close sockets
        for sock in self._socketlist:
            sock.close()
コード例 #52
0
ファイル: coap.py プロジェクト: LinuxIoT/IoT-Gateway
    def receive_datagram(self, args):
        """
        Receive datagram from the udp socket.

        :rtype : Message
        """
        data, client_address = args

        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")

            rst = Message()
            rst.destination = client_address
            rst.type = defines.Types["RST"]
            rst.code = message
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))
        if isinstance(message, Request):

            transaction = self._messageLayer.receive_request(message)

            if transaction.request.duplicated and transaction.completed:
                logger.debug("message duplicated,transaction completed")
                transaction = self._observeLayer.send_response(transaction)
                transaction = self._blockLayer.send_response(transaction)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return
            elif transaction.request.duplicated and not transaction.completed:
                logger.debug("message duplicated,transaction NOT completed")
                self._send_ack(transaction)
                return

            transaction.separate_timer = self._start_separate_timer(transaction)

            transaction = self._blockLayer.receive_request(transaction)

            if transaction.block_transfer:
                self._stop_separate_timer(transaction.separate_timer)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return

            transaction = self._observeLayer.receive_request(transaction)

            transaction = self._forwardLayer.receive_request_reverse(transaction)

            transaction = self._observeLayer.send_response(transaction)

            transaction = self._blockLayer.send_response(transaction)

            self._stop_separate_timer(transaction.separate_timer)

            transaction = self._messageLayer.send_response(transaction)

            if transaction.response is not None:
                if transaction.response.type == defines.Types["CON"]:
                    self._start_retrasmission(transaction, transaction.response)
                self.send_datagram(transaction.response)

        elif isinstance(message, Message):
            transaction = self._messageLayer.receive_empty(message)
            if transaction is not None:
                transaction = self._blockLayer.receive_empty(message, transaction)
                self._observeLayer.receive_empty(message, transaction)

        else:  # pragma: no cover
            logger.error("Received response from %s", message.source)
コード例 #53
0
ファイル: coap.py プロジェクト: shouxian92/final-year-project
    def listen(self, timeout=10):
        """
        Listen for incoming messages. Timeout is used to check if the server must be switched off.

        :param timeout: Socket Timeout in seconds
        """
        self._socket.settimeout(float(timeout))
        while not self.stopped.isSet():
            try:
                data, client_address = self._socket.recvfrom(4096)
                if len(client_address) > 2:
                    client_address = (client_address[0], client_address[1])
            except socket.timeout:
                continue
            try:
                serializer = Serializer()
                message = serializer.deserialize(data, client_address)
                if isinstance(message, int):
                    logger.error("receive_datagram - BAD REQUEST")

                    rst = Message()
                    rst.destination = client_address
                    rst.type = defines.Types["RST"]
                    rst.code = message
                    rst.mid = self._messageLayer._current_mid
                    self._messageLayer._current_mid += 1 % 65535
                    self.send_datagram(rst)
                    continue

                logger.debug("receive_datagram - " + str(message))
                if isinstance(message, Request):
                    transaction = self._messageLayer.receive_request(message)
                    if transaction.request.duplicated and transaction.completed:
                        logger.debug(
                            "message duplicated, transaction completed")
                        if transaction.response is not None:
                            self.send_datagram(transaction.response)
                        continue
                    elif transaction.request.duplicated and not transaction.completed:
                        logger.debug(
                            "message duplicated, transaction NOT completed")
                        self._send_ack(transaction)
                        continue
                    args = (transaction, )
                    t = threading.Thread(target=self.receive_request,
                                         args=args)
                    t.start()
                # self.receive_datagram(data, client_address)
                elif isinstance(message, Response):
                    logger.error("Received response from %s", message.source)

                else:  # is Message
                    transaction = self._messageLayer.receive_empty(message)
                    if transaction is not None:
                        with transaction:
                            self._blockLayer.receive_empty(
                                message, transaction)
                            self._observeLayer.receive_empty(
                                message, transaction)

            except RuntimeError:
                print "Exception with Executor"
        self._socket.close()
コード例 #54
0
ファイル: coap.py プロジェクト: Tanganelli/CoAPthon
    def receive_datagram(self, args):
        """
        Handle messages coming from the udp socket.

        :param args: (data, client_address)
        """
        data, client_address = args

        logging.debug("receiving datagram")

        try:
            host, port = client_address
        except ValueError:
            host, port, tmp1, tmp2 = client_address

        client_address = (host, port)
        
        serializer = Serializer()
        message = serializer.deserialize(data, client_address)
        if isinstance(message, int):
            logger.error("receive_datagram - BAD REQUEST")

            rst = Message()
            rst.destination = client_address
            rst.type = defines.Types["RST"]
            rst.code = message
            self.send_datagram(rst)
            return
        logger.debug("receive_datagram - " + str(message))
        if isinstance(message, Request):

            transaction = self._messageLayer.receive_request(message)

            if transaction.request.duplicated and transaction.completed:
                logger.debug("message duplicated,transaction completed")
                transaction = self._observeLayer.send_response(transaction)
                transaction = self._blockLayer.send_response(transaction)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return
            elif transaction.request.duplicated and not transaction.completed:
                logger.debug("message duplicated,transaction NOT completed")
                self._send_ack(transaction)
                return

            transaction.separate_timer = self._start_separate_timer(transaction)

            transaction = self._blockLayer.receive_request(transaction)

            if transaction.block_transfer:
                self._stop_separate_timer(transaction.separate_timer)
                transaction = self._messageLayer.send_response(transaction)
                self.send_datagram(transaction.response)
                return

            transaction = self._observeLayer.receive_request(transaction)

            """
            call to the cache layer to check if there's a cached response for the request
            if not, call the forward layer
            """
            if self._cacheLayer is not None:
                transaction = self._cacheLayer.receive_request(transaction)

                if transaction.cacheHit is False:
                    logging.debug(transaction.request)
                    transaction = self._forwardLayer.receive_request(transaction)
                    logging.debug(transaction.response)

                transaction = self._observeLayer.send_response(transaction)

                transaction = self._blockLayer.send_response(transaction)

                transaction = self._cacheLayer.send_response(transaction)
            else:
                transaction = self._forwardLayer.receive_request(transaction)

                transaction = self._observeLayer.send_response(transaction)

                transaction = self._blockLayer.send_response(transaction)

            self._stop_separate_timer(transaction.separate_timer)

            transaction = self._messageLayer.send_response(transaction)

            if transaction.response is not None:
                if transaction.response.type == defines.Types["CON"]:
                    self._start_retransmission(transaction, transaction.response)
                self.send_datagram(transaction.response)

        elif isinstance(message, Message):
            transaction = self._messageLayer.receive_empty(message)
            if transaction is not None:
                transaction = self._blockLayer.receive_empty(message, transaction)
                self._observeLayer.receive_empty(message, transaction)

        else:  # is Response
            logger.error("Received response from %s", message.source)