diff options
author | Joe Crayne <joe@jerkface.net> | 2020-01-14 01:47:57 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-14 03:37:41 -0500 |
commit | f876f153013604d8af647541be58c59862a85ca7 (patch) | |
tree | 6f6b1f03bdf93ac223199279809691f8210c03c5 | |
parent | cb28281a2acabf87e91582ce5ace562544ae2730 (diff) |
Added startup nodes to CommonAPI search query.
-rw-r--r-- | dht/examples/dhtd.hs | 27 | ||||
-rw-r--r-- | kad/src/Network/Kademlia/CommonAPI.hs | 9 |
2 files changed, 25 insertions, 11 deletions
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs index f9dc777d..3095e7b4 100644 --- a/dht/examples/dhtd.hs +++ b/dht/examples/dhtd.hs | |||
@@ -295,12 +295,11 @@ forkSearch :: | |||
295 | -> nid | 295 | -> nid |
296 | -> DHTQuery nid ni | 296 | -> DHTQuery nid ni |
297 | -> TVar (Map.Map (String,nid) (DHTSearch nid ni)) | 297 | -> TVar (Map.Map (String,nid) (DHTSearch nid ni)) |
298 | -> TVar (BucketList ni) | 298 | -> [ni] |
299 | -> ThreadId | 299 | -> ThreadId |
300 | -> TVar (Maybe (IO ())) | 300 | -> TVar (Maybe (IO ())) |
301 | -> STM () | 301 | -> STM () |
302 | forkSearch method nid DHTQuery{qsearch,qshowTok,qshowR} dhtSearches dhtBuckets tid kvar = do | 302 | forkSearch method nid DHTQuery{qsearch,qshowTok,qshowR} dhtSearches ns tid kvar = do |
303 | ns <- R.kclosest (searchSpace qsearch) (searchK qsearch) nid <$> readTVar dhtBuckets | ||
304 | st <- newSearch qsearch nid ns | 303 | st <- newSearch qsearch nid ns |
305 | results <- newTVar Set.empty | 304 | results <- newTVar Set.empty |
306 | let storeResult r = modifyTVar' results (Set.insert (qshowR r)) | 305 | let storeResult r = modifyTVar' results (Set.insert (qshowR r)) |
@@ -1046,10 +1045,11 @@ clientSession s@Session{..} sock cnum h = do | |||
1046 | -- STM action decides not to add a new search. This is so that | 1045 | -- STM action decides not to add a new search. This is so that |
1047 | -- I can store the ThreadId into new DHTSearch structure. | 1046 | -- I can store the ThreadId into new DHTSearch structure. |
1048 | tid <- fork $ join $ atomically (readTVar kvar >>= maybe retry return) | 1047 | tid <- fork $ join $ atomically (readTVar kvar >>= maybe retry return) |
1048 | ns <- case qry of DHTQuery{qbootNodes} -> qbootNodes nid | ||
1049 | join $ atomically $ do | 1049 | join $ atomically $ do |
1050 | schs <- readTVar dhtSearches | 1050 | schs <- readTVar dhtSearches |
1051 | case Map.lookup (method,nid) schs of | 1051 | case Map.lookup (method,nid) schs of |
1052 | Nothing -> do forkSearch method nid qry dhtSearches (refreshBuckets dhtBuckets) tid kvar | 1052 | Nothing -> do forkSearch method nid qry dhtSearches ns tid kvar |
1053 | return $ presentSearches | 1053 | return $ presentSearches |
1054 | Just sch -> do writeTVar kvar (Just $ return ()) | 1054 | Just sch -> do writeTVar kvar (Just $ return ()) |
1055 | return $ reportSearchResults method h sch | 1055 | return $ reportSearchResults method h sch |
@@ -1503,8 +1503,8 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1503 | 1503 | ||
1504 | tcpSearches <- atomically $ newTVar Map.empty | 1504 | tcpSearches <- atomically $ newTVar Map.empty |
1505 | 1505 | ||
1506 | let toxDHT bkts wantip = DHT | 1506 | let toxDHT bkts wantip = let toxBkts = bkts (Tox.toxRouting tox) in DHT |
1507 | { dhtBuckets = bkts (Tox.toxRouting tox) | 1507 | { dhtBuckets = toxBkts |
1508 | , dhtPing = Map.fromList | 1508 | , dhtPing = Map.fromList |
1509 | [ ("ping", DHTPing | 1509 | [ ("ping", DHTPing |
1510 | { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Tox.pingUDP (Tox.toxDHT tox) | 1510 | { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Tox.pingUDP (Tox.toxDHT tox) |
@@ -1521,7 +1521,7 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1521 | , pingShowResult = show | 1521 | , pingShowResult = show |
1522 | })] | 1522 | })] |
1523 | , dhtQuery = Map.fromList | 1523 | , dhtQuery = Map.fromList |
1524 | [ ("node", DHTQuery | 1524 | [ ("node", fix $ \q -> DHTQuery |
1525 | { qsearch = Tox.nodeSearch (Tox.toxDHT tox) | 1525 | { qsearch = Tox.nodeSearch (Tox.toxDHT tox) |
1526 | (Tox.nodesOfInterest $ Tox.toxRouting tox) | 1526 | (Tox.nodesOfInterest $ Tox.toxRouting tox) |
1527 | , qhandler = (\ni -> fmap Tox.unwrapNodes | 1527 | , qhandler = (\ni -> fmap Tox.unwrapNodes |
@@ -1529,8 +1529,9 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1529 | . Tox.GetNodes) | 1529 | . Tox.GetNodes) |
1530 | , qshowR = show -- NodeInfo | 1530 | , qshowR = show -- NodeInfo |
1531 | , qshowTok = (const Nothing) | 1531 | , qshowTok = (const Nothing) |
1532 | , qbootNodes = genericBootNodes (refreshBuckets toxBkts) q | ||
1532 | }) | 1533 | }) |
1533 | , ("toxid", DHTQuery | 1534 | , ("toxid", fix $ \q -> DHTQuery |
1534 | { qsearch = Tox.toxQSearch tox | 1535 | { qsearch = Tox.toxQSearch tox |
1535 | , qhandler = -- qhandler :: ni -> nid -> IO ([ni], [r], tok) | 1536 | , qhandler = -- qhandler :: ni -> nid -> IO ([ni], [r], tok) |
1536 | (\ni nid -> | 1537 | (\ni nid -> |
@@ -1544,6 +1545,7 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1544 | (Tox.AnnounceRequest zeros32 nid Tox.zeroID)) | 1545 | (Tox.AnnounceRequest zeros32 nid Tox.zeroID)) |
1545 | , qshowR = show -- Rendezvous | 1546 | , qshowR = show -- Rendezvous |
1546 | , qshowTok = Just . show -- Nonce32 | 1547 | , qshowTok = Just . show -- Nonce32 |
1548 | , qbootNodes = genericBootNodes (refreshBuckets toxBkts) q | ||
1547 | }) | 1549 | }) |
1548 | ] | 1550 | ] |
1549 | , dhtParseId = readEither :: String -> Either String Tox.NodeId | 1551 | , dhtParseId = readEither :: String -> Either String Tox.NodeId |
@@ -1690,7 +1692,7 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1690 | { pingQuery = noArgPing $ TCP.tcpPing (TCP.tcpClient tcpclient) | 1692 | { pingQuery = noArgPing $ TCP.tcpPing (TCP.tcpClient tcpclient) |
1691 | , pingShowResult = show | 1693 | , pingShowResult = show |
1692 | } | 1694 | } |
1693 | , dhtQuery = Map.singleton "node" DHTQuery | 1695 | , dhtQuery = Map.singleton "node" $ fix $ \q -> DHTQuery |
1694 | { qsearch = TCP.nodeSearch tcpprober tcpclient | 1696 | { qsearch = TCP.nodeSearch tcpprober tcpclient |
1695 | , qhandler = \ni nid -> do | 1697 | , qhandler = \ni nid -> do |
1696 | ns <- R.kclosest (searchSpace $ TCP.nodeSearch tcpprober tcpclient) | 1698 | ns <- R.kclosest (searchSpace $ TCP.nodeSearch tcpprober tcpclient) |
@@ -1700,6 +1702,7 @@ initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of | |||
1700 | return (ns,ns,Just ()) | 1702 | return (ns,ns,Just ()) |
1701 | , qshowR = show -- TCP.NodeInfo | 1703 | , qshowR = show -- TCP.NodeInfo |
1702 | , qshowTok = (const Nothing) | 1704 | , qshowTok = (const Nothing) |
1705 | , qbootNodes = genericBootNodes (refreshBuckets tcpRefresher) q | ||
1703 | } | 1706 | } |
1704 | , dhtAnnouncables = Map.empty | 1707 | , dhtAnnouncables = Map.empty |
1705 | , dhtParseId = readEither :: String -> Either String Tox.NodeId | 1708 | , dhtParseId = readEither :: String -> Either String Tox.NodeId |
@@ -1808,19 +1811,20 @@ main = do | |||
1808 | , pingShowResult = show | 1811 | , pingShowResult = show |
1809 | } | 1812 | } |
1810 | , dhtQuery = Map.fromList | 1813 | , dhtQuery = Map.fromList |
1811 | [ ("node", DHTQuery | 1814 | [ ("node", fix $ \q -> DHTQuery |
1812 | { qsearch = (Mainline.nodeSearch bt) | 1815 | { qsearch = (Mainline.nodeSearch bt) |
1813 | , qhandler = (\ni -> fmap Mainline.unwrapNodes | 1816 | , qhandler = (\ni -> fmap Mainline.unwrapNodes |
1814 | . Mainline.findNodeH btR ni | 1817 | . Mainline.findNodeH btR ni |
1815 | . flip Mainline.FindNode (Just Want_Both)) | 1818 | . flip Mainline.FindNode (Just Want_Both)) |
1816 | , qshowR = show | 1819 | , qshowR = show |
1817 | , qshowTok = (const Nothing) | 1820 | , qshowTok = (const Nothing) |
1821 | , qbootNodes = genericBootNodes (refreshBuckets $ bkts btR) q | ||
1818 | }) | 1822 | }) |
1819 | -- Maybe (sr :~: pr, stok :~: ptok, sni :~: pni ) | 1823 | -- Maybe (sr :~: pr, stok :~: ptok, sni :~: pni ) |
1820 | -- sr = InfoHash | 1824 | -- sr = InfoHash |
1821 | -- stok = Token | 1825 | -- stok = Token |
1822 | -- sni = NodeInfo | 1826 | -- sni = NodeInfo |
1823 | , ("peer", DHTQuery | 1827 | , ("peer", fix $ \q -> DHTQuery |
1824 | { qsearch = (Mainline.peerSearch bt) | 1828 | { qsearch = (Mainline.peerSearch bt) |
1825 | , qhandler = (\ni -> fmap Mainline.unwrapPeers | 1829 | , qhandler = (\ni -> fmap Mainline.unwrapPeers |
1826 | . Mainline.getPeersH btR swarms ni | 1830 | . Mainline.getPeersH btR swarms ni |
@@ -1828,6 +1832,7 @@ main = do | |||
1828 | . (read . show)) -- TODO: InfoHash -> NodeId | 1832 | . (read . show)) -- TODO: InfoHash -> NodeId |
1829 | , qshowR = (show . pPrint) | 1833 | , qshowR = (show . pPrint) |
1830 | , qshowTok = (Just . show) | 1834 | , qshowTok = (Just . show) |
1835 | , qbootNodes = genericBootNodes (refreshBuckets $ bkts btR) q | ||
1831 | }) | 1836 | }) |
1832 | ] | 1837 | ] |
1833 | , dhtParseId = readEither :: String -> Either String Mainline.NodeId | 1838 | , dhtParseId = readEither :: String -> Either String Mainline.NodeId |
diff --git a/kad/src/Network/Kademlia/CommonAPI.hs b/kad/src/Network/Kademlia/CommonAPI.hs index bcbfe9d8..4de7909d 100644 --- a/kad/src/Network/Kademlia/CommonAPI.hs +++ b/kad/src/Network/Kademlia/CommonAPI.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE ExistentialQuantification #-} | 1 | {-# LANGUAGE ExistentialQuantification #-} |
2 | {-# LANGUAGE NamedFieldPuns #-} | ||
2 | module Network.Kademlia.CommonAPI | 3 | module Network.Kademlia.CommonAPI |
3 | ( module Network.Kademlia.CommonAPI | 4 | ( module Network.Kademlia.CommonAPI |
4 | , refreshBuckets | 5 | , refreshBuckets |
@@ -58,8 +59,16 @@ data DHTQuery nid ni = forall addr r tok qk. | |||
58 | , qhandler :: ni -> nid -> IO ([ni], [r], Maybe tok) -- ^ Invoked on local node, when there is no query destination. | 59 | , qhandler :: ni -> nid -> IO ([ni], [r], Maybe tok) -- ^ Invoked on local node, when there is no query destination. |
59 | , qshowR :: r -> String | 60 | , qshowR :: r -> String |
60 | , qshowTok :: tok -> Maybe String | 61 | , qshowTok :: tok -> Maybe String |
62 | , qbootNodes :: nid -> IO [ni] | ||
61 | } | 63 | } |
62 | 64 | ||
65 | -- Can be used to initialize qbootNodes like this: | ||
66 | -- fix \q -> DHTQuery { ... , qbootNodes = genericBootNodes bkts q } | ||
67 | genericBootNodes :: Ord nid => TVar (BucketList ni) -> DHTQuery nid ni -> nid -> IO [ni] | ||
68 | genericBootNodes dhtBuckets DHTQuery{qsearch} nid = | ||
69 | atomically $ R.kclosest (searchSpace qsearch) (searchK qsearch) nid | ||
70 | <$> readTVar dhtBuckets | ||
71 | |||
63 | data DHTAnnouncable nid = forall dta tok ni r. | 72 | data DHTAnnouncable nid = forall dta tok ni r. |
64 | ( Show r | 73 | ( Show r |
65 | , Typeable dta -- information being announced | 74 | , Typeable dta -- information being announced |