11use std:: net:: { IpAddr , SocketAddr } ;
22
3+ use bittorrent_primitives:: info_hash:: InfoHash ;
34use torrust_tracker_metrics:: label:: { LabelSet , LabelValue } ;
45use torrust_tracker_metrics:: label_name;
6+ use torrust_tracker_primitives:: peer:: PeerAnnouncement ;
57use torrust_tracker_primitives:: service_binding:: ServiceBinding ;
68
9+ use crate :: services:: RemoteClientAddr ;
10+
711pub mod sender;
812
913/// A HTTP core event.
1014#[ derive( Debug , PartialEq , Eq , Clone ) ]
1115pub enum Event {
12- TcpAnnounce { connection : ConnectionContext } ,
13- TcpScrape { connection : ConnectionContext } ,
16+ TcpAnnounce {
17+ connection : ConnectionContext ,
18+ info_hash : InfoHash ,
19+ announcement : PeerAnnouncement ,
20+ } ,
21+ TcpScrape {
22+ connection : ConnectionContext ,
23+ } ,
1424}
1525
1626#[ derive( Debug , PartialEq , Eq , Clone ) ]
@@ -21,12 +31,9 @@ pub struct ConnectionContext {
2131
2232impl ConnectionContext {
2333 #[ must_use]
24- pub fn new ( client_ip_addr : IpAddr , opt_client_port : Option < u16 > , server_service_binding : ServiceBinding ) -> Self {
34+ pub fn new ( remote_client_addr : RemoteClientAddr , server_service_binding : ServiceBinding ) -> Self {
2535 Self {
26- client : ClientConnectionContext {
27- ip_addr : client_ip_addr,
28- port : opt_client_port,
29- } ,
36+ client : ClientConnectionContext { remote_client_addr } ,
3037 server : ServerConnectionContext {
3138 service_binding : server_service_binding,
3239 } ,
@@ -35,12 +42,12 @@ impl ConnectionContext {
3542
3643 #[ must_use]
3744 pub fn client_ip_addr ( & self ) -> IpAddr {
38- self . client . ip_addr
45+ self . client . ip_addr ( )
3946 }
4047
4148 #[ must_use]
4249 pub fn client_port ( & self ) -> Option < u16 > {
43- self . client . port
50+ self . client . port ( )
4451 }
4552
4653 #[ must_use]
@@ -51,10 +58,19 @@ impl ConnectionContext {
5158
5259#[ derive( Debug , PartialEq , Eq , Clone ) ]
5360pub struct ClientConnectionContext {
54- ip_addr : IpAddr ,
61+ remote_client_addr : RemoteClientAddr ,
62+ }
5563
56- /// It's provided if you use the `torrust-axum-http-tracker-server` crate.
57- port : Option < u16 > ,
64+ impl ClientConnectionContext {
65+ #[ must_use]
66+ pub fn ip_addr ( & self ) -> IpAddr {
67+ self . remote_client_addr . ip
68+ }
69+
70+ #[ must_use]
71+ pub fn port ( & self ) -> Option < u16 > {
72+ self . remote_client_addr . port
73+ }
5874}
5975
6076#[ derive( Debug , PartialEq , Eq , Clone ) ]
@@ -80,3 +96,72 @@ impl From<ConnectionContext> for LabelSet {
8096 ] )
8197 }
8298}
99+
100+ #[ cfg( test) ]
101+ pub mod test {
102+
103+ use torrust_tracker_primitives:: peer:: Peer ;
104+ use torrust_tracker_primitives:: service_binding:: Protocol ;
105+
106+ use super :: Event ;
107+ use crate :: services:: RemoteClientAddr ;
108+ use crate :: tests:: sample_info_hash;
109+
110+ #[ must_use]
111+ pub fn events_match ( event : & Event , expected_event : & Event ) -> bool {
112+ match ( event, expected_event) {
113+ (
114+ Event :: TcpAnnounce {
115+ connection,
116+ info_hash,
117+ announcement,
118+ } ,
119+ Event :: TcpAnnounce {
120+ connection : expected_connection,
121+ info_hash : expected_info_hash,
122+ announcement : expected_announcement,
123+ } ,
124+ ) => {
125+ * connection == * expected_connection
126+ && * info_hash == * expected_info_hash
127+ && announcement. peer_addr == expected_announcement. peer_addr
128+ }
129+ _ => false ,
130+ }
131+ }
132+
133+ #[ test]
134+ fn events_should_be_comparable ( ) {
135+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
136+
137+ use torrust_tracker_primitives:: service_binding:: ServiceBinding ;
138+
139+ use crate :: event:: { ConnectionContext , Event } ;
140+
141+ let remote_client_ip = IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) ;
142+ let info_hash = sample_info_hash ( ) ;
143+
144+ let event1 = Event :: TcpAnnounce {
145+ connection : ConnectionContext :: new (
146+ RemoteClientAddr :: new ( remote_client_ip, Some ( 8080 ) ) ,
147+ ServiceBinding :: new ( Protocol :: HTTP , SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ) . unwrap ( ) ,
148+ ) ,
149+ info_hash,
150+ announcement : Peer :: default ( ) ,
151+ } ;
152+
153+ let event2 = Event :: TcpAnnounce {
154+ connection : ConnectionContext :: new (
155+ RemoteClientAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 2 ) ) , Some ( 8080 ) ) ,
156+ ServiceBinding :: new ( Protocol :: HTTP , SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) ) , 7070 ) ) . unwrap ( ) ,
157+ ) ,
158+ info_hash,
159+ announcement : Peer :: default ( ) ,
160+ } ;
161+
162+ let event1_clone = event1. clone ( ) ;
163+
164+ assert ! ( event1 == event1_clone) ;
165+ assert ! ( event1 != event2) ;
166+ }
167+ }
0 commit comments