2012-07-28 19:41:49 +00:00
|
|
|
{- notification broadcaster
|
|
|
|
-
|
|
|
|
- This is used to allow clients to block until there is a new notification
|
|
|
|
- that some thing occurred. It does not communicate what the change is,
|
|
|
|
- it only provides blocking reads to wait on notifications.
|
|
|
|
-
|
|
|
|
- Multiple clients are supported. Each has a unique id.
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-07-28 20:01:50 +00:00
|
|
|
module Utility.NotificationBroadcaster (
|
|
|
|
NotificationBroadcaster,
|
2012-07-28 19:41:49 +00:00
|
|
|
NotificationHandle,
|
2012-07-29 01:11:40 +00:00
|
|
|
NotificationId,
|
2012-07-28 20:01:50 +00:00
|
|
|
newNotificationBroadcaster,
|
2012-07-28 19:41:49 +00:00
|
|
|
newNotificationHandle,
|
|
|
|
notificationHandleToId,
|
|
|
|
notificationHandleFromId,
|
|
|
|
sendNotification,
|
|
|
|
waitNotification,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Concurrent.SampleVar
|
|
|
|
|
|
|
|
{- One SampleVar per client. The TMVar is never empty, so never blocks. -}
|
2012-07-28 20:01:50 +00:00
|
|
|
type NotificationBroadcaster = TMVar [SampleVar ()]
|
2012-07-28 19:41:49 +00:00
|
|
|
|
2012-07-29 00:30:46 +00:00
|
|
|
newtype NotificationId = NotificationId Int
|
2012-07-29 01:21:22 +00:00
|
|
|
deriving (Read, Show, Eq, Ord)
|
2012-07-29 00:30:46 +00:00
|
|
|
|
2012-07-28 19:41:49 +00:00
|
|
|
{- Handle given out to an individual client. -}
|
2012-07-29 00:30:46 +00:00
|
|
|
data NotificationHandle = NotificationHandle NotificationBroadcaster NotificationId
|
2012-07-28 19:41:49 +00:00
|
|
|
|
2012-07-28 20:01:50 +00:00
|
|
|
newNotificationBroadcaster :: IO NotificationBroadcaster
|
2012-07-29 01:11:40 +00:00
|
|
|
newNotificationBroadcaster = atomically $ newTMVar []
|
2012-07-28 19:41:49 +00:00
|
|
|
|
|
|
|
{- Allocates a notification handle for a client to use. -}
|
2012-07-28 20:01:50 +00:00
|
|
|
newNotificationHandle :: NotificationBroadcaster -> IO NotificationHandle
|
2012-07-28 19:41:49 +00:00
|
|
|
newNotificationHandle b = NotificationHandle
|
|
|
|
<$> pure b
|
2012-07-28 20:01:50 +00:00
|
|
|
<*> addclient
|
2012-07-28 19:41:49 +00:00
|
|
|
where
|
2012-07-28 20:01:50 +00:00
|
|
|
addclient = do
|
2012-07-28 19:41:49 +00:00
|
|
|
s <- newEmptySampleVar
|
|
|
|
atomically $ do
|
2012-07-29 01:11:40 +00:00
|
|
|
l <- takeTMVar b
|
2012-07-28 19:41:49 +00:00
|
|
|
putTMVar b $ l ++ [s]
|
2012-07-29 00:30:46 +00:00
|
|
|
return $ NotificationId $ length l
|
2012-07-28 19:41:49 +00:00
|
|
|
|
2012-07-29 00:30:46 +00:00
|
|
|
{- Extracts the identifier from a notification handle.
|
2012-07-28 19:41:49 +00:00
|
|
|
- This can be used to eg, pass the identifier through to a WebApp. -}
|
2012-07-29 00:30:46 +00:00
|
|
|
notificationHandleToId :: NotificationHandle -> NotificationId
|
2012-07-28 19:41:49 +00:00
|
|
|
notificationHandleToId (NotificationHandle _ i) = i
|
|
|
|
|
2012-07-29 00:30:46 +00:00
|
|
|
notificationHandleFromId :: NotificationBroadcaster -> NotificationId -> NotificationHandle
|
2012-07-28 19:41:49 +00:00
|
|
|
notificationHandleFromId = NotificationHandle
|
|
|
|
|
|
|
|
{- Sends a notification to all clients. -}
|
2012-07-28 20:01:50 +00:00
|
|
|
sendNotification :: NotificationBroadcaster -> IO ()
|
2012-07-28 19:41:49 +00:00
|
|
|
sendNotification b = do
|
|
|
|
l <- atomically $ readTMVar b
|
|
|
|
mapM_ notify l
|
|
|
|
where
|
|
|
|
notify s = writeSampleVar s ()
|
|
|
|
|
|
|
|
{- Used by a client to block until a new notification is available since
|
|
|
|
- the last time it tried. -}
|
|
|
|
waitNotification :: NotificationHandle -> IO ()
|
2012-07-29 00:30:46 +00:00
|
|
|
waitNotification (NotificationHandle b (NotificationId i)) = do
|
2012-07-28 19:41:49 +00:00
|
|
|
l <- atomically $ readTMVar b
|
|
|
|
readSampleVar (l !! i)
|