Refactor architecture.
This commit is contained in:
parent
b766e73e24
commit
701dcb1d1c
@ -1,7 +1,9 @@
|
||||
//! Block
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Block payload type
|
||||
pub type Data = Arc<Vec<u8>>;
|
||||
/// Block content identifier
|
||||
pub type Cid = Arc<cid::Cid>;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -1,21 +1,17 @@
|
||||
use crate::bitswap::Bitswap;
|
||||
use crate::block::{Block, Cid};
|
||||
use crate::repo::Repo;
|
||||
use futures::prelude::*;
|
||||
use futures::try_ready;
|
||||
use std::io::Error;
|
||||
|
||||
pub struct BlockFuture {
|
||||
cid: Cid,
|
||||
repo: Repo,
|
||||
bitswap: Bitswap,
|
||||
cid: Cid,
|
||||
}
|
||||
|
||||
impl BlockFuture {
|
||||
pub fn new(repo: Repo, bitswap: Bitswap, cid: Cid) -> Self {
|
||||
pub fn new(repo: Repo, cid: Cid) -> Self {
|
||||
BlockFuture {
|
||||
repo,
|
||||
bitswap,
|
||||
cid,
|
||||
}
|
||||
}
|
||||
@ -27,7 +23,6 @@ impl Future for BlockFuture {
|
||||
|
||||
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
|
||||
self.repo.get(&self.cid).map_or_else(|| {
|
||||
try_ready!(self.bitswap.poll());
|
||||
Ok(Async::NotReady)
|
||||
}, |block| {
|
||||
Ok(Async::Ready(block))
|
||||
|
29
src/lib.rs
29
src/lib.rs
@ -1,6 +1,8 @@
|
||||
//! IPFS node implementation
|
||||
#![deny(missing_docs)]
|
||||
#![deny(warnings)]
|
||||
#![feature(drain_filter)]
|
||||
use libp2p::secio::SecioKeyPair;
|
||||
|
||||
mod bitswap;
|
||||
pub mod block;
|
||||
@ -8,7 +10,7 @@ mod future;
|
||||
mod p2p;
|
||||
mod repo;
|
||||
|
||||
use self::bitswap::Bitswap;
|
||||
use self::bitswap::{Bitswap, strategy::AltruisticStrategy, Strategy};
|
||||
pub use self::block::{Block, Cid};
|
||||
use self::future::BlockFuture;
|
||||
use self::repo::Repo;
|
||||
@ -16,30 +18,43 @@ use self::repo::Repo;
|
||||
/// Ipfs struct creates a new IPFS node and is the main entry point
|
||||
/// for interacting with IPFS.
|
||||
pub struct Ipfs {
|
||||
bitswap: Bitswap,
|
||||
repo: Repo,
|
||||
bitswap: Bitswap<AltruisticStrategy>,
|
||||
}
|
||||
|
||||
impl Ipfs {
|
||||
/// Creates a new ipfs node.
|
||||
pub fn new() -> Self {
|
||||
let repo = Repo::new();
|
||||
let local_key = SecioKeyPair::ed25519_generated().unwrap();
|
||||
let strategy = AltruisticStrategy::new(repo.clone());
|
||||
let bitswap = Bitswap::new(local_key, strategy);
|
||||
|
||||
Ipfs {
|
||||
bitswap: Bitswap::new(),
|
||||
repo: Repo::new(),
|
||||
repo,
|
||||
bitswap,
|
||||
}
|
||||
}
|
||||
|
||||
/// Puts a block into the ipfs repo.
|
||||
pub fn put_block(&mut self, block: Block) -> Cid {
|
||||
self.repo.put(block)
|
||||
let cid = self.repo.put(block);
|
||||
self.bitswap.provide_block(&cid);
|
||||
cid
|
||||
}
|
||||
|
||||
/// Retrives a block from the ipfs repo.
|
||||
pub fn get_block(&mut self, cid: Cid) -> BlockFuture {
|
||||
if !self.repo.contains(&cid) {
|
||||
self.bitswap.get_block(cid.clone());
|
||||
self.bitswap.want_block(cid.clone());
|
||||
}
|
||||
BlockFuture::new(self.repo.clone(), self.bitswap.clone(), cid)
|
||||
BlockFuture::new(self.repo.clone(), cid)
|
||||
}
|
||||
|
||||
/// Remove block from the ipfs repo.
|
||||
pub fn remove_block(&mut self, cid: Cid) {
|
||||
self.repo.remove(&cid);
|
||||
self.bitswap.stop_providing_block(&cid);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,32 +1,23 @@
|
||||
//! P2P handling for IPFS nodes.
|
||||
use libp2p::core::Swarm;
|
||||
use libp2p::secio::SecioKeyPair;
|
||||
pub use libp2p::secio::SecioKeyPair;
|
||||
|
||||
mod behaviour;
|
||||
mod transport;
|
||||
|
||||
/// IPFS Service
|
||||
pub struct Service {
|
||||
/// The swarm.
|
||||
pub swarm: Swarm<transport::TTransport, behaviour::TBehaviour>
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Creates a new IPFS Service.
|
||||
pub fn new() -> Self {
|
||||
// Create a random key for ourselves.
|
||||
let local_key = SecioKeyPair::ed25519_generated().unwrap();
|
||||
let local_peer_id = local_key.to_peer_id();
|
||||
|
||||
// Set up an encrypted TCP transport over the Mplex protocol.
|
||||
let transport = transport::build_transport(local_key);
|
||||
|
||||
// Create a Kademlia behaviour
|
||||
let behaviour = behaviour::build_behaviour(local_peer_id.clone());
|
||||
|
||||
// Create a Swarm
|
||||
let swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
|
||||
Service { swarm }
|
||||
}
|
||||
pub type Swarm = libp2p::core::Swarm<transport::TTransport, behaviour::TBehaviour>;
|
||||
|
||||
/// Creates a new IPFS swarm.
|
||||
pub fn create_swarm(
|
||||
local_private_key: SecioKeyPair,
|
||||
) -> Swarm {
|
||||
let local_peer_id = local_private_key.to_peer_id();
|
||||
|
||||
// Set up an encrypted TCP transport over the Mplex protocol.
|
||||
let transport = transport::build_transport(local_private_key);
|
||||
|
||||
// Create a Kademlia behaviour
|
||||
let behaviour = behaviour::build_behaviour(local_peer_id.clone());
|
||||
|
||||
// Create a Swarm
|
||||
libp2p::core::Swarm::new(transport, behaviour, local_peer_id)
|
||||
}
|
||||
|
@ -31,4 +31,8 @@ impl Repo {
|
||||
.insert(cid.clone(), block);
|
||||
cid
|
||||
}
|
||||
|
||||
pub fn remove(&self, cid: &Cid) {
|
||||
self.blocks.lock().unwrap().remove(cid);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user