summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Search.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT/Search.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Search.hs22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/Network/BitTorrent/DHT/Search.hs b/src/Network/BitTorrent/DHT/Search.hs
index 854f26c7..844b4575 100644
--- a/src/Network/BitTorrent/DHT/Search.hs
+++ b/src/Network/BitTorrent/DHT/Search.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE PatternSynonyms #-} 2{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE RecordWildCards #-} 3{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE ScopedTypeVariables #-} 4{-# LANGUAGE ScopedTypeVariables #-}
@@ -24,21 +25,28 @@ import qualified Data.Wrapper.PSQ as PSQ
24 ;import Data.Wrapper.PSQ (pattern (:->), Binding, PSQ) 25 ;import Data.Wrapper.PSQ (pattern (:->), Binding, PSQ)
25import Network.BitTorrent.Address hiding (NodeId) 26import Network.BitTorrent.Address hiding (NodeId)
26import Network.RPC 27import Network.RPC
27import Network.KRPC.Message (KMessageOf) 28#ifdef VERSION_bencoding
28import Network.DHT.Mainline () 29import Network.DHT.Mainline ()
30import Network.KRPC.Message (KMessageOf)
31type Ann = ()
32#else
33import Data.Tox as Tox
34type KMessageOf = Tox.Message
35type Ann = Bool
36#endif
29 37
30data IterativeSearch ip r = IterativeSearch 38data IterativeSearch ip r = IterativeSearch
31 { searchTarget :: NodeId KMessageOf 39 { searchTarget :: NodeId KMessageOf
32 , searchQuery :: NodeInfo KMessageOf ip () -> IO ([NodeInfo KMessageOf ip ()], [r]) 40 , searchQuery :: NodeInfo KMessageOf ip Ann -> IO ([NodeInfo KMessageOf ip Ann], [r])
33 , searchPendingCount :: TVar Int 41 , searchPendingCount :: TVar Int
34 , searchQueued :: TVar (MinMaxPSQ (NodeInfo KMessageOf ip ()) (NodeDistance (NodeId KMessageOf))) 42 , searchQueued :: TVar (MinMaxPSQ (NodeInfo KMessageOf ip Ann) (NodeDistance (NodeId KMessageOf)))
35 , searchInformant :: TVar (MinMaxPSQ (NodeInfo KMessageOf ip ()) (NodeDistance (NodeId KMessageOf))) 43 , searchInformant :: TVar (MinMaxPSQ (NodeInfo KMessageOf ip Ann) (NodeDistance (NodeId KMessageOf)))
36 , searchVisited :: TVar (Set (NodeAddr ip)) 44 , searchVisited :: TVar (Set (NodeAddr ip))
37 , searchResults :: TVar (Set r) 45 , searchResults :: TVar (Set r)
38 } 46 }
39 47
40newSearch :: Eq ip => (NodeInfo KMessageOf ip () -> IO ([NodeInfo KMessageOf ip ()], [r])) 48newSearch :: Eq ip => (NodeInfo KMessageOf ip Ann -> IO ([NodeInfo KMessageOf ip Ann], [r]))
41 -> NodeId KMessageOf -> [NodeInfo KMessageOf ip ()] -> IO (IterativeSearch ip r) 49 -> NodeId KMessageOf -> [NodeInfo KMessageOf ip Ann] -> IO (IterativeSearch ip r)
42newSearch qry target ns = atomically $ do 50newSearch qry target ns = atomically $ do
43 c <- newTVar 0 51 c <- newTVar 0
44 q <- newTVar $ MM.fromList $ map (\n -> n :-> distance target (nodeId n)) ns 52 q <- newTVar $ MM.fromList $ map (\n -> n :-> distance target (nodeId n)) ns
@@ -55,7 +63,7 @@ searchK = 8
55 63
56sendQuery :: forall a ip. (Ord a, Ord ip) => 64sendQuery :: forall a ip. (Ord a, Ord ip) =>
57 IterativeSearch ip a 65 IterativeSearch ip a
58 -> Binding (NodeInfo KMessageOf ip ()) (NodeDistance (NodeId KMessageOf)) 66 -> Binding (NodeInfo KMessageOf ip Ann) (NodeDistance (NodeId KMessageOf))
59 -> IO () 67 -> IO ()
60sendQuery IterativeSearch{..} (ni :-> d) = do 68sendQuery IterativeSearch{..} (ni :-> d) = do
61 (ns,rs) <- handle (\(SomeException e) -> return ([],[])) 69 (ns,rs) <- handle (\(SomeException e) -> return ([],[]))