-
Notifications
You must be signed in to change notification settings - Fork 3
/
CmdClient.hs
58 lines (45 loc) · 1.78 KB
/
CmdClient.hs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Main (main) where
import Control.Monad (when)
import Data.Function ((&))
import Data.Word (Word8)
import Network.Socket (PortNumber)
import Streamly.Data.Stream.Prelude (Stream)
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.Data.Stream.Prelude as Stream
import qualified Streamly.Internal.Network.Inet.TCP as TCP (pipeBytes)
import qualified Streamly.Unicode.Stream as Unicode
remoteAddr :: (Word8,Word8,Word8,Word8)
remoteAddr = (192, 168, 1, 4)
remotePort :: PortNumber
remotePort = 8091
chunkSize :: Int
chunkSize = 10000
nChunks :: Int
nChunks = 10
counter :: String -> Int -> () -> IO Int
counter tag n () = do
let i = n + 1
when (i `mod` nChunks == 0) $
putStrLn $ tag ++ show (i * chunkSize)
return i
sender :: Stream IO ()
sender =
Stream.repeat "time\nrandom\n" -- Stream IO String
& Stream.unfoldMany Unfold.fromList -- Stream IO Char
& Unicode.encodeLatin1 -- Stream IO Word8
& TCP.pipeBytes remoteAddr remotePort -- Stream IO Word8
& Unicode.decodeLatin1 -- Stream IO Char
& split (== '\n') Fold.drain -- Stream IO String
& Stream.foldMany chunk -- Stream IO ()
where
chunk = Fold.take chunkSize Fold.drain
split p f = Stream.foldMany (Fold.takeEndBy_ p f)
main :: IO ()
main = do
Stream.replicate 4 sender -- Stream IO (Stream IO ())
& Stream.parConcat id -- Stream IO ()
& Stream.postscan counts -- Stream IO Int
& Stream.fold Fold.drain -- IO ()
where
counts = Fold.foldlM' (counter "rcvd") (return 0 :: IO Int)