summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker/Message.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-22 06:20:23 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-22 06:20:23 +0400
commit0af9747748cc4fa5b551f835eee140e0c414a9b6 (patch)
tree9e29b944881a6213726f31ab767d0d29fd220626 /src/Network/BitTorrent/Tracker/Message.hs
parent1c0b414c732507851454cf75da1e74b1c89fed7d (diff)
Add documentation to Message module
Diffstat (limited to 'src/Network/BitTorrent/Tracker/Message.hs')
-rw-r--r--src/Network/BitTorrent/Tracker/Message.hs42
1 files changed, 12 insertions, 30 deletions
diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs
index c46d5d58..508ff4c5 100644
--- a/src/Network/BitTorrent/Tracker/Message.hs
+++ b/src/Network/BitTorrent/Tracker/Message.hs
@@ -25,23 +25,20 @@
25{-# OPTIONS -fno-warn-orphans #-} 25{-# OPTIONS -fno-warn-orphans #-}
26module Network.BitTorrent.Tracker.Message 26module Network.BitTorrent.Tracker.Message
27 ( -- * Announce 27 ( -- * Announce
28 -- ** Request
28 Event(..) 29 Event(..)
29 , AnnounceQuery(..) 30 , AnnounceQuery(..)
31 , encodeRequest
32
33 -- ** Response
30 , PeerList (..) 34 , PeerList (..)
31 , AnnounceInfo(..) 35 , AnnounceInfo(..)
32
33 -- ** Defaults
34 , defaultNumWant 36 , defaultNumWant
35 , defaultPorts
36 37
37 -- * Scrape 38 -- * Scrape
38 , ScrapeQuery 39 , ScrapeQuery
39 , ScrapeInfo(..) 40 , ScrapeInfo(..)
40 , Scrape 41 , Scrape
41
42 -- * TODO
43 , Tracker(..)
44 , scrapeOne
45 ) 42 )
46 where 43 where
47 44
@@ -212,10 +209,16 @@ instance Serialize AnnounceQuery where
212 , reqEvent = ev 209 , reqEvent = ev
213 } 210 }
214 211
212encodeRequest :: URI -> AnnounceQuery -> URI
213encodeRequest announceURI req = URL.urlEncode req
214 `addToURI` announceURI
215 `addHashToURI` reqInfoHash req
216
215{----------------------------------------------------------------------- 217{-----------------------------------------------------------------------
216-- Announce response 218-- Announce response
217-----------------------------------------------------------------------} 219-----------------------------------------------------------------------}
218 220
221-- | For more info see: <http://www.bittorrent.org/beps/bep_0023.html>
219data PeerList 222data PeerList
220 = PeerList { getPeerList :: [PeerAddr] } 223 = PeerList { getPeerList :: [PeerAddr] }
221 | CompactPeerList { getPeerList :: [PeerAddr] } 224 | CompactPeerList { getPeerList :: [PeerAddr] }
@@ -323,24 +326,16 @@ instance Serialize AnnounceInfo where
323 , respPeers = PeerList peers 326 , respPeers = PeerList peers
324 } 327 }
325 328
326-- TODO move this somewhere else
327-- | Ports typically reserved for bittorrent P2P listener.
328defaultPorts :: [PortNumber]
329defaultPorts = [6881..6889]
330
331-- | Above 25, new peers are highly unlikely to increase download 329-- | Above 25, new peers are highly unlikely to increase download
332-- speed. Even 30 peers is /plenty/, the official client version 3 330-- speed. Even 30 peers is /plenty/, the official client version 3
333-- in fact only actively forms new connections if it has less than 331-- in fact only actively forms new connections if it has less than
334-- 30 peers and will refuse connections if it has 55. 332-- 30 peers and will refuse connections if it has 55.
335-- 333--
336-- So the default value is set to 50 because usually 30-50% of peers 334-- <https://wiki.theory.org/BitTorrent_Tracker_Protocol#Basic_Tracker_Announce_Request>
337-- are not responding.
338-- 335--
339defaultNumWant :: Int 336defaultNumWant :: Int
340defaultNumWant = 50 337defaultNumWant = 50
341 338
342-- default value here: <https://wiki.theory.org/BitTorrent_Tracker_Protocol>
343
344{----------------------------------------------------------------------- 339{-----------------------------------------------------------------------
345 Scrape message 340 Scrape message
346-----------------------------------------------------------------------} 341-----------------------------------------------------------------------}
@@ -384,7 +379,7 @@ instance BEncode ScrapeInfo where
384 <*>! "incomplete" 379 <*>! "incomplete"
385 <*>? "name" 380 <*>? "name"
386 381
387-- | UDP tracker protocol complatble encoding. 382-- | UDP tracker protocol compatible encoding.
388instance Serialize ScrapeInfo where 383instance Serialize ScrapeInfo where
389 put ScrapeInfo {..} = do 384 put ScrapeInfo {..} = do
390 putWord32be $ fromIntegral siComplete 385 putWord32be $ fromIntegral siComplete
@@ -402,16 +397,3 @@ instance Serialize ScrapeInfo where
402 , siIncomplete = fromIntegral leechers 397 , siIncomplete = fromIntegral leechers
403 , siName = Nothing 398 , siName = Nothing
404 } 399 }
405
406-- | Set of tracker RPCs.
407class Tracker s where
408 connect :: URI -> IO s
409 announce :: s -> AnnounceQuery -> IO AnnounceInfo
410 scrape :: s -> ScrapeQuery -> IO Scrape
411
412-- | More particular version of 'scrape', just for one torrent.
413--
414scrapeOne :: Tracker t => t -> InfoHash -> IO ScrapeInfo
415scrapeOne uri ih = scrape uri [ih] >>= maybe err return . M.lookup ih
416 where
417 err = throwIO $ userError "unable to find info hash in response dict"