forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.rs
More file actions
173 lines (146 loc) · 5.3 KB
/
environment.rs
File metadata and controls
173 lines (146 loc) · 5.3 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
use std::net::SocketAddr;
use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_core::container::TrackerCoreContainer;
use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer;
use torrust_server_lib::registar::Registar;
use torrust_tracker_configuration::{logging, Configuration, DEFAULT_TIMEOUT};
use torrust_tracker_primitives::peer;
use crate::container::UdpTrackerServerContainer;
use crate::server::spawner::Spawner;
use crate::server::states::{Running, Stopped};
use crate::server::Server;
pub type Started = Environment<Running>;
pub struct Environment<S>
where
S: std::fmt::Debug + std::fmt::Display,
{
pub container: Arc<EnvContainer>,
pub registar: Registar,
pub server: Server<S>,
}
impl<S> Environment<S>
where
S: std::fmt::Debug + std::fmt::Display,
{
/// Add a torrent to the tracker
#[allow(dead_code)]
pub fn add_torrent(&self, info_hash: &InfoHash, peer: &peer::Peer) {
let _number_of_downloads_increased = self
.container
.tracker_core_container
.in_memory_torrent_repository
.upsert_peer(info_hash, peer, None);
}
}
impl Environment<Stopped> {
#[allow(dead_code)]
#[must_use]
pub fn new(configuration: &Arc<Configuration>) -> Self {
initialize_global_services(configuration);
let container = Arc::new(EnvContainer::initialize(configuration));
let bind_to = container.udp_tracker_core_container.udp_tracker_config.bind_address;
let server = Server::new(Spawner::new(bind_to));
Self {
container,
registar: Registar::default(),
server,
}
}
/// # Panics
///
/// Will panic if it cannot start the server.
#[allow(dead_code)]
pub async fn start(self) -> Environment<Running> {
let cookie_lifetime = self.container.udp_tracker_core_container.udp_tracker_config.cookie_lifetime;
Environment {
container: self.container.clone(),
registar: self.registar.clone(),
server: self
.server
.start(
self.container.udp_tracker_core_container.clone(),
self.container.udp_tracker_server_container.clone(),
self.registar.give_form(),
cookie_lifetime,
)
.await
.unwrap(),
}
}
}
impl Environment<Running> {
/// # Panics
///
/// Will panic if it cannot start the server within the timeout.
pub async fn new(configuration: &Arc<Configuration>) -> Self {
tokio::time::timeout(DEFAULT_TIMEOUT, Environment::<Stopped>::new(configuration).start())
.await
.expect("it should create an environment within the timeout")
}
/// # Panics
///
/// Will panic if it cannot stop the service within the timeout.
#[allow(dead_code)]
pub async fn stop(self) -> Environment<Stopped> {
let stopped = tokio::time::timeout(DEFAULT_TIMEOUT, self.server.stop())
.await
.expect("it should stop the environment within the timeout");
Environment {
container: self.container,
registar: Registar::default(),
server: stopped.expect("it should stop the udp tracker service"),
}
}
#[must_use]
pub fn bind_address(&self) -> SocketAddr {
self.server.state.local_addr
}
}
pub struct EnvContainer {
pub tracker_core_container: Arc<TrackerCoreContainer>,
pub udp_tracker_core_container: Arc<UdpTrackerCoreContainer>,
pub udp_tracker_server_container: Arc<UdpTrackerServerContainer>,
}
impl EnvContainer {
/// # Panics
///
/// Will panic if the configuration is missing the UDP tracker configuration.
#[must_use]
pub fn initialize(configuration: &Configuration) -> Self {
let core_config = Arc::new(configuration.core.clone());
let udp_tracker_configurations = configuration.udp_trackers.clone().expect("missing UDP tracker configuration");
let udp_tracker_config = Arc::new(udp_tracker_configurations[0].clone());
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(&core_config));
let udp_tracker_core_container = UdpTrackerCoreContainer::initialize_from(&tracker_core_container, &udp_tracker_config);
let udp_tracker_server_container = UdpTrackerServerContainer::initialize(&core_config);
Self {
tracker_core_container,
udp_tracker_core_container,
udp_tracker_server_container,
}
}
}
fn initialize_global_services(configuration: &Configuration) {
initialize_static();
logging::setup(&configuration.logging);
}
fn initialize_static() {
torrust_tracker_clock::initialize_static();
bittorrent_udp_tracker_core::initialize_static();
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use tokio::time::sleep;
use torrust_tracker_test_helpers::{configuration, logging};
use crate::environment::Started;
#[tokio::test]
async fn it_should_make_and_stop_udp_server() {
logging::setup();
let env = Started::new(&configuration::ephemeral().into()).await;
sleep(Duration::from_secs(1)).await;
env.stop().await;
sleep(Duration::from_secs(1)).await;
}
}