2020-08-12 19:17:53 +00:00
|
|
|
{- External remote protocol async extension.
|
|
|
|
-
|
2020-08-13 19:49:43 +00:00
|
|
|
- Copyright 2020 Joey Hess <id@joeyh.name>
|
2020-08-12 19:17:53 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2020-08-12 19:17:53 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
module Remote.External.AsyncExtension (runRelayToExternalAsync) where
|
2020-08-12 19:17:53 +00:00
|
|
|
|
2020-08-12 19:54:30 +00:00
|
|
|
import Common
|
2020-08-13 19:49:43 +00:00
|
|
|
import Messages
|
2020-08-12 19:17:53 +00:00
|
|
|
import Remote.External.Types
|
2020-08-13 19:49:43 +00:00
|
|
|
import Utility.SimpleProtocol as Proto
|
2020-08-12 19:17:53 +00:00
|
|
|
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
import Control.Concurrent.STM.TBMChan
|
|
|
|
import qualified Data.Map.Strict as M
|
|
|
|
|
|
|
|
-- | Starts a thread that will handle all communication with the external
|
|
|
|
-- process. The input ExternalState communicates directly with the external
|
|
|
|
-- process.
|
2020-08-12 19:54:30 +00:00
|
|
|
runRelayToExternalAsync :: External -> ExternalState -> IO ExternalAsyncRelay
|
|
|
|
runRelayToExternalAsync external st = do
|
2020-08-13 19:49:43 +00:00
|
|
|
jidmap <- newTVarIO M.empty
|
|
|
|
sendq <- newSendQueue
|
2020-08-14 00:18:06 +00:00
|
|
|
nextjid <- newTVarIO (JobId 1)
|
2020-08-14 18:24:46 +00:00
|
|
|
void $ async $ sendloop st sendq
|
2020-08-14 00:18:06 +00:00
|
|
|
void $ async $ receiveloop external st jidmap sendq
|
2020-08-12 20:25:53 +00:00
|
|
|
return $ ExternalAsyncRelay $ do
|
2020-08-13 19:49:43 +00:00
|
|
|
receiveq <- newReceiveQueue
|
2020-08-14 18:24:46 +00:00
|
|
|
jid <- atomically $ do
|
|
|
|
jid@(JobId n) <- readTVar nextjid
|
|
|
|
let !jid' = JobId (succ n)
|
|
|
|
writeTVar nextjid jid'
|
|
|
|
modifyTVar' jidmap $ M.insert jid receiveq
|
|
|
|
return jid
|
2020-08-12 19:54:30 +00:00
|
|
|
return $ ExternalState
|
2020-08-13 19:49:43 +00:00
|
|
|
{ externalSend = \msg ->
|
|
|
|
atomically $ writeTBMChan sendq
|
2020-08-14 18:24:46 +00:00
|
|
|
(toAsyncWrapped msg, jid)
|
2020-08-13 19:49:43 +00:00
|
|
|
, externalReceive = atomically (readTBMChan receiveq)
|
2020-08-12 20:25:53 +00:00
|
|
|
-- This shuts down the whole relay.
|
2020-08-13 19:49:43 +00:00
|
|
|
, externalShutdown = shutdown external st sendq
|
2020-08-14 18:40:30 +00:00
|
|
|
-- These three TMVars are shared amoung all
|
2020-08-12 19:17:53 +00:00
|
|
|
-- ExternalStates that use this relay; they're
|
|
|
|
-- common state about the external process.
|
|
|
|
, externalPrepared = externalPrepared st
|
|
|
|
, externalConfig = externalConfig st
|
|
|
|
, externalConfigChanges = externalConfigChanges st
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
type ReceiveQueue = TBMChan String
|
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
type SendQueue = TBMChan (AsyncWrapped, JobId)
|
2020-08-13 19:49:43 +00:00
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
type JidMap = TVar (M.Map JobId ReceiveQueue)
|
2020-08-14 00:18:06 +00:00
|
|
|
|
2020-08-13 19:49:43 +00:00
|
|
|
newReceiveQueue :: IO ReceiveQueue
|
|
|
|
newReceiveQueue = newTBMChanIO 10
|
|
|
|
|
|
|
|
newSendQueue :: IO SendQueue
|
|
|
|
newSendQueue = newTBMChanIO 10
|
|
|
|
|
2020-08-14 00:18:06 +00:00
|
|
|
receiveloop :: External -> ExternalState -> JidMap -> SendQueue -> IO ()
|
|
|
|
receiveloop external st jidmap sendq = externalReceive st >>= \case
|
2020-08-13 19:49:43 +00:00
|
|
|
Just l -> case parseMessage l :: Maybe AsyncMessage of
|
2020-08-14 00:18:06 +00:00
|
|
|
Just (AsyncMessage jid msg) ->
|
|
|
|
M.lookup jid <$> readTVarIO jidmap >>= \case
|
2020-08-14 18:24:46 +00:00
|
|
|
Just c -> do
|
2020-08-14 00:18:06 +00:00
|
|
|
atomically $ writeTBMChan c msg
|
|
|
|
receiveloop external st jidmap sendq
|
|
|
|
Nothing -> protoerr "unknown job number"
|
2020-08-14 18:24:46 +00:00
|
|
|
Nothing -> case parseMessage l :: Maybe ExceptionalMessage of
|
2020-08-15 18:33:18 +00:00
|
|
|
Just _ -> do
|
2020-08-14 18:24:46 +00:00
|
|
|
-- ERROR is relayed to all listeners
|
|
|
|
m <- readTVarIO jidmap
|
2020-08-15 18:33:18 +00:00
|
|
|
forM_ (M.elems m) $ \c ->
|
2020-08-14 18:24:46 +00:00
|
|
|
atomically $ writeTBMChan c l
|
|
|
|
receiveloop external st jidmap sendq
|
|
|
|
Nothing -> protoerr "unexpected non-async message"
|
2020-08-13 20:11:38 +00:00
|
|
|
Nothing -> closeandshutdown
|
2020-08-12 19:17:53 +00:00
|
|
|
where
|
2020-08-13 20:11:38 +00:00
|
|
|
protoerr s = do
|
|
|
|
warningIO $ "async external special remote protocol error: " ++ s
|
|
|
|
closeandshutdown
|
|
|
|
|
|
|
|
closeandshutdown = do
|
2020-08-13 19:49:43 +00:00
|
|
|
shutdown external st sendq True
|
2020-08-14 00:18:06 +00:00
|
|
|
m <- atomically $ readTVar jidmap
|
2020-08-14 18:24:46 +00:00
|
|
|
forM_ (M.elems m) (atomically . closeTBMChan)
|
2020-08-13 19:49:43 +00:00
|
|
|
|
2020-08-14 18:24:46 +00:00
|
|
|
sendloop :: ExternalState -> SendQueue -> IO ()
|
|
|
|
sendloop st sendq = atomically (readTBMChan sendq) >>= \case
|
|
|
|
Just (wrappedmsg, jid) -> do
|
2020-08-13 19:49:43 +00:00
|
|
|
case wrappedmsg of
|
|
|
|
AsyncWrappedRemoteResponse msg ->
|
2020-08-14 18:24:46 +00:00
|
|
|
externalSend st $ wrapjid msg jid
|
|
|
|
AsyncWrappedRequest msg ->
|
|
|
|
externalSend st $ wrapjid msg jid
|
|
|
|
AsyncWrappedExceptionalMessage msg ->
|
2020-08-14 00:18:06 +00:00
|
|
|
externalSend st msg
|
|
|
|
AsyncWrappedAsyncMessage msg ->
|
2020-08-13 19:49:43 +00:00
|
|
|
externalSend st msg
|
2020-08-14 18:24:46 +00:00
|
|
|
sendloop st sendq
|
2020-08-13 19:49:43 +00:00
|
|
|
Nothing -> return ()
|
|
|
|
where
|
2020-08-14 00:18:06 +00:00
|
|
|
wrapjid msg jid = AsyncMessage jid $ unwords $ Proto.formatMessage msg
|
2020-08-13 19:49:43 +00:00
|
|
|
|
|
|
|
shutdown :: External -> ExternalState -> SendQueue -> Bool -> IO ()
|
|
|
|
shutdown external st sendq b = do
|
|
|
|
r <- atomically $ do
|
|
|
|
r <- tryTakeTMVar (externalAsync external)
|
|
|
|
putTMVar (externalAsync external)
|
|
|
|
UncheckedExternalAsync
|
|
|
|
return r
|
|
|
|
case r of
|
|
|
|
Just (ExternalAsync _) -> externalShutdown st b
|
|
|
|
_ -> noop
|
|
|
|
atomically $ closeTBMChan sendq
|