-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Haskell implementation of several ZeroMQ patterns.
--   
--   Haskell implementation of several ZeroMQ patterns that you can find in
--   the official ZeroMQ guide.
@package zeromq4-patterns
@version 0.3.1.0

module System.ZMQ4.Patterns.Clone.Internal

-- | Actual message sent between server and client.
data Message a
Message :: {-# UNPACK #-} !Word64 -> !a -> Message a
[messageVersion] :: Message a -> {-# UNPACK #-} !Word64
[messageValue] :: Message a -> !a

-- | Message sent by a client to request a state snapshot
iSTATE_REQUEST :: ByteString

-- | A server publishing messages to a client.
--   
--   This function will start serving messages on the current thread.
--   
--   It will send all objects that are pushed on the channel to a PUB
--   socket.
--   
--   It will listen for snapshot requests on a ROUTER socket.
--   
--   The server will automatically keep the last sent object cached, and
--   use at as a snapshot. Sequencing is automatically handled using a
--   <a>Word64</a> sequence counter.
publisher :: Binary a => String -> String -> MVar a -> IO ()

-- | A client receiving messages from a server.
--   
--   This function will start reading messages on the current thread.
--   
--   It will connect to the server's PUB and ROUTER ports. It will backlog
--   messages from the PUB socket, while requesting the initial state
--   snapshot from the ROUTER socket. Afterwards, it will forever read from
--   the PUB socket, and process all in-sequence updates.
--   
--   Note that this pattern is not 100% reliable. Messages might be dropped
--   between the initial state request and the first update.
subscriber :: Binary a => String -> String -> MVar a -> IO ()

-- | Only request the most recent state from the server.
--   
--   This function will query the ROUTER port for the latest state, and
--   return it.
queryLastState :: Binary a => String -> IO a
instance GHC.Show.Show a => GHC.Show.Show (System.ZMQ4.Patterns.Clone.Internal.Message a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (System.ZMQ4.Patterns.Clone.Internal.Message a)
instance Data.Binary.Class.Binary a => Data.Binary.Class.Binary (System.ZMQ4.Patterns.Clone.Internal.Message a)


-- | This module implements the ZeroMQ reliable pub-sub (clone) pattern.
--   
--   See
--   &lt;<a>http://zguide.zeromq.org/page:all#Reliable-Pub-Sub-Clone-Pattern</a>
--   the ZeroMQ guide&gt; for more information.
--   
--   Example usage
--   
--   <pre>
--   import Control.Concurrent (threadDelay)
--   import Control.Concurrent.MVar
--   import Control.Concurrent.Async
--   
--   import Control.Monad (forever)
--   
--   import System.ZMQ4.Patterns.Clone (server, client)
--   
--   -- | Produce an infinite stream of numbers
--   producer :: IO ()
--   producer = do
--       chan &lt;- newEmptyMVar
--       withAsync (server "tcp://:5009" "tcp://:5010" chan) $ \_ -&gt;
--           let send i = putMVar chan i &gt;&gt; threadDelay (100*1000)
--           mapM_ send [(1 :: Integer)..]
--   
--   -- | Consume the stream of numbers
--   consumer :: IO ()
--   consumer = do
--       chan &lt;- newEmptyMVar
--       withAsync (client "tcp://127.0.0.1:5009" "tcp://127.0.0.1:5010" chan) $ \_ -&gt;
--           forever $ do
--               n &lt;- takeMVar chan
--               print n
--   </pre>
module System.ZMQ4.Patterns.Clone

-- | A server publishing messages to a client.
--   
--   This function will start serving messages on the current thread.
--   
--   It will send all objects that are pushed on the channel to a PUB
--   socket.
--   
--   It will listen for snapshot requests on a ROUTER socket.
--   
--   The server will automatically keep the last sent object cached, and
--   use at as a snapshot. Sequencing is automatically handled using a
--   <a>Word64</a> sequence counter.
publisher :: Binary a => String -> String -> MVar a -> IO ()

-- | A client receiving messages from a server.
--   
--   This function will start reading messages on the current thread.
--   
--   It will connect to the server's PUB and ROUTER ports. It will backlog
--   messages from the PUB socket, while requesting the initial state
--   snapshot from the ROUTER socket. Afterwards, it will forever read from
--   the PUB socket, and process all in-sequence updates.
--   
--   Note that this pattern is not 100% reliable. Messages might be dropped
--   between the initial state request and the first update.
subscriber :: Binary a => String -> String -> MVar a -> IO ()

-- | Only request the most recent state from the server.
--   
--   This function will query the ROUTER port for the latest state, and
--   return it.
queryLastState :: Binary a => String -> IO a

module System.ZMQ4.Patterns.RequestReply

-- | A request-reply type class.
--   
--   <tt>a</tt> is the request type, <tt>b</tt> is the response type.
--   
--   Example:
--   
--   <pre>
--   &gt;&gt;&gt; {-# LANGUAGE DataKinds #-}
--   
--   &gt;&gt;&gt; {-# LANGUAGE TypeApplications #-}
--   
--   &gt;&gt;&gt; 
--   
--   &gt;&gt;&gt; import Control.Concurrent.Async
--   
--   &gt;&gt;&gt; import Data.Binary
--   
--   &gt;&gt;&gt; 
--   
--   &gt;&gt;&gt; data A = A deriving (Binary, Show)
--   
--   &gt;&gt;&gt; data B = B deriving (Binary, Show)
--   
--   &gt;&gt;&gt; 
--   
--   &gt;&gt;&gt; instance RequestReply A B
--   
--   &gt;&gt;&gt; 
--   
--   &gt;&gt;&gt; reply :: A -&gt; IO B
--   
--   &gt;&gt;&gt; reply _ = return B
--   
--   &gt;&gt;&gt; 
--   
--   &gt;&gt;&gt; main :: IO ()
--   
--   &gt;&gt;&gt; main = withAsync (responder "tcp://*:5000" reply) $ \_ -&gt;
--   
--   &gt;&gt;&gt; requester "tcp://127.0.0.1:5000" A &gt;&gt;= print
--   </pre>
class (Binary a, Binary b) => RequestReply a b | a -> b

-- | Start responding using the given type class.
--   
--   See <a>RequestReply</a> for an example.
--   
--   Silently ignores a request when decoding fails
responder :: forall a b. RequestReply a b => String -> (a -> IO b) -> IO ()

-- | Request a reply.
--   
--   See <a>RequestReply</a> for an example.
--   
--   Throws an error when the response cannot be decoded.
request :: forall a b. RequestReply a b => String -> a -> IO b
