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.hs61
1 files changed, 10 insertions, 51 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index 7c892349..71803ccf 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -28,58 +28,17 @@ module Network.BitTorrent.DHT
28 ) where 28 ) where
29 29
30import Control.Applicative 30import Control.Applicative
31import Control.Exception.Lifted
32import Control.Monad as M
33import Control.Monad.Logger 31import Control.Monad.Logger
34import Control.Monad.Trans 32import Control.Monad.Trans
35import Data.Conduit as C 33import Data.Conduit as C
36import Data.Conduit.List as C 34import Data.Conduit.List as C
37import Data.List as L
38import Data.Monoid
39import Data.Text as T
40import Network.Socket (PortNumber) 35import Network.Socket (PortNumber)
41import Text.PrettyPrint as PP hiding ((<>), ($$))
42import Text.PrettyPrint.Class
43 36
44import Data.Torrent.InfoHash 37import Data.Torrent.InfoHash
45import Network.KRPC (QueryFailure)
46import Network.BitTorrent.Core 38import Network.BitTorrent.Core
47import Network.BitTorrent.DHT.Message
48import Network.BitTorrent.DHT.Routing
49import Network.BitTorrent.DHT.Session 39import Network.BitTorrent.DHT.Session
50 40
51 41
52{-----------------------------------------------------------------------
53-- Handlers
54-----------------------------------------------------------------------}
55
56pingH :: Address ip => NodeHandler ip
57pingH = nodeHandler $ \ _ Ping -> do
58 return Ping
59
60findNodeH :: Address ip => NodeHandler ip
61findNodeH = nodeHandler $ \ _ (FindNode nid) -> do
62 NodeFound <$> getClosest nid
63
64getPeersH :: Address ip => NodeHandler ip
65getPeersH = nodeHandler $ \ naddr (GetPeers ih) -> do
66 GotPeers <$> getPeerList ih <*> grantToken naddr
67
68announceH :: Address ip => NodeHandler ip
69announceH = nodeHandler $ \ naddr @ NodeAddr {..} (Announce {..}) -> do
70 checkToken naddr sessionToken
71 let annPort = if impliedPort then nodePort else port
72 let peerAddr = PeerAddr Nothing nodeHost annPort
73 insertPeer topic peerAddr
74 return Announced
75
76handlers :: Address ip => [NodeHandler ip]
77handlers = [pingH, findNodeH, getPeersH, announceH]
78
79{-----------------------------------------------------------------------
80-- DHT operations
81-----------------------------------------------------------------------}
82
83-- | Run DHT on specified port. <add note about resources> 42-- | Run DHT on specified port. <add note about resources>
84dht :: Address ip 43dht :: Address ip
85 => Options -- ^ normally you need to use 'Data.Default.def'; 44 => Options -- ^ normally you need to use 'Data.Default.def';
@@ -93,7 +52,8 @@ dht = runDHT handlers
93-- usually obtained from 'Data.Torrent.tNodes' field. Bootstrapping 52-- usually obtained from 'Data.Torrent.tNodes' field. Bootstrapping
94-- process can take up to 5 minutes. 53-- process can take up to 5 minutes.
95-- 54--
96-- This operation is synchronous and do block, use 'async' if needed. 55-- This operation is synchronous and do block, use
56-- 'Control.Concurrent.Async.Lifted.async' if needed.
97-- 57--
98bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () 58bootstrap :: Address ip => [NodeAddr ip] -> DHT ip ()
99bootstrap startNodes = do 59bootstrap startNodes = do
@@ -118,19 +78,18 @@ lookup topic = do -- TODO retry getClosest if bucket is empty
118-- | Announce that /this/ peer may have some pieces of the specified 78-- | Announce that /this/ peer may have some pieces of the specified
119-- torrent. 79-- torrent.
120-- 80--
121-- This operation is synchronous and do block, use 'async' if needed. 81-- This operation is synchronous and do block, use
82-- 'Control.Concurrent.Async.Lifted.async' if needed.
122-- 83--
123insert :: Address ip => InfoHash -> PortNumber -> DHT ip () 84insert :: Address ip => InfoHash -> PortNumber -> DHT ip ()
124insert ih port = do 85insert ih p = do
125 nodes <- getClosest ih 86 publish ih p
126 forM_ (nodeAddr <$> nodes) $ \ addr -> do 87 insertTopic ih p
127-- GotPeers {..} <- GetPeers ih <@> addr
128-- Announced <- Announce False ih undefined grantedToken <@> addr
129 return ()
130 88
131-- | Stop announcing /this/ peer for the specified torrent. 89-- | Stop announcing /this/ peer for the specified torrent.
132-- 90--
133-- This operation is atomic and may block for a while. 91-- This operation is atomic and may block for a while.
134-- 92--
135delete :: Address ip => InfoHash -> DHT ip () 93delete :: Address ip => InfoHash -> PortNumber -> DHT ip ()
136delete = error "DHT.delete: not implemented" \ No newline at end of file 94delete = deleteTopic
95{-# INLINE delete #-} \ No newline at end of file