diff options
author | joe <joe@jerkface.net> | 2017-07-27 01:19:55 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-27 01:19:55 -0400 |
commit | 51f8d96fe88323753cbbfa455fb41165628f7794 (patch) | |
tree | 820406bb9d38aadb33eb59634cc818d9e56e1b3b /Mainline.hs | |
parent | 0e20eb6683761362ee282e3188fccdab46b02ee4 (diff) |
Mainline queries: implementation (TODO: interface).
Diffstat (limited to 'Mainline.hs')
-rw-r--r-- | Mainline.hs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/Mainline.hs b/Mainline.hs index 7c54a096..921bea0f 100644 --- a/Mainline.hs +++ b/Mainline.hs | |||
@@ -92,6 +92,13 @@ instance S.Serialize NodeId where | |||
92 | instance FiniteBits NodeId where | 92 | instance FiniteBits NodeId where |
93 | finiteBitSize _ = 160 | 93 | finiteBitSize _ = 160 |
94 | 94 | ||
95 | instance Read NodeId where | ||
96 | readsPrec _ str | ||
97 | | (bs, xs) <- Base16.decode $ Char8.pack str | ||
98 | , B.length bs == 20 | ||
99 | = [ (NodeId bs, drop 40 str) ] | ||
100 | | otherwise = [] | ||
101 | |||
95 | zeroID :: NodeId | 102 | zeroID :: NodeId |
96 | zeroID = NodeId $ B.replicate 20 0 | 103 | zeroID = NodeId $ B.replicate 20 0 |
97 | 104 | ||
@@ -465,7 +472,7 @@ traced (TableMethods ins del lkup) | |||
465 | 472 | ||
466 | type MainlineClient = Client String Method TransactionId NodeInfo (Message BValue) | 473 | type MainlineClient = Client String Method TransactionId NodeInfo (Message BValue) |
467 | 474 | ||
468 | newClient :: SockAddr -> IO (MainlineClient, Routing) | 475 | newClient :: SockAddr -> IO (MainlineClient, Routing, SwarmsDatabase) |
469 | newClient addr = do | 476 | newClient addr = do |
470 | udp <- udpTransport addr | 477 | udp <- udpTransport addr |
471 | nid <- NodeId <$> getRandomBytes 20 | 478 | nid <- NodeId <$> getRandomBytes 20 |
@@ -564,7 +571,7 @@ newClient addr = do | |||
564 | (sched6 routing) | 571 | (sched6 routing) |
565 | (refreshBucket (nodeSearch client) (routing6 routing) (nodeId tenative_info)) | 572 | (refreshBucket (nodeSearch client) (routing6 routing) (nodeId tenative_info)) |
566 | 573 | ||
567 | return (client, routing) | 574 | return (client, routing, swarms) |
568 | 575 | ||
569 | -- | Modifies a purely random 'NodeId' to one that is related to a given | 576 | -- | Modifies a purely random 'NodeId' to one that is related to a given |
570 | -- routable address in accordance with BEP 42. | 577 | -- routable address in accordance with BEP 42. |
@@ -969,14 +976,14 @@ ping client addr = | |||
969 | 976 | ||
970 | -- searchQuery :: ni -> IO (Maybe [ni], [r], tok)) | 977 | -- searchQuery :: ni -> IO (Maybe [ni], [r], tok)) |
971 | getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],())) | 978 | getNodes :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[NodeInfo],())) |
972 | getNodes = mainlineSend (Method "find_node") unwrap $ flip FindNode (Just Want_Both) | 979 | getNodes = mainlineSend (Method "find_node") unwrapNodes $ flip FindNode (Just Want_Both) |
973 | where | 980 | |
974 | unwrap (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,()) | 981 | unwrapNodes (NodeFound ns4 ns6) = (ns4++ns6, ns4++ns6,()) |
975 | 982 | ||
976 | getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token)) | 983 | getPeers :: MainlineClient -> NodeId -> NodeInfo -> IO (Maybe ([NodeInfo],[PeerAddr],Token)) |
977 | getPeers = mainlineSend (Method "get_peers") unwrap $ flip GetPeers (Just Want_Both) . coerce | 984 | getPeers = mainlineSend (Method "get_peers") unwrapPeers $ flip GetPeers (Just Want_Both) . coerce |
978 | where | 985 | |
979 | unwrap (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok) | 986 | unwrapPeers (GotPeers ps (NodeFound ns4 ns6) tok) = (ns4++ns6, ps, tok) |
980 | 987 | ||
981 | data TriadSlot = SlotA | SlotB | SlotC | 988 | data TriadSlot = SlotA | SlotB | SlotC |
982 | deriving (Eq,Ord,Enum,Show,Read) | 989 | deriving (Eq,Ord,Enum,Show,Read) |