Minimize interface to ephemeral ports.

parent 55552c8a
...@@ -10,7 +10,7 @@ module IHaskell.IPython.ZeroMQ ...@@ -10,7 +10,7 @@ module IHaskell.IPython.ZeroMQ
, ZeroMQStdin(..) , ZeroMQStdin(..)
, serveProfile , serveProfile
, serveStdin , serveStdin
, ZeroMQEphemeralPorts(..) , ZeroMQEphemeralPorts
, withEphemeralPorts , withEphemeralPorts
) where ) where
...@@ -101,6 +101,8 @@ serveProfile profile debug = do ...@@ -101,6 +101,8 @@ serveProfile profile debug = do
return channels return channels
-- | Describes ports used when creating an ephemeral ZeroMQ session.
-- Used to generate the ipython JSON config file.
data ZeroMQEphemeralPorts data ZeroMQEphemeralPorts
= ZeroMQEphemeralPorts { ephHbPort :: !Port = ZeroMQEphemeralPorts { ephHbPort :: !Port
, ephControlPort :: !Port , ephControlPort :: !Port
...@@ -134,16 +136,21 @@ bindLocalEphemeralPort socket = do ...@@ -134,16 +136,21 @@ bindLocalEphemeralPort socket = do
Just endpointIndex -> Just endpointIndex ->
return endpointIndex return endpointIndex
-- | Start responding on all ZeroMQ channels used to communicate with IPython -- | @withFork thread main@ runs @thread@ in a separate
-- with ephemerally allocated ports. -- thread, and kills it when @main@ finishes.
-- Profide the callback with the ports chosen and a ZeroMQInterface. withFork :: IO () -> (ThreadId -> IO a) -> IO a
withFork thread = bracket (forkIO thread) killThread
-- | Run session for communicating with an IPython instance on ephemerally allocated
-- ZMQ4 sockets. The sockets will be closed when the callback returns.
withEphemeralPorts :: ByteString withEphemeralPorts :: ByteString
-- ^ HMAC encryption key -- ^ HMAC encryption key
-> Bool -> Bool
-- ^ Print debug output -- ^ Print debug output
-> (ZeroMQEphemeralPorts -> ZeroMQInterface -> IO a) -> (ZeroMQEphemeralPorts -> ZeroMQInterface -> IO a)
-- ^ Callback that takes the interface to the sockets. -- ^ Callback that takes the interface to the sockets.
-> IO a -> IO a
withEphemeralPorts key debug callback = do withEphemeralPorts key debug callback = do
channels <- newZeroMQInterface key channels <- newZeroMQInterface key
-- Create the ZMQ4 context -- Create the ZMQ4 context
...@@ -166,15 +173,13 @@ withEphemeralPorts key debug callback = do ...@@ -166,15 +173,13 @@ withEphemeralPorts key debug callback = do
, ephIOPubPort = iopubPort , ephIOPubPort = iopubPort
, ephSignatureKey = key , ephSignatureKey = key
} }
-- Launch actions to listen to communicate between channels and cockets. -- Launch actions to listen to communicate between channels and cockets.
_ <- forkIO $ forever $ heartbeat channels heartbeatSocket withFork (forever $ heartbeat channels heartbeatSocket) $ \_ -> do
_ <- forkIO $ forever $ control debug channels controlportSocket withFork (forever $ control debug channels controlportSocket) $ \_ -> do
_ <- forkIO $ forever $ shell debug channels shellportSocket withFork (forever $ shell debug channels shellportSocket) $ \_ -> do
_ <- forkIO $ forever $ checkedIOpub debug channels iopubSocket withFork (forever $ checkedIOpub debug channels iopubSocket) $ \_ -> do
-- Run callback function; provide it with both ports and channels.
-- Run callback function; provide it with both ports and channels. callback ports channels
callback ports channels
serveStdin :: Profile -> IO ZeroMQStdin serveStdin :: Profile -> IO ZeroMQStdin
serveStdin profile = do serveStdin profile = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment