Skip to content

Commit

Permalink
Merge pull request #58 from Bicheka:client
Browse files Browse the repository at this point in the history
added download function to client and improved docs
  • Loading branch information
Bicheka authored Nov 2, 2024
2 parents 180647c + f7ce001 commit 3247c36
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
84 changes: 81 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ use tokio::{io::AsyncWriteExt, net::TcpStream, sync::Mutex, time};
use bincode;
use crate::{file_transfer::{Connection, FileTransferProtocol, TransferError}, network::Request};

/// 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.
pub struct Client {
client_storage_path: String,
server_address: String,
Expand Down Expand Up @@ -38,7 +61,7 @@ impl Client {
Ok(())
}

/// Sends a request to the server.
/// Sends a request to the server. Ok if if ok to continue, Err if server declines for some reason
pub async fn send_request(&self, request: Request) -> Result<(), anyhow::Error> {
let mut connection = self.connection.lock().await;
if let Some(ref mut connection) = *connection {
Expand All @@ -53,16 +76,71 @@ impl Client {
Ok(())
}

/// Uses File Transfer Protocol to init a send to server through an already stablished connection
/// 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.
pub async fn send(&self, path_to_send: &str) -> Result<(), TransferError> {
let mut connection = self.connection.lock().await;
let connection = connection.as_mut().expect("Connection is not stablished");
let connection = connection.as_mut().expect("Connection is not established");
FileTransferProtocol::new(&Path::new(path_to_send), 64 * 1024)
.init_send(&mut Connection { stream: connection })
.await?;
Ok(())
}

/// 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`.
pub async fn download(&self) -> Result<(), TransferError>{
let mut connection = self.connection.lock().await;
let connection = connection.as_mut().expect("Connection is not established");
FileTransferProtocol::new(&Path::new(&self.client_storage_path), 64 * 1024)
.init_receive(&mut Connection { stream: connection })
.await?;
Ok(())
}

/// Closes the connection to the server.
pub async fn close(&mut self) -> Result<(), anyhow::Error> {
let mut connection = self.connection.lock().await;
Expand Down
2 changes: 1 addition & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum Request {
/// Request to retrieve a file or directory located at a given path.
Get(Box<Path>),
/// Request to upload a file or directory, along with its `PathType`.
Upload(),
Upload,
}

/// Enum representing the types of IP addresses.
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Server {
.init_send(&mut Connection { stream })
.await?;
}
Request::Upload() => {
Request::Upload => {
FileTransferProtocol::new(&self.path, self.buffer_size)
.init_receive(&mut Connection { stream })
.await?;
Expand Down

0 comments on commit 3247c36

Please sign in to comment.