file_transfer_system::client

Struct Client

Source
pub struct Client { /* private fields */ }
Expand description

Represents a client for managing file transfers over a TCP connection.

The Client struct encapsulates the necessary details for establishing and managing connections to a server for file transfer operations. It holds connection details, configuration options, and storage settings for the client.

§Fields

  • client_storage_path - A String specifying the local directory path where files will be stored or retrieved for transfer.

  • server_address - A String containing the address (IP and port) of the server to which the client will connect for file transfers.

  • timeout - An Option<Duration> specifying the maximum amount of time to wait for connection attempts or operations before timing out. If None, the client will use a default timeout or no timeout, depending on the underlying connection logic.

  • connection - An Arc<Mutex<Option<TcpStream>>> that holds the TCP connection to the server. This field is wrapped in Arc and Mutex to allow for safe, concurrent access across async contexts. The Option<TcpStream> is None until the client successfully connects to the server.

Implementations§

Source§

impl Client

Source

pub fn new(client_storage_path: &str, server_address: &str) -> Self

Source

pub fn set_timeout(&mut self, timeout: Duration)

Sets a timeout duration for the client.

Source

pub async fn connect(&mut self) -> Result<(), Error>

Connects to the server.

Source

pub async fn send_request(&self, request: Request) -> Result<(), Error>

Sends a request to the server. Ok if if ok to continue, Err if server declines for some reason

Source

pub async fn send(&self, path_to_send: &str) -> Result<(), TransferError>

Initiates a file transfer to the server using the File Transfer Protocol (FTP).

This asynchronous function sends a file located at path_to_send through an existing connection to the server. It establishes the transfer by setting up the file path and buffer size, then utilizes the init_send function of FileTransferProtocol to handle the transmission over the connection.

§Arguments
  • path_to_send - A string slice that specifies the path to the file intended for transfer to the server.
§Returns

Returns Ok(()) if the file transfer is successfully initiated and completes without errors, or Err(TransferError) if any issue arises during the process.

§Errors

This function will return an error in the following cases:

  • The connection is not established, causing a “Connection is not established” error to be raised.
  • The init_send function encounters an error while transferring the file.
Source

pub async fn download(&self) -> Result<(), TransferError>

Downloads a file from the server to the client’s storage path using the File Transfer Protocol.

This asynchronous function initiates a file download from the server through an already established connection. The file will be saved at the path specified by client_storage_path, using a buffer size of 64 KB for efficient data transfer.

§Arguments

This function does not take any additional arguments but relies on the client_storage_path field of the Client struct to determine the location where the file should be saved.

§Returns

Returns Ok(()) if the file is successfully downloaded, or Err(TransferError) if an error occurs during the download process.

§Errors

This function may return an error in the following cases:

  • The connection is not established, resulting in an “Connection is not established” error.
  • The init_receive function encounters an issue during the download process, returning a TransferError.
Source

pub async fn close(&mut self) -> Result<(), Error>

Closes the connection to the server.

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more