rust-ipfs/examples/client2.rs

34 lines
1012 B
Rust
Raw Normal View History

2019-02-21 22:28:59 +01:00
#![feature(async_await, await_macro, futures_api)]
2019-02-22 15:56:52 +01:00
use ipfs::{Block, Ipfs, IpfsOptions, RepoTypes, SwarmTypes, IpfsTypes};
#[derive(Clone)]
struct Types;
impl RepoTypes for Types {
type TBlockStore = ipfs::repo::mem::MemBlockStore;
type TDataStore = ipfs::repo::mem::MemDataStore;
}
impl SwarmTypes for Types {
type TStrategy = ipfs::bitswap::strategy::AltruisticStrategy<Self>;
}
impl IpfsTypes for Types {}
2019-02-13 23:46:55 +01:00
fn main() {
2019-02-18 18:52:45 +01:00
let options = IpfsOptions::test();
env_logger::Builder::new().parse(&options.ipfs_log).init();
2019-02-18 20:49:57 +01:00
let mut ipfs = Ipfs::<Types>::new(options);
2019-02-13 23:46:55 +01:00
let block = Block::from("hello block\n");
2019-02-27 16:41:36 +01:00
let cid = Block::from("hello block2\n").cid().to_owned();
2019-02-21 22:28:59 +01:00
2019-02-25 17:15:12 +01:00
tokio::run_async(async move {
tokio::spawn_async(ipfs.start_daemon());
2019-02-21 22:28:59 +01:00
2019-02-22 13:00:28 +01:00
await!(ipfs.put_block(block)).unwrap();
2019-02-27 16:41:36 +01:00
let block = await!(ipfs.get_block(&cid)).unwrap();
2019-02-21 22:28:59 +01:00
println!("Received block with contents: {:?}",
2019-02-13 23:46:55 +01:00
String::from_utf8_lossy(&block.data()));
2019-02-25 17:15:12 +01:00
});
2019-02-13 23:46:55 +01:00
}