diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-07 03:53:05 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-07 03:53:05 +0400 |
commit | 6e77e14e2c011760eccc9d6989cd229420bdc741 (patch) | |
tree | 43db9de85968af0bffdc6c6b5714963b75df9a69 /src/Network/KRPC/Manager.hs | |
parent | 2812bdadb55e1ca7a1e5685f3fb2dafe19259970 (diff) |
Allow to pass options from outside
Diffstat (limited to 'src/Network/KRPC/Manager.hs')
-rw-r--r-- | src/Network/KRPC/Manager.hs | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/src/Network/KRPC/Manager.hs b/src/Network/KRPC/Manager.hs index 4d1cfb69..7edcf72d 100644 --- a/src/Network/KRPC/Manager.hs +++ b/src/Network/KRPC/Manager.hs | |||
@@ -18,6 +18,7 @@ | |||
18 | module Network.KRPC.Manager | 18 | module Network.KRPC.Manager |
19 | ( -- * Manager | 19 | ( -- * Manager |
20 | MonadKRPC (..) | 20 | MonadKRPC (..) |
21 | , Options (..) | ||
21 | , Manager | 22 | , Manager |
22 | , newManager | 23 | , newManager |
23 | , closeManager | 24 | , closeManager |
@@ -43,6 +44,7 @@ import Data.BEncode as BE | |||
43 | import Data.ByteString as BS | 44 | import Data.ByteString as BS |
44 | import Data.ByteString.Char8 as BC | 45 | import Data.ByteString.Char8 as BC |
45 | import Data.ByteString.Lazy as BL | 46 | import Data.ByteString.Lazy as BL |
47 | import Data.Default.Class | ||
46 | import Data.IORef | 48 | import Data.IORef |
47 | import Data.List as L | 49 | import Data.List as L |
48 | import Data.Map as M | 50 | import Data.Map as M |
@@ -58,6 +60,41 @@ import System.IO.Error | |||
58 | import System.Timeout | 60 | import System.Timeout |
59 | 61 | ||
60 | 62 | ||
63 | {----------------------------------------------------------------------- | ||
64 | -- Options | ||
65 | -----------------------------------------------------------------------} | ||
66 | |||
67 | -- | RPC manager options. | ||
68 | data Options = Options | ||
69 | { -- | Initial 'TransactionId' incremented with each 'query'; | ||
70 | optSeedTransaction :: Int | ||
71 | |||
72 | -- | Time to wait for response from remote node, in seconds. | ||
73 | , optQueryTimeout :: Int | ||
74 | } deriving (Show, Eq) | ||
75 | |||
76 | defaultSeedTransaction :: Int | ||
77 | defaultSeedTransaction = 0 | ||
78 | |||
79 | defaultQueryTimeout :: Int | ||
80 | defaultQueryTimeout = 120 | ||
81 | |||
82 | -- | Permissive defaults. | ||
83 | instance Default Options where | ||
84 | def = Options | ||
85 | { optSeedTransaction = defaultSeedTransaction | ||
86 | , optQueryTimeout = defaultQueryTimeout | ||
87 | } | ||
88 | |||
89 | validateOptions :: Options -> IO () | ||
90 | validateOptions Options {..} | ||
91 | | optQueryTimeout < 1 = throwIO (userError "non-positive query timeout") | ||
92 | | otherwise = return () | ||
93 | |||
94 | {----------------------------------------------------------------------- | ||
95 | -- Options | ||
96 | -----------------------------------------------------------------------} | ||
97 | |||
61 | type KResult = Either KError KResponse | 98 | type KResult = Either KError KResponse |
62 | 99 | ||
63 | type TransactionCounter = IORef Int | 100 | type TransactionCounter = IORef Int |
@@ -108,23 +145,19 @@ sockAddrFamily (SockAddrInet _ _ ) = AF_INET | |||
108 | sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 | 145 | sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 |
109 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX | 146 | sockAddrFamily (SockAddrUnix _ ) = AF_UNIX |
110 | 147 | ||
111 | seedTransaction :: Int | ||
112 | seedTransaction = 0 | ||
113 | |||
114 | defaultQueryTimeout :: Int | ||
115 | defaultQueryTimeout = 120 | ||
116 | |||
117 | -- | Bind socket to the specified address. To enable query handling | 148 | -- | Bind socket to the specified address. To enable query handling |
118 | -- run 'listen'. | 149 | -- run 'listen'. |
119 | newManager :: SockAddr -- ^ address to listen on; | 150 | newManager :: Options -- ^ various protocol options; |
151 | -> SockAddr -- ^ address to listen on; | ||
120 | -> [Handler h] -- ^ handlers to run on incoming queries. | 152 | -> [Handler h] -- ^ handlers to run on incoming queries. |
121 | -> IO (Manager h) -- ^ new manager. | 153 | -> IO (Manager h) -- ^ new rpc manager. |
122 | newManager servAddr handlers = do | 154 | newManager opts @ Options {..} servAddr handlers = do |
155 | validateOptions opts | ||
123 | sock <- bindServ | 156 | sock <- bindServ |
124 | tref <- newEmptyMVar | 157 | tref <- newEmptyMVar |
125 | tran <- newIORef seedTransaction | 158 | tran <- newIORef optSeedTransaction |
126 | calls <- newIORef M.empty | 159 | calls <- newIORef M.empty |
127 | return $ Manager sock defaultQueryTimeout tref tran calls handlers | 160 | return $ Manager sock optQueryTimeout tref tran calls handlers |
128 | where | 161 | where |
129 | bindServ = do | 162 | bindServ = do |
130 | let family = sockAddrFamily servAddr | 163 | let family = sockAddrFamily servAddr |
@@ -143,8 +176,9 @@ closeManager Manager {..} = do | |||
143 | 176 | ||
144 | -- | Normally you should use Control.Monad.Trans.Resource.allocate | 177 | -- | Normally you should use Control.Monad.Trans.Resource.allocate |
145 | -- function. | 178 | -- function. |
146 | withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a | 179 | withManager :: Options -> SockAddr -> [Handler h] |
147 | withManager addr hs = bracket (newManager addr hs) closeManager | 180 | -> (Manager h -> IO a) -> IO a |
181 | withManager opts addr hs = bracket (newManager opts addr hs) closeManager | ||
148 | 182 | ||
149 | {----------------------------------------------------------------------- | 183 | {----------------------------------------------------------------------- |
150 | -- Logging | 184 | -- Logging |