Skip to content

Commit

Permalink
feat: start writing libp2p server
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Jan 17, 2024
1 parent de5bbe8 commit 2fc5071
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/torii/libp2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "torii-libp2p"
edition.workspace = true
license.workspace = true
license-file.workspace = true
repository.workspace = true
version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libp2p = { path = "0.53.2", features = [ "tokio", "gossipsub", "mdns", "noise", "macros", "tcp", "yamux", "quic"] }
1 change: 1 addition & 0 deletions crates/torii/libp2p/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod server;
45 changes: 45 additions & 0 deletions crates/torii/libp2p/src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use libp2p::{gossipsub, mdns, noise, swarm::NetworkBehaviour, swarm::SwarmEvent, tcp, yamux};

use std::collections::HashMap;

#[derive(NetworkBehaviour)]
pub struct Behaviour {
kademlia: Kademlia<MemoryStore>,
rooms: HashMap<String, Vec<PeerId>>,
}

impl NetworkBehaviourEventProcess<KademliaEvent> for Behaviour {
fn inject_event(&mut self, event: KademliaEvent) {
match event {
KademliaEvent::UnroutedQuery { .. } => {}
KademliaEvent::RoutableQuery { .. } => {}
KademliaEvent::RoutingUpdated { .. } => {}
KademliaEvent::RoutingUpdateFailed { .. } => {}
KademliaEvent::QueryResult { .. } => {}
}
}
}

pub fn new() -> Swarm<Behaviour> {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());

let transport = DnsConfig::new(
TcpConfig::new()
.upgrade(libp2p::core::upgrade::Version::V1)
.authenticate(NoiseConfig::xx(Keypair::new().into_authentic(&local_key)).unwrap())
.multiplex(libp2p::yamux::Config::default())
.boxed(),
);

let store = MemoryStore::new(local_peer_id.clone());
let kademlia = Kademlia::with_config(local_peer_id.clone(), store, KademliaConfig::default());

let behaviour = Behaviour { kademlia, rooms: HashMap::new() };

SwarmBuilder::new(transport, behaviour, local_peer_id)
.executor(Box::new(|fut| {
async_std::task::spawn(fut);
}))
.build()
}

0 comments on commit 2fc5071

Please sign in to comment.