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
82 lines (67 loc) · 2.42 KB
/
environment.rs
File metadata and controls
82 lines (67 loc) · 2.42 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
use std::sync::Arc;
use bittorrent_primitives::info_hash::InfoHash;
use futures::executor::block_on;
use torrust_tracker::bootstrap::app::initialize_with_configuration;
use torrust_tracker::bootstrap::jobs::make_rust_tls;
use torrust_tracker::core::Tracker;
use torrust_tracker::servers::http::server::{HttpServer, Launcher, Running, Stopped};
use torrust_tracker::servers::registar::Registar;
use torrust_tracker_configuration::{Configuration, HttpTracker};
use torrust_tracker_primitives::peer;
pub struct Environment<S> {
pub config: Arc<HttpTracker>,
pub tracker: Arc<Tracker>,
pub registar: Registar,
pub server: HttpServer<S>,
}
impl<S> Environment<S> {
/// Add a torrent to the tracker
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
self.tracker.upsert_peer_and_get_stats(info_hash, peer);
}
}
impl Environment<Stopped> {
#[allow(dead_code)]
pub fn new(configuration: &Arc<Configuration>) -> Self {
let tracker = initialize_with_configuration(configuration);
let http_tracker = configuration
.http_trackers
.clone()
.expect("missing HTTP tracker configuration");
let config = Arc::new(http_tracker[0].clone());
let bind_to = config.bind_address;
let tls = block_on(make_rust_tls(&config.tsl_config)).map(|tls| tls.expect("tls config failed"));
let server = HttpServer::new(Launcher::new(bind_to, tls));
Self {
config,
tracker,
registar: Registar::default(),
server,
}
}
#[allow(dead_code)]
pub async fn start(self) -> Environment<Running> {
Environment {
config: self.config,
tracker: self.tracker.clone(),
registar: self.registar.clone(),
server: self.server.start(self.tracker, self.registar.give_form()).await.unwrap(),
}
}
}
impl Environment<Running> {
pub async fn new(configuration: &Arc<Configuration>) -> Self {
Environment::<Stopped>::new(configuration).start().await
}
pub async fn stop(self) -> Environment<Stopped> {
Environment {
config: self.config,
tracker: self.tracker,
registar: Registar::default(),
server: self.server.stop().await.unwrap(),
}
}
pub fn bind_address(&self) -> &std::net::SocketAddr {
&self.server.state.binding
}
}