More channel creation code into separate function.

parent 5989188c
...@@ -61,6 +61,22 @@ data ZeroMQStdin = ...@@ -61,6 +61,22 @@ data ZeroMQStdin =
, stdinReplyChannel :: Chan Message , stdinReplyChannel :: Chan Message
} }
-- | Create new channels for a ZeroMQInterface
newZeroMQInterface :: ByteString -> IO ZeroMQInterface
newZeroMQInterface key = do
shellReqChan <- newChan
shellRepChan <- newChan
controlReqChan <- dupChan shellReqChan
controlRepChan <- dupChan shellRepChan
iopubChan <- newChan
return $! Channels { shellRequestChannel = shellReqChan
, shellReplyChannel = shellRepChan
, controlRequestChannel = controlReqChan
, controlReplyChannel = controlRepChan
, iopubChannel = iopubChan
, hmacKey = key
}
-- | Start responding on all ZeroMQ channels used to communicate with IPython | via the provided -- | Start responding on all ZeroMQ channels used to communicate with IPython | via the provided
-- profile. Return a set of channels which can be used to | communicate with IPython in a more -- profile. Return a set of channels which can be used to | communicate with IPython in a more
-- structured manner. -- structured manner.
...@@ -68,14 +84,7 @@ serveProfile :: Profile -- ^ The profile specifying which ports and t ...@@ -68,14 +84,7 @@ serveProfile :: Profile -- ^ The profile specifying which ports and t
-> Bool -- ^ Print debug output -> Bool -- ^ Print debug output
-> IO ZeroMQInterface -- ^ The Message-channel based interface to the sockets. -> IO ZeroMQInterface -- ^ The Message-channel based interface to the sockets.
serveProfile profile debug = do serveProfile profile debug = do
-- Create all channels which will be used for higher level communication. channels <- newZeroMQInterface (signatureKey profile)
shellReqChan <- newChan
shellRepChan <- newChan
controlReqChan <- dupChan shellReqChan
controlRepChan <- dupChan shellRepChan
iopubChan <- newChan
let channels = Channels shellReqChan shellRepChan controlReqChan controlRepChan iopubChan
(signatureKey profile)
-- Create the context in a separate thread that never finishes. If withContext or withSocket -- Create the context in a separate thread that never finishes. If withContext or withSocket
-- complete, the context or socket become invalid. -- complete, the context or socket become invalid.
...@@ -136,14 +145,7 @@ withEphemeralPorts :: ByteString ...@@ -136,14 +145,7 @@ withEphemeralPorts :: ByteString
-- ^ 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
-- Create all channels which will be used for higher level communication. channels <- newZeroMQInterface key
shellReqChan <- newChan
shellRepChan <- newChan
controlReqChan <- dupChan shellReqChan
controlRepChan <- dupChan shellRepChan
iopubChan <- newChan
let channels = Channels shellReqChan shellRepChan controlReqChan controlRepChan iopubChan key
-- Create the ZMQ4 context -- Create the ZMQ4 context
withContext $ \context -> do withContext $ \context -> do
-- Create the sockets to communicate with. -- Create the sockets to communicate with.
...@@ -157,18 +159,21 @@ withEphemeralPorts key debug callback = do ...@@ -157,18 +159,21 @@ withEphemeralPorts key debug callback = do
controlPort <- bindLocalEphemeralPort controlportSocket controlPort <- bindLocalEphemeralPort controlportSocket
shellPort <- bindLocalEphemeralPort shellportSocket shellPort <- bindLocalEphemeralPort shellportSocket
iopubPort <- bindLocalEphemeralPort iopubSocket iopubPort <- bindLocalEphemeralPort iopubSocket
-- Create object to store ephemeral ports
_ <- forkIO $ forever $ heartbeat channels heartbeatSocket
_ <- forkIO $ forever $ control debug channels controlportSocket
_ <- forkIO $ forever $ shell debug channels shellportSocket
_ <- forkIO $ forever $ checkedIOpub debug channels iopubSocket
let ports = ZeroMQEphemeralPorts { ephHbPort = hbPort let ports = ZeroMQEphemeralPorts { ephHbPort = hbPort
, ephControlPort = controlPort , ephControlPort = controlPort
, ephShellPort = shellPort , ephShellPort = shellPort
, ephIOPubPort = iopubPort , ephIOPubPort = iopubPort
, ephSignatureKey = key , ephSignatureKey = key
} }
-- Launch actions to listen to communicate between channels and cockets.
_ <- forkIO $ forever $ heartbeat channels heartbeatSocket
_ <- forkIO $ forever $ control debug channels controlportSocket
_ <- forkIO $ forever $ shell debug channels shellportSocket
_ <- forkIO $ forever $ checkedIOpub debug channels iopubSocket
-- Run callback function; provide it with both ports and channels.
callback ports channels callback ports channels
serveStdin :: Profile -> IO ZeroMQStdin serveStdin :: Profile -> IO ZeroMQStdin
......
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