2018-11-20 10:35:22 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
//! A basic chat application demonstrating libp2p and the mDNS and floodsub protocols.
|
2018-11-20 10:35:22 +01:00
|
|
|
//!
|
2019-01-26 23:57:53 +01:00
|
|
|
//! Using two terminal windows, start two instances. If you local network allows mDNS,
|
2018-12-10 16:00:16 +01:00
|
|
|
//! they will automatically connect. Type a message in either terminal and hit return: the
|
|
|
|
//! message is sent and printed in the other terminal. Close with Ctrl-c.
|
|
|
|
//!
|
|
|
|
//! You can of course open more terminal windows and add more participants.
|
|
|
|
//! Dialing any of the other peers will propagate the new participant to all
|
|
|
|
//! chat members and everyone will receive all messages.
|
|
|
|
//!
|
|
|
|
//! # If they don't automatically connect
|
|
|
|
//!
|
|
|
|
//! If the nodes don't automatically connect, take note of the listening address of the first
|
|
|
|
//! instance and start the second with this address as the first argument. In the first terminal
|
|
|
|
//! window, run:
|
|
|
|
//!
|
|
|
|
//! ```sh
|
2018-11-20 10:35:22 +01:00
|
|
|
//! cargo run --example chat
|
|
|
|
//! ```
|
2018-12-10 16:00:16 +01:00
|
|
|
//!
|
2018-11-20 10:35:22 +01:00
|
|
|
//! It will print the PeerId and the listening address, e.g. `Listening on
|
|
|
|
//! "/ip4/0.0.0.0/tcp/24915"`
|
|
|
|
//!
|
|
|
|
//! In the second terminal window, start a new instance of the example with:
|
2018-12-10 16:00:16 +01:00
|
|
|
//!
|
|
|
|
//! ```sh
|
2018-11-20 10:35:22 +01:00
|
|
|
//! cargo run --example chat -- /ip4/127.0.0.1/tcp/24915
|
|
|
|
//! ```
|
|
|
|
//!
|
2019-01-26 23:57:53 +01:00
|
|
|
//! The two nodes then connect.
|
2018-11-20 10:35:22 +01:00
|
|
|
|
2019-11-25 10:45:04 +01:00
|
|
|
use async_std::{io, task};
|
|
|
|
use futures::{future, prelude::*};
|
2018-11-20 10:35:22 +01:00
|
|
|
use libp2p::{
|
2019-11-25 10:45:04 +01:00
|
|
|
Multiaddr,
|
2019-03-11 13:42:53 +01:00
|
|
|
PeerId,
|
2019-04-16 19:57:16 +02:00
|
|
|
Swarm,
|
2018-12-14 10:41:54 +01:00
|
|
|
NetworkBehaviour,
|
2019-03-11 13:42:53 +01:00
|
|
|
identity,
|
2019-10-10 11:31:44 +02:00
|
|
|
floodsub::{self, Floodsub, FloodsubEvent},
|
|
|
|
mdns::{Mdns, MdnsEvent},
|
|
|
|
swarm::NetworkBehaviourEventProcess
|
2018-11-20 10:35:22 +01:00
|
|
|
};
|
2019-11-25 10:45:04 +01:00
|
|
|
use std::{error::Error, task::{Context, Poll}};
|
2018-11-20 10:35:22 +01:00
|
|
|
|
2019-11-25 10:45:04 +01:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2018-11-23 14:18:30 +01:00
|
|
|
env_logger::init();
|
2018-12-04 13:54:04 +01:00
|
|
|
|
2018-11-20 10:35:22 +01:00
|
|
|
// Create a random PeerId
|
2019-03-11 13:42:53 +01:00
|
|
|
let local_key = identity::Keypair::generate_ed25519();
|
|
|
|
let local_peer_id = PeerId::from(local_key.public());
|
2019-01-26 23:57:53 +01:00
|
|
|
println!("Local peer id: {:?}", local_peer_id);
|
2018-11-20 10:35:22 +01:00
|
|
|
|
2018-12-14 10:41:54 +01:00
|
|
|
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex and Yamux protocols
|
2019-11-25 10:45:04 +01:00
|
|
|
let transport = libp2p::build_development_transport(local_key)?;
|
2018-11-20 10:35:22 +01:00
|
|
|
|
|
|
|
// Create a Floodsub topic
|
2019-10-10 11:31:44 +02:00
|
|
|
let floodsub_topic = floodsub::TopicBuilder::new("chat").build();
|
2018-11-20 10:35:22 +01:00
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
// We create a custom network behaviour that combines floodsub and mDNS.
|
|
|
|
// In the future, we want to improve libp2p to make this easier to do.
|
|
|
|
#[derive(NetworkBehaviour)]
|
2019-10-10 11:31:44 +02:00
|
|
|
struct MyBehaviour<TSubstream: AsyncRead + AsyncWrite> {
|
|
|
|
floodsub: Floodsub<TSubstream>,
|
|
|
|
mdns: Mdns<TSubstream>,
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
|
2019-10-10 11:31:44 +02:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<MdnsEvent> for MyBehaviour<TSubstream> {
|
|
|
|
fn inject_event(&mut self, event: MdnsEvent) {
|
2019-01-26 23:57:53 +01:00
|
|
|
match event {
|
2019-11-25 10:45:04 +01:00
|
|
|
MdnsEvent::Discovered(list) =>
|
2019-01-26 23:57:53 +01:00
|
|
|
for (peer, _) in list {
|
|
|
|
self.floodsub.add_node_to_partial_view(peer);
|
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
MdnsEvent::Expired(list) =>
|
2019-01-26 23:57:53 +01:00
|
|
|
for (peer, _) in list {
|
|
|
|
if !self.mdns.has_node(&peer) {
|
|
|
|
self.floodsub.remove_node_from_partial_view(&peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 15:21:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:31:44 +02:00
|
|
|
impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<FloodsubEvent> for MyBehaviour<TSubstream> {
|
2018-12-10 16:00:16 +01:00
|
|
|
// Called when `floodsub` produces an event.
|
2019-10-10 11:31:44 +02:00
|
|
|
fn inject_event(&mut self, message: FloodsubEvent) {
|
|
|
|
if let FloodsubEvent::Message(message) = message {
|
2019-01-07 13:42:47 +01:00
|
|
|
println!("Received: '{:?}' from {:?}", String::from_utf8_lossy(&message.data), message.source);
|
|
|
|
}
|
2018-12-10 16:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:35:22 +01:00
|
|
|
// Create a Swarm to manage peers and events
|
|
|
|
let mut swarm = {
|
2019-11-25 10:45:04 +01:00
|
|
|
let mdns = task::block_on(Mdns::new())?;
|
2018-12-10 16:00:16 +01:00
|
|
|
let mut behaviour = MyBehaviour {
|
2019-10-10 11:31:44 +02:00
|
|
|
floodsub: Floodsub::new(local_peer_id.clone()),
|
2019-11-25 10:45:04 +01:00
|
|
|
mdns
|
2018-12-10 16:00:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
behaviour.floodsub.subscribe(floodsub_topic.clone());
|
2019-10-10 11:31:44 +02:00
|
|
|
Swarm::new(transport, behaviour, local_peer_id)
|
2018-11-20 10:35:22 +01:00
|
|
|
};
|
|
|
|
|
2018-12-10 16:00:16 +01:00
|
|
|
// Reach out to another node if specified
|
2018-11-20 10:35:22 +01:00
|
|
|
if let Some(to_dial) = std::env::args().nth(1) {
|
2019-11-25 10:45:04 +01:00
|
|
|
let addr: Multiaddr = to_dial.parse()?;
|
|
|
|
Swarm::dial_addr(&mut swarm, addr)?;
|
|
|
|
println!("Dialed {:?}", to_dial)
|
2018-11-20 10:35:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read full lines from stdin
|
2019-11-25 10:45:04 +01:00
|
|
|
let mut stdin = io::BufReader::new(io::stdin()).lines();
|
2018-11-20 10:35:22 +01:00
|
|
|
|
2019-04-16 19:57:16 +02:00
|
|
|
// Listen on all interfaces and whatever port the OS assigns
|
2019-11-25 10:45:04 +01:00
|
|
|
Swarm::listen_on(&mut swarm, "/ip4/0.0.0.0/tcp/0".parse()?)?;
|
2019-04-16 19:57:16 +02:00
|
|
|
|
2018-11-20 10:35:22 +01:00
|
|
|
// Kick it off
|
2019-04-16 19:57:16 +02:00
|
|
|
let mut listening = false;
|
2019-11-25 10:45:04 +01:00
|
|
|
task::block_on(future::poll_fn(move |cx: &mut Context| {
|
2018-11-20 10:35:22 +01:00
|
|
|
loop {
|
2019-11-25 10:45:04 +01:00
|
|
|
match stdin.try_poll_next_unpin(cx)? {
|
|
|
|
Poll::Ready(Some(line)) => swarm.floodsub.publish(&floodsub_topic, line.as_bytes()),
|
|
|
|
Poll::Ready(None) => panic!("Stdin closed"),
|
|
|
|
Poll::Pending => break
|
|
|
|
}
|
2018-11-20 10:35:22 +01:00
|
|
|
}
|
|
|
|
loop {
|
2019-11-25 10:45:04 +01:00
|
|
|
match swarm.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(Some(event)) => println!("{:?}", event),
|
|
|
|
Poll::Ready(None) => return Poll::Ready(Ok(())),
|
|
|
|
Poll::Pending => {
|
2019-04-16 19:57:16 +02:00
|
|
|
if !listening {
|
|
|
|
if let Some(a) = Swarm::listeners(&swarm).next() {
|
|
|
|
println!("Listening on {:?}", a);
|
|
|
|
listening = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-11-20 10:35:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
Poll::Pending
|
|
|
|
}))
|
2018-11-20 10:35:22 +01:00
|
|
|
}
|