diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 15:34:38 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 15:34:38 +0400 |
commit | f165e574c8254dbaac892b6cfb29916e9bc1a63b (patch) | |
tree | c0fc7a3de75f16567d832d3dc3b0f0bfcbc87653 /src | |
parent | 0fe0fd3f825a5445179d869336dfef2c49fff1fd (diff) |
+ Implement all request constructors.
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/Torrent/Tracker.hs | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/src/Network/Torrent/Tracker.hs b/src/Network/Torrent/Tracker.hs index cd6d2637..0d38cc05 100644 --- a/src/Network/Torrent/Tracker.hs +++ b/src/Network/Torrent/Tracker.hs | |||
@@ -12,8 +12,8 @@ module Network.Torrent.Tracker | |||
12 | , TResponse(..) | 12 | , TResponse(..) |
13 | , sendRequest | 13 | , sendRequest |
14 | 14 | ||
15 | -- * Extra | 15 | -- * Defaults |
16 | , defaultPorts | 16 | , defaultPorts, defaultNumWant |
17 | ) | 17 | ) |
18 | where | 18 | where |
19 | 19 | ||
@@ -161,9 +161,15 @@ encodeRequest req = URL.urlEncode req | |||
161 | defaultPorts :: [PortNumber] | 161 | defaultPorts :: [PortNumber] |
162 | defaultPorts = [6881..6889] | 162 | defaultPorts = [6881..6889] |
163 | 163 | ||
164 | -- | Above 25, new peers are highly unlikely to increase download speed. | ||
165 | -- Even 30 peers is _plenty_, the official client version 3 in fact only | ||
166 | -- actively forms new connections if it has less than 30 peers and will | ||
167 | -- refuse connections if it has 55. So default value is set to 25. | ||
168 | -- | ||
164 | defaultNumWant :: Int | 169 | defaultNumWant :: Int |
165 | defaultNumWant = 25 | 170 | defaultNumWant = 25 |
166 | 171 | ||
172 | |||
167 | -- | 'TSession' (shorthand for Tracker session) combines tracker request | 173 | -- | 'TSession' (shorthand for Tracker session) combines tracker request |
168 | -- fields neccessary for tracker, torrent and client identification. | 174 | -- fields neccessary for tracker, torrent and client identification. |
169 | -- This data is considered as static within one session. | 175 | -- This data is considered as static within one session. |
@@ -185,14 +191,9 @@ data Progress = Progress { | |||
185 | , prLeft :: Integer -- ^ Total amount of bytes left. | 191 | , prLeft :: Integer -- ^ Total amount of bytes left. |
186 | } deriving Show | 192 | } deriving Show |
187 | 193 | ||
188 | 194 | -- | used to avoid boilerplate; do NOT export me | |
189 | 195 | genericReq :: TSession -> Progress -> TRequest | |
190 | -- | The first request to the tracker that should be created is 'startedReq'. | 196 | genericReq ses pr = TRequest { |
191 | -- It includes necessary 'Started' event field. | ||
192 | -- | ||
193 | startedReq :: TSession -> Progress -> TRequest | ||
194 | startedReq ses pr = | ||
195 | TRequest { | ||
196 | reqAnnounce = tsesAnnounce ses | 197 | reqAnnounce = tsesAnnounce ses |
197 | , reqInfoHash = tsesInfoHash ses | 198 | , reqInfoHash = tsesInfoHash ses |
198 | , reqPeerID = tsesPeerID ses | 199 | , reqPeerID = tsesPeerID ses |
@@ -203,6 +204,17 @@ startedReq ses pr = | |||
203 | , reqLeft = prLeft pr | 204 | , reqLeft = prLeft pr |
204 | 205 | ||
205 | , reqIP = Nothing | 206 | , reqIP = Nothing |
207 | , reqNumWant = Nothing | ||
208 | , reqEvent = Nothing | ||
209 | } | ||
210 | |||
211 | |||
212 | -- | The first request to the tracker that should be created is 'startedReq'. | ||
213 | -- It includes necessary 'Started' event field. | ||
214 | -- | ||
215 | startedReq :: TSession -> Progress -> TRequest | ||
216 | startedReq ses pr = (genericReq ses pr) { | ||
217 | reqIP = Nothing | ||
206 | , reqNumWant = Just defaultNumWant | 218 | , reqNumWant = Just defaultNumWant |
207 | , reqEvent = Just Started | 219 | , reqEvent = Just Started |
208 | } | 220 | } |
@@ -211,20 +223,31 @@ startedReq ses pr = | |||
211 | -- notify tracker about current state of the client | 223 | -- notify tracker about current state of the client |
212 | -- so new peers could connect to the client. | 224 | -- so new peers could connect to the client. |
213 | -- | 225 | -- |
214 | regularReq :: TRequest | 226 | regularReq :: Int -> TSession -> Progress -> TRequest |
215 | regularReq = undefined | 227 | regularReq numWant ses pr = (genericReq ses pr) { |
228 | reqIP = Nothing | ||
229 | , reqNumWant = Just numWant | ||
230 | , reqEvent = Nothing | ||
231 | } | ||
216 | 232 | ||
217 | -- | Must be sent to the tracker if the client is shutting down gracefully. | 233 | -- | Must be sent to the tracker if the client is shutting down gracefully. |
218 | -- | 234 | -- |
219 | stoppedReq :: TRequest | 235 | stoppedReq :: TSession -> Progress -> TRequest |
220 | stoppedReq = undefined | 236 | stoppedReq ses pr = (genericReq ses pr) { |
237 | reqIP = Nothing | ||
238 | , reqNumWant = Nothing | ||
239 | , reqEvent = Just Stopped | ||
240 | } | ||
221 | 241 | ||
222 | -- | Must be sent to the tracker when the download completes. | 242 | -- | Must be sent to the tracker when the download completes. |
223 | -- However, must not be sent if the download was already 100% complete. | 243 | -- However, must not be sent if the download was already 100% complete. |
224 | -- | 244 | -- |
225 | completedReq :: TRequest | 245 | completedReq :: TSession -> Progress -> TRequest |
226 | completedReq = undefined | 246 | completedReq ses pr = (genericReq ses pr) { |
227 | 247 | reqIP = Nothing | |
248 | , reqNumWant = Nothing | ||
249 | , reqEvent = Just Completed | ||
250 | } | ||
228 | 251 | ||
229 | 252 | ||
230 | -- | TODO rename to ask for peers | 253 | -- | TODO rename to ask for peers |