forked from dojoengine/dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |