summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT.hs')
-rw-r--r--src/Network/BitTorrent/DHT.hs36
1 files changed, 25 insertions, 11 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index 38de1f91..c35b5bd6 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -16,6 +16,7 @@
16-- 16--
17{-# LANGUAGE FlexibleInstances #-} 17{-# LANGUAGE FlexibleInstances #-}
18{-# LANGUAGE TemplateHaskell #-} 18{-# LANGUAGE TemplateHaskell #-}
19{-# LANGUAGE TypeOperators #-}
19module Network.BitTorrent.DHT 20module Network.BitTorrent.DHT
20 ( -- * Distributed Hash Table 21 ( -- * Distributed Hash Table
21 DHT 22 DHT
@@ -29,13 +30,16 @@ module Network.BitTorrent.DHT
29import Control.Applicative 30import Control.Applicative
30import Control.Concurrent.Lifted hiding (yield) 31import Control.Concurrent.Lifted hiding (yield)
31import Control.Exception.Lifted 32import Control.Exception.Lifted
32import Control.Monad 33import Control.Monad as M
33import Control.Monad.Logger 34import Control.Monad.Logger
35import Control.Monad.Trans
36import Data.Conduit as C
37import Data.Conduit.List as C
34import Data.List as L 38import Data.List as L
35import Data.Monoid 39import Data.Monoid
36import Data.Text as T 40import Data.Text as T
37import Network.Socket (PortNumber) 41import Network.Socket (PortNumber)
38import Text.PrettyPrint as PP hiding ((<>)) 42import Text.PrettyPrint as PP hiding ((<>), ($$))
39import Text.PrettyPrint.Class 43import Text.PrettyPrint.Class
40 44
41import Data.Torrent.InfoHash 45import Data.Torrent.InfoHash
@@ -82,6 +86,20 @@ handlers = [pingH, findNodeH, getPeersH, announceH]
82-- Query 86-- Query
83-----------------------------------------------------------------------} 87-----------------------------------------------------------------------}
84 88
89findNodeQ :: Address ip => NodeId -> Iteration ip NodeAddr NodeInfo
90findNodeQ nid addr = do
91 NodeFound closest <- FindNode nid <@> addr
92 return $ Right closest
93
94getPeersQ :: Address ip => InfoHash -> Iteration ip NodeInfo PeerAddr
95getPeersQ topic NodeInfo {..} = do
96 GotPeers {..} <- GetPeers topic <@> nodeAddr
97 return peers
98
99{-----------------------------------------------------------------------
100-- DHT operations
101-----------------------------------------------------------------------}
102
85-- | Run DHT on specified port. <add note about resources> 103-- | Run DHT on specified port. <add note about resources>
86dht :: Address ip 104dht :: Address ip
87 => Options -- ^ normally you need to use 'Data.Default.def'; 105 => Options -- ^ normally you need to use 'Data.Default.def';
@@ -100,7 +118,7 @@ dht = runDHT handlers
100bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () 118bootstrap :: Address ip => [NodeAddr ip] -> DHT ip ()
101bootstrap startNodes = do 119bootstrap startNodes = do
102 $(logInfoS) "bootstrap" "Start node bootstrapping" 120 $(logInfoS) "bootstrap" "Start node bootstrapping"
103 mapM_ insertClosest startNodes 121 M.mapM_ insertClosest startNodes
104 $(logInfoS) "bootstrap" "Node bootstrapping finished" 122 $(logInfoS) "bootstrap" "Node bootstrapping finished"
105 where 123 where
106 insertClosest addr = do 124 insertClosest addr = do
@@ -124,14 +142,10 @@ bootstrap startNodes = do
124-- 142--
125-- (TODO) This operation is synchronous and do block. 143-- (TODO) This operation is synchronous and do block.
126-- 144--
127lookup :: Address ip => InfoHash -> DHT ip [PeerAddr ip] 145lookup :: Address ip => InfoHash -> DHT ip `Source` [PeerAddr ip]
128lookup topic = getClosestHash topic >>= collect 146lookup topic = do -- TODO retry getClosestHash if bucket is empty
129 -- TODO retry getClosestHash if bucket is empty 147 closest <- lift $ getClosestHash topic
130 where 148 sourceList [closest] $= search (getPeersQ topic)
131 collect nodes = L.concat <$> forM (nodeAddr <$> nodes) retrieve
132 retrieve addr = do
133 GotPeers {..} <- GetPeers topic <@> addr
134 either collect pure peers
135 149
136-- | Announce that /this/ peer may have some pieces of the specified 150-- | Announce that /this/ peer may have some pieces of the specified
137-- torrent. 151-- torrent.