Skip to content

Commit 4618f70

Browse files
committed
refactor: exatract response builders for UDP handlers
1 parent 91525af commit 4618f70

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/servers/udp/handlers/announce.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use bittorrent_primitives::info_hash::InfoHash;
1111
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
1212
use bittorrent_tracker_core::whitelist;
1313
use torrust_tracker_configuration::Core;
14+
use torrust_tracker_primitives::core::AnnounceData;
1415
use tracing::{instrument, Level};
1516
use zerocopy::network_endian::I32;
1617

@@ -41,7 +42,7 @@ pub async fn handle_announce(
4142

4243
tracing::trace!("handle announce");
4344

44-
let response = udp_tracker_core::services::announce::handle_announce(
45+
let announce_data = udp_tracker_core::services::announce::handle_announce(
4546
remote_addr,
4647
request,
4748
announce_handler,
@@ -52,18 +53,25 @@ pub async fn handle_announce(
5253
.await
5354
.map_err(|e| (e.into(), request.transaction_id))?;
5455

55-
// todo: extract `build_response` function.
56+
Ok(build_response(remote_addr, request, core_config, &announce_data))
57+
}
5658

59+
fn build_response(
60+
remote_addr: SocketAddr,
61+
request: &AnnounceRequest,
62+
core_config: &Arc<Core>,
63+
announce_data: &AnnounceData,
64+
) -> Response {
5765
#[allow(clippy::cast_possible_truncation)]
5866
if remote_addr.is_ipv4() {
5967
let announce_response = AnnounceResponse {
6068
fixed: AnnounceResponseFixedData {
6169
transaction_id: request.transaction_id,
6270
announce_interval: AnnounceInterval(I32::new(i64::from(core_config.announce_policy.interval) as i32)),
63-
leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)),
64-
seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)),
71+
leechers: NumberOfPeers(I32::new(i64::from(announce_data.stats.incomplete) as i32)),
72+
seeders: NumberOfPeers(I32::new(i64::from(announce_data.stats.complete) as i32)),
6573
},
66-
peers: response
74+
peers: announce_data
6775
.peers
6876
.iter()
6977
.filter_map(|peer| {
@@ -79,16 +87,16 @@ pub async fn handle_announce(
7987
.collect(),
8088
};
8189

82-
Ok(Response::from(announce_response))
90+
Response::from(announce_response)
8391
} else {
8492
let announce_response = AnnounceResponse {
8593
fixed: AnnounceResponseFixedData {
8694
transaction_id: request.transaction_id,
8795
announce_interval: AnnounceInterval(I32::new(i64::from(core_config.announce_policy.interval) as i32)),
88-
leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)),
89-
seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)),
96+
leechers: NumberOfPeers(I32::new(i64::from(announce_data.stats.incomplete) as i32)),
97+
seeders: NumberOfPeers(I32::new(i64::from(announce_data.stats.complete) as i32)),
9098
},
91-
peers: response
99+
peers: announce_data
92100
.peers
93101
.iter()
94102
.filter_map(|peer| {
@@ -104,7 +112,7 @@ pub async fn handle_announce(
104112
.collect(),
105113
};
106114

107-
Ok(Response::from(announce_response))
115+
Response::from(announce_response)
108116
}
109117
}
110118

src/servers/udp/handlers/connect.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::net::SocketAddr;
33
use std::sync::Arc;
44

5-
use aquatic_udp_protocol::{ConnectRequest, ConnectResponse, Response};
5+
use aquatic_udp_protocol::{ConnectRequest, ConnectResponse, ConnectionId, Response};
66
use tracing::{instrument, Level};
77

88
use crate::packages::udp_tracker_core;
@@ -25,12 +25,9 @@ pub async fn handle_connect(
2525

2626
tracing::trace!("handle connect");
2727

28-
let connection_id = make(gen_remote_fingerprint(&remote_addr), cookie_issue_time).expect("it should be a normal value");
28+
// todo: move to connect service in udp_tracker_core
2929

30-
let response = ConnectResponse {
31-
transaction_id: request.transaction_id,
32-
connection_id,
33-
};
30+
let connection_id = make(gen_remote_fingerprint(&remote_addr), cookie_issue_time).expect("it should be a normal value");
3431

3532
if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
3633
match remote_addr {
@@ -47,6 +44,15 @@ pub async fn handle_connect(
4744
}
4845
}
4946

47+
build_response(*request, connection_id)
48+
}
49+
50+
fn build_response(request: ConnectRequest, connection_id: ConnectionId) -> Response {
51+
let response = ConnectResponse {
52+
transaction_id: request.transaction_id,
53+
connection_id,
54+
};
55+
5056
Response::from(response)
5157
}
5258

src/servers/udp/handlers/scrape.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use aquatic_udp_protocol::{
77
NumberOfDownloads, NumberOfPeers, Response, ScrapeRequest, ScrapeResponse, TorrentScrapeStatistics, TransactionId,
88
};
99
use bittorrent_tracker_core::scrape_handler::ScrapeHandler;
10+
use torrust_tracker_primitives::core::ScrapeData;
1011
use tracing::{instrument, Level};
1112
use zerocopy::network_endian::I32;
1213

@@ -43,8 +44,10 @@ pub async fn handle_scrape(
4344
.await
4445
.map_err(|e| (e.into(), request.transaction_id))?;
4546

46-
// todo: extract `build_response` function.
47+
Ok(build_response(request, &scrape_data))
48+
}
4749

50+
fn build_response(request: &ScrapeRequest, scrape_data: &ScrapeData) -> Response {
4851
let mut torrent_stats: Vec<TorrentScrapeStatistics> = Vec::new();
4952

5053
for file in &scrape_data.files {
@@ -67,7 +70,7 @@ pub async fn handle_scrape(
6770
torrent_stats,
6871
};
6972

70-
Ok(Response::from(response))
73+
Response::from(response)
7174
}
7275

7376
#[cfg(test)]

0 commit comments

Comments
 (0)