Skip to content

A peer-to-peer stack for code collaboration

Extract 'installMachineMessageHandler'

Thomas Scholtesf07e09db0f75
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index f69a337..6c93b0e 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -310,15 +310,6 @@ initDaemonSubscription id =
logError' "Non-decodable message on machine's pubsub topic"
[("machine-id", getMachineId id), ("message", bad)]
--- | Turns a 'Daemon' subscription handler into an IO one. Errors which are
--- encountered in a subscription handler are just logged.
-daemonHandler :: Env -> (Message -> Daemon ()) -> Message -> IO ()
-daemonHandler env h msg = do
- x_ <- runDaemon env (h msg)
- case x_ of
- Left err -> logDaemonError err
- Right _ -> pure ()
-
-- | Loads a machine fresh from IPFS.
loadMachine :: ReaderOrWriter -> MachineId -> Daemon Machine
loadMachine mode id = do
@@ -416,8 +407,7 @@ emptyMachine id mode sub = do
initAsReader :: MachineId -> Daemon Machine
initAsReader id = do
m <- loadMachine Reader id
- env <- ask
- _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env onMsg)
+ installMachineMessageHandler m onMsg
logInfo "Following as reader" [ ("machine-id", getMachineId id)
, ("current-input-index", show (machineLastIndex m)) ]
pure m
@@ -484,8 +474,7 @@ initAsWriter id idx = do
-- input requests.
actAsWriter :: Machine -> Daemon ()
actAsWriter m = do
- env <- ask
- _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env onMsg)
+ installMachineMessageHandler m onMsg
logInfo "Acting as writer" [("machine-id", getMachineId id)]
where
id = machineId m
@@ -493,6 +482,18 @@ actAsWriter m = do
Submit SubmitInputs{..} -> writeInputs id expressions (Just nonce) >> pure ()
_ -> pure ()
+-- | Installs a handler for messages sent on the machine's IPFS pubsub channel.
+--
+-- If the handler produces an error it is logged.
+installMachineMessageHandler :: Machine -> (Message -> Daemon ()) -> Daemon ()
+installMachineMessageHandler m handleMessage = do
+ env <- ask
+ void $ liftIO $ addHandler (machineSubscription m) $ \msg -> do
+ result <- runDaemon env (handleMessage msg)
+ case result of
+ Left err -> logDaemonError err
+ Right () -> pure ()
+
-- | Write and evaluate inputs in a machine we control.
--
-- Returns the outputs generated by evaluating the inputs.