summaryrefslogtreecommitdiff
path: root/src/Network/KRPC
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-07 03:53:05 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-07 03:53:05 +0400
commit6e77e14e2c011760eccc9d6989cd229420bdc741 (patch)
tree43db9de85968af0bffdc6c6b5714963b75df9a69 /src/Network/KRPC
parent2812bdadb55e1ca7a1e5685f3fb2dafe19259970 (diff)
Allow to pass options from outside
Diffstat (limited to 'src/Network/KRPC')
-rw-r--r--src/Network/KRPC/Manager.hs60
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 @@
18module Network.KRPC.Manager 18module 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
43import Data.ByteString as BS 44import Data.ByteString as BS
44import Data.ByteString.Char8 as BC 45import Data.ByteString.Char8 as BC
45import Data.ByteString.Lazy as BL 46import Data.ByteString.Lazy as BL
47import Data.Default.Class
46import Data.IORef 48import Data.IORef
47import Data.List as L 49import Data.List as L
48import Data.Map as M 50import Data.Map as M
@@ -58,6 +60,41 @@ import System.IO.Error
58import System.Timeout 60import System.Timeout
59 61
60 62
63{-----------------------------------------------------------------------
64-- Options
65-----------------------------------------------------------------------}
66
67-- | RPC manager options.
68data 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
76defaultSeedTransaction :: Int
77defaultSeedTransaction = 0
78
79defaultQueryTimeout :: Int
80defaultQueryTimeout = 120
81
82-- | Permissive defaults.
83instance Default Options where
84 def = Options
85 { optSeedTransaction = defaultSeedTransaction
86 , optQueryTimeout = defaultQueryTimeout
87 }
88
89validateOptions :: Options -> IO ()
90validateOptions Options {..}
91 | optQueryTimeout < 1 = throwIO (userError "non-positive query timeout")
92 | otherwise = return ()
93
94{-----------------------------------------------------------------------
95-- Options
96-----------------------------------------------------------------------}
97
61type KResult = Either KError KResponse 98type KResult = Either KError KResponse
62 99
63type TransactionCounter = IORef Int 100type TransactionCounter = IORef Int
@@ -108,23 +145,19 @@ sockAddrFamily (SockAddrInet _ _ ) = AF_INET
108sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6 145sockAddrFamily (SockAddrInet6 _ _ _ _) = AF_INET6
109sockAddrFamily (SockAddrUnix _ ) = AF_UNIX 146sockAddrFamily (SockAddrUnix _ ) = AF_UNIX
110 147
111seedTransaction :: Int
112seedTransaction = 0
113
114defaultQueryTimeout :: Int
115defaultQueryTimeout = 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'.
119newManager :: SockAddr -- ^ address to listen on; 150newManager :: 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.
122newManager servAddr handlers = do 154newManager 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.
146withManager :: SockAddr -> [Handler h] -> (Manager h -> IO a) -> IO a 179withManager :: Options -> SockAddr -> [Handler h]
147withManager addr hs = bracket (newManager addr hs) closeManager 180 -> (Manager h -> IO a) -> IO a
181withManager opts addr hs = bracket (newManager opts addr hs) closeManager
148 182
149{----------------------------------------------------------------------- 183{-----------------------------------------------------------------------
150-- Logging 184-- Logging