module SocketActivation.CheckRecipient where
import Control.Monad (Monad (return, (>>=)))
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Either (Either)
import Data.Eq (Eq ((==)))
import Data.Function ((.))
import System.IO (IO)
import qualified System.Posix.Process as Sys
import SocketActivation.Concepts
import SocketActivation.Env
import SocketActivation.IO
checkRecipient :: IO (Either Error ())
checkRecipient :: IO (Either Error ())
checkRecipient = IO' () -> IO (Either Error ())
forall a. IO' a -> IO (Either Error a)
run (IO' Recipient
getIt IO' Recipient -> (Recipient -> IO' ()) -> IO' ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Recipient -> IO' ()
checkIt)
where
getIt :: IO' Recipient
getIt = IO (Either Error Recipient) -> IO' Recipient
forall a. IO (Either Error a) -> IO' a
IO' (Env' Recipient => IO (Either Error Recipient)
forall a. Env' a => IO (Either Error a)
getEnv' @Recipient)
checkIt :: Recipient -> IO' ()
checkIt = IO (Either Error ()) -> IO' ()
forall a. IO (Either Error a) -> IO' a
IO' (IO (Either Error ()) -> IO' ())
-> (Recipient -> IO (Either Error ())) -> Recipient -> IO' ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Recipient -> IO (Either Error ())
checkRecipient'
checkRecipient' :: Recipient -> IO (Either Error ())
checkRecipient' :: Recipient -> IO (Either Error ())
checkRecipient' Recipient
x = IO' () -> IO (Either Error ())
forall a. IO' a -> IO (Either Error a)
run (IO' ProcessID
getMyPid IO' ProcessID -> (ProcessID -> IO' ()) -> IO' ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ProcessID -> IO' ()
throwIfDifferent)
where
getMyPid :: IO' ProcessID
getMyPid = IO ProcessID -> IO' ProcessID
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ProcessID
Sys.getProcessID
throwIfDifferent :: ProcessID -> IO' ()
throwIfDifferent ProcessID
y = if Recipient -> ProcessID
recipientPID Recipient
x ProcessID -> ProcessID -> Bool
forall a. Eq a => a -> a -> Bool
== ProcessID
y then () -> IO' ()
forall (m :: * -> *) a. Monad m => a -> m a
return () else Error -> IO' ()
forall a. Error -> IO' a
throwError Error
WrongProcess