Skip to content

Commit

Permalink
Merge pull request #61 from nervosnetwork/identify-protocol
Browse files Browse the repository at this point in the history
Add identify protocol
  • Loading branch information
TheWaWaR authored Mar 14, 2019
2 parents af223da + 5c341eb commit 065ccd1
Show file tree
Hide file tree
Showing 14 changed files with 923 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ fnv = "1.0"
discovery = { path = "discovery", package="tentacle-discovery" }
crossbeam-channel = "0.3.6"
ping = { path = "ping", package = "tentacle-ping" }
identify = { path = "identify", package = "tentacle-identify" }
generic-channel = { version = "0.2.0", features = ["all"] }
systemstat = "0.1.3"

[target.'cfg(unix)'.dev-dependencies]
nix = "0.13.0"

[workspace]
members = ["yamux", "secio", "discovery", "ping", "bench"]
members = ["yamux", "secio", "discovery", "identify", "ping", "bench"]
2 changes: 1 addition & 1 deletion discovery/src/gen_proto.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh
# flatc version 1.10.0

flatc --rust message.fbs
flatc --rust protocol.fbs
6 changes: 3 additions & 3 deletions discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ use p2p::{
use rand::seq::SliceRandom;

mod addr;
mod message;
mod protocol;
mod substream;

#[rustfmt::skip]
#[allow(clippy::all)]
mod message_generated;
mod protocol_generated;

pub use crate::{
addr::{AddrKnown, AddressManager, MisbehaveResult, Misbehavior, RawAddr},
message::{DiscoveryMessage, Node, Nodes},
protocol::{DiscoveryMessage, Node, Nodes},
substream::{Direction, Substream, SubstreamKey, SubstreamValue},
};

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion discovery/src/message.rs → discovery/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::codec::length_delimited::LengthDelimitedCodec;
use tokio::codec::{Decoder, Encoder};

use crate::addr::RawAddr;
use crate::message_generated::p2p::discovery::{
use crate::protocol_generated::p2p::discovery::{
BytesBuilder, DiscoveryMessage as FbsDiscoveryMessage, DiscoveryMessageBuilder,
DiscoveryPayload as FbsDiscoveryPayload, GetNodes as FbsGetNodes, GetNodesBuilder, NodeBuilder,
Nodes as FbsNodes, NodesBuilder,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion discovery/src/substream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio::timer::Interval;

use crate::addr::{AddrKnown, AddressManager, Misbehavior, RawAddr};
use crate::message::{DiscoveryCodec, DiscoveryMessage, Node, Nodes};
use crate::protocol::{DiscoveryCodec, DiscoveryMessage, Node, Nodes};

// FIXME: should be a more high level version number
const VERSION: u32 = 0;
Expand Down
79 changes: 79 additions & 0 deletions examples/id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use env_logger;
use log::debug;

use futures::{future::lazy, prelude::*};
use identify::{AddrManager, IdentifyProtocol, MisbehaveResult, Misbehavior};
use tentacle::{
builder::{MetaBuilder, ServiceBuilder},
context::ServiceContext,
multiaddr::Multiaddr,
secio::{PeerId, SecioKeyPair},
service::{DialProtocol, ProtocolHandle, ServiceError, ServiceEvent},
traits::ServiceHandle,
};

fn main() {
env_logger::init();
let addr_mgr = SimpleAddrManager {};
let protocol = MetaBuilder::default()
.id(1)
.service_handle(move || {
ProtocolHandle::Callback(Box::new(IdentifyProtocol::new(1, addr_mgr)))
})
.build();
if std::env::args().nth(1) == Some("server".to_string()) {
debug!("Starting server ......");
let mut service = ServiceBuilder::default()
.insert_protocol(protocol)
.key_pair(SecioKeyPair::secp256k1_generated())
.forever(true)
.build(SimpleHandler {});
let _ = service.dial(
"/ip4/127.0.0.1/tcp/1338".parse().unwrap(),
DialProtocol::All,
);
let _ = service.listen("/ip4/127.0.0.1/tcp/1337".parse().unwrap());
tokio::run(lazy(|| service.for_each(|_| Ok(()))))
} else {
debug!("Starting client ......");
let mut service = ServiceBuilder::default()
.insert_protocol(protocol)
.key_pair(SecioKeyPair::secp256k1_generated())
.forever(true)
.build(SimpleHandler {});
let _ = service.dial(
"/ip4/127.0.0.1/tcp/1337".parse().unwrap(),
DialProtocol::All,
);
let _ = service.listen("/ip4/127.0.0.1/tcp/1338".parse().unwrap());
tokio::run(lazy(|| service.for_each(|_| Ok(()))))
}
}

#[derive(Clone)]
struct SimpleAddrManager {}

impl AddrManager for SimpleAddrManager {
/// Add remote peer's listen addresses
fn add_listen_addrs(&mut self, _peer: &PeerId, _addrs: Vec<Multiaddr>) {}
/// Add our address observed by remote peer
fn add_observed_addr(&mut self, _peer: &PeerId, _addr: Multiaddr) -> MisbehaveResult {
MisbehaveResult::Continue
}
/// Report misbehavior
fn misbehave(&mut self, _peer: &PeerId, _kind: Misbehavior) -> MisbehaveResult {
MisbehaveResult::Disconnect
}
}

struct SimpleHandler {}

impl ServiceHandle for SimpleHandler {
fn handle_error(&mut self, _env: &mut ServiceContext, error: ServiceError) {
debug!("service error: {:?}", error);
}

fn handle_event(&mut self, _env: &mut ServiceContext, event: ServiceEvent) {
debug!("service event: {:?}", event);
}
}
12 changes: 12 additions & 0 deletions identify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "tentacle-identify"
version = "0.1.0"
authors = ["Qian Linfeng <[email protected]>"]
edition = "2018"

[dependencies]
p2p = { path = "..", package = "tentacle" }
bytes = "0.4"
flatbuffers = "0.5.0"
tokio = "0.1"
log = "0.4"
4 changes: 4 additions & 0 deletions identify/src/gen_proto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/sh
# flatc version 1.10.0

flatc --rust protocol.fbs
Loading

0 comments on commit 065ccd1

Please sign in to comment.