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
199
200
201
202
203
204
205
206
207
208
209
use std::io::{self};
use std::net::{SocketAddr};
use std::sync::{Arc};
use std::sync::atomic::{AtomicUsize, Ordering};
use bip_handshake::{Handshaker};
use bip_util::bt::{InfoHash};
use bip_util::trans::{TIDGenerator};
use umio::external::{Sender};
use announce::{AnnounceResponse, ClientState};
use client::dispatcher::{DispatchMessage};
use client::error::{ClientResult};
use scrape::{ScrapeResponse};
mod dispatcher;
pub mod error;
const DEFAULT_CAPACITY: usize = 4096;
#[derive(Debug)]
pub enum ClientRequest {
Announce(InfoHash, ClientState),
Scrape(InfoHash)
}
#[derive(Debug)]
pub struct ClientMetadata {
token: ClientToken,
result: ClientResult<ClientResponse>
}
impl ClientMetadata {
pub fn new(token: ClientToken, result: ClientResult<ClientResponse>) -> ClientMetadata {
ClientMetadata{ token: token, result: result }
}
pub fn token(&self) -> ClientToken {
self.token
}
pub fn result(&self) -> &ClientResult<ClientResponse> {
&self.result
}
}
#[derive(Debug)]
pub enum ClientResponse {
Announce(AnnounceResponse<'static>),
Scrape(ScrapeResponse<'static>)
}
impl ClientResponse {
pub fn announce_response(&self) -> Option<&AnnounceResponse<'static>> {
match self {
&ClientResponse::Announce(ref res) => Some(res),
&ClientResponse::Scrape(_) => None
}
}
pub fn scrape_response(&self) -> Option<&ScrapeResponse<'static>> {
match self {
&ClientResponse::Announce(_) => None,
&ClientResponse::Scrape(ref res) => Some(res)
}
}
}
pub struct TrackerClient {
send: Sender<DispatchMessage>,
limiter: RequestLimiter,
generator: TokenGenerator
}
impl TrackerClient {
pub fn new<H>(bind: SocketAddr, handshaker: H) -> io::Result<TrackerClient>
where H: Handshaker + 'static, H::MetadataEnvelope: From<ClientMetadata> {
TrackerClient::with_capacity(bind, handshaker, DEFAULT_CAPACITY)
}
pub fn with_capacity<H>(bind: SocketAddr, handshaker: H, capacity: usize) -> io::Result<TrackerClient>
where H: Handshaker + 'static, H::MetadataEnvelope: From<ClientMetadata> {
let (chan_capacity, would_overflow) = capacity.overflowing_add(1);
if would_overflow {
panic!("bip_utracker: Tracker Client Capacity Must Be Less Than Max Size");
}
let limiter = RequestLimiter::new(capacity);
dispatcher::create_dispatcher(bind, handshaker, chan_capacity, limiter.clone()).map(|chan|
TrackerClient{ send: chan, limiter: limiter, generator: TokenGenerator::new() }
)
}
pub fn request(&mut self, addr: SocketAddr, request: ClientRequest) -> Option<ClientToken> {
if self.limiter.can_initiate() {
let token = self.generator.generate();
self.send.send(DispatchMessage::Request(addr, token, request))
.expect("bip_utracker: Failed To Send Client Request Message...");
Some(token)
} else {
None
}
}
}
impl Drop for TrackerClient {
fn drop(&mut self) {
self.send.send(DispatchMessage::Shutdown)
.expect("bip_utracker: Failed To Send Client Shutdown Message...");
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ClientToken(u32);
struct TokenGenerator {
generator: TIDGenerator<u32>
}
impl TokenGenerator {
pub fn new() -> TokenGenerator {
TokenGenerator{ generator: TIDGenerator::<u32>::new() }
}
pub fn generate(&mut self) -> ClientToken {
ClientToken(self.generator.generate())
}
}
#[derive(Clone)]
pub struct RequestLimiter {
active: Arc<AtomicUsize>,
capacity: usize
}
impl RequestLimiter {
pub fn new(capacity: usize) -> RequestLimiter {
RequestLimiter{ active: Arc::new(AtomicUsize::new(0)), capacity: capacity }
}
pub fn acknowledge(&self) {
self.active.fetch_sub(1, Ordering::AcqRel);
}
pub fn can_initiate(&self) -> bool {
let current_active_requests = self.active.fetch_add(1, Ordering::AcqRel);
if current_active_requests < self.capacity {
true
} else {
self.acknowledge();
false
}
}
}