forked from composewell/streamly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSinkServer.hs
More file actions
38 lines (32 loc) · 1.07 KB
/
FileSinkServer.hs
File metadata and controls
38 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- A concurrent TCP server that:
--
-- * receives connections from clients
-- * splits the incoming data into lines
-- * lines from concurrent connections are merged into a single srteam
-- * writes the line stream to an output file
import Control.Monad.IO.Class (liftIO)
import Network.Socket (close)
import System.Environment (getArgs)
import Streamly
import Streamly.Data.Unicode.Stream
import qualified Streamly.FileSystem.Handle as FH
import qualified Streamly.Memory.Array as A
import qualified Streamly.Network.Socket as NS
import qualified Streamly.Network.Inet.TCP as TCP
import qualified Streamly.Prelude as S
import System.IO (withFile, IOMode(..))
main :: IO ()
main = do
file <- fmap head getArgs
withFile file AppendMode
(\src -> S.fold (FH.write src)
$ encodeLatin1Lax
$ S.concatUnfold A.read
$ S.concatMapWith parallel use
$ S.unfold TCP.acceptOnPort 8090)
where
use sk = S.finally (liftIO $ close sk) (recv sk)
recv =
S.splitWithSuffix (== '\n') A.write
. decodeLatin1
. S.unfold NS.read