1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
pub use broker::CHUNK_SIZE;
pub use chunks::Chunks;
use gantry_protocol as protocol;
pub use protocol::catalog::{CatalogQuery, CatalogQueryResults, Token};
pub use protocol::stream::{DownloadRequest, FileChunk, TransferAck, UploadRequest};

pub mod broker;
pub mod chunks;

#[macro_use]
extern crate serde_derive;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfiguration {
    pub server_urls: Vec<String>,
    pub user_jwt: String,
    pub user_seed: String,
}

/// An instance of a Gantry client connection
#[derive(Clone)]
pub struct Client {
    natsclient: natsclient::Client,
}

impl Client {
    pub fn new(nats_urls: Vec<String>, jwt: &str, seed: &str) -> Client {
        Client {
            natsclient: broker::get_client(nats_urls, Some(jwt), Some(seed)).unwrap(),
        }
    }

    pub fn from_config(config: ConnectionConfiguration) -> Client {
        Client {
            natsclient: broker::get_client(
                config.server_urls,
                Some(&config.user_jwt),
                Some(&config.user_seed),
            )
            .unwrap(),
        }
    }

    pub fn default() -> Client {
        Client {
            natsclient: broker::get_client(vec!["nats://localhost:4222".into()], None, None)
                .unwrap(),
        }
    }

    pub fn put_token(&self, token: &Token) -> Result<(), Box<dyn ::std::error::Error>> {
        broker::put(&self.natsclient, token)
    }

    pub fn query_catalog(
        &self,
        query: &CatalogQuery,
    ) -> Result<CatalogQueryResults, Box<dyn ::std::error::Error>> {
        broker::query(&self.natsclient, query)
    }

    pub fn remove_token(&self, _token: &Token) -> Result<(), Box<dyn ::std::error::Error>> {
        unimplemented!()
    }

    pub fn start_upload(
        &self,
        req: &UploadRequest,
    ) -> Result<TransferAck, Box<dyn ::std::error::Error>> {
        broker::start_upload(&self.natsclient, req)
    }

    pub fn upload_chunk(
        &self,
        sequence_no: u64,
        actor: &str,
        chunk_size: u64,
        total_bytes: u64,
        total_chunks: u64,
        bytes: Vec<u8>,
    ) -> Result<(), Box<dyn ::std::error::Error>> {
        broker::upload_chunk(
            &self.natsclient,
            sequence_no,
            actor,
            chunk_size,
            total_bytes,
            total_chunks,
            bytes,
        )
    }

    pub fn download_actor<F>(
        &self,
        actor: &str,
        chunk_handler: F,
    ) -> Result<TransferAck, Box<dyn ::std::error::Error>>
    where
        F: Fn(FileChunk) -> Result<(), Box<dyn ::std::error::Error>> + Sync + Send,
        F: 'static,
    {
        let req = DownloadRequest {
            actor: actor.to_string(),
        };
        broker::request_download(&self.natsclient, req, chunk_handler)
    }
}