summaryrefslogtreecommitdiff
path: root/Mainline.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-27 00:09:36 -0400
committerjoe <joe@jerkface.net>2017-07-27 00:09:36 -0400
commit0e20eb6683761362ee282e3188fccdab46b02ee4 (patch)
tree05043c1b75ba331ffd7d645b544badcecad6657c /Mainline.hs
parentaee5037c333abc77174d4867b75b1ef068fbaf1b (diff)
peer search.
Diffstat (limited to 'Mainline.hs')
-rw-r--r--Mainline.hs65
1 files changed, 33 insertions, 32 deletions
diff --git a/Mainline.hs b/Mainline.hs
index 30e18a09..7c54a096 100644
--- a/Mainline.hs
+++ b/Mainline.hs
@@ -945,30 +945,38 @@ announceH (SwarmsDatabase peers toks _) naddr announcement = do
945 945
946isReadonlyClient client = False -- TODO 946isReadonlyClient client = False -- TODO
947 947
948ping :: MainlineClient -> NodeInfo -> IO Bool 948mainlineSend meth unwrap msg client nid addr = do
949ping client addr = fromMaybe False <$> sendQuery client serializer Ping addr 949 reply <- sendQuery client serializer (msg nid) addr
950 -- sendQuery will return (Just (Left _)) on a parse error. We're going to
951 -- blow it away with the join-either sequence.
952 -- TODO: Do something with parse errors.
953 return $ join $ either (const Nothing) Just <$> reply
950 where 954 where
951 serializer = MethodSerializer 955 serializer = MethodSerializer
952 { methodTimeout = 5 956 { methodTimeout = 5
953 , method = Method "ping" 957 , method = meth
954 , wrapQuery = encodeQueryPayload (Method "ping") (isReadonlyClient client) 958 , wrapQuery = encodeQueryPayload meth (isReadonlyClient client)
955 , unwrapResponse = const True 959 , unwrapResponse = (>>= either (Left . Error GenericError . Char8.pack)
960 (Right . unwrap)
961 . BE.fromBEncode)
962 . rspPayload
956 } 963 }
957 964
958-- searchQuery :: ni -> IO ([ni], [r]) 965ping :: MainlineClient -> NodeInfo -> IO Bool
959getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO ([NodeInfo],[NodeInfo]) 966ping client addr =
960getNodes client nid addr = 967 fromMaybe False
961 fromMaybe ([],[]) <$> sendQuery client serializer (FindNode nid (Just Want_Both)) addr 968 <$> mainlineSend (Method "ping") (\Ping -> True) (const Ping) client () addr
969
970-- searchQuery :: ni -> IO (Maybe [ni], [r], tok))
971getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],()))
972getNodes = mainlineSend (Method "find_node") unwrap $ flip FindNode (Just Want_Both)
962 where 973 where
963 serializer = MethodSerializer 974 unwrap (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,())
964 { methodTimeout = 5 975
965 , method = Method "find_node" 976getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token))
966 , wrapQuery = encodeQueryPayload (Method "find_node") (isReadonlyClient client) 977getPeers = mainlineSend (Method "get_peers") unwrap $ flip GetPeers (Just Want_Both) . coerce
967 , unwrapResponse = \case 978 where
968 R { rspPayload = Right bval } | Right (NodeFound ns4 ns6) <- BE.fromBEncode bval 979 unwrap (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok)
969 -> (ns4++ns6, ns4++ns6)
970 _ -> ([],[])
971 }
972 980
973data TriadSlot = SlotA | SlotB | SlotC 981data TriadSlot = SlotA | SlotB | SlotC
974 deriving (Eq,Ord,Enum,Show,Read) 982 deriving (Eq,Ord,Enum,Show,Read)
@@ -1048,23 +1056,16 @@ delVote triad voter = do
1048 writeTVar (triadSlot slot triad) Nothing 1056 writeTVar (triadSlot slot triad) Nothing
1049 triadCountVotes prior triad 1057 triadCountVotes prior triad
1050 1058
1051nodeSearch client = Search 1059mainlineSearch qry = Search
1052 { searchSpace = mainlineSpace 1060 { searchSpace = mainlineSpace
1053 , searchNodeAddress = nodeIP &&& nodePort 1061 , searchNodeAddress = nodeIP &&& nodePort
1054 , searchQuery = \nid ni -> do 1062 , searchQuery = qry
1055 hPutStrLn stderr $ "findNodes "++show nid++" --> "++show ni
1056 handle (\(SomeException e) -> do
1057 hPutStrLn stderr $ "got "++show e
1058 -- threadDelay 1000000
1059 return ([],[]))
1060 $ do
1061 (xs,y) <- getNodes client nid ni
1062 forM_ xs $ \x -> do
1063 hPutStrLn stderr $ "got "++show x
1064 -- threadDelay 1000000
1065 return (xs,y)
1066 } 1063 }
1067 1064
1065nodeSearch client = mainlineSearch (getNodes client)
1066
1067peerSearch client = mainlineSearch (getPeers client)
1068
1068-- | List of bootstrap nodes maintained by different bittorrent 1069-- | List of bootstrap nodes maintained by different bittorrent
1069-- software authors. 1070-- software authors.
1070bootstrapNodes :: WantIP -> IO [NodeInfo] 1071bootstrapNodes :: WantIP -> IO [NodeInfo]