Skip to content

Commit 47f903a

Browse files
committed
Merge #1331: Refactor packages: review AnnounceEvent struct
8167598 refactor: [#1309] align our HTTP Announce Events with the aquatic one (Jose Celano) 5362a6d refactor: [#1309] remove unused code adn rename function (Jose Celano) Pull request description: Refactor packages: review `AnnounceEvent` struct. - Modify the `bittorrent_http_tracker_protocol::v1::requests::announce::Event` introducing a new variant `Empty`. This aligns our implementation with the aquatic one and make conversions between HTTP and UDP Announce events direct. It's also part of the protocol in the [BEP 3](https://www.bittorrent.org/beps/bep_0003.html). - Keep the UDP Announce Event as the primitive type used everywhere. I wanted to removed it from packages that are not the `udp-tracker-core` package but we are using other primitives types from the aquatic UDP tracker protocol crate. We need to carefully study it and open a new issue if we want to remove that dependency. ACKs for top commit: josecelano: ACK 8167598 Tree-SHA512: b2184cefca215d848fed0e2b4ea234428a402295f36d2d3e2a44b4156bbe35209ca446b29c5b49e8aed60c938b1b22dc4d350c0ed8087a9c46f9cf89be00bf02
2 parents ee2fc24 + 8167598 commit 47f903a

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

packages/axum-http-tracker-server/src/v1/handlers/announce.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
//! and resolve the client IP address.
55
use std::sync::Arc;
66

7-
use aquatic_udp_protocol::AnnounceEvent;
87
use axum::extract::State;
98
use axum::response::{IntoResponse, Response};
109
use bittorrent_http_tracker_core::services::announce::HttpAnnounceError;
11-
use bittorrent_http_tracker_protocol::v1::requests::announce::{Announce, Compact, Event};
10+
use bittorrent_http_tracker_protocol::v1::requests::announce::{Announce, Compact};
1211
use bittorrent_http_tracker_protocol::v1::responses::{self};
1312
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
1413
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
@@ -158,30 +157,6 @@ fn build_response(announce_request: &Announce, announce_data: AnnounceData) -> R
158157
}
159158
}
160159

