diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dhtd.hs | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 3fb1c641..4de735bc 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -127,7 +127,7 @@ data DHTSearch nid ni = forall addr tok r. DHTSearch | |||
127 | } | 127 | } |
128 | 128 | ||
129 | data DHTPing ni = forall r. DHTPing | 129 | data DHTPing ni = forall r. DHTPing |
130 | { pingQuery :: ni -> IO (Maybe r) | 130 | { pingQuery :: [String] -> ni -> IO (Maybe r) |
131 | , pingShowResult :: r -> String | 131 | , pingShowResult :: r -> String |
132 | } | 132 | } |
133 | 133 | ||
@@ -202,7 +202,7 @@ pingNodes netname DHT{dhtPing} | Just DHTPing{pingQuery=ping} <- Map.lookup "pin | |||
202 | myThreadId >>= flip labelThread ("pinging."++fname) | 202 | myThreadId >>= flip labelThread ("pinging."++fname) |
203 | putStrLn $ "Forked "++show fname | 203 | putStrLn $ "Forked "++show fname |
204 | withTaskGroup ("withTaskGroup."++fname) 10 $ \g -> do | 204 | withTaskGroup ("withTaskGroup."++fname) 10 $ \g -> do |
205 | mapM_ (\n -> forkTask g (show n) $ void $ ping n) | 205 | mapM_ (\n -> forkTask g (show n) $ void $ ping [] n) |
206 | (ns `asTypeOf` []) | 206 | (ns `asTypeOf` []) |
207 | putStrLn $ "Load finished "++show fname | 207 | putStrLn $ "Load finished "++show fname |
208 | return True | 208 | return True |
@@ -412,9 +412,10 @@ clientSession s@Session{..} sock cnum h = do | |||
412 | (pinglike, s) | Just DHT{dhtPing} <- Map.lookup netname dhts | 412 | (pinglike, s) | Just DHT{dhtPing} <- Map.lookup netname dhts |
413 | , Just DHTPing{ pingQuery=ping | 413 | , Just DHTPing{ pingQuery=ping |
414 | , pingShowResult=showr } <- Map.lookup pinglike dhtPing | 414 | , pingShowResult=showr } <- Map.lookup pinglike dhtPing |
415 | , ws@(_:_) <- words s | ||
415 | -> cmd0 $ do | 416 | -> cmd0 $ do |
416 | case readEither s of | 417 | case readEither $ last ws of |
417 | Right addr -> do result <- ping addr | 418 | Right addr -> do result <- ping (init ws) addr |
418 | let rs = [" ", maybe "Timeout." showr result] | 419 | let rs = [" ", maybe "Timeout." showr result] |
419 | hPutClient h $ unlines rs | 420 | hPutClient h $ unlines rs |
420 | Left er -> hPutClient h er | 421 | Left er -> hPutClient h er |
@@ -644,6 +645,10 @@ parseArgs (arg:args) opts = parseArgs args opts | |||
644 | . break (=='=') ) | 645 | . break (=='=') ) |
645 | $ groupBy (const (/= ',')) arg | 646 | $ groupBy (const (/= ',')) arg |
646 | 647 | ||
648 | noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r) | ||
649 | noArgPing f [] x = f x | ||
650 | noArgPing _ _ _ = return Nothing | ||
651 | |||
647 | main :: IO () | 652 | main :: IO () |
648 | main = do | 653 | main = do |
649 | args <- getArgs | 654 | args <- getArgs |
@@ -668,7 +673,7 @@ main = do | |||
668 | let mainlineDHT bkts wantip = DHT | 673 | let mainlineDHT bkts wantip = DHT |
669 | { dhtBuckets = bkts btR | 674 | { dhtBuckets = bkts btR |
670 | , dhtPing = Map.singleton "ping" $ DHTPing | 675 | , dhtPing = Map.singleton "ping" $ DHTPing |
671 | { pingQuery = fmap (bool Nothing (Just ())) . Mainline.ping bt | 676 | { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Mainline.ping bt |
672 | , pingShowResult = show | 677 | , pingShowResult = show |
673 | } | 678 | } |
674 | , dhtQuery = Map.fromList | 679 | , dhtQuery = Map.fromList |
@@ -736,11 +741,16 @@ main = do | |||
736 | { dhtBuckets = bkts (Tox.toxRouting tox) | 741 | { dhtBuckets = bkts (Tox.toxRouting tox) |
737 | , dhtPing = Map.fromList | 742 | , dhtPing = Map.fromList |
738 | [ ("ping", DHTPing | 743 | [ ("ping", DHTPing |
739 | { pingQuery = fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox) | 744 | { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox) |
740 | , pingShowResult = show | 745 | , pingShowResult = show |
741 | }) | 746 | }) |
742 | , ("cookie", DHTPing | 747 | , ("cookie", DHTPing |
743 | { pingQuery = Tox.cookieRequest (Tox.toxCryptoKeys tox) (Tox.toxDHT tox) | 748 | { pingQuery = \case |
749 | [keystr] | Just mykey <- readMaybe keystr | ||
750 | -> Tox.cookieRequest (Tox.toxCryptoKeys tox) | ||
751 | (Tox.toxDHT tox) | ||
752 | (Tox.id2key mykey) | ||
753 | _ -> const $ return Nothing | ||
744 | , pingShowResult = show | 754 | , pingShowResult = show |
745 | })] | 755 | })] |
746 | , dhtQuery = Map.fromList | 756 | , dhtQuery = Map.fromList |
@@ -872,7 +882,7 @@ main = do | |||
872 | let isNodesSearch :: ni :~: r -> Search nid addr tok ni r -> Search nid addr tok ni ni | 882 | let isNodesSearch :: ni :~: r -> Search nid addr tok ni r -> Search nid addr tok ni ni |
873 | isNodesSearch Refl sch = sch | 883 | isNodesSearch Refl sch = sch |
874 | ping = maybe (const $ return False) | 884 | ping = maybe (const $ return False) |
875 | (\DHTPing{pingQuery} -> fmap (maybe False (const True)) . pingQuery) | 885 | (\DHTPing{pingQuery} -> fmap (maybe False (const True)) . pingQuery []) |
876 | $ Map.lookup "ping" pings | 886 | $ Map.lookup "ping" pings |
877 | fork $ do | 887 | fork $ do |
878 | myThreadId >>= flip labelThread ("bootstrap."++netname) | 888 | myThreadId >>= flip labelThread ("bootstrap."++netname) |