-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunix-sockets.hs
More file actions
50 lines (46 loc) · 1.34 KB
/
unix-sockets.hs
File metadata and controls
50 lines (46 loc) · 1.34 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
39
40
41
42
43
44
45
46
47
48
49
50
{-# LANGUAGE OverloadedStrings #-}
-- |
-- Module: Main
-- Description: UNIX Sockets client example.
-- Copyright: (c) 2014-2015, Peter Trško
-- License: BSD3
module Main (main)
where
import Control.Concurrent
( forkIO
, newEmptyMVar
, putMVar
, readMVar
, threadDelay
)
import Control.Monad (void, mapM_)
import System.Environment (getArgs)
import Control.Lens ((.~), (&))
import Data.ConnectionPool
( createUnixClientPool
, numberOfResourcesPerStripe
, numberOfStripes
, withUnixClientConnection
)
import Data.Default.Class (Default(def))
import Data.Streaming.Network (appWrite, clientSettingsUnix)
main :: IO ()
main = do
[socket, numStripes, numPerStripe] <- getArgs
pool <- createUnixClientPool
(poolParams numStripes numPerStripe)
(clientSettingsUnix socket)
thread1 <- newEmptyMVar
thread2 <- newEmptyMVar
void . forkIO . withUnixClientConnection pool $ \appData -> do
threadDelay 1000
appWrite appData "1: I'm alive!\n"
putMVar thread1 ()
void . forkIO . withUnixClientConnection pool $ \appData -> do
appWrite appData "2: I'm alive!\n"
putMVar thread2 ()
mapM_ readMVar [thread1, thread2]
where
poolParams m n =
def & numberOfStripes .~ read m
& numberOfResourcesPerStripe .~ read n