Ejemplo n.º 1
0
    def receive(self, timeout=None):
        """ Receive data from the connection

        :param timeout: The timeout, or None to wait forever
        :type timeout: float or None
        :return: The data received
        :rtype: bytestring
        :raise SpinnmanTimeoutException: \
            If a timeout occurs before any data is received
        :raise SpinnmanIOException: If an error occurs receiving the data
        """
        try:
            self.__socket.settimeout(timeout)
            return self.__socket.recv(1024)
        except socket.timeout:
            raise SpinnmanTimeoutException("receive", timeout)
        except Exception as e:
            raise_from(SpinnmanIOException(str(e)), e)
Ejemplo n.º 2
0
    def receive(self, timeout=None):
        """ Receive data from the connection

        :param float timeout: The timeout in seconds, or None to wait forever
        :return: The data received as a bytestring
        :rtype: bytes
        :raise SpinnmanTimeoutException:
            If a timeout occurs before any data is received
        :raise SpinnmanIOException: If an error occurs receiving the data
        """
        if self._socket._closed:
            raise SpinnmanEOFException()
        try:
            self._socket.settimeout(timeout)
            return self._socket.recv(300)
        except socket.timeout as e:
            raise SpinnmanTimeoutException("receive", timeout) from e
        except Exception as e:  # pylint: disable=broad-except
            raise SpinnmanIOException(str(e)) from e
Ejemplo n.º 3
0
    def receive_with_address(self, timeout=None):
        """ Receive data from the connection along with the address where the\
            data was received from

        :param timeout: The timeout, or None to wait forever
        :type timeout: None
        :return: A tuple of the data received and a tuple of the\
                (address, port) received from
        :rtype: (bytestring, (str, int))
        :raise SpinnmanTimeoutException: If a timeout occurs before any data\
                    is received
        :raise SpinnmanIOException: If an error occurs receiving the data
        """
        try:
            self._socket.settimeout(timeout)
            return self._socket.recvfrom(300)
        except socket.timeout:
            raise SpinnmanTimeoutException("receive", timeout)
        except Exception as e:
            raise SpinnmanIOException(str(e))
Ejemplo n.º 4
0
    def receive_with_address(self, timeout=None):
        """ Receive data from the connection along with the address where the\
            data was received from

        :param float timeout: The timeout, or None to wait forever
        :return: A tuple of the data received and a tuple of the
            (address, port) received from
        :rtype: tuple(bytes, tuple(str, int))
        :raise SpinnmanTimeoutException:
            If a timeout occurs before any data is received
        :raise SpinnmanIOException: If an error occurs receiving the data
        """
        if self._socket._closed:
            raise SpinnmanEOFException()
        try:
            self._socket.settimeout(timeout)
            return self._socket.recvfrom(300)
        except socket.timeout as e:
            raise SpinnmanTimeoutException("receive", timeout) from e
        except Exception as e:  # pylint: disable=broad-except
            raise SpinnmanIOException(str(e)) from e
Ejemplo n.º 5
0
    def _resend(self, seq, request_sent, reason):
        if self._retries[seq] <= 0:
            # Report timeouts as timeout exception
            if all(reason == "timeout" for reason in self._retry_reason[seq]):
                raise SpinnmanTimeoutException(
                    request_sent.scp_request_header.command,
                    self._packet_timeout)

            # Report any other exception
            raise SpinnmanIOException(
                "Errors sending request {} to {}, {}, {} over {} retries: {}".
                format(request_sent.scp_request_header.command,
                       request_sent.sdp_header.destination_chip_x,
                       request_sent.sdp_header.destination_chip_y,
                       request_sent.sdp_header.destination_cpu,
                       self._n_retries, self._retry_reason[seq]))

        # If the request can be retried, retry it
        self._retries[seq] -= 1
        self._in_progress += 1
        self._requests[seq] = request_sent
        self._retry_reason[seq].append(reason)
        self._connection.send(self._request_data[seq])
        self._n_resent += 1
 def receive_scp_response(self, timeout=1.0):
     raise SpinnmanTimeoutException("Test", timeout)
    def _do_retrieve(self, n_packets, timeout):
        """ Receives responses until there are only n_packets responses left

        :param n_packets: The number of packets that can remain after running
        """

        # Keep a set of packets to resend
        to_resend = list()

        # While there are still more packets in progress than some threshold
        while self._in_progress > n_packets:
            try:

                # Receive the next response
                result, seq, raw_data, offset = \
                    self._connection.receive_scp_response(timeout)

                # Only process responses which have matching requests
                if seq in self._requests:
                    request_sent = self._requests[seq]

                    # If the response can be retried, retry it, as long as the
                    # timeout hasn't expired

                    if (result in self._retry_codes
                            and (time.time() - self._send_time[seq] <
                                 self._packet_timeout)):
                        self._connection.send(self._request_data[seq])
                        self._n_retry_code_resent += 1
                    else:

                        # No retry is possible - try constructing the result
                        try:
                            response = request_sent.get_scp_response()
                            response.read_bytestring(raw_data, offset)
                            if self._callbacks[seq] is not None:
                                self._callbacks[seq](response)
                        except Exception as e:
                            self._error_callbacks[seq](request_sent, e,
                                                       sys.exc_info()[2])

                        # Remove the sequence from the outstanding responses
                        del self._send_time[seq]
                        del self._requests[seq]
                        del self._request_data[seq]
                        del self._retries[seq]
                        del self._callbacks[seq]
                        del self._error_callbacks[seq]
                        self._in_progress -= 1
            except SpinnmanTimeoutException:
                self._n_timeouts += 1

                # If there is a timeout, all packets remaining are resent
                time_now = time.time()
                to_remove = list()
                for seq, request_sent in self._requests.iteritems():
                    if time_now - self._send_time[seq] >= self._packet_timeout:
                        to_resend.append((seq, request_sent))
                        self._in_progress -= 1
                        to_remove.append(seq)
                for seq in to_remove:
                    del self._requests[seq]

        # Try to resend the packets
        for seq, request_sent in to_resend:
            if self._retries[seq] > 0:

                # If the request can be retried, retry it
                self._retries[seq] -= 1
                self._in_progress += 1
                self._requests[seq] = request_sent
                self._send_time[seq] = time.time()
                self._connection.send(self._request_data[seq])
                self._n_resent += 1
            else:

                # Otherwise, report it as a timeout
                try:
                    raise SpinnmanTimeoutException(
                        request_sent.scp_request_header.command,
                        self._packet_timeout)
                except Exception as e:
                    self._error_callbacks[seq](request_sent, e,
                                               sys.exc_info()[2])
                    del self._request_data[seq]
                    del self._send_time[seq]
                    del self._retries[seq]
                    del self._callbacks[seq]
                    del self._error_callbacks[seq]