summaryrefslogtreecommitdiff
path: root/dht/src/Network/BitTorrent/MainlineDHT.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-03 18:22:16 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-07 13:24:59 -0500
commit15ab3290ad04280764968ba4760474a8c0cbfa52 (patch)
tree8df7bdfe38005f5478243427bb2b692d32843283 /dht/src/Network/BitTorrent/MainlineDHT.hs
parentb411ab66ceee7386e4829e2337c735a08fb3d54d (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.hs26
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 (..))
70import Network.BitTorrent.DHT.Token as Token 70import Network.BitTorrent.DHT.Token as Token
71import qualified Network.Kademlia.Routing as R 71import qualified Network.Kademlia.Routing as R
72 ;import Network.Kademlia.Routing (getTimestamp) 72 ;import Network.Kademlia.Routing (getTimestamp)
73import Network.QueryResponse 73import Network.QueryResponse as QR
74import Network.Socket 74import Network.Socket
75import System.IO.Error 75import System.IO.Error
76import System.IO.Unsafe (unsafeInterleaveIO) 76import 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)
1049mainlineSend meth unwrap msg client nid addr = do 1049mainlineSend 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
1067ping :: MainlineClient -> NodeInfo -> IO Bool 1067ping :: MainlineClient -> NodeInfo -> IO Bool
1068ping client addr = 1068ping 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))
1073getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],Maybe ())) 1073getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (QR.Result ([NodeInfo],[NodeInfo],Maybe ()))
1074getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both) 1074getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both)
1075 1075
1076unwrapNodes :: NodeFound -> ([NodeInfo], [NodeInfo], Maybe ()) 1076unwrapNodes :: NodeFound -> ([NodeInfo], [NodeInfo], Maybe ())
1077unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6, Just ()) 1077unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6, Just ())
1078 1078
1079getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Maybe Token)) 1079getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (QR.Result ([NodeInfo],[PeerAddr],Maybe Token))
1080getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce 1080getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce
1081 1081
1082unwrapPeers :: GotPeers -> ([NodeInfo], [PeerAddr], Maybe Token) 1082unwrapPeers :: GotPeers -> ([NodeInfo], [PeerAddr], Maybe Token)
1083unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, Just tok) 1083unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, Just tok)
1084 1084
1085mainlineSearch :: (NodeId -> NodeInfo -> IO (Maybe ([NodeInfo], [r], Maybe tok))) 1085mainlineSearch :: (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
1087mainlineSearch qry = Search 1087mainlineSearch qry = Search
1088 { searchSpace = mainlineSpace 1088 { searchSpace = mainlineSpace
@@ -1140,5 +1140,5 @@ resolve want hostAndPort = do
1140 1140
1141 1141
1142announce :: MainlineClient -> Announce -> NodeInfo -> IO (Maybe Announced) 1142announce :: MainlineClient -> Announce -> NodeInfo -> IO (Maybe Announced)
1143announce client msg addr = do 1143announce client msg addr =
1144 mainlineSend (Method "announce_peer") id (\() -> msg) client () addr 1144 resultToMaybe <$> mainlineSend (Method "announce_peer") id (\() -> msg) client () addr