on master help sign in
sync server example with changes in the library
75ef6c2Harendra Kumar
examples/FileSinkServer.hs+11−16
@@ -5,33 +5,28 @@
5-- * lines from concurrent connections are merged into a single srteam
5-- * lines from concurrent connections are merged into a single srteam
6-- * writes the line stream to an output file
6-- * writes the line stream to an output file
7
7
8−import Control.Monad.IO.Class (liftIO)
9−import Data.Char (ord, chr)
10−import System.IO (openFile, IOMode(..), hClose, hPutStrLn)
11import System.Environment (getArgs)
8import System.Environment (getArgs)
12
9
13import Streamly
10import Streamly
11+import Streamly.String
12+import qualified Streamly.FileSystem.File as File
14import qualified Streamly.Fold as FL
13import qualified Streamly.Fold as FL
15import qualified Streamly.Mem.Array as A
14import qualified Streamly.Mem.Array as A
16import qualified Streamly.Network.Socket as NS
15import qualified Streamly.Network.Socket as NS
17−import qualified Network.Socket as NS
18import qualified Streamly.Prelude as S
16import qualified Streamly.Prelude as S
19
17
20main :: IO ()
18main :: IO ()
21main = do
19main = do
22− h <- openOutput
20+ file <- fmap head getArgs
23− S.mapM_ (hPutLines h)
21+ File.append file
22+ $ encodeChar8Unchecked
23+ $ S.concatMap A.read
24− $ S.concatMapBy parallel (withSocket recv)
24+ $ S.concatMapBy parallel recv
25− $ fmap fst (NS.recvConnectionsOn 8090)
25+ $ NS.recvConnectionsOn 8090
26− hClose h
27
26
28 where
27 where
29
28
30− openOutput = fmap head getArgs >>= \file -> openFile file AppendMode
31−
32− withSocket f sk = S.finally (liftIO (NS.close sk)) (f sk)
33− hPutLines h = hPutStrLn h . map (chr . fromIntegral) . A.toList
34− ord' = (fromIntegral . ord)
35 recv =
29 recv =
36− FL.splitSuffixBy (== ord' '\n') (A.toArrayN 1024)
30+ FL.splitSuffixBy' (== '\n') (A.toArrayN 1024)
37− . NS.read
31+ . decodeChar8
32+ . NS.fromSocket