on bbfffe8 help sign in
streamly/examples/FileSinkServer.hs
38 lines 1.1 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 Control.Monad.IO.Class (liftIO)
9import Network.Socket (close)
10import System.Environment (getArgs)
12import Streamly
13import Streamly.Data.Unicode.Stream
14import qualified Streamly.FileSystem.Handle as FH
15import qualified Streamly.Memory.Array as A
16import qualified Streamly.Network.Socket as NS
17import qualified Streamly.Network.Inet.TCP as TCP
18import qualified Streamly.Prelude as S
20import System.IO (withFile, IOMode(..))
22main :: IO ()
23main = do
24 file <- fmap head getArgs
25 withFile file AppendMode
26 (\src -> S.fold (FH.write src)
27 $ encodeLatin1Lax
28 $ S.concatUnfold A.read
29 $ S.concatMapWith parallel use
30 $ S.unfold TCP.acceptOnPort 8090)
32 where
34 use sk = S.finally (liftIO $ close sk) (recv sk)
35 recv =
36 S.splitWithSuffix (== '\n') A.write
37 . decodeLatin1
38 . S.unfold NS.read