Esempio n. 1
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()
Esempio n. 2
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 = []
Esempio n. 3
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()
Esempio n. 4
0
 def handle_notification(self, response, client_callback):
     host, port = response.source
     key = hash(str(host) + str(port) + str(response.token))
     self.relation[key] = (response, time.time(), client_callback)
     if response.type == defines.inv_types["CON"]:
         ack = Message.new_ack(response)
         self.send(ack)
Esempio n. 5
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 = []
Esempio n. 6
0
 def handle_response(self, response):
     if response.type == defines.inv_types["CON"]:
         ack = Message.new_ack(response)
         self.send(ack)
     key_token = hash(str(self.server[0]) + str(self.server[1]) + str(response.token))
     if key_token in self.sent_token.keys():
         self.received_token[key_token] = response
         req, timestamp, callback, client_callback = self.sent_token[key_token]
         key = hash(str(self.server[0]) + str(self.server[1]) + str(req.mid))
         self.received[key] = response
         callback(req.mid, client_callback)
Esempio n. 7
0
    def send_ack(self, request):
        # Handle separate
        """
        Sends an ACK message for the request.

        :param request: [request] or request
        """
        if isinstance(request, list):
            request = request[0]
        ack = Message.new_ack(request)
        host, port = request.source
        self._parent.send(ack, host, port)
        request.acknowledged = True
Esempio n. 8
0
    def send_ack(self, request):
        # Handle separate
        """
        Sends an ACK message for the request.

        :param request: [request, sleep_time] or request
        """
        if isinstance(request, list):
            if len(request) == 2:
                time.sleep(request[1])
            request = request[0]
        ack = Message.new_ack(request)
        host, port = request.source
        if not request.acknowledged:
            self._parent.send(ack, host, port)
            request.acknowledged = True
    def send_ack(self, list_request):
        """
        Send an ack to the client. Used mostly with separate.

        :param list_request: the request to be acknowledge.
        :type list_request: [Request] or Request
        """

        if isinstance(list_request, list):
            request = list_request[0]
        else:
            request = list_request
        del self.timer[request.mid]
        host, port = request.source
        ack = Message.new_ack(request)
        self.send(ack, host, port)
Esempio n. 10
0
 def handle_response(self, response):
     if response.type == defines.inv_types["CON"]:
         ack = Message.new_ack(response)
         self.send(ack, self._endpoint)
     key_token = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(response.token))
     if key_token in self.sent_token.keys():
         self.received_token[key_token] = response
         req = self.sent_token[key_token]
         key = hash(str(self._endpoint[0]) + str(self._endpoint[1]) + str(req.mid))
         timer, counter = self.call_id[key]
         timer.cancel()
         self.received[key] = response
         self.condition.acquire()
         self._response = response
         self.condition.notify()
         self.condition.release()
Esempio n. 11
0
    def send_ack(self, list_request):
        """
        Send an ack to the client. Used mostly with separate.

        :param list_request: the request to be acknowledge.
        :type list_request: [Request] or Request
        """

        if isinstance(list_request, list):
            request = list_request[0]
        else:
            request = list_request
        key_timer = hash(str(request.source[0]) + str(request.source[1]) + str(request.mid))
        if self.timer.get(key_timer) is not None:
            del self.timer[key_timer]
        host, port = request.source
        ack = Message.new_ack(request)
        self.send(ack, host, port)
Esempio n. 12
0
    def handle_request(self, request):
        """
        Handles requests.

        :param request: the request
        :return: the response
        """
        host, port = request.source
        key = hash(str(host) + str(port) + str(request.mid))
        if key not in self._parent.received:
            if request.blockwise:
                # Blockwise
                last, request = self._parent.blockwise_transfer(request)
                if last:
                    self._parent.received[key] = (request, time.time())
                    return request
            else:
                self._parent.received[key] = (request, time.time())
                return request
        else:
            request, timestamp = self._parent.received.get(key)
            request.duplicated = True
            self._parent.received[key] = (request, timestamp)
            try:
                response, timestamp = self._parent.sent.get(key)
            except TypeError:
                response = None
            if isinstance(response, Response):
                return response
            elif request.acknowledged:
                ack = Message.new_ack(request)
                return ack
            elif request.rejected:
                rst = Message.new_rst(request)
                return rst
            else:
                # The server has not yet decided, whether to acknowledge or
                # reject the request. We know for sure that the server has
                # received the request though and can drop this duplicate here.
                return None