-- A concurrent TCP server that: -- -- * receives connections from clients -- * splits the incoming data into lines -- * lines from concurrent connections are merged into a single srteam -- * writes the line stream to an output file import System.Environment (getArgs) import Streamly import Streamly.String import qualified Streamly.FileSystem.Handle as FH import qualified Streamly.Memory.Array as A import qualified Streamly.Network.Socket as NS import qualified Streamly.Network.Server as NS import qualified Streamly.Prelude as S import System.IO (withFile, IOMode(..)) main :: IO () main = do file <- fmap head getArgs withFile file AppendMode (\src -> S.runFold (FH.write src) $ encodeChar8Unchecked $ S.concatMap A.read $ S.concatMapBy parallel (flip NS.withSocketS recv) $ NS.connectionsOnAllAddrs 8090) where recv = S.splitBySuffix (== '\n') A.write . decodeChar8 . NS.read