Esempio n. 1
0
    def receive_data(self, data):

        if self.IS_COMMAND_RESTORED:
            error_code = get_error_code(data)
            if error_code != ErrorIds.DOWNLOAD_POINTER_MOVED:
                return
            else:
                self.IS_COMMAND_RESTORED = False
                return

        if self._first_recived_package:
            if get_error_code(data) == ErrorIds.ERROR:
                print(get_payload(data))
                raise CommandFinished

            self._first_recived_package = False
            self._file_size = get_payload_size(data)

            self._write_client_data()

            if self._file_size + get_header_size() <= _config.package_size:
                self._file_writer.write(data[get_header_size():self._file_size+get_header_size()])
                self._send_approve = True
            else:
                self._file_writer.write(data[get_header_size():])
                self._readed_bytes += _config.package_size - get_header_size()
        else:
            if self._file_size - self._readed_bytes <= _config.package_size:
                self._file_writer.write(data[:self._file_size - self._readed_bytes])

                self._send_approve = True
            else:
                self._file_writer.write(data)
                self._readed_bytes += len(data)
Esempio n. 2
0
 def _initialize(self, first_packet_data):
     self._uuid = get_payload(first_packet_data)
     self._client_address = SessionManager().delete_session(uuid=self._uuid)
     if self._client_address is not None:
         SocketManager().delete_socket_by_address(self._client_address)
         LOG.info('Client %s:%d: logical connection closed' %
                  self._client_address)
         LOG.info('Client %s:%d: physical connection closed' %
                  self._client_address)
     self._finished = True
Esempio n. 3
0
    def receive_data(self, data):
        if data:
            if get_command_id(data) != CommandIds.CONNECT_COMMAND_ID:
                LOG.warning('Client %s:%d: Unexpected command packet arrived!' % self.session_instance.client_address)
                return

            self._send_response_flag = True
            client_uuid = get_payload(data)
            session = _session_manager.get_session(client_uuid, create_new=False)
            if session is not None:
                self._session_reactivated = True
                self._reactivated_session = session
                _session_manager.delete_session(client_uuid)
                return
            _session_manager.activate_session(session=self.session_instance, uuid=client_uuid)
    def receive_data(self, data):

        if self._raise_command_finished:
            raise CommandFinished

        error_code = get_error_code(data)
        if error_code == ErrorIds.DOWNLOAD_SUCCESSFUL:
            self._file_descriptor.close()

            print("Download finished")
            raise CommandFinished

        elif error_code == ErrorIds.DOWNLOAD_START_FROM:
            start_from = int(get_payload(data))
            self._file_descriptor.seek(start_from)
            self._send_pointer_moved = True
    def __init__(self, first_packet):

        self._file_path = os.path.join(STORAGE_PATH, get_payload(first_packet))

        self._sended_bytes = 0
        self._generate_first_packet = True
        self._send_error = False
        self._send_pointer_moved = False
        self._wait_for_approve = False
        self._raise_command_finished = False

        if os.path.exists(self._file_path):
            self._file_size = os.path.getsize(self._file_path)
            self._file_descriptor = open(self._file_path, "rb")
        else:
            self._send_error = True
 def receive_data(self, data):
     print(get_payload(data))
     raise CommandFinished