def __handle_client_connection(self, client_sock): """ 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() except Exception: ClientListener.logger.exception("Error while reading socket {}".format(client_sock)) socket_transferer.abort() return try: parsed_msg = json.loads(msg) except json.decoder.JSONDecodeError as e: ClientListener.logger.exception("Error parsing json") socket_transferer.send_plain_text(json.dumps({"message": "ERROR", "data": str(e)})) socket_transferer.close() return self.backup_scheduler_write.send((parsed_msg['command'], parsed_msg['args'])) try: message, data = self.backup_scheduler_read.recv() except EOFError as e: ClientListener.logger.exception("Backup scheduler death") socket_transferer.abort() raise e try: socket_transferer.send_plain_text(json.dumps({"message": message, "data": data})) except Exception: ClientListener.logger.exception("Error writing through socket") socket_transferer.abort() return socket_transferer.close()
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