@@ -60,7 +60,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
6060use std:: str:: FromStr ;
6161
6262use anyhow:: Context ;
63- use aquatic_udp_protocol:: Response :: { AnnounceIpv4 , AnnounceIpv6 , Scrape } ;
63+ use aquatic_udp_protocol:: Response :: { self , AnnounceIpv4 , AnnounceIpv6 , Scrape } ;
6464use aquatic_udp_protocol:: { Port , TransactionId } ;
6565use clap:: { Parser , Subcommand } ;
6666use log:: { debug, LevelFilter } ;
@@ -110,33 +110,11 @@ pub async fn run() -> anyhow::Result<()> {
110110 Command :: Announce {
111111 tracker_socket_addr,
112112 info_hash,
113- } => {
114- let transaction_id = TransactionId ( RANDOM_TRANSACTION_ID ) ;
115-
116- let mut client = checker:: Client :: default ( ) ;
117-
118- let bound_to = client. bind_and_connect ( ASSIGNED_BY_OS , & tracker_socket_addr) . await ?;
119-
120- let connection_id = client. send_connection_request ( transaction_id) . await ?;
121-
122- client
123- . send_announce_request ( connection_id, transaction_id, info_hash, Port ( bound_to. port ( ) ) )
124- . await ?
125- }
113+ } => handle_announce ( & tracker_socket_addr, & info_hash) . await ?,
126114 Command :: Scrape {
127115 tracker_socket_addr,
128116 info_hashes,
129- } => {
130- let transaction_id = TransactionId ( RANDOM_TRANSACTION_ID ) ;
131-
132- let mut client = checker:: Client :: default ( ) ;
133-
134- let _bound_to = client. bind_and_connect ( ASSIGNED_BY_OS , & tracker_socket_addr) . await ?;
135-
136- let connection_id = client. send_connection_request ( transaction_id) . await ?;
137-
138- client. send_scrape_request ( connection_id, transaction_id, info_hashes) . await ?
139- }
117+ } => handle_scrape ( & tracker_socket_addr, & info_hashes) . await ?,
140118 } ;
141119
142120 match response {
@@ -201,6 +179,34 @@ fn setup_logging(level: LevelFilter) {
201179 debug ! ( "logging initialized." ) ;
202180}
203181
182+ async fn handle_announce ( tracker_socket_addr : & SocketAddr , info_hash : & TorrustInfoHash ) -> anyhow:: Result < Response > {
183+ let transaction_id = TransactionId ( RANDOM_TRANSACTION_ID ) ;
184+
185+ let mut client = checker:: Client :: default ( ) ;
186+
187+ let bound_to = client. bind_and_connect ( ASSIGNED_BY_OS , tracker_socket_addr) . await ?;
188+
189+ let connection_id = client. send_connection_request ( transaction_id) . await ?;
190+
191+ client
192+ . send_announce_request ( connection_id, transaction_id, * info_hash, Port ( bound_to. port ( ) ) )
193+ . await
194+ }
195+
196+ async fn handle_scrape ( tracker_socket_addr : & SocketAddr , info_hashes : & [ TorrustInfoHash ] ) -> anyhow:: Result < Response > {
197+ let transaction_id = TransactionId ( RANDOM_TRANSACTION_ID ) ;
198+
199+ let mut client = checker:: Client :: default ( ) ;
200+
201+ let _bound_to = client. bind_and_connect ( ASSIGNED_BY_OS , tracker_socket_addr) . await ?;
202+
203+ let connection_id = client. send_connection_request ( transaction_id) . await ?;
204+
205+ client
206+ . send_scrape_request ( connection_id, transaction_id, info_hashes. to_vec ( ) )
207+ . await
208+ }
209+
204210fn parse_socket_addr ( tracker_socket_addr_str : & str ) -> anyhow:: Result < SocketAddr > {
205211 debug ! ( "Tracker socket address: {tracker_socket_addr_str:#?}" ) ;
206212
0 commit comments