forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.rs
More file actions
198 lines (167 loc) · 6.22 KB
/
event.rs
File metadata and controls
198 lines (167 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::net::{IpAddr, SocketAddr};
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::RemoteClientAddr;
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
use torrust_tracker_metrics::label_name;
use torrust_tracker_primitives::peer::PeerAnnouncement;
use torrust_tracker_primitives::service_binding::ServiceBinding;
/// A HTTP core event.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Event {
TcpAnnounce {
connection: ConnectionContext,
info_hash: InfoHash,
announcement: PeerAnnouncement,
},
TcpScrape {
connection: ConnectionContext,
},
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ConnectionContext {
client: ClientConnectionContext,
server: ServerConnectionContext,
}
impl ConnectionContext {
#[must_use]
pub fn new(remote_client_addr: RemoteClientAddr, server_service_binding: ServiceBinding) -> Self {
Self {
client: ClientConnectionContext { remote_client_addr },
server: ServerConnectionContext {
service_binding: server_service_binding,
},
}
}
#[must_use]
pub fn client_ip_addr(&self) -> IpAddr {
self.client.ip_addr()
}
#[must_use]
pub fn client_port(&self) -> Option<u16> {
self.client.port()
}
#[must_use]
pub fn server_socket_addr(&self) -> SocketAddr {
self.server.service_binding.bind_address()
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ClientConnectionContext {
remote_client_addr: RemoteClientAddr,
}
impl ClientConnectionContext {
#[must_use]
pub fn ip_addr(&self) -> IpAddr {
self.remote_client_addr.ip()
}
#[must_use]
pub fn port(&self) -> Option<u16> {
self.remote_client_addr.port()
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ServerConnectionContext {
service_binding: ServiceBinding,
}
impl From<ConnectionContext> for LabelSet {
fn from(connection_context: ConnectionContext) -> Self {
LabelSet::from([
(
label_name!("server_binding_protocol"),
LabelValue::new(&connection_context.server.service_binding.protocol().to_string()),
),
(
label_name!("server_binding_ip"),
LabelValue::new(&connection_context.server.service_binding.bind_address().ip().to_string()),
),
(
label_name!("server_binding_port"),
LabelValue::new(&connection_context.server.service_binding.bind_address().port().to_string()),
),
])
}
}
pub mod sender {
use std::sync::Arc;
use super::Event;
pub type Sender = Option<Arc<dyn torrust_tracker_events::sender::Sender<Event = Event>>>;
pub type Broadcaster = torrust_tracker_events::broadcaster::Broadcaster<Event>;
}
pub mod receiver {
use super::Event;
pub type Receiver = Box<dyn torrust_tracker_events::receiver::Receiver<Event = Event>>;
}
pub mod bus {
use crate::event::Event;
pub type EventBus = torrust_tracker_events::bus::EventBus<Event>;
}
#[cfg(test)]
pub mod test {
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::{RemoteClientAddr, ResolvedIp};
use torrust_tracker_primitives::peer::Peer;
use torrust_tracker_primitives::service_binding::Protocol;
use super::Event;
use crate::tests::sample_info_hash;
#[must_use]
pub fn announce_events_match(event: &Event, expected_event: &Event) -> bool {
match (event, expected_event) {
(
Event::TcpAnnounce {
connection,
info_hash,
announcement,
},
Event::TcpAnnounce {
connection: expected_connection,
info_hash: expected_info_hash,
announcement: expected_announcement,
},
) => {
*connection == *expected_connection
&& *info_hash == *expected_info_hash
&& announcement.peer_id == expected_announcement.peer_id
&& announcement.peer_addr == expected_announcement.peer_addr
// Events can't be compared due to the `updated` field.
// The `announcement.uploaded` contains the current time
// when the test is executed.
// todo: mock time
//&& announcement.updated == expected_announcement.updated
&& announcement.uploaded == expected_announcement.uploaded
&& announcement.downloaded == expected_announcement.downloaded
&& announcement.left == expected_announcement.left
&& announcement.event == expected_announcement.event
}
_ => false,
}
}
#[test]
fn events_should_be_comparable() {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use torrust_tracker_primitives::service_binding::ServiceBinding;
use crate::event::{ConnectionContext, Event};
let remote_client_ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
let info_hash = sample_info_hash();
let event1 = Event::TcpAnnounce {
connection: ConnectionContext::new(
RemoteClientAddr::new(ResolvedIp::FromSocketAddr(remote_client_ip), Some(8080)),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
),
info_hash,
announcement: Peer::default(),
};
let event2 = Event::TcpAnnounce {
connection: ConnectionContext::new(
RemoteClientAddr::new(
ResolvedIp::FromSocketAddr(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2))),
Some(8080),
),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070)).unwrap(),
),
info_hash,
announcement: Peer::default(),
};
let event1_clone = event1.clone();
assert!(event1 == event1_clone);
assert!(event1 != event2);
}
}