diff options
author | Joe Crayne <joe@jerkface.net> | 2020-01-03 18:22:16 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-07 13:24:59 -0500 |
commit | 15ab3290ad04280764968ba4760474a8c0cbfa52 (patch) | |
tree | 8df7bdfe38005f5478243427bb2b692d32843283 /dht/src/Network/BitTorrent/MainlineDHT.hs | |
parent | b411ab66ceee7386e4829e2337c735a08fb3d54d (diff) |
Modify kademlia search to distinguish a Canceled from timed-out query.
Diffstat (limited to 'dht/src/Network/BitTorrent/MainlineDHT.hs')
-rw-r--r-- | dht/src/Network/BitTorrent/MainlineDHT.hs | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/dht/src/Network/BitTorrent/MainlineDHT.hs b/dht/src/Network/BitTorrent/MainlineDHT.hs index fc69fedd..8532b492 100644 --- a/dht/src/Network/BitTorrent/MainlineDHT.hs +++ b/dht/src/Network/BitTorrent/MainlineDHT.hs | |||
@@ -70,7 +70,7 @@ import Network.Kademlia.Search (Search (..)) | |||
70 | import Network.BitTorrent.DHT.Token as Token | 70 | import Network.BitTorrent.DHT.Token as Token |
71 | import qualified Network.Kademlia.Routing as R | 71 | import qualified Network.Kademlia.Routing as R |
72 | ;import Network.Kademlia.Routing (getTimestamp) | 72 | ;import Network.Kademlia.Routing (getTimestamp) |
73 | import Network.QueryResponse | 73 | import Network.QueryResponse as QR |
74 | import Network.Socket | 74 | import Network.Socket |
75 | import System.IO.Error | 75 | import System.IO.Error |
76 | import System.IO.Unsafe (unsafeInterleaveIO) | 76 | import System.IO.Unsafe (unsafeInterleaveIO) |
@@ -569,7 +569,7 @@ newClient swarms addr = do | |||
569 | -- We defer initializing the refreshSearch and refreshPing until we | 569 | -- We defer initializing the refreshSearch and refreshPing until we |
570 | -- have a client to send queries with. | 570 | -- have a client to send queries with. |
571 | let nullPing = const $ return False | 571 | let nullPing = const $ return False |
572 | nullSearch = mainlineSearch $ \_ _ -> return Nothing | 572 | nullSearch = mainlineSearch $ \_ _ -> return Canceled |
573 | tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info R.defaultBucketCount | 573 | tbl4 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info R.defaultBucketCount |
574 | refresher4 <- newBucketRefresher tbl4 nullSearch nullPing | 574 | refresher4 <- newBucketRefresher tbl4 nullSearch nullPing |
575 | tbl6 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info6 R.defaultBucketCount | 575 | tbl6 <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) tentative_info6 R.defaultBucketCount |
@@ -1045,14 +1045,14 @@ mainlineSend :: ( BEncode xqry | |||
1045 | -> MainlineClient | 1045 | -> MainlineClient |
1046 | -> qry | 1046 | -> qry |
1047 | -> NodeInfo | 1047 | -> NodeInfo |
1048 | -> IO (Maybe rsp) | 1048 | -> IO (QR.Result rsp) |
1049 | mainlineSend meth unwrap msg client nid addr = do | 1049 | mainlineSend meth unwrap msg client nid addr = do |
1050 | reply <- sendQuery client serializer (msg nid) addr | 1050 | reply <- sendQuery client serializer (msg nid) addr |
1051 | return $ case reply of | 1051 | return $ case reply of |
1052 | Success (Right x) -> Just x | 1052 | Success (Right x) -> Success x |
1053 | Success (Left e) -> Nothing -- TODO: Do something with parse errors. | 1053 | Success (Left e) -> Canceled -- TODO: Do something with parse errors. |
1054 | Canceled -> Nothing | 1054 | Canceled -> Canceled |
1055 | TimedOut -> Nothing | 1055 | TimedOut -> TimedOut |
1056 | where | 1056 | where |
1057 | serializer = MethodSerializer | 1057 | serializer = MethodSerializer |
1058 | { methodTimeout = \ni -> return (ni, 5000000) | 1058 | { methodTimeout = \ni -> return (ni, 5000000) |
@@ -1066,23 +1066,23 @@ mainlineSend meth unwrap msg client nid addr = do | |||
1066 | 1066 | ||
1067 | ping :: MainlineClient -> NodeInfo -> IO Bool | 1067 | ping :: MainlineClient -> NodeInfo -> IO Bool |
1068 | ping client addr = | 1068 | ping client addr = |
1069 | fromMaybe False | 1069 | fromMaybe False . resultToMaybe |
1070 | <$> mainlineSend (Method "ping") (\Pong -> True) (const Ping) client () addr | 1070 | <$> mainlineSend (Method "ping") (\Pong -> True) (const Ping) client () addr |
1071 | 1071 | ||
1072 | -- searchQuery :: ni -> IO (Maybe [ni], [r], tok)) | 1072 | -- searchQuery :: ni -> IO (Maybe [ni], [r], tok)) |
1073 | getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) | 1073 | getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (QR.Result ([NodeInfo],[NodeInfo],Maybe ())) |
1074 | getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both) | 1074 | getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both) |
1075 | 1075 | ||
1076 | unwrapNodes :: NodeFound -> ([NodeInfo], [NodeInfo], Maybe ()) | 1076 | unwrapNodes :: NodeFound -> ([NodeInfo], [NodeInfo], Maybe ()) |
1077 | unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6, Just ()) | 1077 | unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6, Just ()) |
1078 | 1078 | ||
1079 | getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Maybe Token)) | 1079 | getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (QR.Result ([NodeInfo],[PeerAddr],Maybe Token)) |
1080 | getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce | 1080 | getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce |
1081 | 1081 | ||
1082 | unwrapPeers :: GotPeers -> ([NodeInfo], [PeerAddr], Maybe Token) | 1082 | unwrapPeers :: GotPeers -> ([NodeInfo], [PeerAddr], Maybe Token) |
1083 | unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, Just tok) | 1083 | unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, Just tok) |
1084 | 1084 | ||
1085 | mainlineSearch :: (NodeId -> NodeInfo -> IO (Maybe ([NodeInfo], [r], Maybe tok))) | 1085 | mainlineSearch :: (NodeId -> NodeInfo -> IO (QR.Result ([NodeInfo], [r], Maybe tok))) |
1086 | -> Search NodeId (IP, PortNumber) tok NodeInfo r | 1086 | -> Search NodeId (IP, PortNumber) tok NodeInfo r |
1087 | mainlineSearch qry = Search | 1087 | mainlineSearch qry = Search |
1088 | { searchSpace = mainlineSpace | 1088 | { searchSpace = mainlineSpace |
@@ -1140,5 +1140,5 @@ resolve want hostAndPort = do | |||
1140 | 1140 | ||
1141 | 1141 | ||
1142 | announce :: MainlineClient -> Announce -> NodeInfo -> IO (Maybe Announced) | 1142 | announce :: MainlineClient -> Announce -> NodeInfo -> IO (Maybe Announced) |
1143 | announce client msg addr = do | 1143 | announce client msg addr = |
1144 | mainlineSend (Method "announce_peer") id (\() -> msg) client () addr | 1144 | resultToMaybe <$> mainlineSend (Method "announce_peer") id (\() -> msg) client () addr |