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- AStringspecifying the local directory path where files will be stored or retrieved for transfer. -
server_address- AStringcontaining the address (IP and port) of the server to which the client will connect for file transfers. -
timeout- AnOption<Duration>specifying the maximum amount of time to wait for connection attempts or operations before timing out. IfNone, the client will use a default timeout or no timeout, depending on the underlying connection logic. -
connection- AnArc<Mutex<Option<TcpStream>>>that holds the TCP connection to the server. This field is wrapped inArcandMutexto allow for safe, concurrent access across async contexts. TheOption<TcpStream>isNoneuntil the client successfully connects to the server.
Implementations§
Source§impl Client
impl Client
pub fn new(client_storage_path: &str, server_address: &str) -> Self
Sourcepub fn set_timeout(&mut self, timeout: Duration)
pub fn set_timeout(&mut self, timeout: Duration)
Sets a timeout duration for the client.
Sourcepub async fn send_request(&self, request: Request) -> Result<(), Error>
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
Sourcepub async fn send(&self, path_to_send: &str) -> Result<(), TransferError>
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_sendfunction encounters an error while transferring the file.
Sourcepub async fn download(&self) -> Result<(), TransferError>
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_receivefunction encounters an issue during the download process, returning aTransferError.