on a608695 help sign in
streamly/examples/FileSinkServer.hs
37 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.Unicode.Stream
13import qualified Streamly.FileSystem.Handle as FH
14import qualified Streamly.Data.Array.Storable.Foreign as A
15import qualified Streamly.Network.Socket as NS
16import qualified Streamly.Network.Inet.TCP as TCP
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 $ encodeLatin1
27 $ S.concatUnfold A.read
28 $ S.concatMapWith S.parallel use
29 $ S.unfold TCP.acceptOnPort 8090)
31 where
33 use sk = S.finally (liftIO $ close sk) (recv sk)
34 recv =
35 S.splitWithSuffix (== '\n') A.write
36 . decodeLatin1
37 . S.unfold NS.read