summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Session.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index b85e97fa..339b18eb 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -53,6 +53,7 @@ module Network.BitTorrent.DHT.Session
53 -- ** Routing table 53 -- ** Routing table
54 , getTable 54 , getTable
55 , getClosest 55 , getClosest
56 , getSwarms
56 57
57 -- ** Peer storage 58 -- ** Peer storage
58 , insertPeer 59 , insertPeer
@@ -74,6 +75,7 @@ import Control.Monad.Logger
74import Control.Monad.Reader 75import Control.Monad.Reader
75import Control.Monad.Trans.Control 76import Control.Monad.Trans.Control
76import Control.Monad.Trans.Resource 77import Control.Monad.Trans.Resource
78import Data.ByteString
77import Data.Conduit.Lazy 79import Data.Conduit.Lazy
78import Data.Default 80import Data.Default
79import Data.Fixed 81import Data.Fixed
@@ -89,7 +91,8 @@ import Data.Torrent as Torrent
89import Network.KRPC as KRPC hiding (Options, def) 91import Network.KRPC as KRPC hiding (Options, def)
90import qualified Network.KRPC as KRPC (def) 92import qualified Network.KRPC as KRPC (def)
91import Network.BitTorrent.Address 93import Network.BitTorrent.Address
92import Network.BitTorrent.DHT.ContactInfo as P 94import Network.BitTorrent.DHT.ContactInfo (PeerStore)
95import qualified Network.BitTorrent.DHT.ContactInfo as P
93import Network.BitTorrent.DHT.Message 96import Network.BitTorrent.DHT.Message
94import Network.BitTorrent.DHT.Routing as R 97import Network.BitTorrent.DHT.Routing as R
95import Network.BitTorrent.DHT.Token as T 98import Network.BitTorrent.DHT.Token as T
@@ -395,6 +398,11 @@ getTable = do
395 let nil = nullTable myId (optBucketCount opts) 398 let nil = nullTable myId (optBucketCount opts)
396 liftIO (maybe nil R.myBuckets <$> atomically (readTVar var)) 399 liftIO (maybe nil R.myBuckets <$> atomically (readTVar var))
397 400
401getSwarms :: Ord ip => DHT ip [ (InfoHash, Int, Maybe ByteString) ]
402getSwarms = do
403 store <- asks contactInfo >>= liftIO . atomically . readTVar
404 return $ P.knownSwarms store
405
398-- | Find a set of closest nodes from routing table of this node. (in 406-- | Find a set of closest nodes from routing table of this node. (in
399-- no particular order) 407-- no particular order)
400-- 408--
@@ -416,14 +424,14 @@ refreshContacts =
416 424
417 425
418-- | Insert peer to peer store. Used to handle announce requests. 426-- | Insert peer to peer store. Used to handle announce requests.
419insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip () 427insertPeer :: Ord ip => InfoHash -> Maybe ByteString -> PeerAddr ip -> DHT ip ()
420insertPeer ih addr = do 428insertPeer ih name addr = do
421 refreshContacts 429 refreshContacts
422 var <- asks contactInfo 430 var <- asks contactInfo
423 liftIO $ atomically $ modifyTVar' var (P.insert ih addr) 431 liftIO $ atomically $ modifyTVar' var (P.insertPeer ih name addr)
424 432
425-- | Get peer set for specific swarm. 433-- | Get peer set for specific swarm.
426lookupPeers :: InfoHash -> DHT ip [PeerAddr ip] 434lookupPeers :: Ord ip => InfoHash -> DHT ip [PeerAddr ip]
427lookupPeers ih = do 435lookupPeers ih = do
428 refreshContacts 436 refreshContacts
429 var <- asks contactInfo 437 var <- asks contactInfo
@@ -433,7 +441,7 @@ lookupPeers ih = do
433-- 441--
434-- This operation use 'getClosest' as failback so it may block. 442-- This operation use 'getClosest' as failback so it may block.
435-- 443--
436getPeerList :: Eq ip => InfoHash -> DHT ip (PeerList ip) 444getPeerList :: Ord ip => InfoHash -> DHT ip (PeerList ip)
437getPeerList ih = do 445getPeerList ih = do
438 ps <- lookupPeers ih 446 ps <- lookupPeers ih
439 if L.null ps 447 if L.null ps