diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 20 |
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 | |||
74 | import Control.Monad.Reader | 75 | import Control.Monad.Reader |
75 | import Control.Monad.Trans.Control | 76 | import Control.Monad.Trans.Control |
76 | import Control.Monad.Trans.Resource | 77 | import Control.Monad.Trans.Resource |
78 | import Data.ByteString | ||
77 | import Data.Conduit.Lazy | 79 | import Data.Conduit.Lazy |
78 | import Data.Default | 80 | import Data.Default |
79 | import Data.Fixed | 81 | import Data.Fixed |
@@ -89,7 +91,8 @@ import Data.Torrent as Torrent | |||
89 | import Network.KRPC as KRPC hiding (Options, def) | 91 | import Network.KRPC as KRPC hiding (Options, def) |
90 | import qualified Network.KRPC as KRPC (def) | 92 | import qualified Network.KRPC as KRPC (def) |
91 | import Network.BitTorrent.Address | 93 | import Network.BitTorrent.Address |
92 | import Network.BitTorrent.DHT.ContactInfo as P | 94 | import Network.BitTorrent.DHT.ContactInfo (PeerStore) |
95 | import qualified Network.BitTorrent.DHT.ContactInfo as P | ||
93 | import Network.BitTorrent.DHT.Message | 96 | import Network.BitTorrent.DHT.Message |
94 | import Network.BitTorrent.DHT.Routing as R | 97 | import Network.BitTorrent.DHT.Routing as R |
95 | import Network.BitTorrent.DHT.Token as T | 98 | import 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 | ||
401 | getSwarms :: Ord ip => DHT ip [ (InfoHash, Int, Maybe ByteString) ] | ||
402 | getSwarms = 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. |
419 | insertPeer :: Eq ip => InfoHash -> PeerAddr ip -> DHT ip () | 427 | insertPeer :: Ord ip => InfoHash -> Maybe ByteString -> PeerAddr ip -> DHT ip () |
420 | insertPeer ih addr = do | 428 | insertPeer 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. |
426 | lookupPeers :: InfoHash -> DHT ip [PeerAddr ip] | 434 | lookupPeers :: Ord ip => InfoHash -> DHT ip [PeerAddr ip] |
427 | lookupPeers ih = do | 435 | lookupPeers 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 | -- |
436 | getPeerList :: Eq ip => InfoHash -> DHT ip (PeerList ip) | 444 | getPeerList :: Ord ip => InfoHash -> DHT ip (PeerList ip) |
437 | getPeerList ih = do | 445 | getPeerList ih = do |
438 | ps <- lookupPeers ih | 446 | ps <- lookupPeers ih |
439 | if L.null ps | 447 | if L.null ps |