Skip to content

Commit

Permalink
feat: [#670] new JSON serialization for connect and error aquatic res…
Browse files Browse the repository at this point in the history
…ponses
  • Loading branch information
mario-nt committed Jun 3, 2024
1 parent a0a7056 commit 4de7793
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ use std::net::{SocketAddr, ToSocketAddrs};
use std::str::FromStr;

use anyhow::Context;
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Scrape};
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Connect, Error, Scrape};
use aquatic_udp_protocol::{Port, TransactionId};
use clap::{Parser, Subcommand};
use log::{debug, LevelFilter};
use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash;
use url::Url;

use crate::console::clients::udp::checker;
use crate::console::clients::udp::responses::{AnnounceResponseDto, ScrapeResponseDto};
use crate::console::clients::udp::responses::{AnnounceResponseDto, ConnectResponseDto, ErrorResponseDto, ScrapeResponseDto};

const ASSIGNED_BY_OS: u16 = 0;
const RANDOM_TRANSACTION_ID: i32 = -888_840_697;
Expand Down Expand Up @@ -171,6 +171,11 @@ async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustI

fn print_response(response: Response) -> anyhow::Result<()> {
match response {
Connect(response) => {
let pretty_json = serde_json::to_string_pretty(&ConnectResponseDto::from(response))
.context("connect response JSON serialization")?;
println!("{pretty_json}");
}
AnnounceIpv4(response) => {
let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response))
.context("announce IPv4 response JSON serialization")?;
Expand All @@ -186,7 +191,11 @@ fn print_response(response: Response) -> anyhow::Result<()> {
serde_json::to_string_pretty(&ScrapeResponseDto::from(response)).context("scrape response JSON serialization")?;
println!("{pretty_json}");
}
_ => println!("{response:#?}"), // todo: serialize to JSON all aquatic responses.
Error(response) => {
let pretty_json =
serde_json::to_string_pretty(&ErrorResponseDto::from(response)).context("error response JSON serialization")?;
println!("{pretty_json}");
}
};

Ok(())
Expand Down
32 changes: 31 additions & 1 deletion src/console/clients/udp/responses.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
//! Aquatic responses are not serializable. These are the serializable wrappers.
use std::net::{Ipv4Addr, Ipv6Addr};

use aquatic_udp_protocol::{AnnounceResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse};
use aquatic_udp_protocol::{AnnounceResponse, ConnectResponse, ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse};
use serde::Serialize;

#[derive(Serialize)]
pub struct ConnectResponseDto {
transaction_id: i32,
connection_id: i64,
}

impl From<ConnectResponse> for ConnectResponseDto {
fn from(connect: ConnectResponse) -> Self {
Self {
transaction_id: connect.transaction_id.0.into(),
connection_id: connect.connection_id.0.into(),
}
}
}

#[derive(Serialize)]
pub struct AnnounceResponseDto {
transaction_id: i32,
Expand Down Expand Up @@ -68,6 +83,21 @@ impl From<ScrapeResponse> for ScrapeResponseDto {
}
}

#[derive(Serialize)]
pub struct ErrorResponseDto {
transaction_id: i32,
message: String,
}

impl From<ErrorResponse> for ErrorResponseDto {
fn from(error: ErrorResponse) -> Self {
Self {
transaction_id: error.transaction_id.0.into(),
message: error.message.to_string(),
}
}
}

#[derive(Serialize)]
struct TorrentStats {
seeders: i32,
Expand Down

0 comments on commit 4de7793

Please sign in to comment.