summaryrefslogtreecommitdiff
path: root/src/Network/Torrent/Tracker.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-17 15:34:38 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-17 15:34:38 +0400
commitf165e574c8254dbaac892b6cfb29916e9bc1a63b (patch)
treec0fc7a3de75f16567d832d3dc3b0f0bfcbc87653 /src/Network/Torrent/Tracker.hs
parent0fe0fd3f825a5445179d869336dfef2c49fff1fd (diff)
+ Implement all request constructors.
Diffstat (limited to 'src/Network/Torrent/Tracker.hs')
-rw-r--r--src/Network/Torrent/Tracker.hs57
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
161defaultPorts :: [PortNumber] 161defaultPorts :: [PortNumber]
162defaultPorts = [6881..6889] 162defaultPorts = [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--
164defaultNumWant :: Int 169defaultNumWant :: Int
165defaultNumWant = 25 170defaultNumWant = 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 195genericReq :: TSession -> Progress -> TRequest
190-- | The first request to the tracker that should be created is 'startedReq'. 196genericReq ses pr = TRequest {
191-- It includes necessary 'Started' event field.
192--
193startedReq :: TSession -> Progress -> TRequest
194startedReq 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--
215startedReq :: TSession -> Progress -> TRequest
216startedReq 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--
214regularReq :: TRequest 226regularReq :: Int -> TSession -> Progress -> TRequest
215regularReq = undefined 227regularReq 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--
219stoppedReq :: TRequest 235stoppedReq :: TSession -> Progress -> TRequest
220stoppedReq = undefined 236stoppedReq 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--
225completedReq :: TRequest 245completedReq :: TSession -> Progress -> TRequest
226completedReq = undefined 246completedReq 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