on 64524bf help sign in
streamly/examples/FileSinkServer.hs
33 lines 910 B history raw
1-- A concurrent TCP server that:
2--
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
8import System.Environment (getArgs)
10import Streamly
11import Streamly.String
12import qualified Streamly.FileSystem.File as File
13import qualified Streamly.Fold as FL
14import qualified Streamly.Mem.Array as A
15import qualified Streamly.Network.Socket as NS
16import qualified Streamly.Network.Server as NS
17import qualified Streamly.Prelude as S
19main :: IO ()
20main = do
21 file <- fmap head getArgs
22 File.append file
23 $ encodeChar8Unchecked
24 $ S.concatMap A.read
25 $ S.concatMapBy parallel recv
26 $ NS.connectionsOnAllAddrs 8090
28 where
30 recv =
31 FL.splitSuffixBy' (== '\n') (A.toArrayN 1024)
32 . decodeChar8
33 . NS.fromSocket