zmq: router and reqs
authorMax Voit <max.voit+gtdv@with-eyes.net>
Fri, 29 May 2015 11:16:35 +0000 (13:16 +0200)
committerMax Voit <max.voit+gtdv@with-eyes.net>
Fri, 29 May 2015 11:16:35 +0000 (13:16 +0200)
zmq-concept/master.hs [new file with mode: 0644]
zmq-concept/slave.hs [new file with mode: 0644]

diff --git a/zmq-concept/master.hs b/zmq-concept/master.hs
new file mode 100644 (file)
index 0000000..2d2427e
--- /dev/null
@@ -0,0 +1,26 @@
+{-# LANGUAGE OverloadedStrings #-}
+import Control.Monad
+import System.IO
+import System.ZMQ4.Monadic
+import qualified Data.ByteString.Char8 as CS
+
+addr :: String
+addr = "tcp://127.0.0.1:5000"
+
+main :: IO ()
+main = do
+    runZMQ $ do
+        sock <- socket Router
+        bind sock addr
+        forM_ [1..] $ \i -> do
+            ident <- receive sock
+            _ <- receive sock
+            msg <- receive sock
+            send sock [SendMore] ident
+            send sock [SendMore] ""
+            send sock [] (CS.pack (show (i :: Int)))
+
+            liftIO $ CS.putStrLn $ CS.append (formatID ident) msg
+            liftIO $ hFlush stdout
+        return ()
+    where formatID i = CS.cons '[' $ CS.append i "] "
diff --git a/zmq-concept/slave.hs b/zmq-concept/slave.hs
new file mode 100644 (file)
index 0000000..07325cd
--- /dev/null
@@ -0,0 +1,22 @@
+{-# LANGUAGE OverloadedStrings #-}
+import Control.Monad
+import Control.Concurrent (threadDelay)
+import System.IO
+import System.ZMQ4.Monadic
+import qualified Data.ByteString.Char8 as CS
+
+addr :: String
+addr = "tcp://127.0.0.1:5000"
+
+main :: IO ()
+main = do
+    runZMQ $ do
+        sock <- socket Req
+        connect sock addr
+        send sock [] "started"
+        forever $ do
+            liftIO $ threadDelay 500000
+            c <- receive sock 
+            liftIO . CS.putStrLn $ c
+            send sock [] $ CS.append "Done: " $ c
+            liftIO $ hFlush stdout