Skip to content

Commit bc02e9b

Browse files
committed
feat: [#1376] add info-hash to bittorrent_http_tracker_core::event::TcpAnnounce
1 parent 657a5d0 commit bc02e9b

3 files changed

Lines changed: 52 additions & 44 deletions

File tree

packages/http-tracker-core/src/event/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::net::{IpAddr, SocketAddr};
22

3+
use bittorrent_primitives::info_hash::InfoHash;
34
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
45
use torrust_tracker_metrics::label_name;
56
use torrust_tracker_primitives::peer::PeerAnnouncement;
@@ -12,6 +13,7 @@ pub mod sender;
1213
pub enum Event {
1314
TcpAnnounce {
1415
connection: ConnectionContext,
16+
info_hash: InfoHash,
1517
announcement: PeerAnnouncement,
1618
},
1719
TcpScrape {
@@ -93,6 +95,32 @@ pub mod test {
9395
use torrust_tracker_primitives::peer::Peer;
9496
use torrust_tracker_primitives::service_binding::Protocol;
9597

98+
use super::Event;
99+
use crate::tests::sample_info_hash;
100+
101+
#[must_use]
102+
pub fn events_match(event: &Event, expected_event: &Event) -> bool {
103+
match (event, expected_event) {
104+
(
105+
Event::TcpAnnounce {
106+
connection,
107+
info_hash,
108+
announcement,
109+
},
110+
Event::TcpAnnounce {
111+
connection: expected_connection,
112+
info_hash: expected_info_hash,
113+
announcement: expected_announcement,
114+
},
115+
) => {
116+
*connection == *expected_connection
117+
&& *info_hash == *expected_info_hash
118+
&& announcement.peer_addr == expected_announcement.peer_addr
119+
}
120+
_ => false,
121+
}
122+
}
123+
96124
#[test]
97125
fn events_should_be_comparable() {
98126
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
@@ -102,13 +130,15 @@ pub mod test {
102130
use crate::event::{ConnectionContext, Event};
103131

104132
let remote_client_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
133+
let info_hash = sample_info_hash();
105134

106135
let event1 = Event::TcpAnnounce {
107136
connection: ConnectionContext::new(
108137
remote_client_ip,
109138
Some(8080),
110139
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
111140
),
141+
info_hash,
112142
announcement: Peer::default(),
113143
};
114144

@@ -118,6 +148,7 @@ pub mod test {
118148
Some(8080),
119149
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
120150
),
151+
info_hash,
121152
announcement: Peer::default(),
122153
};
123154

packages/http-tracker-core/src/services/announce.rs

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ impl AnnounceService {
8989
.announce(&announce_request.info_hash, &mut peer, &remote_client_ip, &peers_wanted)
9090
.await?;
9191

92-
self.send_event(remote_client_ip, opt_remote_client_port, server_service_binding.clone(), peer)
93-
.await;
92+
self.send_event(
93+
announce_request.info_hash,
94+
remote_client_ip,
95+
opt_remote_client_port,
96+
server_service_binding.clone(),
97+
peer,
98+
)
99+
.await;
94100

95101
Ok(announce_data)
96102
}
@@ -142,6 +148,7 @@ impl AnnounceService {
142148

143149
async fn send_event(
144150
&self,
151+
info_hash: InfoHash,
145152
remote_client_ip: IpAddr,
146153
opt_peer_ip_port: Option<u16>,
147154
server_service_binding: ServiceBinding,
@@ -150,6 +157,7 @@ impl AnnounceService {
150157
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
151158
let event = Event::TcpAnnounce {
152159
connection: event::ConnectionContext::new(remote_client_ip, opt_peer_ip_port, server_service_binding),
160+
info_hash,
153161
announcement,
154162
};
155163

@@ -327,13 +335,14 @@ mod tests {
327335
use torrust_tracker_test_helpers::configuration;
328336

329337
use crate::event;
338+
use crate::event::test::events_match;
330339
use crate::event::{ConnectionContext, Event};
331340
use crate::services::announce::tests::{
332341
initialize_core_tracker_services, initialize_core_tracker_services_with_config, sample_announce_request_for_peer,
333342
MockHttpStatsEventSender,
334343
};
335344
use crate::services::announce::AnnounceService;
336-
use crate::tests::{sample_peer, sample_peer_using_ipv4, sample_peer_using_ipv6};
345+
use crate::tests::{sample_info_hash, sample_peer, sample_peer_using_ipv4, sample_peer_using_ipv6};
337346

338347
#[tokio::test]
339348
async fn it_should_return_the_announce_data() {
@@ -394,22 +403,11 @@ mod tests {
394403

395404
let expected_event = Event::TcpAnnounce {
396405
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
406+
info_hash: sample_info_hash(),
397407
announcement,
398408
};
399409

400-
match (event, expected_event) {
401-
(
402-
Event::TcpAnnounce {
403-
connection: a_conn,
404-
announcement: a2,
405-
},
406-
Event::TcpAnnounce {
407-
connection: b_conn,
408-
announcement: b2,
409-
},
410-
) => *a_conn == b_conn && a2.peer_addr == b2.peer_addr,
411-
_ => false,
412-
}
410+
events_match(event, &expected_event)
413411
}))
414412
.times(1)
415413
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -479,22 +477,11 @@ mod tests {
479477

480478
let expected_event = Event::TcpAnnounce {
481479
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
480+
info_hash: sample_info_hash(),
482481
announcement: peer_announcement,
483482
};
484483

485-
match (event, expected_event) {
486-
(
487-
Event::TcpAnnounce {
488-
connection: a_conn,
489-
announcement: a2,
490-
},
491-
Event::TcpAnnounce {
492-
connection: b_conn,
493-
announcement: b2,
494-
},
495-
) => *a_conn == b_conn && a2.peer_addr == b2.peer_addr,
496-
_ => false,
497-
}
484+
events_match(event, &expected_event)
498485
}))
499486
.times(1)
500487
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));
@@ -537,22 +524,10 @@ mod tests {
537524
.with(predicate::function(move |event| {
538525
let expected_event = Event::TcpAnnounce {
539526
connection: ConnectionContext::new(remote_client_ip, Some(8080), server_service_binding.clone()),
527+
info_hash: sample_info_hash(),
540528
announcement: peer,
541529
};
542-
543-
match (event, expected_event) {
544-
(
545-
Event::TcpAnnounce {
546-
connection: a_conn,
547-
announcement: a2,
548-
},
549-
Event::TcpAnnounce {
550-
connection: b_conn,
551-
announcement: b2,
552-
},
553-
) => *a_conn == b_conn && a2.peer_addr == b2.peer_addr,
554-
_ => false,
555-
}
530+
events_match(event, &expected_event)
556531
}))
557532
.times(1)
558533
.returning(|_| Box::pin(future::ready(Some(Ok(1)))));

packages/http-tracker-core/src/statistics/event/handler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod tests {
7979
use crate::event::{ConnectionContext, Event};
8080
use crate::statistics::event::handler::handle_event;
8181
use crate::statistics::repository::Repository;
82-
use crate::tests::{sample_peer_using_ipv4, sample_peer_using_ipv6};
82+
use crate::tests::{sample_info_hash, sample_peer_using_ipv4, sample_peer_using_ipv6};
8383
use crate::CurrentClock;
8484

8585
#[tokio::test]
@@ -95,6 +95,7 @@ mod tests {
9595
Some(8080),
9696
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
9797
),
98+
info_hash: sample_info_hash(),
9899
announcement: peer,
99100
},
100101
&stats_repository,
@@ -142,6 +143,7 @@ mod tests {
142143
Some(8080),
143144
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
144145
),
146+
info_hash: sample_info_hash(),
145147
announcement: peer,
146148
},
147149
&stats_repository,

0 commit comments

Comments
 (0)