summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-28 03:11:02 -0400
committerjoe <joe@jerkface.net>2017-07-28 03:11:02 -0400
commit45a1baad8bf90a07654b6ade1a9ed2e5a2d5c92b (patch)
treeb3a3eab385fb8621c2fa5dcaca0639f8fac77ecf /src
parent81b49153a856d497a562bc1bb7867d319a26a830 (diff)
rewrite: search feature.
Diffstat (limited to 'src')
-rw-r--r--src/Data/MinMaxPSQ.hs8
-rw-r--r--src/Network/BitTorrent/DHT/Search.hs20
2 files changed, 15 insertions, 13 deletions
diff --git a/src/Data/MinMaxPSQ.hs b/src/Data/MinMaxPSQ.hs
index a48e62f9..dc9f1882 100644
--- a/src/Data/MinMaxPSQ.hs
+++ b/src/Data/MinMaxPSQ.hs
@@ -1,5 +1,9 @@
1{-# LANGUAGE BangPatterns #-} 1{-# LANGUAGE BangPatterns, PatternSynonyms #-}
2module Data.MinMaxPSQ where 2module Data.MinMaxPSQ
3 ( module Data.MinMaxPSQ
4 , Binding'
5 , pattern Binding
6 ) where
3 7
4import Data.Ord 8import Data.Ord
5import qualified Data.Wrapper.PSQ as PSQ 9import qualified Data.Wrapper.PSQ as PSQ
diff --git a/src/Network/BitTorrent/DHT/Search.hs b/src/Network/BitTorrent/DHT/Search.hs
index 51b40b2c..12fc29f6 100644
--- a/src/Network/BitTorrent/DHT/Search.hs
+++ b/src/Network/BitTorrent/DHT/Search.hs
@@ -83,8 +83,8 @@ newSearch :: ( Ord addr
83 Search nid addr tok ni r 83 Search nid addr tok ni r
84 -> nid 84 -> nid
85 -> [ni] -- Initial nodes to query. 85 -> [ni] -- Initial nodes to query.
86 -> IO (SearchState nid addr tok ni r) 86 -> STM (SearchState nid addr tok ni r)
87newSearch (Search space nAddr qry) target ns = atomically $ do 87newSearch (Search space nAddr qry) target ns = do
88 c <- newTVar 0 88 c <- newTVar 0
89 q <- newTVar $ MM.fromList 89 q <- newTVar $ MM.fromList
90 $ map (\n -> n :-> kademliaXor space target (kademliaLocation space n)) 90 $ map (\n -> n :-> kademliaXor space target (kademliaLocation space n))
@@ -102,7 +102,6 @@ searchK = 8
102 102
103sendQuery :: forall addr nid tok ni r. 103sendQuery :: forall addr nid tok ni r.
104 ( Ord addr 104 ( Ord addr
105 , Ord r
106 , PSQKey nid 105 , PSQKey nid
107 , PSQKey ni 106 , PSQKey ni
108 , Show nid 107 , Show nid
@@ -144,8 +143,7 @@ sendQuery Search{..} searchTarget searchResult sch@SearchState{..} (ni :-> d) =
144 [] -> return () 143 [] -> return ()
145 144
146 145
147searchIsFinished :: ( Ord addr 146searchIsFinished :: ( PSQKey nid
148 , PSQKey nid
149 , PSQKey ni 147 , PSQKey ni
150 ) => SearchState nid addr tok ni r -> STM Bool 148 ) => SearchState nid addr tok ni r -> STM Bool
151searchIsFinished SearchState{ ..} = do 149searchIsFinished SearchState{ ..} = do
@@ -170,13 +168,13 @@ search ::
170 , PSQKey ni 168 , PSQKey ni
171 , Show nid 169 , Show nid
172 ) => Search nid addr tok ni r -> R.BucketList ni -> nid -> (r -> STM Bool) -> IO (SearchState nid addr tok ni r) 170 ) => Search nid addr tok ni r -> R.BucketList ni -> nid -> (r -> STM Bool) -> IO (SearchState nid addr tok ni r)
173search sch@Search{..} buckets target result = do 171search sch buckets target result = do
174 let ns = R.kclosest searchSpace searchK target buckets 172 let ns = R.kclosest (searchSpace sch) searchK target buckets
175 st <- newSearch sch target ns 173 st <- atomically $ newSearch sch target ns
176 fork $ go st 174 fork $ searchLoop sch target result st
177 return st 175 return st
178 where 176
179 go s@SearchState{..} = do 177searchLoop sch@Search{..} target result s@SearchState{..} = do
180 myThreadId >>= flip labelThread ("search."++show target) 178 myThreadId >>= flip labelThread ("search."++show target)
181 withTaskGroup searchAlpha $ \g -> fix $ \again -> do 179 withTaskGroup searchAlpha $ \g -> fix $ \again -> do
182 join $ atomically $ do 180 join $ atomically $ do