@@ -1,4 +1,14 @@
1−-- A concurrent TCP server that prints everything it receives to stdout.
1+-- A concurrent TCP server that:
3+-- * receives connections from clients
4+-- * splits the incoming data into lines
5+-- * lines from concurrent connections are merged into a single srteam
6+-- * writes the line stream to an output file
8+import Control.Monad.IO.Class (liftIO)
9+import Data.Char (ord, chr)
10+import System.IO (openFile, IOMode(..), hClose, hPutStrLn)
11+import System.Environment (getArgs)
3import Streamly
13import Streamly
4import qualified Streamly.Fold as FL
14import qualified Streamly.Fold as FL
@@ -8,15 +18,20 @@ import qualified Network.Socket as NS
8import qualified Streamly.Prelude as S
18import qualified Streamly.Prelude as S
10main :: IO ()
20main :: IO ()
11−main = S.drain
21+main = do
23+ S.mapM_ (hPutLines h)
12− $ parallely $ S.mapM (withSocket recv)
24+ $ S.concatMapBy parallel (withSocket recv)
13− $ serially $ fmap fst (NS.recvConnectionsOn 8090)
25+ $ fmap fst (NS.recvConnectionsOn 8090)
30+ openOutput = fmap head getArgs >>= \file -> openFile file AppendMode
17− withSocket f sk = f sk >> NS.close sk
32+ withSocket f sk = S.finally (liftIO (NS.close sk)) (f sk)
33+ hPutLines h = hPutStrLn h . map (chr . fromIntegral) . A.toList
34+ ord' = (fromIntegral . ord)
19− S.mapM_ print
36+ FL.splitSuffixBy (== ord' '\n') (A.toArrayN 1024)
20− . FL.sessionsOf 3 FL.sum
37+ . NS.read