-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketProducer.hs
More file actions
29 lines (24 loc) · 789 Bytes
/
SocketProducer.hs
File metadata and controls
29 lines (24 loc) · 789 Bytes
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
{-# LANGUAGE DeriveGeneric #-}
module SocketProducer where
import F
import Flag
import Log
import Network
import Control.Monad
import Data.Aeson
import Control.Monad.IO.Class
import Control.Concurrent.STM
import qualified Data.ByteString.Lazy as BS
socketProducer :: PortNumber -> FlagProducer
socketProducer p = FlagProducer ("socket " ++ show p) (sProduce p)
sProduce :: PortNumber -> TChan SrcFlags -> F ()
sProduce p tchan = do
logI $ "Starting socket producer on port " ++ show p
s <- liftIO $ listenOn (PortNumber p)
forever $ do
(h, n, _) <- liftIO $ accept s
logI $ "Connection from " ++ n
c <- liftIO $ BS.hGetContents h
case decode c of
Nothing -> logE "Could not decode JSON"
Just srcfs -> liftIO . atomically $ writeTChan tchan srcfs