diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT/Search.hs')
-rw-r--r-- | src/Network/BitTorrent/DHT/Search.hs | 22 |
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) |
25 | import Network.BitTorrent.Address hiding (NodeId) | 26 | import Network.BitTorrent.Address hiding (NodeId) |
26 | import Network.RPC | 27 | import Network.RPC |
27 | import Network.KRPC.Message (KMessageOf) | 28 | #ifdef VERSION_bencoding |
28 | import Network.DHT.Mainline () | 29 | import Network.DHT.Mainline () |
30 | import Network.KRPC.Message (KMessageOf) | ||
31 | type Ann = () | ||
32 | #else | ||
33 | import Data.Tox as Tox | ||
34 | type KMessageOf = Tox.Message | ||
35 | type Ann = Bool | ||
36 | #endif | ||
29 | 37 | ||
30 | data IterativeSearch ip r = IterativeSearch | 38 | data 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 | ||
40 | newSearch :: Eq ip => (NodeInfo KMessageOf ip () -> IO ([NodeInfo KMessageOf ip ()], [r])) | 48 | newSearch :: 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) |
42 | newSearch qry target ns = atomically $ do | 50 | newSearch 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 | ||
56 | sendQuery :: forall a ip. (Ord a, Ord ip) => | 64 | sendQuery :: 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 () |
60 | sendQuery IterativeSearch{..} (ni :-> d) = do | 68 | sendQuery IterativeSearch{..} (ni :-> d) = do |
61 | (ns,rs) <- handle (\(SomeException e) -> return ([],[])) | 69 | (ns,rs) <- handle (\(SomeException e) -> return ([],[])) |