@@ -64,10 +64,10 @@ 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 } ;
67
- use serde_json:: json;
68
67
use url:: Url ;
69
68
70
69
use crate :: console:: clients:: udp:: checker;
70
+ use crate :: console:: clients:: udp:: responses:: { AnnounceResponseDto , ScrapeResponseDto } ;
71
71
use crate :: shared:: bit_torrent:: info_hash:: InfoHash as TorrustInfoHash ;
72
72
73
73
const ASSIGNED_BY_OS : u16 = 0 ;
@@ -117,45 +117,7 @@ pub async fn run() -> anyhow::Result<()> {
117
117
} => handle_scrape ( & tracker_socket_addr, & info_hashes) . await ?,
118
118
} ;
119
119
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)
159
121
}
160
122
161
123
fn setup_logging ( level : LevelFilter ) {
@@ -207,6 +169,29 @@ async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustI
207
169
. await
208
170
}
209
171
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
+
210
195
fn parse_socket_addr ( tracker_socket_addr_str : & str ) -> anyhow:: Result < SocketAddr > {
211
196
debug ! ( "Tracker socket address: {tracker_socket_addr_str:#?}" ) ;
212
197
0 commit comments