mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-19 04:51:22 +00:00
protocols/mdns: Update to if-watch v2.0.0 (#2978)
This commit is contained in:
@ -105,7 +105,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// Create a Swarm to manage peers and events.
|
// Create a Swarm to manage peers and events.
|
||||||
let mut swarm = {
|
let mut swarm = {
|
||||||
let mdns = TokioMdns::new(Default::default()).await?;
|
let mdns = TokioMdns::new(Default::default())?;
|
||||||
let mut behaviour = MyBehaviour {
|
let mut behaviour = MyBehaviour {
|
||||||
floodsub: Floodsub::new(peer_id),
|
floodsub: Floodsub::new(peer_id),
|
||||||
mdns,
|
mdns,
|
||||||
|
@ -49,7 +49,7 @@
|
|||||||
//!
|
//!
|
||||||
//! The two nodes then connect.
|
//! The two nodes then connect.
|
||||||
|
|
||||||
use async_std::{io, task};
|
use async_std::io;
|
||||||
use futures::{
|
use futures::{
|
||||||
prelude::{stream::StreamExt, *},
|
prelude::{stream::StreamExt, *},
|
||||||
select,
|
select,
|
||||||
@ -108,7 +108,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// Create a Swarm to manage peers and events
|
// Create a Swarm to manage peers and events
|
||||||
let mut swarm = {
|
let mut swarm = {
|
||||||
let mdns = task::block_on(Mdns::new(MdnsConfig::default()))?;
|
let mdns = Mdns::new(MdnsConfig::default())?;
|
||||||
let mut behaviour = MyBehaviour {
|
let mut behaviour = MyBehaviour {
|
||||||
floodsub: Floodsub::new(local_peer_id),
|
floodsub: Floodsub::new(local_peer_id),
|
||||||
mdns,
|
mdns,
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
//!
|
//!
|
||||||
//! 4. Close with Ctrl-c.
|
//! 4. Close with Ctrl-c.
|
||||||
|
|
||||||
use async_std::{io, task};
|
use async_std::io;
|
||||||
use futures::{prelude::*, select};
|
use futures::{prelude::*, select};
|
||||||
use libp2p::kad::record::store::MemoryStore;
|
use libp2p::kad::record::store::MemoryStore;
|
||||||
use libp2p::kad::{
|
use libp2p::kad::{
|
||||||
@ -96,7 +96,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
// Create a Kademlia behaviour.
|
// Create a Kademlia behaviour.
|
||||||
let store = MemoryStore::new(local_peer_id);
|
let store = MemoryStore::new(local_peer_id);
|
||||||
let kademlia = Kademlia::new(local_peer_id, store);
|
let kademlia = Kademlia::new(local_peer_id, store);
|
||||||
let mdns = task::block_on(Mdns::new(MdnsConfig::default()))?;
|
let mdns = Mdns::new(MdnsConfig::default())?;
|
||||||
let behaviour = MyBehaviour { kademlia, mdns };
|
let behaviour = MyBehaviour { kademlia, mdns };
|
||||||
Swarm::new(transport, behaviour, local_peer_id)
|
Swarm::new(transport, behaviour, local_peer_id)
|
||||||
};
|
};
|
||||||
|
@ -40,7 +40,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let transport = libp2p::development_transport(id_keys).await?;
|
let transport = libp2p::development_transport(id_keys).await?;
|
||||||
|
|
||||||
// Create an MDNS network behaviour.
|
// Create an MDNS network behaviour.
|
||||||
let behaviour = Mdns::new(MdnsConfig::default()).await?;
|
let behaviour = Mdns::new(MdnsConfig::default())?;
|
||||||
|
|
||||||
// Create a Swarm that establishes connections through the given transport.
|
// Create a Swarm that establishes connections through the given transport.
|
||||||
// Note that the MDNS behaviour itself will not actually inititiate any connections,
|
// Note that the MDNS behaviour itself will not actually inititiate any connections,
|
||||||
|
@ -10,9 +10,12 @@
|
|||||||
|
|
||||||
- Removed the `lazy_static` dependency. See [PR 2977].
|
- Removed the `lazy_static` dependency. See [PR 2977].
|
||||||
|
|
||||||
|
- Update to `if-watch` `v2.0.0` and thus the `async` method `Mdns::new` and `TokioMdns::new` becomes synchronous. See [PR 2978].
|
||||||
|
|
||||||
[PR 2918]: https://github.com/libp2p/rust-libp2p/pull/2918
|
[PR 2918]: https://github.com/libp2p/rust-libp2p/pull/2918
|
||||||
[PR 2939]: https://github.com/libp2p/rust-libp2p/pull/2939
|
[PR 2939]: https://github.com/libp2p/rust-libp2p/pull/2939
|
||||||
[PR 2977]: https://github.com/libp2p/rust-libp2p/pull/2977
|
[PR 2977]: https://github.com/libp2p/rust-libp2p/pull/2977
|
||||||
|
[PR 2978]: https://github.com/libp2p/rust-libp2p/pull/2978
|
||||||
|
|
||||||
# 0.40.0
|
# 0.40.0
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
data-encoding = "2.3.2"
|
data-encoding = "2.3.2"
|
||||||
dns-parser = "0.8.0"
|
dns-parser = "0.8.0"
|
||||||
futures = "0.3.13"
|
futures = "0.3.13"
|
||||||
if-watch = "1.1.1"
|
if-watch = "2.0.0"
|
||||||
libp2p-core = { version = "0.37.0", path = "../../core" }
|
libp2p-core = { version = "0.37.0", path = "../../core" }
|
||||||
libp2p-swarm = { version = "0.40.0", path = "../../swarm" }
|
libp2p-swarm = { version = "0.40.0", path = "../../swarm" }
|
||||||
log = "0.4.14"
|
log = "0.4.14"
|
||||||
|
@ -25,7 +25,6 @@ mod timer;
|
|||||||
use self::iface::InterfaceState;
|
use self::iface::InterfaceState;
|
||||||
use crate::behaviour::{socket::AsyncSocket, timer::Builder};
|
use crate::behaviour::{socket::AsyncSocket, timer::Builder};
|
||||||
use crate::MdnsConfig;
|
use crate::MdnsConfig;
|
||||||
use futures::prelude::*;
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use if_watch::{IfEvent, IfWatcher};
|
use if_watch::{IfEvent, IfWatcher};
|
||||||
use libp2p_core::transport::ListenerId;
|
use libp2p_core::transport::ListenerId;
|
||||||
@ -81,8 +80,8 @@ where
|
|||||||
T: Builder,
|
T: Builder,
|
||||||
{
|
{
|
||||||
/// Builds a new `Mdns` behaviour.
|
/// Builds a new `Mdns` behaviour.
|
||||||
pub async fn new(config: MdnsConfig) -> io::Result<Self> {
|
pub fn new(config: MdnsConfig) -> io::Result<Self> {
|
||||||
let if_watch = if_watch::IfWatcher::new().await?;
|
let if_watch = if_watch::IfWatcher::new()?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
config,
|
config,
|
||||||
if_watch,
|
if_watch,
|
||||||
@ -169,7 +168,7 @@ where
|
|||||||
params: &mut impl PollParameters,
|
params: &mut impl PollParameters,
|
||||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, dummy::ConnectionHandler>> {
|
) -> Poll<NetworkBehaviourAction<Self::OutEvent, dummy::ConnectionHandler>> {
|
||||||
// Poll ifwatch.
|
// Poll ifwatch.
|
||||||
while let Poll::Ready(event) = Pin::new(&mut self.if_watch).poll(cx) {
|
while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) {
|
||||||
match event {
|
match event {
|
||||||
Ok(IfEvent::Up(inet)) => {
|
Ok(IfEvent::Up(inet)) => {
|
||||||
let addr = inet.addr();
|
let addr = inet.addr();
|
||||||
|
@ -61,7 +61,7 @@ async fn create_swarm(config: MdnsConfig) -> Result<Swarm<Mdns>, Box<dyn Error>>
|
|||||||
let id_keys = identity::Keypair::generate_ed25519();
|
let id_keys = identity::Keypair::generate_ed25519();
|
||||||
let peer_id = PeerId::from(id_keys.public());
|
let peer_id = PeerId::from(id_keys.public());
|
||||||
let transport = libp2p::development_transport(id_keys).await?;
|
let transport = libp2p::development_transport(id_keys).await?;
|
||||||
let behaviour = Mdns::new(config).await?;
|
let behaviour = Mdns::new(config)?;
|
||||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||||
Ok(swarm)
|
Ok(swarm)
|
||||||
|
@ -57,7 +57,7 @@ async fn create_swarm(config: MdnsConfig) -> Result<Swarm<TokioMdns>, Box<dyn Er
|
|||||||
let id_keys = identity::Keypair::generate_ed25519();
|
let id_keys = identity::Keypair::generate_ed25519();
|
||||||
let peer_id = PeerId::from(id_keys.public());
|
let peer_id = PeerId::from(id_keys.public());
|
||||||
let transport = libp2p::tokio_development_transport(id_keys)?;
|
let transport = libp2p::tokio_development_transport(id_keys)?;
|
||||||
let behaviour = TokioMdns::new(config).await?;
|
let behaviour = TokioMdns::new(config)?;
|
||||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||||
Ok(swarm)
|
Ok(swarm)
|
||||||
|
Reference in New Issue
Block a user