diff options
author | joe <joe@jerkface.net> | 2018-06-17 17:00:55 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-17 17:00:55 -0400 |
commit | 75a18e4cb814044a714aa3f487d2e6475de6127a (patch) | |
tree | 5b7739e7f3e7aadd82f6e8a6fd6b9b7e9f65e243 | |
parent | 7a73651baae06c1b2556aee91315165ba5d22b7e (diff) |
Generalizing Announcer, re-interpreted Announce/NewAnnouncement items.
-rw-r--r-- | Announcer.hs | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/Announcer.hs b/Announcer.hs index e9d83e65..7fd72e2d 100644 --- a/Announcer.hs +++ b/Announcer.hs | |||
@@ -1,6 +1,7 @@ | |||
1 | {-# LANGUAGE DeriveDataTypeable #-} | 1 | {-# LANGUAGE DeriveDataTypeable #-} |
2 | {-# LANGUAGE DeriveGeneric #-} | 2 | {-# LANGUAGE DeriveGeneric #-} |
3 | {-# LANGUAGE ExistentialQuantification #-} | 3 | {-# LANGUAGE ExistentialQuantification #-} |
4 | {-# LANGUAGE FlexibleContexts #-} | ||
4 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 5 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
5 | {-# LANGUAGE LambdaCase #-} | 6 | {-# LANGUAGE LambdaCase #-} |
6 | {-# LANGUAGE NamedFieldPuns #-} | 7 | {-# LANGUAGE NamedFieldPuns #-} |
@@ -57,9 +58,6 @@ unpackAnnounceKey _ (AnnounceKey bs) = return $ Char8.unpack bs | |||
57 | -- 'Announce'. | 58 | -- 'Announce'. |
58 | data ScheduledItem | 59 | data ScheduledItem |
59 | = DeleteAnnouncement | 60 | = DeleteAnnouncement |
60 | -- NewAnnouncement searchAgain search announce aInterval | ||
61 | | NewAnnouncement (STM (IO ())) (IO ()) (IO ()) POSIXTime | ||
62 | | Announce (STM (IO ())) (IO ()) POSIXTime | ||
63 | | ScheduledItem (Announcer -> AnnounceKey -> POSIXTime -> STM (IO ())) | 61 | | ScheduledItem (Announcer -> AnnounceKey -> POSIXTime -> STM (IO ())) |
64 | | StopAnnouncer | 62 | | StopAnnouncer |
65 | -- Can't use Data because STM and IO. :( | 63 | -- Can't use Data because STM and IO. :( |
@@ -72,10 +70,8 @@ data ScheduledItem | |||
72 | 70 | ||
73 | itemStatusNum :: ScheduledItem -> Int | 71 | itemStatusNum :: ScheduledItem -> Int |
74 | itemStatusNum (DeleteAnnouncement ) = 0 | 72 | itemStatusNum (DeleteAnnouncement ) = 0 |
75 | itemStatusNum (NewAnnouncement {}) = 1 | 73 | itemStatusNum (ScheduledItem {}) = 1 |
76 | itemStatusNum (Announce {}) = 2 | 74 | itemStatusNum (StopAnnouncer ) = 2 |
77 | itemStatusNum (ScheduledItem {}) = 3 | ||
78 | itemStatusNum (StopAnnouncer ) = 4 | ||
79 | itemStatusNum _ = error "itemStatusNum not in sync with ScheduledItem declaration." | 75 | itemStatusNum _ = error "itemStatusNum not in sync with ScheduledItem declaration." |
80 | 76 | ||
81 | data Announcer = Announcer | 77 | data Announcer = Announcer |
@@ -215,7 +211,7 @@ schedule announcer k AnnounceMethod{aSearch,aPublish,aBuckets,aTarget,aInterval} | |||
215 | $ SearchFinished {- st -} search announce aInterval | 211 | $ SearchFinished {- st -} search announce aInterval |
216 | interruptDelay (interrutible announcer) | 212 | interruptDelay (interrutible announcer) |
217 | -} | 213 | -} |
218 | atomically $ scheduleImmediately announcer k $ NewAnnouncement searchAgain search announce aInterval | 214 | atomically $ scheduleImmediately announcer k $ ScheduledItem (newAnnouncement searchAgain search announce aInterval) |
219 | interruptDelay (interrutible announcer) | 215 | interruptDelay (interrutible announcer) |
220 | 216 | ||
221 | cancel :: Announcer -> AnnounceKey -> AnnounceMethod r -> r -> IO () | 217 | cancel :: Announcer -> AnnounceKey -> AnnounceMethod r -> r -> IO () |
@@ -277,20 +273,35 @@ performScheduledItem announcer now = \case | |||
277 | 273 | ||
278 | (Binding k (ScheduledItem action) _) -> Just <$> action announcer k now | 274 | (Binding k (ScheduledItem action) _) -> Just <$> action announcer k now |
279 | 275 | ||
280 | -- announcement started: | 276 | -- announcement started: |
281 | (Binding k (NewAnnouncement checkFin search announce interval) _) -> do | 277 | newAnnouncement :: STM (IO a) |
278 | -> IO () | ||
279 | -> IO () | ||
280 | -> POSIXTime | ||
281 | -> Announcer | ||
282 | -> AnnounceKey | ||
283 | -> POSIXTime | ||
284 | -> STM (IO ()) | ||
285 | newAnnouncement checkFin search announce interval = \announcer k now -> do | ||
282 | modifyTVar (scheduled announcer) | 286 | modifyTVar (scheduled announcer) |
283 | (PSQ.insert' k (Announce checkFin announce interval) (now + interval)) | 287 | (PSQ.insert' k (ScheduledItem $ reAnnounce checkFin announce interval) (now + interval)) |
284 | return $ Just $ void $ fork search | 288 | return $ void $ fork search |
285 | 289 | ||
286 | -- time for periodic announce: | 290 | -- time for periodic announce: |
287 | -- (re-)announce to the current known set of storing-nodes. | 291 | -- (re-)announce to the current known set of storing-nodes. |
288 | -- TODO: If the search is finished, restart the search. | 292 | -- TODO: If the search is finished, restart the search. |
289 | (Binding k (Announce checkFin announce interval) _) -> do | 293 | reAnnounce :: STM (IO a) |
294 | -> IO () | ||
295 | -> POSIXTime | ||
296 | -> Announcer | ||
297 | -> AnnounceKey | ||
298 | -> POSIXTime | ||
299 | -> STM (IO ()) | ||
300 | reAnnounce checkFin announce interval = \announcer k now -> do | ||
290 | isfin <- checkFin | 301 | isfin <- checkFin |
291 | modifyTVar (scheduled announcer) | 302 | modifyTVar (scheduled announcer) |
292 | (PSQ.insert' k (Announce checkFin announce interval) (now + interval)) | 303 | (PSQ.insert' k (ScheduledItem $ reAnnounce checkFin announce interval) (now + interval)) |
293 | return $ Just $ do | 304 | return $ do |
294 | isfin | 305 | isfin |
295 | hPutStrLn stderr $ "This print avoids negative-time future scheduling. Weird bug. TODO: fix it. "++show now | 306 | hPutStrLn stderr $ "This print avoids negative-time future scheduling. Weird bug. TODO: fix it. "++show now |
296 | announce | 307 | announce |