-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileSender.hs
45 lines (37 loc) · 1.4 KB
/
FileSender.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
{-# LANGUAGE FlexibleContexts #-}
-- A TCP client that does the following:
--
-- o Reads multiple filenames passed on the command line
-- o Opens as many concurrent connections to the server
-- o Sends all the files concurrently to the server
import Data.Function ((&))
import Network.Socket (Socket)
import System.Environment (getArgs)
import System.IO (Handle, withFile, IOMode(..))
import qualified Control.Monad.Catch as Catch
import qualified Network.Socket as Net
import qualified Streamly.Data.Fold as Fold
import qualified Streamly.Data.Stream.Prelude as Stream
import qualified Streamly.FileSystem.Handle as Handle
import qualified Streamly.Network.Inet.TCP as TCP
import qualified Streamly.Network.Socket as Socket
main :: IO ()
main = do
files <- getArgs
sendAll files
where
connect :: IO Socket
connect = TCP.connect (127,0,0,1) 8090
fileToSocket :: Handle -> Socket -> IO ()
fileToSocket fh sk =
Handle.readChunks fh -- Stream IO (Array Word8)
& Stream.fold (Socket.writeChunks sk) -- IO ()
sendFile :: String -> IO ()
sendFile file =
withFile file ReadMode $ \fh ->
Catch.bracket connect Net.close (fileToSocket fh)
sendAll :: [String] -> IO ()
sendAll files =
Stream.fromList files -- Stream IO String
& Stream.parMapM id sendFile -- Stream IO ()
& Stream.fold Fold.drain -- IO ()