module Network.Transport.Util (spawn) where
import Network.Transport
( Transport
, EndPoint(..)
, EndPointAddress
, newEndPoint
)
import Control.Exception (throwIO)
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
spawn :: Transport -> (EndPoint -> IO ()) -> IO EndPointAddress
spawn transport proc = do
addrMVar <- newEmptyMVar
forkIO $ do
mEndPoint <- newEndPoint transport
case mEndPoint of
Left err ->
putMVar addrMVar (Left err)
Right endPoint -> do
putMVar addrMVar (Right (address endPoint))
proc endPoint
mAddr <- takeMVar addrMVar
case mAddr of
Left err -> throwIO err
Right addr -> return addr