Skip to content

Commit

Permalink
refactor: [#639] UDP client. Extract command handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 1, 2024
1 parent 011fdb7 commit a2e123c
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
use std::str::FromStr;

use anyhow::Context;
use aquatic_udp_protocol::Response::{AnnounceIpv4, AnnounceIpv6, Scrape};
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Scrape};
use aquatic_udp_protocol::{Port, TransactionId};
use clap::{Parser, Subcommand};
use log::{debug, LevelFilter};
Expand Down Expand Up @@ -110,33 +110,11 @@ pub async fn run() -> anyhow::Result<()> {
Command::Announce {
tracker_socket_addr,
info_hash,
} => {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

let bound_to = client.bind_and_connect(ASSIGNED_BY_OS, &tracker_socket_addr).await?;

let connection_id = client.send_connection_request(transaction_id).await?;

client
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
.await?
}
} => handle_announce(&tracker_socket_addr, &info_hash).await?,
Command::Scrape {
tracker_socket_addr,
info_hashes,
} => {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

let _bound_to = client.bind_and_connect(ASSIGNED_BY_OS, &tracker_socket_addr).await?;

let connection_id = client.send_connection_request(transaction_id).await?;

client.send_scrape_request(connection_id, transaction_id, info_hashes).await?
}
} => handle_scrape(&tracker_socket_addr, &info_hashes).await?,
};

match response {
Expand Down Expand Up @@ -201,6 +179,34 @@ fn setup_logging(level: LevelFilter) {
debug!("logging initialized.");
}

async fn handle_announce(tracker_socket_addr: &SocketAddr, info_hash: &TorrustInfoHash) -> anyhow::Result<Response> {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

let bound_to = client.bind_and_connect(ASSIGNED_BY_OS, tracker_socket_addr).await?;

let connection_id = client.send_connection_request(transaction_id).await?;

client
.send_announce_request(connection_id, transaction_id, *info_hash, Port(bound_to.port()))
.await
}

async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustInfoHash]) -> anyhow::Result<Response> {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

let _bound_to = client.bind_and_connect(ASSIGNED_BY_OS, tracker_socket_addr).await?;

let connection_id = client.send_connection_request(transaction_id).await?;

client
.send_scrape_request(connection_id, transaction_id, info_hashes.to_vec())
.await
}

fn parse_socket_addr(tracker_socket_addr_str: &str) -> anyhow::Result<SocketAddr> {
debug!("Tracker socket address: {tracker_socket_addr_str:#?}");

Expand Down

0 comments on commit a2e123c

Please sign in to comment.