on e25db62 help sign in
streamly/examples/FileSinkServer.hs
35 lines 989 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.Data.String
12import qualified Streamly.FileSystem.Handle as FH
13import qualified Streamly.Memory.Array as A
14import qualified Streamly.Network.Socket as NS
15import qualified Streamly.Network.Server as NS
16import qualified Streamly.Prelude as S
18import System.IO (withFile, IOMode(..))
20main :: IO ()
21main = do
22 file <- fmap head getArgs
23 withFile file AppendMode
24 (\src -> S.fold (FH.write src)
25 $ encodeChar8Unchecked
26 $ S.concatUnfold A.read
27 $ S.concatMapWith parallel (flip NS.withSocket recv)
28 $ S.unfold NS.listenOnPort 8090)
30 where
32 recv =
33 S.splitWithSuffix (== '\n') A.write
34 . decodeChar8
35 . S.unfold NS.read