Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 11, 2024
1 parent 9574a7e commit 71d1704
Show file tree
Hide file tree
Showing 125 changed files with 1,547 additions and 1,500 deletions.
20 changes: 9 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[workspace]
members = [
"examples/get_metadata",
"examples/simple_torrent",
"packages/bencode",
"packages/dht",
"packages/disk",
Expand All @@ -13,33 +15,29 @@ members = [
"packages/util",
"packages/utp",
"packages/utracker",
"examples/get_metadata",
"examples/simple_torrent",
]

resolver = "2"

[workspace.package]
authors = [
"Nautilus Cyberneering <info@nautilus-cyberneering.de>, Andrew <amiller4421@gmail.com>",
]
authors = ["Nautilus Cyberneering <info@nautilus-cyberneering.de>, Andrew <amiller4421@gmail.com>"]
categories = ["network-programming", "web-programming"]
description = "A collection of crates for building applications using bittorrent technologies."
documentation = "https://github.com/torrust/bittorrent-infrastructure-project"
edition = "2021"
homepage = "https://github.com/torrust/bittorrent-infrastructure-project"
keywords = ["bittorrent"]
license = "Apache-2.0"
publish = false # until we decide where to publish.
publish = false # until we decide where to publish.
repository = "https://github.com/torrust/bittorrent-infrastructure-project"
rust-version = "1.71"
version = "1.0.0-alpha.1"

[profile.bench]
opt-level = 3
codegen-units = 1
debug = false
rpath = false
lto = false
debug-assertions = false
codegen-units = 1
panic = 'unwind'
lto = false
opt-level = 3
panic = 'unwind'
rpath = false
8 changes: 4 additions & 4 deletions examples/get_metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "get_metadata"
description = "Examples For bip-rs"
name = "get_metadata"
readme = "README.md"

authors.workspace = true
Expand All @@ -16,14 +16,14 @@ version.workspace = true
[dependencies]
dht = { path = "../../packages/dht" }
handshake = { path = "../../packages/handshake" }
metainfo = { path = "../../packages/metainfo" }
peer = { path = "../../packages/peer" }
select = { path = "../../packages/select" }
metainfo = {path ="../../packages/metainfo" }

clap = "4"
hex = "0"
futures = "0"
hex = "0"
tokio = { version = "1", features = ["full"] }
tokio-util = {version = "0", features = ["codec"]}
tokio-util = { version = "0", features = ["codec"] }
tracing = "0"
tracing-subscriber = "0"
6 changes: 2 additions & 4 deletions examples/get_metadata/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::Debug;
use std::fs::File;
use std::io::Write as _;
use std::net::SocketAddr;
use std::sync::{Arc, Once};
Expand Down Expand Up @@ -53,7 +51,7 @@ where
impl<S> HandshakerTrait for LegacyHandshaker<S>
where
S: Sink<InitiateMessage> + Send + Unpin,
S::Error: Debug,
S::Error: std::fmt::Debug,
{
type MetadataEnvelope = ();

Expand Down Expand Up @@ -340,7 +338,7 @@ async fn main() {
};

// Write the metainfo file out to the user provided path
File::create(output)
std::fs::File::create(output)
.expect("Failed to create output file")
.write_all(&metainfo.to_bytes())
.expect("Failed to write metainfo to file");
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_torrent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simple_torrent"
description = "Examples For bip-rs"
name = "simple_torrent"
readme = "README.md"

authors.workspace = true
Expand All @@ -23,6 +23,6 @@ peer = { path = "../../packages/peer" }
clap = "4"
futures = "0"
tokio = { version = "1", features = ["full"] }
tokio-util = {version = "0", features = ["codec"]}
tokio-util = { version = "0", features = ["codec"] }
tracing = "0"
tracing-subscriber = "0"
42 changes: 19 additions & 23 deletions examples/simple_torrent/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::cmp;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::io::Read as _;
use std::sync::{Arc, Once};

use disk::fs::NativeFileSystem;
Expand All @@ -10,7 +8,7 @@ use disk::{
Block, BlockMetadata, BlockMut, DiskManager, DiskManagerBuilder, DiskManagerSink, DiskManagerStream, IDiskMessage, InfoHash,
ODiskMessage,
};
use futures::channel::mpsc::{self, Receiver, Sender};
use futures::channel::mpsc;
use futures::future::Either;
use futures::lock::Mutex;
use futures::{stream, SinkExt as _, StreamExt as _};
Expand All @@ -22,8 +20,8 @@ use metainfo::{Info, Metainfo};
use peer::messages::{BitFieldMessage, HaveMessage, PeerWireProtocolMessage, PieceMessage, RequestMessage};
use peer::protocols::{NullProtocol, PeerWireProtocol};
use peer::{
PeerInfo, PeerManagerBuilder, PeerManagerInputMessage, PeerManagerOutputError, PeerManagerOutputMessage, PeerManagerSink,
PeerManagerStream, PeerProtocolCodec,
ManagerSink, ManagerStream, PeerInfo, PeerManagerBuilder, PeerManagerInputMessage, PeerManagerOutputError,
PeerManagerOutputMessage, PeerProtocolCodec,
};
use tokio::net::TcpStream;
use tokio::signal;
Expand Down Expand Up @@ -182,7 +180,7 @@ fn extract_arguments(matches: &clap::ArgMatches) -> (String, String, String) {

fn load_and_parse_torrent_file(torrent_file_path: &str) -> (Metainfo, InfoHash) {
let mut torrent_file_bytes = Vec::new();
File::open(torrent_file_path)
std::fs::File::open(torrent_file_path)
.unwrap()
.read_to_end(&mut torrent_file_bytes)
.unwrap();
Expand Down Expand Up @@ -318,10 +316,8 @@ async fn setup_handshaker() -> (TcpHandshaker, JoinSet<()>) {
.unwrap()
}

type PeerManager = peer::PeerManager<
Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>,
PeerWireProtocolMessage<NullProtocol>,
>;
type PeerManager =
peer::Manager<Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>, PeerWireProtocolMessage<NullProtocol>>;

#[allow(clippy::type_complexity)]
fn setup_peer_manager() -> PeerManager {
Expand All @@ -333,7 +329,7 @@ fn setup_peer_manager() -> PeerManager {

async fn handle_new_connections(
handshaker_receiver: HandshakerStream<TcpStream>,
peer_manager_sender: PeerManagerSink<
peer_manager_sender: ManagerSink<
Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>,
PeerWireProtocolMessage<NullProtocol>,
>,
Expand All @@ -357,13 +353,13 @@ async fn handle_new_connections(
}

async fn handle_peer_manager_messages(
mut peer_manager_receiver: PeerManagerStream<
mut peer_manager_receiver: ManagerStream<
Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>,
PeerWireProtocolMessage<NullProtocol>,
>,
info_hash: InfoHash,
disk_request_map: Arc<Mutex<HashMap<BlockMetadata, Vec<PeerInfo>>>>,
selection_sender: Sender<PeerSelectionState>,
selection_sender: mpsc::Sender<PeerSelectionState>,
disk_manager_sender: DiskManagerSink<FileHandleCache<NativeFileSystem>>,
) {
while let Some(result) = peer_manager_receiver.next().await {
Expand Down Expand Up @@ -450,8 +446,8 @@ async fn handle_peer_manager_messages(
async fn handle_disk_manager_messages(
mut disk_manager_receiver: DiskManagerStream,
disk_request_map: Arc<Mutex<HashMap<BlockMetadata, Vec<PeerInfo>>>>,
selection_sender: Sender<PeerSelectionState>,
peer_manager_sender: PeerManagerSink<
selection_sender: mpsc::Sender<PeerSelectionState>,
peer_manager_sender: ManagerSink<
Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>,
PeerWireProtocolMessage<NullProtocol>,
>,
Expand Down Expand Up @@ -500,10 +496,10 @@ async fn handle_disk_manager_messages(
}

async fn handle_existing_pieces(
mut selection_receiver: Receiver<PeerSelectionState>,
mut selection_receiver: mpsc::Receiver<PeerSelectionState>,
mut piece_requests: Vec<RequestMessage>,
mut current_pieces: usize,
) -> (Receiver<PeerSelectionState>, Vec<RequestMessage>, usize) {
) -> (mpsc::Receiver<PeerSelectionState>, Vec<RequestMessage>, usize) {
loop {
match selection_receiver.next().await {
Some(PeerSelectionState::GoodPiece(index)) => {
Expand All @@ -514,16 +510,16 @@ async fn handle_existing_pieces(
break (selection_receiver, piece_requests, current_pieces);
}
Some(message) => {
panic!("Unexpected Message Received In Selection Receiver: {message:?}");
panic!("Unexpected Message Received In Selection mpsc::Receiver: {message:?}");
}
}
}
}

#[allow(clippy::too_many_arguments)]
async fn handle_selection_messages(
mut selection_receiver: Receiver<PeerSelectionState>,
mut peer_manager_sender: PeerManagerSink<
mut selection_receiver: mpsc::Receiver<PeerSelectionState>,
mut peer_manager_sender: ManagerSink<
Framed<TcpStream, PeerProtocolCodec<PeerWireProtocol<NullProtocol>>>,
PeerWireProtocolMessage<NullProtocol>,
>,
Expand Down Expand Up @@ -584,7 +580,7 @@ async fn handle_selection_messages(
break;
} else if let Some(peer_info) = optional_peer {
let next_piece_requests = if is_unchoked {
let take_blocks = cmp::min(MAX_PENDING_BLOCKS - pending_blocks, piece_requests.len());
let take_blocks = std::cmp::min(MAX_PENDING_BLOCKS - pending_blocks, piece_requests.len());
pending_blocks += take_blocks;

piece_requests
Expand Down Expand Up @@ -636,7 +632,7 @@ fn generate_piece_requests(info: &Info, block_size: usize) -> Vec<RequestMessage
// Loop over each piece (keep subtracting total file length by piece size, use cmp::min to handle last, smaller piece)
let mut piece_index: u64 = 0;
while total_file_length != 0 {
let next_piece_length = cmp::min(total_file_length, piece_length);
let next_piece_length = std::cmp::min(total_file_length, piece_length);

// For all whole blocks, push the block index and block_size
let whole_blocks = next_piece_length / block_size as u64;
Expand Down
4 changes: 2 additions & 2 deletions packages/bencode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bencode"
description = "Efficient decoding and encoding for bencode."
keywords = ["bencode"]
name = "bencode"
readme = "README.md"

authors.workspace = true
Expand All @@ -22,5 +22,5 @@ thiserror = "1"
criterion = "0"

[[bench]]
harness = false
name = "bencode_benchmark"
harness = false
1 change: 0 additions & 1 deletion packages/bencode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! ```rust
//! extern crate bencode;
//!
//! use std::default::Default;
//! use bencode::{BencodeRef, BRefAccess, BDecodeOpt};
//!
//! fn main() {
Expand Down
1 change: 0 additions & 1 deletion packages/bencode/src/reference/bencode_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl<'a> BRefAccessExt<'a> for BencodeRef<'a> {

#[cfg(test)]
mod tests {
use std::default::Default;

use crate::access::bencode::BRefAccess;
use crate::reference::bencode_ref::BencodeRef;
Expand Down
3 changes: 1 addition & 2 deletions packages/bencode/src/reference/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::str::{self};
use std::str;

use crate::error::{BencodeParseError, BencodeParseResult};
use crate::reference::bencode_ref::{BencodeRef, Inner};
Expand Down Expand Up @@ -157,7 +157,6 @@ fn peek_byte(bytes: &[u8], pos: usize) -> BencodeParseResult<u8> {

#[cfg(test)]
mod tests {
use std::default::Default;

use crate::access::bencode::BRefAccess;
use crate::reference::bencode_ref::BencodeRef;
Expand Down
2 changes: 0 additions & 2 deletions packages/bencode/src/reference/decode_opt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::default::Default;

const DEFAULT_MAX_RECURSION: usize = 50;
const DEFAULT_CHECK_KEY_SORT: bool = false;
const DEFAULT_ENFORCE_FULL_DECODE: bool = true;
Expand Down
8 changes: 4 additions & 4 deletions packages/dht/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "dht"
description = "Implementation of the bittorrent mainline DHT"
keywords = ["dht", "distributed", "hash", "mainline", "bittorrent"]
keywords = ["bittorrent", "dht", "distributed", "hash", "mainline"]
name = "dht"
readme = "README.md"

authors.workspace = true
Expand All @@ -20,11 +20,11 @@ bencode = { path = "../bencode" }
handshake = { path = "../handshake" }
util = { path = "../util" }

chrono = "0"
crc = "3"
futures = "0"
tokio = { version = "1", features = ["full"] }
rand = "0"
chrono = "0"
thiserror = "1"
tokio = { version = "1", features = ["full"] }
tracing = "0"
tracing-subscriber = "0"
2 changes: 1 addition & 1 deletion packages/dht/examples/debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashSet;
use std::io::Read;
use std::io::Read as _;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::sync::Once;

Expand Down
6 changes: 3 additions & 3 deletions packages/dht/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;
use std::net::SocketAddr;
use std::sync::Arc;

use futures::channel::mpsc::{self, Receiver, Sender};
use futures::channel::mpsc;
use futures::SinkExt as _;
use tokio::net::UdpSocket;
use tokio::task::JoinSet;
Expand All @@ -15,7 +15,7 @@ use crate::worker::{self, DhtEvent, OneshotTask, ShutdownCause};

/// Maintains a Distributed Hash (Routing) Table.
pub struct MainlineDht {
main_task_sender: Sender<OneshotTask>,
main_task_sender: mpsc::Sender<OneshotTask>,
_tasks: JoinSet<()>,
}

Expand Down Expand Up @@ -84,7 +84,7 @@ impl MainlineDht {
/// It is important to at least monitor the DHT for shutdown events as any calls
/// after that event occurs will not be processed but no indication will be given.
#[must_use]
pub async fn events(&self) -> Receiver<DhtEvent> {
pub async fn events(&self) -> mpsc::Receiver<DhtEvent> {
let (send, recv) = mpsc::channel(1);

if let Err(e) = self.main_task_sender.clone().send(OneshotTask::RegisterSender(send)).await {
Expand Down
4 changes: 1 addition & 3 deletions packages/dht/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use bencode::BencodeConvertError;
use thiserror::Error;

Expand All @@ -11,7 +9,7 @@ pub enum DhtError {
#[error("Bencode error: {0}")]
Bencode(#[from] BencodeConvertError),
#[error("IO error: {0}")]
Io(#[from] io::Error),
Io(#[from] std::io::Error),
#[error("Node Sent An Invalid Message With Message Code {code}")]
InvalidMessage { code: String },
#[error("Node Sent Us An Invalid Response: {details}")]
Expand Down
3 changes: 1 addition & 2 deletions packages/dht/src/message/compact_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::hash::Hash;
use std::net::{Ipv4Addr, SocketAddrV4};

Expand Down Expand Up @@ -132,7 +131,7 @@ where
impl<'a, B> IntoIterator for CompactValueInfo<'a, B>
where
B: BRefAccess<BType = B> + Clone,
B::BType: PartialEq + Eq + core::hash::Hash + Debug,
B::BType: PartialEq + Eq + core::hash::Hash + std::fmt::Debug,
{
type Item = SocketAddrV4;
type IntoIter = CompactValueInfoIter<'a, B>;
Expand Down
Loading

0 comments on commit 71d1704

Please sign in to comment.