summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-07-01 11:18:04 -0400
committerjoe <joe@jerkface.net>2017-07-01 11:18:04 -0400
commite22ca7f163b6b771a570013d506f5d5f6576c2aa (patch)
treefd4b59f3302cd352a745db1659383643fb2166ab /src/Network
parentac8bced8dafa1a52bd02bdec8c1959af67442ed7 (diff)
Simplified Kademlia class.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/DHT/Mainline.hs14
-rw-r--r--src/Network/DHT/Types.hs43
2 files changed, 31 insertions, 26 deletions
diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs
index b756ff6a..6ef6d450 100644
--- a/src/Network/DHT/Mainline.hs
+++ b/src/Network/DHT/Mainline.hs
@@ -438,7 +438,7 @@ data Announced = Announced
438 deriving (Show, Eq, Typeable) 438 deriving (Show, Eq, Typeable)
439 439
440instance BEncode Announced where 440instance BEncode Announced where
441 toBEncode _ = toBEncode Ping 441 toBEncode _ = toBEncode ( Ping :: Ping KMessageOf )
442 fromBEncode _ = pure Announced 442 fromBEncode _ = pure Announced
443 443
444-- | \"q" = \"announce\" 444-- | \"q" = \"announce\"
@@ -536,23 +536,11 @@ checkToken sessionTokens addr questionableToken = do
536 536
537 537
538instance Kademlia KMessageOf where 538instance Kademlia KMessageOf where
539 data Ping KMessageOf = Ping
540 deriving (Show, Eq, Typeable)
541 newtype FindNode KMessageOf ip = FindNode (NodeId KMessageOf)
542 deriving (Show, Eq, Typeable)
543 newtype NodeFound KMessageOf ip = NodeFound [NodeInfo KMessageOf ip ()]
544 deriving (Show, Eq, Typeable)
545 data DHTData KMessageOf ip = TorrentData 539 data DHTData KMessageOf ip = TorrentData
546 { contactInfo :: !(TVar (PeerStore ip )) -- ^ published by other nodes; 540 { contactInfo :: !(TVar (PeerStore ip )) -- ^ published by other nodes;
547 , sessionTokens :: !(TVar SessionTokens ) -- ^ query session IDs. 541 , sessionTokens :: !(TVar SessionTokens ) -- ^ query session IDs.
548 } 542 }
549 543
550 pingMessage _ = Ping
551 pongMessage _ = Ping
552 findNodeMessage _ k = FindNode (toNodeId k)
553 findWho (FindNode nid) = nid
554 foundNodes (NodeFound ns) = ns
555 foundNodesMessage ns = NodeFound ns
556 544
557 dhtAdjustID _ fallback ip0 arrival 545 dhtAdjustID _ fallback ip0 arrival
558 = fromMaybe fallback $ do 546 = fromMaybe fallback $ do
diff --git a/src/Network/DHT/Types.hs b/src/Network/DHT/Types.hs
index 31ae5948..91d1fc22 100644
--- a/src/Network/DHT/Types.hs
+++ b/src/Network/DHT/Types.hs
@@ -44,22 +44,39 @@ data Response dht a = Response
44deriving instance (Eq (NodeId dht), Eq a ) => Eq (Response dht a) 44deriving instance (Eq (NodeId dht), Eq a ) => Eq (Response dht a)
45deriving instance (Show (NodeId dht), Show a ) => Show (Response dht a) 45deriving instance (Show (NodeId dht), Show a ) => Show (Response dht a)
46 46
47-- | The most basic query is a ping. Ping query is used to check if a
48-- quered node is still alive.
49data Ping ( dht :: * -> *) = Ping
50 deriving (Show, Eq, Typeable)
51-- | Find node is used to find the contact information for a node
52-- given its ID.
53newtype FindNode dht ip = FindNode (NodeId dht)
54 deriving (Typeable)
55newtype NodeFound dht ip = NodeFound [NodeInfo dht ip ()]
56 deriving (Typeable)
57
58deriving instance Eq (NodeId dht) => Eq (FindNode dht ip)
59deriving instance Eq (NodeId dht) => Eq (NodeFound dht ip)
60deriving instance Show (NodeId dht) => Show (FindNode dht ip)
61deriving instance ( Show (NodeId dht)
62 , Show ip
63 ) => Show (NodeFound dht ip)
64
65pingMessage :: Proxy dht -> Ping dht
66pingMessage _ = Ping
67pongMessage :: Proxy dht -> Ping dht
68pongMessage _ = Ping
69findNodeMessage :: TableKey dht k => Proxy dht -> k -> FindNode dht ip
70findNodeMessage _ k = FindNode (toNodeId k)
71foundNodesMessage :: [NodeInfo dht ip ()] -> NodeFound dht ip
72findWho (FindNode nid) = nid
73foundNodes :: NodeFound dht ip -> [NodeInfo dht ip ()]
74foundNodes (NodeFound ns) = ns
75findWho :: FindNode dht ip -> NodeId dht
76foundNodesMessage ns = NodeFound ns
47 77
48class Kademlia dht where 78class Kademlia dht where
49 -- | The most basic query is a ping. Ping query is used to check if a
50 -- quered node is still alive.
51 data Ping dht
52 -- | Find node is used to find the contact information for a node
53 -- given its ID.
54 data FindNode dht ip
55 data NodeFound dht ip
56 data DHTData dht ip 79 data DHTData dht ip
57 pingMessage :: Proxy dht -> Ping dht
58 pongMessage :: Proxy dht -> Ping dht
59 findNodeMessage :: TableKey dht k => Proxy dht -> k -> FindNode dht ip
60 foundNodesMessage :: [NodeInfo dht ip ()] -> NodeFound dht ip
61 foundNodes :: NodeFound dht ip -> [NodeInfo dht ip ()]
62 findWho :: FindNode dht ip -> NodeId dht
63 dhtAdjustID :: Address ip => Proxy dht -> NodeId dht -> SockAddr -> Event dht ip u -> NodeId dht 80 dhtAdjustID :: Address ip => Proxy dht -> NodeId dht -> SockAddr -> Event dht ip u -> NodeId dht
64 namePing :: Proxy dht -> QueryMethod dht 81 namePing :: Proxy dht -> QueryMethod dht
65 nameFindNodes :: Proxy dht -> QueryMethod dht 82 nameFindNodes :: Proxy dht -> QueryMethod dht