diff options
-rw-r--r-- | examples/dhtd.hs | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 4d9072b5..36b8e294 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -21,6 +21,7 @@ import Control.Concurrent.STM | |||
21 | import Control.DeepSeq | 21 | import Control.DeepSeq |
22 | import Control.Exception | 22 | import Control.Exception |
23 | import Control.Monad | 23 | import Control.Monad |
24 | import Data.Bool | ||
24 | import Data.Char | 25 | import Data.Char |
25 | import Data.Hashable | 26 | import Data.Hashable |
26 | import Data.List | 27 | import Data.List |
@@ -125,6 +126,11 @@ data DHTSearch nid ni = forall addr tok r. DHTSearch | |||
125 | , searchResults :: TVar (Set.Set String) | 126 | , searchResults :: TVar (Set.Set String) |
126 | } | 127 | } |
127 | 128 | ||
129 | data DHTPing ni = forall r. DHTPing | ||
130 | { pingQuery :: ni -> IO (Maybe r) | ||
131 | , pingShowResult :: r -> String | ||
132 | } | ||
133 | |||
128 | data DHT = forall nid ni. ( Show ni | 134 | data DHT = forall nid ni. ( Show ni |
129 | , Read ni | 135 | , Read ni |
130 | , ToJSON ni | 136 | , ToJSON ni |
@@ -139,7 +145,7 @@ data DHT = forall nid ni. ( Show ni | |||
139 | ) => | 145 | ) => |
140 | DHT | 146 | DHT |
141 | { dhtBuckets :: TVar (BucketList ni) | 147 | { dhtBuckets :: TVar (BucketList ni) |
142 | , dhtPing :: ni -> IO Bool | 148 | , dhtPing :: Map.Map String (DHTPing ni) |
143 | , dhtQuery :: Map.Map String (DHTQuery nid ni) | 149 | , dhtQuery :: Map.Map String (DHTQuery nid ni) |
144 | , dhtAnnouncables :: Map.Map String DHTAnnouncable | 150 | , dhtAnnouncables :: Map.Map String DHTAnnouncable |
145 | , dhtParseId :: String -> Either String nid | 151 | , dhtParseId :: String -> Either String nid |
@@ -185,7 +191,7 @@ fallbackLoad fname = do | |||
185 | 191 | ||
186 | 192 | ||
187 | pingNodes :: String -> DHT -> IO Bool | 193 | pingNodes :: String -> DHT -> IO Bool |
188 | pingNodes netname DHT{dhtPing} = do | 194 | pingNodes netname DHT{dhtPing} | Just DHTPing{pingQuery=ping} <- Map.lookup "ping" dhtPing = do |
189 | let fname = nodesFileName netname | 195 | let fname = nodesFileName netname |
190 | attempt <- tryIOError $ do | 196 | attempt <- tryIOError $ do |
191 | J.decode <$> L.readFile fname | 197 | J.decode <$> L.readFile fname |
@@ -196,10 +202,11 @@ pingNodes netname DHT{dhtPing} = do | |||
196 | myThreadId >>= flip labelThread ("pinging."++fname) | 202 | myThreadId >>= flip labelThread ("pinging."++fname) |
197 | putStrLn $ "Forked "++show fname | 203 | putStrLn $ "Forked "++show fname |
198 | withTaskGroup ("withTaskGroup."++fname) 10 $ \g -> do | 204 | withTaskGroup ("withTaskGroup."++fname) 10 $ \g -> do |
199 | mapM_ (\n -> forkTask g (show n) $ void $ dhtPing n) | 205 | mapM_ (\n -> forkTask g (show n) $ void $ ping n) |
200 | (ns `asTypeOf` []) | 206 | (ns `asTypeOf` []) |
201 | putStrLn $ "Load finished "++show fname | 207 | putStrLn $ "Load finished "++show fname |
202 | return True | 208 | return True |
209 | pingNodes _ _ = return False | ||
203 | 210 | ||
204 | 211 | ||
205 | 212 | ||
@@ -402,11 +409,13 @@ clientSession s@Session{..} sock cnum h = do | |||
402 | , ("node-id", show $ thisNode bkts) | 409 | , ("node-id", show $ thisNode bkts) |
403 | , ("network", netname) ] | 410 | , ("network", netname) ] |
404 | 411 | ||
405 | ("ping", s) | Just DHT{dhtPing} <- Map.lookup netname dhts | 412 | (pinglike, s) | Just DHT{dhtPing} <- Map.lookup netname dhts |
413 | , Just DHTPing{ pingQuery=ping | ||
414 | , pingShowResult=showr } <- Map.lookup pinglike dhtPing | ||
406 | -> cmd0 $ do | 415 | -> cmd0 $ do |
407 | case readEither s of | 416 | case readEither s of |
408 | Right addr -> do result <- dhtPing addr | 417 | Right addr -> do result <- ping addr |
409 | let rs = [" ", show result] | 418 | let rs = [" ", maybe "Timeout." showr result] |
410 | hPutClient h $ unlines rs | 419 | hPutClient h $ unlines rs |
411 | Left er -> hPutClient h er | 420 | Left er -> hPutClient h er |
412 | ("k", s) | "" <- strp s -> cmd0 $ do | 421 | ("k", s) | "" <- strp s -> cmd0 $ do |
@@ -658,7 +667,10 @@ main = do | |||
658 | peerPort <- atomically $ newTVar 6881 -- BitTorrent client TCP port. | 667 | peerPort <- atomically $ newTVar 6881 -- BitTorrent client TCP port. |
659 | let mainlineDHT bkts wantip = DHT | 668 | let mainlineDHT bkts wantip = DHT |
660 | { dhtBuckets = bkts btR | 669 | { dhtBuckets = bkts btR |
661 | , dhtPing = Mainline.ping bt | 670 | , dhtPing = Map.singleton "ping" $ DHTPing |
671 | { pingQuery = fmap (bool Nothing (Just ())) . Mainline.ping bt | ||
672 | , pingShowResult = show | ||
673 | } | ||
662 | , dhtQuery = Map.fromList | 674 | , dhtQuery = Map.fromList |
663 | [ ("node", DHTQuery (Mainline.nodeSearch bt) | 675 | [ ("node", DHTQuery (Mainline.nodeSearch bt) |
664 | (\ni -> fmap Mainline.unwrapNodes | 676 | (\ni -> fmap Mainline.unwrapNodes |
@@ -722,7 +734,10 @@ main = do | |||
722 | 734 | ||
723 | let toxDHT bkts = DHT | 735 | let toxDHT bkts = DHT |
724 | { dhtBuckets = bkts (Tox.toxRouting tox) | 736 | { dhtBuckets = bkts (Tox.toxRouting tox) |
725 | , dhtPing = Tox.ping (Tox.toxDHT tox) | 737 | , dhtPing = Map.singleton "ping" $ DHTPing |
738 | { pingQuery = fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox) | ||
739 | , pingShowResult = show | ||
740 | } | ||
726 | , dhtQuery = Map.fromList | 741 | , dhtQuery = Map.fromList |
727 | [ ("node", DHTQuery (Tox.nodeSearch $ Tox.toxDHT tox) | 742 | [ ("node", DHTQuery (Tox.nodeSearch $ Tox.toxDHT tox) |
728 | (\ni -> fmap Tox.unwrapNodes | 743 | (\ni -> fmap Tox.unwrapNodes |
@@ -844,13 +859,16 @@ main = do | |||
844 | forM_ (Map.toList dhts) | 859 | forM_ (Map.toList dhts) |
845 | $ \(netname, dht@DHT { dhtBuckets = bkts | 860 | $ \(netname, dht@DHT { dhtBuckets = bkts |
846 | , dhtQuery = qrys | 861 | , dhtQuery = qrys |
847 | , dhtPing = ping | 862 | , dhtPing = pings |
848 | , dhtFallbackNodes = getBootstrapNodes }) -> do | 863 | , dhtFallbackNodes = getBootstrapNodes }) -> do |
849 | btSaved <- loadNodes netname -- :: IO [Mainline.NodeInfo] | 864 | btSaved <- loadNodes netname -- :: IO [Mainline.NodeInfo] |
850 | putStrLn $ "Loaded "++show (length btSaved)++" nodes for "++netname++"." | 865 | putStrLn $ "Loaded "++show (length btSaved)++" nodes for "++netname++"." |
851 | fallbackNodes <- getBootstrapNodes | 866 | fallbackNodes <- getBootstrapNodes |
852 | let isNodesSearch :: ni :~: r -> Search nid addr tok ni r -> Search nid addr tok ni ni | 867 | let isNodesSearch :: ni :~: r -> Search nid addr tok ni r -> Search nid addr tok ni ni |
853 | isNodesSearch Refl sch = sch | 868 | isNodesSearch Refl sch = sch |
869 | ping = maybe (const $ return False) | ||
870 | (\DHTPing{pingQuery} -> fmap (maybe False (const True)) . pingQuery) | ||
871 | $ Map.lookup "ping" pings | ||
854 | fork $ do | 872 | fork $ do |
855 | myThreadId >>= flip labelThread ("bootstrap."++netname) | 873 | myThreadId >>= flip labelThread ("bootstrap."++netname) |
856 | case Map.lookup "node" qrys of | 874 | case Map.lookup "node" qrys of |