@@ -60,7 +60,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
60
60
use std:: str:: FromStr ;
61
61
62
62
use anyhow:: Context ;
63
- use aquatic_udp_protocol:: Response :: { AnnounceIpv4 , AnnounceIpv6 , Scrape } ;
63
+ use aquatic_udp_protocol:: Response :: { self , AnnounceIpv4 , AnnounceIpv6 , Scrape } ;
64
64
use aquatic_udp_protocol:: { Port , TransactionId } ;
65
65
use clap:: { Parser , Subcommand } ;
66
66
use log:: { debug, LevelFilter } ;
@@ -110,33 +110,11 @@ pub async fn run() -> anyhow::Result<()> {
110
110
Command :: Announce {
111
111
tracker_socket_addr,
112
112
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 ?,
126
114
Command :: Scrape {
127
115
tracker_socket_addr,
128
116
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 ?,
140
118
} ;
141
119
142
120
match response {
@@ -201,6 +179,34 @@ fn setup_logging(level: LevelFilter) {
201
179
debug ! ( "logging initialized." ) ;
202
180
}
203
181
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
+
204
210
fn parse_socket_addr ( tracker_socket_addr_str : & str ) -> anyhow:: Result < SocketAddr > {
205
211
debug ! ( "Tracker socket address: {tracker_socket_addr_str:#?}" ) ;
206
212
0 commit comments