diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT.hs | 61 |
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 | ||
30 | import Control.Applicative | 30 | import Control.Applicative |
31 | import Control.Exception.Lifted | ||
32 | import Control.Monad as M | ||
33 | import Control.Monad.Logger | 31 | import Control.Monad.Logger |
34 | import Control.Monad.Trans | 32 | import Control.Monad.Trans |
35 | import Data.Conduit as C | 33 | import Data.Conduit as C |
36 | import Data.Conduit.List as C | 34 | import Data.Conduit.List as C |
37 | import Data.List as L | ||
38 | import Data.Monoid | ||
39 | import Data.Text as T | ||
40 | import Network.Socket (PortNumber) | 35 | import Network.Socket (PortNumber) |
41 | import Text.PrettyPrint as PP hiding ((<>), ($$)) | ||
42 | import Text.PrettyPrint.Class | ||
43 | 36 | ||
44 | import Data.Torrent.InfoHash | 37 | import Data.Torrent.InfoHash |
45 | import Network.KRPC (QueryFailure) | ||
46 | import Network.BitTorrent.Core | 38 | import Network.BitTorrent.Core |
47 | import Network.BitTorrent.DHT.Message | ||
48 | import Network.BitTorrent.DHT.Routing | ||
49 | import Network.BitTorrent.DHT.Session | 39 | import Network.BitTorrent.DHT.Session |
50 | 40 | ||
51 | 41 | ||
52 | {----------------------------------------------------------------------- | ||
53 | -- Handlers | ||
54 | -----------------------------------------------------------------------} | ||
55 | |||
56 | pingH :: Address ip => NodeHandler ip | ||
57 | pingH = nodeHandler $ \ _ Ping -> do | ||
58 | return Ping | ||
59 | |||
60 | findNodeH :: Address ip => NodeHandler ip | ||
61 | findNodeH = nodeHandler $ \ _ (FindNode nid) -> do | ||
62 | NodeFound <$> getClosest nid | ||
63 | |||
64 | getPeersH :: Address ip => NodeHandler ip | ||
65 | getPeersH = nodeHandler $ \ naddr (GetPeers ih) -> do | ||
66 | GotPeers <$> getPeerList ih <*> grantToken naddr | ||
67 | |||
68 | announceH :: Address ip => NodeHandler ip | ||
69 | announceH = 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 | |||
76 | handlers :: Address ip => [NodeHandler ip] | ||
77 | handlers = [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> |
84 | dht :: Address ip | 43 | dht :: 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 | -- |
98 | bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () | 58 | bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () |
99 | bootstrap startNodes = do | 59 | bootstrap 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 | -- |
123 | insert :: Address ip => InfoHash -> PortNumber -> DHT ip () | 84 | insert :: Address ip => InfoHash -> PortNumber -> DHT ip () |
124 | insert ih port = do | 85 | insert 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 | -- |
135 | delete :: Address ip => InfoHash -> DHT ip () | 93 | delete :: Address ip => InfoHash -> PortNumber -> DHT ip () |
136 | delete = error "DHT.delete: not implemented" \ No newline at end of file | 94 | delete = deleteTopic |
95 | {-# INLINE delete #-} \ No newline at end of file | ||