@@ -64,10 +64,10 @@ use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Scrape};
6464use aquatic_udp_protocol:: { Port , TransactionId } ;
6565use clap:: { Parser , Subcommand } ;
6666use log:: { debug, LevelFilter } ;
67- use serde_json:: json;
6867use url:: Url ;
6968
7069use crate :: console:: clients:: udp:: checker;
70+ use crate :: console:: clients:: udp:: responses:: { AnnounceResponseDto , ScrapeResponseDto } ;
7171use crate :: shared:: bit_torrent:: info_hash:: InfoHash as TorrustInfoHash ;
7272
7373const ASSIGNED_BY_OS : u16 = 0 ;
@@ -117,45 +117,7 @@ pub async fn run() -> anyhow::Result<()> {
117117 } => handle_scrape ( & tracker_socket_addr, & info_hashes) . await ?,
118118 } ;
119119
120- match response {
121- AnnounceIpv4 ( announce) => {
122- let json = json ! ( {
123- "transaction_id" : announce. transaction_id. 0 ,
124- "announce_interval" : announce. announce_interval. 0 ,
125- "leechers" : announce. leechers. 0 ,
126- "seeders" : announce. seeders. 0 ,
127- "peers" : announce. peers. iter( ) . map( |peer| format!( "{}:{}" , peer. ip_address, peer. port. 0 ) ) . collect:: <Vec <_>>( ) ,
128- } ) ;
129- let pretty_json = serde_json:: to_string_pretty ( & json) . context ( "announce IPv4 response JSON serialization" ) ?;
130- println ! ( "{pretty_json}" ) ;
131- }
132- AnnounceIpv6 ( announce) => {
133- let json = json ! ( {
134- "transaction_id" : announce. transaction_id. 0 ,
135- "announce_interval" : announce. announce_interval. 0 ,
136- "leechers" : announce. leechers. 0 ,
137- "seeders" : announce. seeders. 0 ,
138- "peers6" : announce. peers. iter( ) . map( |peer| format!( "{}:{}" , peer. ip_address, peer. port. 0 ) ) . collect:: <Vec <_>>( ) ,
139- } ) ;
140- let pretty_json = serde_json:: to_string_pretty ( & json) . context ( "announce IPv6 response JSON serialization" ) ?;
141- println ! ( "{pretty_json}" ) ;
142- }
143- Scrape ( scrape) => {
144- let json = json ! ( {
145- "transaction_id" : scrape. transaction_id. 0 ,
146- "torrent_stats" : scrape. torrent_stats. iter( ) . map( |torrent_scrape_statistics| json!( {
147- "seeders" : torrent_scrape_statistics. seeders. 0 ,
148- "completed" : torrent_scrape_statistics. completed. 0 ,
149- "leechers" : torrent_scrape_statistics. leechers. 0 ,
150- } ) ) . collect:: <Vec <_>>( ) ,
151- } ) ;
152- let pretty_json = serde_json:: to_string_pretty ( & json) . context ( "scrape response JSON serialization" ) ?;
153- println ! ( "{pretty_json}" ) ;
154- }
155- _ => println ! ( "{response:#?}" ) , // todo: serialize to JSON all responses.
156- } ;
157-
158- Ok ( ( ) )
120+ print_response ( response)
159121}
160122
161123fn setup_logging ( level : LevelFilter ) {
@@ -207,6 +169,29 @@ async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustI
207169 . await
208170}
209171
172+ fn print_response ( response : Response ) -> anyhow:: Result < ( ) > {
173+ match response {
174+ AnnounceIpv4 ( response) => {
175+ let pretty_json = serde_json:: to_string_pretty ( & AnnounceResponseDto :: from ( response) )
176+ . context ( "announce IPv4 response JSON serialization" ) ?;
177+ println ! ( "{pretty_json}" ) ;
178+ }
179+ AnnounceIpv6 ( response) => {
180+ let pretty_json = serde_json:: to_string_pretty ( & AnnounceResponseDto :: from ( response) )
181+ . context ( "announce IPv6 response JSON serialization" ) ?;
182+ println ! ( "{pretty_json}" ) ;
183+ }
184+ Scrape ( response) => {
185+ let pretty_json =
186+ serde_json:: to_string_pretty ( & ScrapeResponseDto :: from ( response) ) . context ( "scrape response JSON serialization" ) ?;
187+ println ! ( "{pretty_json}" ) ;
188+ }
189+ _ => println ! ( "{response:#?}" ) , // todo: serialize to JSON all aquatic responses.
190+ } ;
191+
192+ Ok ( ( ) )
193+ }
194+
210195fn parse_socket_addr ( tracker_socket_addr_str : & str ) -> anyhow:: Result < SocketAddr > {
211196 debug ! ( "Tracker socket address: {tracker_socket_addr_str:#?}" ) ;
212197
0 commit comments