summaryrefslogtreecommitdiff
path: root/dht/Announcer/Tox.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-03 21:27:50 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-07 13:24:59 -0500
commitc7fb8cfe16f821e4e148d1855a18cb81255743bc (patch)
treec035afc9ff870ea3bfc5b1dc7c4254ad0c0bf4b3 /dht/Announcer/Tox.hs
parent5ea2de4e858cc89282561922bae257b6f9041d2e (diff)
Async search.
Diffstat (limited to 'dht/Announcer/Tox.hs')
-rw-r--r--dht/Announcer/Tox.hs28
1 files changed, 16 insertions, 12 deletions
diff --git a/dht/Announcer/Tox.hs b/dht/Announcer/Tox.hs
index e2459e0e..00eb219b 100644
--- a/dht/Announcer/Tox.hs
+++ b/dht/Announcer/Tox.hs
@@ -27,22 +27,23 @@ import Data.Time.Clock.POSIX
27announceK :: Int 27announceK :: Int
28announceK = 8 28announceK = 8
29 29
30data AnnounceState = forall nid addr tok ni r. AnnounceState 30data AnnounceState = forall nid addr tok ni r qk. AnnounceState
31 { aState :: SearchState nid addr tok ni r 31 { aState :: SearchState nid addr tok ni r qk
32 , aStoringNodes :: TVar (MM.MinMaxPSQ ni (Down POSIXTime)) 32 , aStoringNodes :: TVar (MM.MinMaxPSQ ni (Down POSIXTime))
33 } 33 }
34 34
35-- | This type specifies an item that can be announced on appropriate nodes in 35-- | This type specifies an item that can be announced on appropriate nodes in
36-- a Kademlia network. 36-- a Kademlia network.
37data AnnounceMethod r = forall nid ni sr addr tok a. 37data AnnounceMethod r = forall nid ni sr addr tok a qk.
38 ( Show nid 38 ( Show nid
39 , Hashable nid 39 , Hashable nid
40 , Hashable ni 40 , Hashable ni
41 , Ord addr 41 , Ord addr
42 , Ord nid 42 , Ord nid
43 , Ord ni 43 , Ord ni
44 , Ord qk
44 ) => AnnounceMethod 45 ) => AnnounceMethod
45 { aSearch :: Search nid addr tok ni sr 46 { aSearch :: Search nid addr tok ni sr qk
46 -- ^ This is the Kademlia search to run repeatedly to find the 47 -- ^ This is the Kademlia search to run repeatedly to find the
47 -- nearby nodes. A new search is started whenever one is not 48 -- nearby nodes. A new search is started whenever one is not
48 -- already in progress at announce time. Repeated searches are 49 -- already in progress at announce time. Repeated searches are
@@ -72,15 +73,16 @@ data AnnounceMethod r = forall nid ni sr addr tok a.
72 } 73 }
73 74
74-- | This type specifies a Kademlia search and an action to perform upon the result. 75-- | This type specifies a Kademlia search and an action to perform upon the result.
75data SearchMethod r = forall nid ni sr addr tok a. 76data SearchMethod r = forall nid ni sr addr tok a qk.
76 ( Show nid 77 ( Show nid
77 , Hashable nid 78 , Hashable nid
78 , Hashable ni 79 , Hashable ni
79 , Ord addr 80 , Ord addr
80 , Ord nid 81 , Ord nid
81 , Ord ni 82 , Ord ni
83 , Ord qk
82 ) => SearchMethod 84 ) => SearchMethod
83 { sSearch :: Search nid addr tok ni sr 85 { sSearch :: Search nid addr tok ni sr qk
84 -- ^ This is the Kademlia search to run repeatedly to find the 86 -- ^ This is the Kademlia search to run repeatedly to find the
85 -- nearby nodes. A new search is started whenever one is not 87 -- nearby nodes. A new search is started whenever one is not
86 -- already in progress at announce time. Repeated searches are 88 -- already in progress at announce time. Repeated searches are
@@ -155,8 +157,6 @@ scheduleAnnounce announcer k AnnounceMethod{aSearch,aPublish,aNearestNodes,aTar
155 publishToNodes is 157 publishToNodes is
156 onResult sr = return True 158 onResult sr = return True
157 searchAgain = do 159 searchAgain = do
158 -- Canceling a pending search here seems to make announcements more reliable.
159 searchCancel st
160 return $ void $ do 160 return $ void $ do
161 t <- fork search 161 t <- fork search
162 labelThread t ("scheduleAnnounce.sch." ++ show aTarget) 162 labelThread t ("scheduleAnnounce.sch." ++ show aTarget)
@@ -164,7 +164,10 @@ scheduleAnnounce announcer k AnnounceMethod{aSearch,aPublish,aNearestNodes,aTar
164 got <- tryTakeMVar mutex 164 got <- tryTakeMVar mutex
165 case got of 165 case got of
166 Just () -> do 166 Just () -> do
167 atomically $ reset aNearestNodes aSearch aTarget st 167 me <- myThreadId
168 labelThread me "scheduleAnnounce.reset"
169 reset aNearestNodes aSearch aTarget st
170 labelThread me "scheduleAnnounce.searchLoop"
168 searchLoop aSearch aTarget onResult st 171 searchLoop aSearch aTarget onResult st
169 -- Announce to any nodes we haven't already announced to. 172 -- Announce to any nodes we haven't already announced to.
170 is <- atomically $ do 173 is <- atomically $ do
@@ -202,8 +205,6 @@ scheduleSearch announcer k SearchMethod{sSearch,sWithResult,sNearestNodes,sTarge
202 return () 205 return ()
203 return True -- True to keep searching. 206 return True -- True to keep searching.
204 searchAgain = do 207 searchAgain = do
205 -- Canceling a pending search here seems to make announcements more reliable.
206 searchCancel st
207 return $ void $ do 208 return $ void $ do
208 t <- fork search 209 t <- fork search
209 labelThread t ("scheduleSearch.sch." ++ show sTarget) 210 labelThread t ("scheduleSearch.sch." ++ show sTarget)
@@ -211,7 +212,10 @@ scheduleSearch announcer k SearchMethod{sSearch,sWithResult,sNearestNodes,sTarge
211 got <- tryTakeMVar mutex 212 got <- tryTakeMVar mutex
212 case got of 213 case got of
213 Just () -> do 214 Just () -> do
214 atomically $ reset sNearestNodes sSearch sTarget st 215 me <- myThreadId
216 labelThread me "scheduleSearch.reset"
217 reset sNearestNodes sSearch sTarget st
218 labelThread me "scheduleSearch.searchLoop"
215 searchLoop sSearch sTarget onResult st 219 searchLoop sSearch sTarget onResult st
216 putMVar mutex () 220 putMVar mutex ()
217 Nothing -> do 221 Nothing -> do