From: Eugene Crosser Date: Sat, 19 Apr 2014 09:37:24 +0000 (+0400) Subject: refactor state handling X-Git-Tag: 0.9.0.0~45 X-Git-Url: http://average.org/gitweb/?a=commitdiff_plain;h=db22eaa53c505149f42cb3a7e3df9ea1fa9e1350;p=pdns-pipe-nmc.git refactor state handling --- diff --git a/pdns-pipe-nmc.hs b/pdns-pipe-nmc.hs index 2ef82c2..1786376 100644 --- a/pdns-pipe-nmc.hs +++ b/pdns-pipe-nmc.hs @@ -79,6 +79,7 @@ mainPdnsNmc = do hSetBuffering stdin LineBuffering hSetBuffering stdout LineBuffering + ver <- do let loopErr e = forever $ do @@ -96,10 +97,18 @@ mainPdnsNmc = do putStrLn $ "OK\tDnsNmc ready to serve, protocol v." ++ (show ver) mgr <- newManager def + let - newcache count name = (insert count name) - . (delete (if count >= 10 then count - 10 else count + 90)) + fetch = lookup + -- Save the name under current count, increment count for the next run + -- so the name is saved under the count that was put into the response. + stow name (count, cache) = + (if count >= 99 then 0 else count + 1 + , insert count name + $ delete (if count >= 10 then count - 10 else count + 90) cache + ) io = liftIO + mainloop = forever $ do l <- io getLine (count, cache) <- get @@ -117,10 +126,9 @@ mainPdnsNmc = do ++ " qtype: " ++ (show qtype) ++ " cache size: " ++ (show (size cache)) -- end debug - put (if count >= 99 then 0 else count + 1, - newcache count qname cache) + put $ stow qname (count, cache) PdnsRequestAXFR xrq -> - case lookup xrq cache of + case fetch xrq cache of Nothing -> io $ putStr $ pdnsReport ("AXFR for unknown id: " ++ (show xrq)) @@ -128,6 +136,7 @@ mainPdnsNmc = do io $ queryDom (queryOpNmc cfg mgr xrq) qname >>= putStr . (pdnsOutXfr ver count qname) PdnsRequestPing -> io $ putStrLn "END" + runStateT mainloop (0, empty) >> return () -- query by key from Namecoin