Skip to content

Commit

Permalink
Merge pull request #8 from wetware/feat/ipfs
Browse files Browse the repository at this point in the history
Initialize IPFS client
  • Loading branch information
mikelsr authored Aug 6, 2024
2 parents a1adbe4 + 43e0d3f commit 4744236
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = [
"ww_net",
"ww_proc"
]
members = ["ww_net", "ww_proc"]

[dependencies]
anyhow = "1"
futures = "0.3.29"
ipfs-api-backend-hyper = "0.6"
libp2p = { version = "0.53.2", features = ["full"] }
rand = "0.8"
tokio = { version = "1.36", features = ["full"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
ww_net = {path = "ww_net"}
ww_net = { path = "ww_net" }
18 changes: 13 additions & 5 deletions src/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::env;

use libp2p::{identity, kad};
use libp2p::{identity, kad, Multiaddr};

// Configuration
pub trait Cfg {
// ID keys uniqely identifying the node.
fn id_keys(&self) -> identity::Keypair;
// Name of the protocol used to identify the node through libpb Identify.
fn identify_protocol(&self) -> String;
// Multiaddress of the IPFS node.
fn ipfs_addr(&self) -> Multiaddr;
// Server or Client. Defaults to server.
fn kad_mode(&self) -> kad::Mode;
// Multiaddress the node listens on.
fn listen_addr(&self) -> String;
fn listen_addr(&self) -> Multiaddr;
// Peer ID of the node. Derived from the public key in id_keys().
fn peer_id(&self) -> identity::PeerId;
}
Expand All @@ -20,7 +22,8 @@ pub trait Cfg {
pub struct DefaultCfg {
id_keys: identity::Keypair,
identify_protocol: String,
listen_addr: String,
ipfs_addr: Multiaddr,
listen_addr: Multiaddr,
}

impl DefaultCfg {
Expand All @@ -29,7 +32,8 @@ impl DefaultCfg {
Self {
id_keys: identity::Keypair::generate_ed25519(),
identify_protocol: "/ww/identify/0.0.1".to_owned(),
listen_addr: "/ip4/0.0.0.0/tcp/0".to_owned(),
ipfs_addr: "/ip4/127.0.0.1/tcp/5001".to_owned().parse().unwrap(),
listen_addr: "/ip4/0.0.0.0/tcp/0".to_owned().parse().unwrap(),
}
}

Expand All @@ -45,7 +49,11 @@ impl Cfg for DefaultCfg {
self.identify_protocol.to_owned()
}

fn listen_addr(&self) -> String {
fn ipfs_addr(&self) -> Multiaddr {
self.ipfs_addr.to_owned()
}

fn listen_addr(&self) -> Multiaddr {
self.listen_addr.to_owned()
}

Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{error::Error, time::Duration};

use anyhow::Result;
use ipfs_api_backend_hyper::{IpfsClient, TryFromUri};
use libp2p::{identify, kad, mdns, noise, ping, swarm, tcp, yamux};
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -67,7 +68,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
swarm.behaviour_mut().kad.set_mode(Some(config.kad_mode()));

// Tell the swarm to listen on all interfaces and a random, OS-assigned port.
swarm.listen_on(config.listen_addr().parse()?)?;
swarm.listen_on(config.listen_addr())?;

// The IPFS library we are using, ferristseng/rust-ipfs-api, requires multiformats::Multiaddr.
let ipfs_client = IpfsClient::from_multiaddr(config.ipfs_addr().to_string().parse().unwrap());
assert!(ipfs_client.is_ok());

loop {
match swarm.select_next_some().await {
Expand Down

0 comments on commit 4744236

Please sign in to comment.