diff options
author | joe <joe@jerkface.net> | 2017-07-01 11:18:04 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-07-01 11:18:04 -0400 |
commit | e22ca7f163b6b771a570013d506f5d5f6576c2aa (patch) | |
tree | fd4b59f3302cd352a745db1659383643fb2166ab /src/Network | |
parent | ac8bced8dafa1a52bd02bdec8c1959af67442ed7 (diff) |
Simplified Kademlia class.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/DHT/Mainline.hs | 14 | ||||
-rw-r--r-- | src/Network/DHT/Types.hs | 43 |
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 | ||
440 | instance BEncode Announced where | 440 | instance 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 | ||
538 | instance Kademlia KMessageOf where | 538 | instance 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 | |||
44 | deriving instance (Eq (NodeId dht), Eq a ) => Eq (Response dht a) | 44 | deriving instance (Eq (NodeId dht), Eq a ) => Eq (Response dht a) |
45 | deriving instance (Show (NodeId dht), Show a ) => Show (Response dht a) | 45 | deriving 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. | ||
49 | data 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. | ||
53 | newtype FindNode dht ip = FindNode (NodeId dht) | ||
54 | deriving (Typeable) | ||
55 | newtype NodeFound dht ip = NodeFound [NodeInfo dht ip ()] | ||
56 | deriving (Typeable) | ||
57 | |||
58 | deriving instance Eq (NodeId dht) => Eq (FindNode dht ip) | ||
59 | deriving instance Eq (NodeId dht) => Eq (NodeFound dht ip) | ||
60 | deriving instance Show (NodeId dht) => Show (FindNode dht ip) | ||
61 | deriving instance ( Show (NodeId dht) | ||
62 | , Show ip | ||
63 | ) => Show (NodeFound dht ip) | ||
64 | |||
65 | pingMessage :: Proxy dht -> Ping dht | ||
66 | pingMessage _ = Ping | ||
67 | pongMessage :: Proxy dht -> Ping dht | ||
68 | pongMessage _ = Ping | ||
69 | findNodeMessage :: TableKey dht k => Proxy dht -> k -> FindNode dht ip | ||
70 | findNodeMessage _ k = FindNode (toNodeId k) | ||
71 | foundNodesMessage :: [NodeInfo dht ip ()] -> NodeFound dht ip | ||
72 | findWho (FindNode nid) = nid | ||
73 | foundNodes :: NodeFound dht ip -> [NodeInfo dht ip ()] | ||
74 | foundNodes (NodeFound ns) = ns | ||
75 | findWho :: FindNode dht ip -> NodeId dht | ||
76 | foundNodesMessage ns = NodeFound ns | ||
47 | 77 | ||
48 | class Kademlia dht where | 78 | class 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 |