161-
#[must_use]
162-
pub fn map_to_aquatic_event(event: &Option<Event>) -> aquatic_udp_protocol::AnnounceEvent {
163-
match event {
164-
Some(event) => match &event {
165-
Event::Started => aquatic_udp_protocol::AnnounceEvent::Started,
166-
Event::Stopped => aquatic_udp_protocol::AnnounceEvent::Stopped,
167-
Event::Completed => aquatic_udp_protocol::AnnounceEvent::Completed,
168-
},
169-
None => aquatic_udp_protocol::AnnounceEvent::None,
170-
}
171-
}
172-
173-
#[must_use]
174-
pub fn map_to_torrust_event(event: &Option<Event>) -> AnnounceEvent {
175-
match event {
176-
Some(event) => match &event {
177-
Event::Started => AnnounceEvent::Started,
178-
Event::Stopped => AnnounceEvent::Stopped,
179-
Event::Completed => AnnounceEvent::Completed,
180-
},
181-
None => AnnounceEvent::None,
182-
}
183-
}
184-
185160
#[cfg(test)]
186161
mod tests {
187162

packages/http-protocol/src/v1/requests/announce.rs

+29-19
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,21 @@ pub enum ParseAnnounceQueryError {
145145
///
146146
/// Refer to [BEP 03. The `BitTorrent Protocol` Specification](https://www.bittorrent.org/beps/bep_0003.html)
147147
/// for more information.
148-
#[derive(PartialEq, Debug)]
148+
#[derive(PartialEq, Debug, Clone)]
149149
pub enum Event {
150150
/// Event sent when a download first begins.
151151
Started,
152+
152153
/// Event sent when the downloader cease downloading.
153154
Stopped,
155+
154156
/// Event sent when the download is complete.
155-
/// No `completed` is sent if the file was complete when started
157+
/// No `completed` is sent if the file was complete when started.
156158
Completed,
159+
160+
/// It is the same as not being present. If not present, this is one of the
161+
/// announcements done at regular intervals.
162+
Empty,
157163
}
158164

159165
impl FromStr for Event {
@@ -164,6 +170,7 @@ impl FromStr for Event {
164170
"started" => Ok(Self::Started),
165171
"stopped" => Ok(Self::Stopped),
166172
"completed" => Ok(Self::Completed),
173+
"empty" => Ok(Self::Empty),
167174
_ => Err(ParseAnnounceQueryError::InvalidParam {
168175
param_name: EVENT.to_owned(),
169176
param_value: raw_param.to_owned(),
@@ -179,17 +186,29 @@ impl fmt::Display for Event {
179186
Event::Started => write!(f, "started"),
180187
Event::Stopped => write!(f, "stopped"),
181188
Event::Completed => write!(f, "completed"),
189+
Event::Empty => write!(f, "empty"),
182190
}
183191
}
184192
}
185193

186194
impl From<aquatic_udp_protocol::request::AnnounceEvent> for Event {
187-
fn from(value: aquatic_udp_protocol::request::AnnounceEvent) -> Self {
188-
match value {
195+
fn from(event: aquatic_udp_protocol::request::AnnounceEvent) -> Self {
196+
match event {
189197
AnnounceEvent::Started => Self::Started,
190198
AnnounceEvent::Stopped => Self::Stopped,
191199
AnnounceEvent::Completed => Self::Completed,
192-
AnnounceEvent::None => panic!("can't convert announce event from aquatic for None variant"),
200+
AnnounceEvent::None => Self::Empty,
201+
}
202+
}
203+
}
204+
205+
impl From<Event> for aquatic_udp_protocol::request::AnnounceEvent {
206+
fn from(event: Event) -> Self {
207+
match event {
208+
Event::Started => Self::Started,
209+
Event::Stopped => Self::Stopped,
210+
Event::Completed => Self::Completed,
211+
Event::Empty => Self::None,
193212
}
194213
}
195214
}
@@ -399,19 +418,10 @@ pub fn peer_from_request(announce_request: &Announce, peer_ip: &IpAddr) -> peer:
399418
uploaded: announce_request.uploaded.unwrap_or(NumberOfBytes::new(0)),
400419
downloaded: announce_request.downloaded.unwrap_or(NumberOfBytes::new(0)),
401420
left: announce_request.left.unwrap_or(NumberOfBytes::new(0)),
402-
event: map_to_torrust_event(&announce_request.event),
403-
}
404-
}
405-
406-
#[must_use]
407-
pub fn map_to_torrust_event(event: &Option<Event>) -> AnnounceEvent {
408-
match event {
409-
Some(event) => match &event {
410-
Event::Started => AnnounceEvent::Started,
411-
Event::Stopped => AnnounceEvent::Stopped,
412-
Event::Completed => AnnounceEvent::Completed,
421+
event: match &announce_request.event {
422+
Some(event) => event.clone().into(),
423+
None => AnnounceEvent::None,
413424
},
414-
None => AnnounceEvent::None,
415425
}
416426
}
417427

@@ -444,7 +454,7 @@ mod tests {
444454
assert_eq!(
445455
announce_request,
446456
Announce {
447-
info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(),
457+
info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(), // DevSkim: ignore DS173237
448458
peer_id: PeerId(*b"-qB00000000000000001"),
449459
port: 17548,
450460
downloaded: None,
@@ -479,7 +489,7 @@ mod tests {
479489
assert_eq!(
480490
announce_request,
481491
Announce {
482-
info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(),
492+
info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap(), // DevSkim: ignore DS173237
483493
peer_id: PeerId(*b"-qB00000000000000001"),
484494
port: 17548,
485495
downloaded: Some(NumberOfBytes::new(1)),

0 commit comments

Comments
 (0)