Esempio n. 1
0
    def _get_message(self):
        message = get_message(self._current_client.connection, PACKET_SIZE)

        if not message:
            raise DisconnectedException
        else:
            return message
Esempio n. 2
0
def _get_message(connection: socket.socket):
    message = get_message(connection, PACKET_SIZE)

    if not message:
        raise DisconnectedException
    else:
        return message
Esempio n. 3
0
    def execute(self):
        data = {'type': Commands.ECHO.value, 'payload': self._data}

        message = compose_message(data)

        self._connection.sendall(message)

        response = get_message(self._connection, PACKET_SIZE)

        echoed_data = response['payload']

        print(f'Echoed data: {echoed_data}')
Esempio n. 4
0
    def execute(self):
        data = {'type': Commands.TIME.value}

        message = compose_message(data)

        self._connection.sendall(message)

        response = get_message(self._connection, PACKET_SIZE)

        time = response['payload']

        print(f'Time, according to server, is {time}')
Esempio n. 5
0
    def _send_request_message(self, file_name: str, file_size: int):
        data = {'type': Commands.DOWNLOAD.value, 'file_name': file_name, 'file_size': file_size}

        message = compose_message(data)

        self._client.connection.send(message)

        response = get_message(self._client.connection)

        if not response['start']:
            print('Client denied download!')

            raise FileExchangeDenial
Esempio n. 6
0
    def execute(self):
        data = {'command': Commands.ECHO.value, 'data_length': len(self._data)}

        message = pk.dumps(data)

        self._connection.sendall(message)

        response = pk.loads(self._connection.recv(PACKET_SIZE))

        if response['ACK'] is True:
            self._connection.sendall(bytes(self._data, 'utf-8'))

            echoed_data = get_message(self._connection, len(self._data),
                                      PACKET_SIZE).decode('utf-8')

            print(f'Echoed data: {echoed_data}')
        else:
            raise Exception('Server couldn\'t process the command!')
Esempio n. 7
0
    def execute(self):
        try:
            start_time = perf_counter()

            message = compose_message({
                'type': Commands.DOWNLOAD.value,
                'file_name': self._file_name
            })

            self._connection.send(message)

            response = get_message(self._connection)

            self._file_size = response['file_size']

            if os.path.exists(self._file_name):
                os.remove(self._file_name)

            message = compose_message({'start': True})

            self._connection.send(message)

            self._get_file()

            end_time = perf_counter()

            if (end_time - start_time) > 0:
                bit_rate = self._file_size / float((end_time - start_time))
            else:
                bit_rate = -1

            print(
                f'\nFile {self._file_name} has been successfully uploaded by client,'
                f' Bit rate: {bit_rate * BIT_RATE_KBPS} kBps')

        except FileNotFoundError:
            print('Such file doesn\'t exist. Try another one')