-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod app; | ||
pub mod checker; | ||
pub mod responses; | ||
pub mod responses_print_response_fn_example; |
140 changes: 140 additions & 0 deletions
140
src/console/clients/udp/responses_print_response_fn_example.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//! Aquatic responses are not serializable. These are the serializable wrappers. | ||
use std::net::{Ipv4Addr, Ipv6Addr}; | ||
|
||
use anyhow::Context; | ||
use aquatic_udp_protocol::Response::{self, AnnounceIpv4, AnnounceIpv6, Connect, Error, Scrape}; | ||
use aquatic_udp_protocol::{AnnounceResponse, ConnectResponse, ErrorResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse}; | ||
use serde::Serialize; | ||
|
||
pub 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")?; | ||
println!("{pretty_json}"); | ||
} | ||
AnnounceIpv6(response) => { | ||
let pretty_json = serde_json::to_string_pretty(&AnnounceResponseDto::from(response)) | ||
.context("announce IPv6 response JSON serialization")?; | ||
println!("{pretty_json}"); | ||
} | ||
Scrape(response) => { | ||
let pretty_json = | ||
serde_json::to_string_pretty(&ScrapeResponseDto::from(response)).context("scrape response JSON serialization")?; | ||
println!("{pretty_json}"); | ||
} | ||
Error(response) => { | ||
let pretty_json = | ||
serde_json::to_string_pretty(&ErrorResponseDto::from(response)).context("error response JSON serialization")?; | ||
println!("{pretty_json}"); | ||
} | ||
}; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[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, | ||
announce_interval: i32, | ||
leechers: i32, | ||
seeders: i32, | ||
peers: Vec<String>, | ||
} | ||
|
||
impl From<AnnounceResponse<Ipv4AddrBytes>> for AnnounceResponseDto { | ||
fn from(announce: AnnounceResponse<Ipv4AddrBytes>) -> Self { | ||
Self { | ||
transaction_id: announce.fixed.transaction_id.0.into(), | ||
announce_interval: announce.fixed.announce_interval.0.into(), | ||
leechers: announce.fixed.leechers.0.into(), | ||
seeders: announce.fixed.seeders.0.into(), | ||
peers: announce | ||
.peers | ||
.iter() | ||
.map(|peer| format!("{}:{}", Ipv4Addr::from(peer.ip_address), peer.port.0)) | ||
.collect::<Vec<_>>(), | ||
} | ||
} | ||
} | ||
|
||
impl From<AnnounceResponse<Ipv6AddrBytes>> for AnnounceResponseDto { | ||
fn from(announce: AnnounceResponse<Ipv6AddrBytes>) -> Self { | ||
Self { | ||
transaction_id: announce.fixed.transaction_id.0.into(), | ||
announce_interval: announce.fixed.announce_interval.0.into(), | ||
leechers: announce.fixed.leechers.0.into(), | ||
seeders: announce.fixed.seeders.0.into(), | ||
peers: announce | ||
.peers | ||
.iter() | ||
.map(|peer| format!("{}:{}", Ipv6Addr::from(peer.ip_address), peer.port.0)) | ||
.collect::<Vec<_>>(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct ScrapeResponseDto { | ||
transaction_id: i32, | ||
torrent_stats: Vec<TorrentStats>, | ||
} | ||
|
||
impl From<ScrapeResponse> for ScrapeResponseDto { | ||
fn from(scrape: ScrapeResponse) -> Self { | ||
Self { | ||
transaction_id: scrape.transaction_id.0.into(), | ||
torrent_stats: scrape | ||
.torrent_stats | ||
.iter() | ||
.map(|torrent_scrape_statistics| TorrentStats { | ||
seeders: torrent_scrape_statistics.seeders.0.into(), | ||
completed: torrent_scrape_statistics.completed.0.into(), | ||
leechers: torrent_scrape_statistics.leechers.0.into(), | ||
}) | ||
.collect::<Vec<_>>(), | ||
} | ||
} | ||
} | ||
|
||
#[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, | ||
completed: i32, | ||
leechers: i32, | ||
} |