示例#1
0
    def request_data(self, num_bytes):
        """Request num_bytes of entropy from server

        Return True if request is sent successfully.

        The variable timer is used to track how long it has been since
        the last communication with the server.

        If timer is None, timer is set to the time of the send.
        Otherwise, timer is not changed.
        """
        if num_bytes == 0:
            return True

        success = False

        logging.debug("Request %i bytes", num_bytes)
        if self.status_ok():
            request = DaemonMsg.construct_request(num_bytes)

            bytes_written = 0
            try:
                bytes_written = self.socket.send(request)
            except IOError as error:
                logging.error("Error on send (error: %s)", errno.errorcode[error.errno])

            if bytes_written == len(request):
                logging.debug("Requested %i bytes", num_bytes)
                self.outstanding_bytes += num_bytes
                if self.timer is None:
                    self.timer = datetime.datetime.now()

                success = True
            else:
                logging.error("Failed to write all bytes: wrote %i", bytes_written)
                self.destroy()
        return success