]> git.immae.eu Git - github/fretlink/haskell-graylog.git/blame - src/Graylog/Types.hs
Defined Graylog type to contain connection informaiton.
[github/fretlink/haskell-graylog.git] / src / Graylog / Types.hs
CommitLineData
2ff46fce
A
1{-# LANGUAGE RecordWildCards #-}
2
3module Graylog.Types
4 ( Graylog
5 , _graylogHost
6 , _graylogPort
7 , _graylogAddress
8
9 , openGraylog
10 , closeGraylog
11 ) where
12
13import Data.Text (Text)
14import qualified Data.Text as T
15import Network.BSD
16import Network.Socket
17
18data Graylog
19 = Graylog
20 { _graylogHost :: String
21 , _graylogPort :: String
22 , _graylogAddress :: AddrInfo
23 , _graylogSocket :: Socket
24 , _graylogHostName :: Text
25 }
26
27openGraylog :: HostName -> ServiceName -> IO (Either String Graylog)
28openGraylog host port = do
29 infos <- getAddrInfo Nothing (Just host) (Just port)
30 case infos of
31 [] -> return $ Left "No address info found."
32 [info] -> do
33 sock <- socket (addrFamily info) Datagram defaultProtocol
34 connect sock (addrAddress info)
35 hostname <- getHostName
36 return $ Right $ Graylog host port info sock (T.pack hostname)
37 _ -> return $ Left "Too many address infos found."
38
39closeGraylog :: Graylog -> IO ()
40closeGraylog Graylog{..} = close _graylogSocket
41
42