def file_sender(barrier, port, input_file):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', port))
    sock.listen(1)
    barrier.wait()
    c, addr = sock.accept()
    transferer = BlockingSocketTransferer(c)
    transferer.send_file(input_file)
    transferer.close()
Ejemplo n.º 2
0
    def __handle_client_connection(client_sock, backup_no: int):
        """
        Read message from a specific client socket and closes the socket

        If a problem arises in the communication with the client, the
        client socket will also be closed
        """
        socket_transferer = BlockingSocketTransferer(client_sock)
        try:
            msg = socket_transferer.receive_plain_text()
            msg = json.loads(msg)
            path, previous_checksum = msg['path'], msg['checksum']
            SidecarProcess.logger.debug("Previous checksum for path %s is '%s'" % (path, previous_checksum))
        except (OSError, TimeoutError) as e:
            SidecarProcess.logger.exception("Error while reading socket %s: %s" % (client_sock, e))
            socket_transferer.abort()
            return
        try:
            backup_file = BackupFile.create_from_path(path, TMP_BACKUP_PATH % backup_no)
        except Exception:
            SidecarProcess.logger.exception("Error while making backup file")
            socket_transferer.abort()
            return
        file_checksum = backup_file.get_hash()
        if file_checksum == previous_checksum:
            SidecarProcess.logger.info("Previous checksum equals to actual data, skipping backup")
            socket_transferer.send_plain_text("SAME")
            socket_transferer.abort()
            return
        else:
            socket_transferer.send_plain_text("DIFF")
        try:
            socket_transferer.send_file(TMP_BACKUP_PATH % backup_no)
            SidecarProcess.logger.debug("Backup file sent")
            socket_transferer.send_plain_text(file_checksum)
        except Exception as e:
            SidecarProcess.logger.exception("Error while writing socket %s: %s" % (client_sock, e))
            socket_transferer.abort()
            return
        finally:
            socket_transferer.close()
        return