Skip to content

Commit 00af70f

Browse files
committed
dev: remove announce event wrapper
1 parent 325df70 commit 00af70f

File tree

17 files changed

+61
-80
lines changed

17 files changed

+61
-80
lines changed

packages/primitives/src/announce_event.rs

-43
This file was deleted.

packages/primitives/src/lib.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use std::collections::BTreeMap;
88
use std::time::Duration;
99

10+
pub use aquatic_udp_protocol::{AnnounceEvent, AnnounceEventBytes};
1011
use info_hash::InfoHash;
1112
use serde::{Deserialize, Serialize};
1213

13-
pub mod announce_event;
1414
pub mod info_hash;
1515
pub mod pagination;
1616
pub mod peer;
@@ -29,6 +29,29 @@ pub fn ser_unix_time_value<S: serde::Serializer>(unix_time_value: &DurationSince
2929
ser.serialize_u64(unix_time_value.as_millis() as u64)
3030
}
3131

32+
#[derive(Serialize)]
33+
pub enum AnnounceEventSer {
34+
Started,
35+
Stopped,
36+
Completed,
37+
None,
38+
}
39+
40+
/// Serializes a `DurationSinceUnixEpoch` as a Unix timestamp in milliseconds.
41+
/// # Errors
42+
///
43+
/// Will return `serde::Serializer::Error` if unable to serialize the `unix_time_value`.
44+
pub fn ser_announce_event<S: serde::Serializer>(announce_event: &AnnounceEvent, ser: S) -> Result<S::Ok, S::Error> {
45+
let event_ser = match announce_event {
46+
AnnounceEvent::Started => AnnounceEventSer::Started,
47+
AnnounceEvent::Stopped => AnnounceEventSer::Stopped,
48+
AnnounceEvent::Completed => AnnounceEventSer::Completed,
49+
AnnounceEvent::None => AnnounceEventSer::None,
50+
};
51+
52+
ser.serialize_some(&event_ser)
53+
}
54+
3255
/// IP version used by the peer to connect to the tracker: IPv4 or IPv6
3356
#[derive(PartialEq, Eq, Debug)]
3457
pub enum IPVersion {

packages/primitives/src/peer.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
use std::net::{IpAddr, SocketAddr};
2525
use std::sync::Arc;
2626

27+
use aquatic_udp_protocol::AnnounceEvent;
2728
use serde::Serialize;
2829

29-
use crate::announce_event::AnnounceEvent;
30-
use crate::{ser_unix_time_value, DurationSinceUnixEpoch, IPVersion, NumberOfBytes};
30+
use crate::{ser_announce_event, ser_unix_time_value, DurationSinceUnixEpoch, IPVersion, NumberOfBytes};
3131

3232
/// Peer struct used by the core `Tracker`.
3333
///
@@ -51,7 +51,7 @@ use crate::{ser_unix_time_value, DurationSinceUnixEpoch, IPVersion, NumberOfByte
5151
/// event: AnnounceEvent::Started,
5252
/// };
5353
/// ```
54-
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
54+
#[derive(Debug, Clone, Serialize, Copy, PartialEq, Eq, Hash)]
5555
pub struct Peer {
5656
/// ID used by the downloader peer
5757
pub peer_id: Id,
@@ -67,9 +67,22 @@ pub struct Peer {
6767
/// The number of bytes this peer still has to download
6868
pub left: NumberOfBytes,
6969
/// This is an optional key which maps to started, completed, or stopped (or empty, which is the same as not being present).
70+
#[serde(serialize_with = "ser_announce_event")]
7071
pub event: AnnounceEvent,
7172
}
7273

74+
impl Ord for Peer {
75+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
76+
self.peer_id.cmp(&other.peer_id)
77+
}
78+
}
79+
80+
impl PartialOrd for Peer {
81+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
82+
Some(self.peer_id.cmp(&other.peer_id))
83+
}
84+
}
85+
7386
pub trait ReadInfo {
7487
fn is_seeder(&self) -> bool;
7588
fn get_event(&self) -> AnnounceEvent;
@@ -344,8 +357,9 @@ impl<P: Encoding> FromIterator<Peer> for Vec<P> {
344357
pub mod fixture {
345358
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
346359

360+
use aquatic_udp_protocol::AnnounceEvent;
361+
347362
use super::{Id, Peer};
348-
use crate::announce_event::AnnounceEvent;
349363
use crate::{DurationSinceUnixEpoch, NumberOfBytes};
350364

351365
#[derive(PartialEq, Debug)]

packages/torrent-repository/benches/helpers/utils.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::collections::HashSet;
22
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
33

4-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
54
use torrust_tracker_primitives::info_hash::InfoHash;
65
use torrust_tracker_primitives::peer::{Id, Peer};
7-
use torrust_tracker_primitives::{DurationSinceUnixEpoch, NumberOfBytes};
6+
use torrust_tracker_primitives::{AnnounceEvent, DurationSinceUnixEpoch, NumberOfBytes};
87

98
pub const DEFAULT_PEER: Peer = Peer {
109
peer_id: Id([0; 20]),

packages/torrent-repository/src/entry/single.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::net::SocketAddr;
22
use std::sync::Arc;
33

44
use torrust_tracker_configuration::TrackerPolicy;
5-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
65
use torrust_tracker_primitives::peer::{self};
76
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
8-
use torrust_tracker_primitives::DurationSinceUnixEpoch;
7+
use torrust_tracker_primitives::{AnnounceEvent, DurationSinceUnixEpoch};
98

109
use super::Entry;
1110
use crate::EntrySingle;

packages/torrent-repository/tests/common/torrent_peer_builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::net::SocketAddr;
22

33
use torrust_tracker_clock::clock::Time;
4-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
5-
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes};
4+
use torrust_tracker_primitives::{peer, AnnounceEvent, DurationSinceUnixEpoch, NumberOfBytes};
65

76
use crate::CurrentClock;
87

packages/torrent-repository/tests/entry/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use rstest::{fixture, rstest};
66
use torrust_tracker_clock::clock::stopped::Stopped as _;
77
use torrust_tracker_clock::clock::{self, Time as _};
88
use torrust_tracker_configuration::{TrackerPolicy, TORRENT_PEERS_LIMIT};
9-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
109
use torrust_tracker_primitives::peer::Peer;
11-
use torrust_tracker_primitives::{peer, NumberOfBytes};
10+
use torrust_tracker_primitives::{peer, AnnounceEvent, NumberOfBytes};
1211
use torrust_tracker_torrent_repository::{
1312
EntryMutexParkingLot, EntryMutexStd, EntryMutexTokio, EntryRwLockParkingLot, EntrySingle,
1413
};

packages/torrent-repository/tests/repository/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::hash::{DefaultHasher, Hash, Hasher};
33

44
use rstest::{fixture, rstest};
55
use torrust_tracker_configuration::TrackerPolicy;
6-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
76
use torrust_tracker_primitives::info_hash::InfoHash;
87
use torrust_tracker_primitives::pagination::Pagination;
98
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
10-
use torrust_tracker_primitives::{NumberOfBytes, PersistentTorrents};
9+
use torrust_tracker_primitives::{AnnounceEvent, NumberOfBytes, PersistentTorrents};
1110
use torrust_tracker_torrent_repository::entry::Entry as _;
1211
use torrust_tracker_torrent_repository::repository::dash_map_mutex_std::XacrimonDashMap;
1312
use torrust_tracker_torrent_repository::repository::rw_lock_std::RwLockStd;

src/core/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
//! Once you have instantiated the `Tracker` you can `announce` a new [`peer::Peer`] with:
5656
//!
5757
//! ```rust,no_run
58-
//! use torrust_tracker_primitives::peer;
59-
//! use torrust_tracker_primitives::info_hash::InfoHash;
60-
//! use torrust_tracker_primitives::{DurationSinceUnixEpoch, NumberOfBytes};
61-
//! use torrust_tracker_primitives::announce_event::AnnounceEvent;
6258
//! use std::net::SocketAddr;
6359
//! use std::net::IpAddr;
6460
//! use std::net::Ipv4Addr;
6561
//! use std::str::FromStr;
6662
//!
63+
//! use aquatic_udp_protocol::AnnounceEvent;
64+
//! use torrust_tracker_primitives::peer;
65+
//! use torrust_tracker_primitives::info_hash::InfoHash;
66+
//! use torrust_tracker_primitives::{DurationSinceUnixEpoch, NumberOfBytes};
6767
//!
6868
//! let info_hash = InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap();
6969
//!
@@ -1198,7 +1198,7 @@ mod tests {
11981198
use std::str::FromStr;
11991199
use std::sync::Arc;
12001200

1201-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
1201+
use aquatic_udp_protocol::AnnounceEvent;
12021202
use torrust_tracker_primitives::info_hash::InfoHash;
12031203
use torrust_tracker_primitives::{DurationSinceUnixEpoch, NumberOfBytes};
12041204
use torrust_tracker_test_helpers::configuration;
@@ -2035,7 +2035,7 @@ mod tests {
20352035

20362036
mod handling_torrent_persistence {
20372037

2038-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
2038+
use aquatic_udp_protocol::AnnounceEvent;
20392039
use torrust_tracker_torrent_repository::entry::EntrySync;
20402040
use torrust_tracker_torrent_repository::repository::Repository;
20412041

src/core/peer_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
44

5+
use aquatic_udp_protocol::AnnounceEvent;
56
use torrust_tracker_clock::clock::stopped::Stopped as _;
67
use torrust_tracker_clock::clock::{self, Time};
7-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
88
use torrust_tracker_primitives::{peer, NumberOfBytes};
99

1010
use crate::CurrentClock;

src/core/services/torrent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub async fn get_torrents(tracker: Arc<Tracker>, info_hashes: &[InfoHash]) -> Ve
105105
mod tests {
106106
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
107107

108-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
108+
use aquatic_udp_protocol::AnnounceEvent;
109109
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes};
110110

111111
fn sample_peer() -> peer::Peer {

src/servers/apis/v1/context/torrent/resources/peer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Peer {
2222
/// The peer's left bytes (pending to download).
2323
pub left: i64,
2424
/// The peer's event: `started`, `stopped`, `completed`.
25-
/// See [`AnnounceEvent`](torrust_tracker_primitives::announce_event::AnnounceEvent).
25+
/// See [`AnnounceEvent`](aquatic_udp_protocol::AnnounceEvent).
2626
pub event: String,
2727
}
2828

src/servers/apis/v1/context/torrent/resources/torrent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod tests {
9797
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
9898
use std::str::FromStr;
9999

100-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
100+
use aquatic_udp_protocol::AnnounceEvent;
101101
use torrust_tracker_primitives::info_hash::InfoHash;
102102
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes};
103103

src/servers/http/v1/handlers/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use std::net::{IpAddr, SocketAddr};
99
use std::panic::Location;
1010
use std::sync::Arc;
1111

12+
use aquatic_udp_protocol::AnnounceEvent;
1213
use axum::extract::State;
1314
use axum::response::{IntoResponse, Response};
1415
use torrust_tracker_clock::clock::Time;
15-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
1616
use torrust_tracker_primitives::{peer, NumberOfBytes};
1717
use tracing::debug;
1818

src/servers/http/v1/services/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut peer:
4848
mod tests {
4949
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
5050

51-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
51+
use aquatic_udp_protocol::AnnounceEvent;
5252
use torrust_tracker_primitives::info_hash::InfoHash;
5353
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes};
5454
use torrust_tracker_test_helpers::configuration;

src/servers/http/v1/services/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mod tests {
6161

6262
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
6363

64-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
64+
use aquatic_udp_protocol::AnnounceEvent;
6565
use torrust_tracker_primitives::info_hash::InfoHash;
6666
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, NumberOfBytes};
6767
use torrust_tracker_test_helpers::configuration;

src/servers/udp/peer_builder.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::net::{IpAddr, SocketAddr};
33

44
use torrust_tracker_clock::clock::Time;
5-
use torrust_tracker_primitives::announce_event::AnnounceEvent;
65
use torrust_tracker_primitives::{peer, NumberOfBytes};
76

87
use crate::CurrentClock;
@@ -16,20 +15,13 @@ use crate::CurrentClock;
1615
/// * `peer_ip` - The real IP address of the peer, not the one in the announce request.
1716
#[must_use]
1817
pub fn from_request(announce_request: &aquatic_udp_protocol::AnnounceRequest, peer_ip: &IpAddr) -> peer::Peer {
19-
let announce_event = match aquatic_udp_protocol::AnnounceEvent::from(announce_request.event) {
20-
aquatic_udp_protocol::AnnounceEvent::Started => AnnounceEvent::Started,
21-
aquatic_udp_protocol::AnnounceEvent::Stopped => AnnounceEvent::Stopped,
22-
aquatic_udp_protocol::AnnounceEvent::Completed => AnnounceEvent::Completed,
23-
aquatic_udp_protocol::AnnounceEvent::None => AnnounceEvent::None,
24-
};
25-
2618
peer::Peer {
2719
peer_id: peer::Id(announce_request.peer_id.0),
2820
peer_addr: SocketAddr::new(*peer_ip, announce_request.port.0.into()),
2921
updated: CurrentClock::now(),
3022
uploaded: NumberOfBytes(announce_request.bytes_uploaded.0.into()),
3123
downloaded: NumberOfBytes(announce_request.bytes_downloaded.0.into()),
3224
left: NumberOfBytes(announce_request.bytes_left.0.into()),
33-
event: announce_event,
25+
event: announce_request.event.into(),
3426
}
3527
}

0 commit comments

Comments
 (0)