diff options
author | joe <joe@jerkface.net> | 2017-02-01 03:21:52 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-02-01 03:21:52 -0500 |
commit | c51e64666b672637843a04c2f279d7d0c9eed01c (patch) | |
tree | d6f50018659ac3c5c3d72ee9bde3824514bd9f6a /src/Network/BitTorrent/DHT/Query.hs | |
parent | 0d1de683de78a70ce9c054b444bb6f19c39d112c (diff) |
New improved iterative search algorithm.
Diffstat (limited to 'src/Network/BitTorrent/DHT/Query.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index 8215c95d..39ef9604 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -13,6 +13,7 @@ | |||
13 | {-# LANGUAGE FlexibleContexts #-} | 13 | {-# LANGUAGE FlexibleContexts #-} |
14 | {-# LANGUAGE ScopedTypeVariables #-} | 14 | {-# LANGUAGE ScopedTypeVariables #-} |
15 | {-# LANGUAGE TemplateHaskell #-} | 15 | {-# LANGUAGE TemplateHaskell #-} |
16 | {-# LANGUAGE TupleSections #-} | ||
16 | module Network.BitTorrent.DHT.Query | 17 | module Network.BitTorrent.DHT.Query |
17 | ( -- * Handler | 18 | ( -- * Handler |
18 | -- | To bind specific set of handlers you need to pass | 19 | -- | To bind specific set of handlers you need to pass |
@@ -40,6 +41,9 @@ module Network.BitTorrent.DHT.Query | |||
40 | , Search | 41 | , Search |
41 | , search | 42 | , search |
42 | , publish | 43 | , publish |
44 | , ioFindNode | ||
45 | , ioGetPeers | ||
46 | , isearch | ||
43 | 47 | ||
44 | -- ** Routing table | 48 | -- ** Routing table |
45 | , insertNode | 49 | , insertNode |
@@ -67,6 +71,8 @@ import Data.Either | |||
67 | import Data.List as L | 71 | import Data.List as L |
68 | import Data.Monoid | 72 | import Data.Monoid |
69 | import Data.Text as T | 73 | import Data.Text as T |
74 | import qualified Data.Set as Set | ||
75 | ;import Data.Set (Set) | ||
70 | import Network | 76 | import Network |
71 | import Text.PrettyPrint as PP hiding ((<>), ($$)) | 77 | import Text.PrettyPrint as PP hiding ((<>), ($$)) |
72 | import Text.PrettyPrint.HughesPJClass hiding ((<>),($$)) | 78 | import Text.PrettyPrint.HughesPJClass hiding ((<>),($$)) |
@@ -82,6 +88,7 @@ import Network.BitTorrent.DHT.Message | |||
82 | import Network.BitTorrent.DHT.Routing as R | 88 | import Network.BitTorrent.DHT.Routing as R |
83 | import Network.BitTorrent.DHT.Session | 89 | import Network.BitTorrent.DHT.Session |
84 | import Control.Concurrent.STM | 90 | import Control.Concurrent.STM |
91 | import qualified Network.BitTorrent.DHT.Search as Search | ||
85 | 92 | ||
86 | {----------------------------------------------------------------------- | 93 | {----------------------------------------------------------------------- |
87 | -- Handlers | 94 | -- Handlers |
@@ -182,6 +189,35 @@ announceQ ih p NodeInfo {..} = do | |||
182 | -- Iterative queries | 189 | -- Iterative queries |
183 | -----------------------------------------------------------------------} | 190 | -----------------------------------------------------------------------} |
184 | 191 | ||
192 | |||
193 | ioGetPeers :: Address ip => InfoHash -> DHT ip (NodeInfo ip -> IO ([NodeInfo ip], [PeerAddr ip])) | ||
194 | ioGetPeers ih = do | ||
195 | session <- ask | ||
196 | return $ \ni -> runDHT session $ do | ||
197 | r <- try $ getPeersQ ih ni | ||
198 | case r of | ||
199 | Right e -> return $ either (,[]) ([],) e | ||
200 | Left e -> let _ = e :: QueryFailure in return ([],[]) | ||
201 | |||
202 | ioFindNode :: Address ip => InfoHash -> DHT ip (NodeInfo ip -> IO ([NodeInfo ip], [NodeInfo ip])) | ||
203 | ioFindNode ih = do | ||
204 | session <- ask | ||
205 | return $ \ni -> runDHT session $ do | ||
206 | NodeFound ns <- FindNode (toNodeId ih) <@> nodeAddr ni | ||
207 | return $ L.partition (\n -> nodeId n /= toNodeId ih) ns | ||
208 | |||
209 | isearch :: (Ord r, Ord ip) => | ||
210 | (InfoHash -> DHT ip (NodeInfo ip -> IO ([NodeInfo ip], [r]))) | ||
211 | -> InfoHash | ||
212 | -> DHT ip (Set r) | ||
213 | isearch f ih = do | ||
214 | qry <- f ih | ||
215 | ns <- kclosest 8 ih <$> getTable | ||
216 | liftIO $ do s <- Search.newSearch qry (toNodeId ih) ns | ||
217 | Search.search s | ||
218 | atomically $ readTVar (Search.searchResults s) | ||
219 | |||
220 | |||
185 | type Search ip o = Conduit [NodeInfo ip] (DHT ip) [o ip] | 221 | type Search ip o = Conduit [NodeInfo ip] (DHT ip) [o ip] |
186 | 222 | ||
187 | -- TODO: use reorder and filter (Traversal option) leftovers | 223 | -- TODO: use reorder and filter (Traversal option) leftovers |