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