From 9d1dad4af93598f403dae2d323539c60073f892d Mon Sep 17 00:00:00 2001 From: joe Date: Mon, 23 Jan 2017 07:04:42 -0500 Subject: PeerStore is now persistent. --- examples/dhtd.hs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 3df77190..19b45acb 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs @@ -18,6 +18,7 @@ import Data.Maybe import Data.String import qualified Data.ByteString as B (ByteString,writeFile,readFile) ; import Data.ByteString (ByteString) +import qualified Data.ByteString.Char8 as B8 import System.IO import System.IO.Error import Text.PrettyPrint.HughesPJClass @@ -114,15 +115,16 @@ clientSession st signalQuit sock n h = do line <- map toLower . dropWhile isSpace <$> hGetLine h let cmd0 action = action >> clientSession st signalQuit sock n h cmd action = cmd0 $ join $ runDHT st action - case line of + (c,args) = second (dropWhile isSpace) $ break isSpace line + case (c,args) of - "quit" -> hPutClient h "" >> hClose h + ("quit", _) -> hPutClient h "" >> hClose h - "stop" -> do hPutClient h "Terminating DHT Daemon." - hClose h - putMVar signalQuit () + ("stop", _) -> do hPutClient h "Terminating DHT Daemon." + hClose h + putMVar signalQuit () - "ls" -> cmd $ do + ("ls", _) -> cmd $ do tbl <- getTable t <- showTable me <- myNodeIdAccordingTo (read "8.8.8.8:6881") @@ -135,13 +137,13 @@ clientSession st signalQuit sock n h = do , ("internet address", show ip) , ("buckets", show $ R.shape tbl)] ] - "external-ip" -> cmd $ do + ("external-ip", _) -> cmd $ do ip <- routableAddress return $ do hPutClient h $ maybe "" (takeWhile (/=':') . show) ip - s | s=="swarms" || "swarms " `isPrefixOf` s -> cmd $ do - let fltr = case dropWhile isSpace (drop 7 s) of + ("swarms", s) -> cmd $ do + let fltr = case s of ('-':'v':cs) | all isSpace (take 1 cs) -> const True _ -> (\(h,c,n) -> c/=0 ) @@ -151,8 +153,8 @@ clientSession st signalQuit sock n h = do return $ do hPutClient h $ showReport r - s | "peers " `isPrefixOf` s -> cmd $ do - let ih = fromString (drop 6 s) + ("peers ", s) -> cmd $ do + let ih = fromString s ps <- allPeers ih return $ do hPutClient h $ showReport $ map (((,) "") . show . pPrint) ps @@ -168,6 +170,15 @@ main = do (Lifted.ioError $ userError "unable to resolve bootstrap nodes") saved_nodes <- resume + peers'trial <- liftIO $ tryIOError $ B.readFile "bt-peers.dat" + saved_peers <- + either (const $ do liftIO $ putStrLn "Error reading bt-peers.dat" + return Nothing) + (return . Just) + peers'trial + + maybe (return ()) mergeSavedPeers saved_peers + when (isJust saved_nodes) $ do b <- isBootstrapped tbl <- getTable @@ -209,7 +220,9 @@ main = do ,("buckets", show $ R.shape tbl) ,("optBucketCount", show bc) ] - snapshot >>= liftIO . B.writeFile "dht-nodes.dat" - waitForSignal + waitForSignal -- Await unix socket to signal termination. + + snapshot >>= liftIO . B.writeFile "dht-nodes.dat" + savePeerStore >>= liftIO . B.writeFile "bt-peers.dat" -- cgit v1.2.3