diff options
author | AndrewRademacher <andrew.rademacher@smrxt.com> | 2016-02-24 20:39:47 -0600 |
---|---|---|
committer | AndrewRademacher <andrew.rademacher@smrxt.com> | 2016-02-24 20:39:47 -0600 |
commit | 2ff46fce748b0e813f13f03c034065092145a28d (patch) | |
tree | 1c70beb0058dba6c3edebe8b7efbfc73c8ea7350 /src/Graylog/Types.hs | |
parent | 15981d57cda18a19fcb3012172a9f0994abfd97a (diff) | |
download | haskell-graylog-2ff46fce748b0e813f13f03c034065092145a28d.tar.gz haskell-graylog-2ff46fce748b0e813f13f03c034065092145a28d.tar.zst haskell-graylog-2ff46fce748b0e813f13f03c034065092145a28d.zip |
Defined Graylog type to contain connection informaiton.
Diffstat (limited to 'src/Graylog/Types.hs')
-rw-r--r-- | src/Graylog/Types.hs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Graylog/Types.hs b/src/Graylog/Types.hs new file mode 100644 index 0000000..e18826b --- /dev/null +++ b/src/Graylog/Types.hs | |||
@@ -0,0 +1,42 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
2 | |||
3 | module Graylog.Types | ||
4 | ( Graylog | ||
5 | , _graylogHost | ||
6 | , _graylogPort | ||
7 | , _graylogAddress | ||
8 | |||
9 | , openGraylog | ||
10 | , closeGraylog | ||
11 | ) where | ||
12 | |||
13 | import Data.Text (Text) | ||
14 | import qualified Data.Text as T | ||
15 | import Network.BSD | ||
16 | import Network.Socket | ||
17 | |||
18 | data Graylog | ||
19 | = Graylog | ||
20 | { _graylogHost :: String | ||
21 | , _graylogPort :: String | ||
22 | , _graylogAddress :: AddrInfo | ||
23 | , _graylogSocket :: Socket | ||
24 | , _graylogHostName :: Text | ||
25 | } | ||
26 | |||
27 | openGraylog :: HostName -> ServiceName -> IO (Either String Graylog) | ||
28 | openGraylog 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 | |||
39 | closeGraylog :: Graylog -> IO () | ||
40 | closeGraylog Graylog{..} = close _graylogSocket | ||
41 | |||
42 | |||