Crate file_protocol[][src]

Kubos File Transfer Protocol

Examples

extern crate file_protocol;

use file_protocol::*;
use std::time::Duration;

fn upload() -> Result<(), String> {
    let f_protocol = FileProtocol::new("0.0.0.0", "0.0.0.0:7000", Some("storage/dir".to_owned()));

    let source_path = "client.txt";
    let target_path = "service.txt";

    // Copy file to upload to temp storage. Calculate the hash and chunk info
    let (hash, num_chunks, mode) = f_protocol.initialize_file(&source_path)?;

    // Tell our destination the hash and number of chunks to expect
    f_protocol.send_metadata(&hash, num_chunks)?;

    // Give the service can have time to set up the temporary storage directory
    ::std::thread::sleep(Duration::from_millis(1));

    // Send export command for file
    f_protocol.send_export(&hash, &target_path, mode)?;

    // Start the engine to send the file data chunks
    Ok(f_protocol.message_engine(Duration::from_millis(10), State::Transmitting)?)
}
extern crate file_protocol;

use file_protocol::*;
use std::time::Duration;

fn download() -> Result<(), String> {
    let f_protocol = FileProtocol::new("0.0.0.0", "0.0.0.0:8000", None);

    let source_path = "service.txt";
    let target_path = "client.txt";

    // Send our file request to the remote addr and verify that it's
    // going to be able to send it
    f_protocol.send_import(source_path)?;

    // Wait for the request reply
    let reply = match f_protocol.recv(None) {
        Ok(Some(message)) => message,
        Ok(None) => return Err("Failed to import file".to_owned()),
        Err(Some(error)) => return Err(format!("Failed to import file: {}", error)),
        Err(None) => return Err("Failed to import file".to_owned()),
    };

    let state = f_protocol.process_message(
        reply,
        State::StartReceive {
            path: target_path.to_string(),
        },
    )?;

    Ok(f_protocol.message_engine(Duration::from_millis(10), state)?)
}

Re-exports

pub use protocol::Protocol as FileProtocol;
pub use protocol::State;

Modules

protocol

File transfer protocol module

Enums

Message

File protocol message types