Skip to content

Commit

Permalink
dht: include get_peers in main loop instead of blocking on its own
Browse files Browse the repository at this point in the history
  • Loading branch information
95th committed Jul 21, 2020
1 parent 0676ec8 commit 4668403
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 81 deletions.
8 changes: 4 additions & 4 deletions dht/src/id.rs
Expand Up @@ -16,15 +16,15 @@ impl fmt::Debug for NodeId {
}

impl NodeId {
pub fn new() -> Self {
Self::default()
pub const fn new() -> Self {
Self::all(0)
}

pub fn max() -> Self {
pub const fn max() -> Self {
Self::all(u8::max_value())
}

pub fn all(b: u8) -> Self {
pub const fn all(b: u8) -> Self {
Self([b; 20])
}

Expand Down
9 changes: 8 additions & 1 deletion dht/src/main.rs
@@ -1,6 +1,7 @@
use dht::id::NodeId;
use dht::{ClientRequest, Server};
use std::time::Duration;
use tokio::sync::oneshot;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand All @@ -15,12 +16,18 @@ async fn main() -> anyhow::Result<()> {
tokio::spawn(server.run());

let info_hash = NodeId::from_hex(b"d04480dfa670f72f591439b51a9f82dcc58711b5").unwrap();
let (tx, rx) = oneshot::channel();
client
.tx
.send(ClientRequest::GetPeers(info_hash))
.send(ClientRequest::GetPeers(info_hash, tx))
.await
.unwrap();

match rx.await {
Ok(peers) => println!("Found {} peers", peers.len()),
Err(e) => println!("Error in receiving peers: {}", e),
}

tokio::time::delay_for(Duration::from_secs(10000)).await;
Ok(())
}
8 changes: 8 additions & 0 deletions dht/src/msg/txn.rs
Expand Up @@ -3,6 +3,14 @@ use ben::{Encode, Encoder};
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct TxnId(pub u16);

impl TxnId {
pub fn next_id(&mut self) -> Self {
let out = *self;
self.0 = self.0.wrapping_add(1);
out
}
}

impl Encode for TxnId {
fn encode<E: Encoder>(&self, enc: &mut E) {
enc.add_bytes(&self.0.to_be_bytes()[..]);
Expand Down

0 comments on commit 4668403

Please sign in to comment.