def handle_job_finished(self):
        data = self._extract_data_from_message((yield self.read_message()))
        if not Request.is_valid(data, Request.Command.JOB_FINISHED):
            if Request.is_valid(data, Request.Command.JOB_FAILED):
                _logger.warn("[%s] Job failed!" % CLIENT)
                request = Request.request_from_data(data)
                ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED,
                                                 request.args)
                return
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received file transfer notification" % SERVER)

        job_ID = int(request.args[Request.ARGS_JOB_ID])
        file_name = str(job_ID) + CATROBAT_FILE_EXT
        file_size = int(request.args[Request.ARGS_FILE_SIZE])
        file_hash = request.args[Request.ARGS_FILE_HASH]
        _logger.info("[%s]: Job ID: %d, File size: %d, SHA256: %s" %
                     (CLIENT, job_ID, file_size, file_hash))

        if file_size == 0:
            raise TCPConnectionException("Cannot transfer empty file...",
                                         context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(
            Reply(result=True, msg="Ready for file transfer!"))

        download_dir = self.server.settings["download_dir"]
        file_path = os.path.join(download_dir, file_name)
        max_input_buffer_size = int(
            self.server.settings["max_input_buffer_size"])
        with open(file_path, 'wb+', 0) as f:
            if file_size <= max_input_buffer_size:
                f.write((yield self.read_bytes(file_size)))
            else:
                transfered_bytes = 0
                while transfered_bytes < file_size:
                    buffer_size = min((file_size - transfered_bytes),
                                      max_input_buffer_size)
                    f.write((yield self.read_bytes(buffer_size)))
                    transfered_bytes += buffer_size
            f.seek(0, 0)
            computed_file_hash = hashlib.sha256(f.read()).hexdigest()
            if computed_file_hash != file_hash:
                raise TCPConnectionException(
                    "Given hash value is not equal to computed hash value.",
                    context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        _logger.info(
            "OK! Hash is equal to computed hash value. Finished file transfer!"
        )
        yield self.send_message(Reply(result=True, msg="OK! Hash is equal to computed " \
                                      "hash value. Finished file transfer!"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_FINISHED,
                                         request.args)
    def handle_job_output_notification(self, data):
        if not Request.is_valid(data, Request.Command.JOB_OUTPUT_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.debug("[%s]: Received job output notification" % SERVER)
        for line in request.args[Request.ARGS_LINES]:
            _logger.debug("[%s]: %s" % (CLIENT, line))
#         _logger.debug("[%s]: Reply: Accepted!" % SERVER)
#         yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_OUTPUT, request.args)
    def handle_job_output_notification(self, data):
        if not Request.is_valid(data, Request.Command.JOB_OUTPUT_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.debug("[%s]: Received job output notification" % SERVER)
        for line in request.args[Request.ARGS_LINES]:
            _logger.debug("[%s]: %s" % (CLIENT, line))
#         _logger.debug("[%s]: Reply: Accepted!" % SERVER)
#         yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_OUTPUT, request.args)
 def handle_exception(self, exception, msg):
     _logger.error("{}!".format(msg))
     _logger.exception(exception)
     yield self.send_message(Reply(result=False, msg="{}! Closing connection.".format(msg)))
     if isinstance(exception, TCPConnectionException) and exception.context != None:
         args = exception.context
         args["msg"] = msg
         ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED, args)
     else:
         ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED, { "msg": msg })
     self.connection.print_error_and_close_stream()
    def handle_job_started_notification(self):
        data = self._extract_data_from_message((yield self.read_message()))
        if not Request.is_valid(data, Request.Command.JOB_STARTED_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received job start notification" % SERVER)
        _logger.info("[%s]: Title of Scratch program: '%s'" % (CLIENT, unicode(request.args[Request.ARGS_TITLE])))

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_STARTED, request.args)
 def handle_exception(self, exception, msg):
     _logger.error("{}!".format(msg))
     _logger.exception(exception)
     yield self.send_message(Reply(result=False, msg="{}! Closing connection.".format(msg)))
     if isinstance(exception, TCPConnectionException) and exception.context != None:
         args = exception.context
         args["msg"] = msg
         ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED, args)
     else:
         ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED, { "msg": msg })
     self.connection.print_error_and_close_stream()
    def handle_job_started_notification(self):
        data = self._extract_data_from_message((yield self.read_message()))
        if not Request.is_valid(data, Request.Command.JOB_STARTED_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received job start notification" % SERVER)
        _logger.info("[%s]: Title of Scratch program: '%s'" % (CLIENT, unicode(request.args[Request.ARGS_TITLE])))

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_STARTED, request.args)
    def handle_job_progress_notification(self, data):
        if not Request.is_valid(data, Request.Command.JOB_PROGRESS_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        if not isinstance(request.args[Request.ARGS_PROGRESS], int):
            raise TCPConnectionException("Progress parameter must be of type int!")
        _logger.debug("[%s]: Received job progress notification (Progress: %d%%)"
                      % (SERVER, request.args[Request.ARGS_PROGRESS]))

#         _logger.debug("[%s]: Reply: Accepted!" % SERVER)
#         yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_PROGRESS, request.args)
    def handle_job_progress_notification(self, data):
        if not Request.is_valid(data, Request.Command.JOB_PROGRESS_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        if not isinstance(request.args[Request.ARGS_PROGRESS], int):
            raise TCPConnectionException("Progress parameter must be of type int!")
        _logger.debug("[%s]: Received job progress notification (Progress: %d%%)"
                      % (SERVER, request.args[Request.ARGS_PROGRESS]))

#         _logger.debug("[%s]: Reply: Accepted!" % SERVER)
#         yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_PROGRESS, request.args)
    def handle_job_conversion_finished_notification(self, data):
        if data == None or not Request.is_valid(data, Request.Command.JOB_CONVERSION_FINISHED_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received job finished notification" % SERVER)
        _logger.info("[%s]: %s " % (CLIENT, request.args[Request.ARGS_MSG]))
        exit_code = int(request.args[Request.ARGS_RESULT])
        _logger.info("[%s]: Job finished with exit code: %d" % (SERVER, exit_code))
        if exit_code != 0:
            raise TCPConnectionException("Job failed with exit code: %d" % exit_code, context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_CONVERSION_FINISHED, request.args)
    def handle_job_conversion_finished_notification(self, data):
        if data == None or not Request.is_valid(data, Request.Command.JOB_CONVERSION_FINISHED_NOTIFICATION):
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received job finished notification" % SERVER)
        _logger.info("[%s]: %s " % (CLIENT, request.args[Request.ARGS_MSG]))
        exit_code = int(request.args[Request.ARGS_RESULT])
        _logger.info("[%s]: Job finished with exit code: %d" % (SERVER, exit_code))
        if exit_code != 0:
            raise TCPConnectionException("Job failed with exit code: %d" % exit_code, context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(Reply(result=True, msg="ACK"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_CONVERSION_FINISHED, request.args)
    def handle_job_finished(self):
        data = self._extract_data_from_message((yield self.read_message()))
        if not Request.is_valid(data, Request.Command.JOB_FINISHED):
            if Request.is_valid(data, Request.Command.JOB_FAILED):
                _logger.warn("[%s] Job failed!" % CLIENT)
                request = Request.request_from_data(data)
                ConverterWebSocketHandler.notify(NotificationType.JOB_FAILED, request.args)
                return
            raise TCPConnectionException("Invalid data given!")
        request = Request.request_from_data(data)
        _logger.info("[%s]: Received file transfer notification" % SERVER)

        job_ID = int(request.args[Request.ARGS_JOB_ID])
        file_name = str(job_ID) + CATROBAT_FILE_EXT
        file_size = int(request.args[Request.ARGS_FILE_SIZE])
        file_hash = request.args[Request.ARGS_FILE_HASH]
        _logger.info("[%s]: Job ID: %d, File size: %d, SHA256: %s"
                     % (CLIENT, job_ID, file_size, file_hash))

        if file_size == 0:
            raise TCPConnectionException("Cannot transfer empty file...", context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        yield self.send_message(Reply(result=True, msg="Ready for file transfer!"))

        download_dir = self.server.settings["download_dir"]
        file_path = os.path.join(download_dir, file_name)
        max_input_buffer_size = int(self.server.settings["max_input_buffer_size"])
        with open(file_path, 'wb+', 0) as f:
            if file_size <= max_input_buffer_size:
                f.write((yield self.read_bytes(file_size)))
            else:
                transfered_bytes = 0
                while transfered_bytes < file_size:
                    buffer_size = min((file_size - transfered_bytes), max_input_buffer_size)
                    f.write((yield self.read_bytes(buffer_size)))
                    transfered_bytes += buffer_size
            f.seek(0, 0)
            computed_file_hash = hashlib.sha256(f.read()).hexdigest()
            if computed_file_hash != file_hash:
                raise TCPConnectionException("Given hash value is not equal to computed hash value.", context=request.args)

        _logger.debug("[%s]: Reply: Accepted!" % SERVER)
        _logger.info("OK! Hash is equal to computed hash value. Finished file transfer!")
        yield self.send_message(Reply(result=True, msg="OK! Hash is equal to computed " \
                                      "hash value. Finished file transfer!"))
        ConverterWebSocketHandler.notify(NotificationType.JOB_FINISHED, request.args)