-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
26 lines (23 loc) · 772 Bytes
/
Main.hs
File metadata and controls
26 lines (23 loc) · 772 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
import qualified Streaming.Prelude as S
import Streaming.UDP
import Control.Monad.Trans.Resource
import Control.Monad.IO.Class
import qualified Data.ByteString as B
import System.Environment
import System.IO
printNow a = putStrLn a >> hFlush stdout
main = do
args <- getArgs
case args of
[host, portStr] -> case reads portStr of
[] -> printNow "Invalid port"
(port, _):_ -> netcat host port
otherwise -> printNow
"Please specify host and port, e.g.: \n\t./statsd.sh 127.0.0.1 8125"
netcat host port = do
printNow $ "Receiving on "<>host<>":"<>(show port)
runResourceT $ S.mapM_ (liftIO . printNow) $ S.map txfm udpStream
where
udpStream = fromUDP host port len
txfm = (map (toEnum.fromEnum) . B.unpack) . fst
len = 1024