]> git.immae.eu Git - github/fretlink/haskell-graylog.git/blame - src/Graylog/Types.hs
Added readme, license, and light commenting.
[github/fretlink/haskell-graylog.git] / src / Graylog / Types.hs
CommitLineData
b23afe7b 1{-# LANGUAGE LambdaCase #-}
2ff46fce
A
2{-# LANGUAGE RecordWildCards #-}
3
4module Graylog.Types
5 ( Graylog
6 , _graylogHost
7 , _graylogPort
8 , _graylogAddress
c91dbdc0
A
9 , _graylogSocket
10 , _graylogHostName
f82a8dfc 11 , _graylogChunkSize
c91dbdc0 12 , ChunkSize
2ff46fce 13
c91dbdc0 14 , defaultChunkSize
2ff46fce
A
15 , openGraylog
16 , closeGraylog
17 ) where
18
b23afe7b 19import Data.List
2ff46fce
A
20import Data.Text (Text)
21import qualified Data.Text as T
22import Network.BSD
23import Network.Socket
24
d9a5d441 25-- | The maximum size of each datagram when using UDP transit methods.
c91dbdc0
A
26type ChunkSize = Word
27
d9a5d441
A
28-- | Handle for a socket connected to Graylog. In some cases this socket
29-- is UDP and will not have a maintained session.
2ff46fce
A
30data Graylog
31 = Graylog
c91dbdc0
A
32 { _graylogHost :: String
33 , _graylogPort :: String
34 , _graylogAddress :: AddrInfo
35 , _graylogSocket :: Socket
36 , _graylogHostName :: Text
37 , _graylogChunkSize :: ChunkSize
2ff46fce
A
38 }
39
c91dbdc0
A
40defaultChunkSize :: ChunkSize
41defaultChunkSize = 8192
42
b23afe7b 43openGraylog
d9a5d441
A
44 :: HostName -- ^ The host on which graylog is running.
45 -> ServiceName -- ^ The port on which graylog is running.
46 -> ChunkSize -- ^ The maximum size of each UDP datagram.
47 -> IO (Either String Graylog)
f82a8dfc
A
48openGraylog h p cksize
49 | cksize < 1024 = return $ Left "ChunkSize must be at least 1024."
50 | otherwise = getAddrInfo Nothing (Just h) (Just p) >>= \case
b23afe7b
A
51 [] -> return $ Left "No address info found."
52 infos ->
53 case find (\i -> addrSocketType i == Datagram) infos of
54 Nothing -> return $ Left "No datagram info found for address."
55 Just i -> do
56 sock <- socket (addrFamily i) Datagram defaultProtocol
57 connect sock (addrAddress i)
58 hostname <- getHostName
59 return $ Right $ Graylog h p i sock (T.pack hostname) cksize
2ff46fce
A
60
61closeGraylog :: Graylog -> IO ()
62closeGraylog Graylog{..} = close _graylogSocket