diff options
author | joe <joe@jerkface.net> | 2017-06-05 03:21:25 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-06-05 03:31:23 -0400 |
commit | 24df9a12a9240aaed8741d60e4b0b9cbf59a9fd9 (patch) | |
tree | 04791746bb576c40851f441ebc851c9d0d8da777 /src/Network/BitTorrent/DHT | |
parent | 219d72ebde4bab5a516a86608dcb3aede75c1611 (diff) |
WIP: Adapting DHT to Tox network (part 2).
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r-- | src/Network/BitTorrent/DHT/Message.hs | 29 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 4 |
3 files changed, 31 insertions, 11 deletions
diff --git a/src/Network/BitTorrent/DHT/Message.hs b/src/Network/BitTorrent/DHT/Message.hs index 44dc9b2f..0e2bfdd9 100644 --- a/src/Network/BitTorrent/DHT/Message.hs +++ b/src/Network/BitTorrent/DHT/Message.hs | |||
@@ -61,6 +61,7 @@ | |||
61 | {-# LANGUAGE MultiParamTypeClasses #-} | 61 | {-# LANGUAGE MultiParamTypeClasses #-} |
62 | {-# LANGUAGE UndecidableInstances #-} | 62 | {-# LANGUAGE UndecidableInstances #-} |
63 | {-# LANGUAGE ScopedTypeVariables #-} | 63 | {-# LANGUAGE ScopedTypeVariables #-} |
64 | {-# LANGUAGE TypeFamilies #-} | ||
64 | module Network.BitTorrent.DHT.Message | 65 | module Network.BitTorrent.DHT.Message |
65 | ( -- * Envelopes | 66 | ( -- * Envelopes |
66 | Query (..) | 67 | Query (..) |
@@ -217,6 +218,9 @@ instance Serialize (Response Ping) where | |||
217 | -- | \"q\" = \"ping\" | 218 | -- | \"q\" = \"ping\" |
218 | instance KRPC (Query Ping) (Response Ping) where | 219 | instance KRPC (Query Ping) (Response Ping) where |
219 | #ifdef VERSION_bencoding | 220 | #ifdef VERSION_bencoding |
221 | type Envelope (Query Ping) (Response Ping) = BValue | ||
222 | seal = toBEncode | ||
223 | unseal = fromBEncode | ||
220 | method = "ping" | 224 | method = "ping" |
221 | #else | 225 | #else |
222 | method = Method Tox.Ping -- response: Tox.Pong | 226 | method = Method Tox.Ping -- response: Tox.Pong |
@@ -229,9 +233,9 @@ instance KRPC (Query Ping) (Response Ping) where | |||
229 | -- | Find node is used to find the contact information for a node | 233 | -- | Find node is used to find the contact information for a node |
230 | -- given its ID. | 234 | -- given its ID. |
231 | #ifdef VERSION_bencoding | 235 | #ifdef VERSION_bencoding |
232 | newtype FindNode = FindNode NodeId | 236 | newtype FindNode ip = FindNode NodeId |
233 | #else | 237 | #else |
234 | data FindNode = FindNode NodeId Tox.Nonce8 -- Tox: Get Nodes | 238 | data FindNode ip = FindNode NodeId Tox.Nonce8 -- Tox: Get Nodes |
235 | #endif | 239 | #endif |
236 | deriving (Show, Eq, Typeable) | 240 | deriving (Show, Eq, Typeable) |
237 | 241 | ||
@@ -239,11 +243,11 @@ target_key :: BKey | |||
239 | target_key = "target" | 243 | target_key = "target" |
240 | 244 | ||
241 | #ifdef VERSION_bencoding | 245 | #ifdef VERSION_bencoding |
242 | instance BEncode FindNode where | 246 | instance Typeable ip => BEncode (FindNode ip) where |
243 | toBEncode (FindNode nid) = toDict $ target_key .=! nid .: endDict | 247 | toBEncode (FindNode nid) = toDict $ target_key .=! nid .: endDict |
244 | fromBEncode = fromDict $ FindNode <$>! target_key | 248 | fromBEncode = fromDict $ FindNode <$>! target_key |
245 | #else | 249 | #else |
246 | instance Serialize (Query FindNode) where | 250 | instance Serialize (Query (FindNode ip)) where |
247 | get = do | 251 | get = do |
248 | nid <- get | 252 | nid <- get |
249 | nonce <- get | 253 | nonce <- get |
@@ -304,8 +308,11 @@ instance Serialize (Response (NodeFound ip)) where | |||
304 | 308 | ||
305 | -- | \"q\" == \"find_node\" | 309 | -- | \"q\" == \"find_node\" |
306 | instance (Address ip, Typeable ip) | 310 | instance (Address ip, Typeable ip) |
307 | => KRPC (Query FindNode) (Response (NodeFound ip)) where | 311 | => KRPC (Query (FindNode ip)) (Response (NodeFound ip)) where |
308 | #ifdef VERSION_bencoding | 312 | #ifdef VERSION_bencoding |
313 | type Envelope (Query (FindNode ip)) (Response (NodeFound ip)) = BValue | ||
314 | seal = toBEncode | ||
315 | unseal = fromBEncode | ||
309 | method = "find_node" | 316 | method = "find_node" |
310 | #else | 317 | #else |
311 | method = Method Tox.GetNodes -- response: Tox.SendNodes | 318 | method = Method Tox.GetNodes -- response: Tox.SendNodes |
@@ -317,13 +324,13 @@ instance (Address ip, Typeable ip) | |||
317 | -----------------------------------------------------------------------} | 324 | -----------------------------------------------------------------------} |
318 | 325 | ||
319 | -- | Get peers associated with a torrent infohash. | 326 | -- | Get peers associated with a torrent infohash. |
320 | newtype GetPeers = GetPeers InfoHash | 327 | newtype GetPeers ip = GetPeers InfoHash |
321 | deriving (Show, Eq, Typeable) | 328 | deriving (Show, Eq, Typeable) |
322 | 329 | ||
323 | info_hash_key :: BKey | 330 | info_hash_key :: BKey |
324 | info_hash_key = "info_hash" | 331 | info_hash_key = "info_hash" |
325 | 332 | ||
326 | instance BEncode GetPeers where | 333 | instance Typeable ip => BEncode (GetPeers ip) where |
327 | toBEncode (GetPeers ih) = toDict $ info_hash_key .=! ih .: endDict | 334 | toBEncode (GetPeers ih) = toDict $ info_hash_key .=! ih .: endDict |
328 | fromBEncode = fromDict $ GetPeers <$>! info_hash_key | 335 | fromBEncode = fromDict $ GetPeers <$>! info_hash_key |
329 | 336 | ||
@@ -373,7 +380,10 @@ instance (Typeable ip, Serialize ip) => BEncode (GotPeers ip) where | |||
373 | 380 | ||
374 | -- | \"q" = \"get_peers\" | 381 | -- | \"q" = \"get_peers\" |
375 | instance (Typeable ip, Serialize ip) => | 382 | instance (Typeable ip, Serialize ip) => |
376 | KRPC (Query GetPeers) (Response (GotPeers ip)) where | 383 | KRPC (Query (GetPeers ip)) (Response (GotPeers ip)) where |
384 | type Envelope (Query (GetPeers ip)) (Response (GotPeers ip)) = BValue | ||
385 | seal = toBEncode | ||
386 | unseal = fromBEncode | ||
377 | method = "get_peers" | 387 | method = "get_peers" |
378 | 388 | ||
379 | {----------------------------------------------------------------------- | 389 | {----------------------------------------------------------------------- |
@@ -446,6 +456,9 @@ instance BEncode Announced where | |||
446 | 456 | ||
447 | -- | \"q" = \"announce\" | 457 | -- | \"q" = \"announce\" |
448 | instance KRPC (Query Announce) (Response Announced) where | 458 | instance KRPC (Query Announce) (Response Announced) where |
459 | type Envelope (Query Announce) (Response Announced) = BValue | ||
460 | seal = toBEncode | ||
461 | unseal = fromBEncode | ||
449 | method = "announce_peer" | 462 | method = "announce_peer" |
450 | 463 | ||
451 | -- endif VERSION_bencoding | 464 | -- endif VERSION_bencoding |
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index c7e48920..a1934014 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -14,6 +14,7 @@ | |||
14 | {-# LANGUAGE ScopedTypeVariables #-} | 14 | {-# LANGUAGE ScopedTypeVariables #-} |
15 | {-# LANGUAGE TemplateHaskell #-} | 15 | {-# LANGUAGE TemplateHaskell #-} |
16 | {-# LANGUAGE TupleSections #-} | 16 | {-# LANGUAGE TupleSections #-} |
17 | {-# LANGUAGE GADTs #-} | ||
17 | module Network.BitTorrent.DHT.Query | 18 | module Network.BitTorrent.DHT.Query |
18 | ( -- * Handler | 19 | ( -- * Handler |
19 | -- | To bind specific set of handlers you need to pass | 20 | -- | To bind specific set of handlers you need to pass |
@@ -71,6 +72,7 @@ import Data.Either | |||
71 | import Data.List as L | 72 | import Data.List as L |
72 | import Data.Monoid | 73 | import Data.Monoid |
73 | import Data.Text as T | 74 | import Data.Text as T |
75 | import Data.BEncode (BValue) | ||
74 | import qualified Data.Set as Set | 76 | import qualified Data.Set as Set |
75 | ;import Data.Set (Set) | 77 | ;import Data.Set (Set) |
76 | import Network | 78 | import Network |
@@ -89,14 +91,17 @@ import Network.BitTorrent.DHT.Routing as R | |||
89 | import Network.BitTorrent.DHT.Session | 91 | import Network.BitTorrent.DHT.Session |
90 | import Control.Concurrent.STM | 92 | import Control.Concurrent.STM |
91 | import qualified Network.BitTorrent.DHT.Search as Search | 93 | import qualified Network.BitTorrent.DHT.Search as Search |
94 | import Network.DHT.Mainline | ||
92 | 95 | ||
93 | {----------------------------------------------------------------------- | 96 | {----------------------------------------------------------------------- |
94 | -- Handlers | 97 | -- Handlers |
95 | -----------------------------------------------------------------------} | 98 | -----------------------------------------------------------------------} |
96 | 99 | ||
97 | nodeHandler :: Address ip => KRPC (Query a) (Response b) | 100 | nodeHandler :: ( Address ip |
101 | , KRPC (Query a) (Response b) | ||
102 | , Envelope (Query a) (Response b) ~ BValue ) | ||
98 | => (NodeAddr ip -> a -> DHT ip b) -> NodeHandler ip | 103 | => (NodeAddr ip -> a -> DHT ip b) -> NodeHandler ip |
99 | nodeHandler action = handler $ \ sockAddr qry -> do | 104 | nodeHandler action = handler mainline $ \ sockAddr qry -> do |
100 | let remoteId = queringNodeId qry | 105 | let remoteId = queringNodeId qry |
101 | read_only = queryIsReadOnly qry | 106 | read_only = queryIsReadOnly qry |
102 | q = queryParams qry | 107 | q = queryParams qry |
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs index 82926b28..db8e7cff 100644 --- a/src/Network/BitTorrent/DHT/Session.hs +++ b/src/Network/BitTorrent/DHT/Session.hs | |||
@@ -106,6 +106,8 @@ import Data.Serialize as S | |||
106 | import Data.Torrent as Torrent | 106 | import Data.Torrent as Torrent |
107 | import Network.KRPC as KRPC hiding (Options, def) | 107 | import Network.KRPC as KRPC hiding (Options, def) |
108 | import qualified Network.KRPC as KRPC (def) | 108 | import qualified Network.KRPC as KRPC (def) |
109 | import Network.KRPC.Message (KMessageOf) | ||
110 | import Data.BEncode (BValue) | ||
109 | import Network.BitTorrent.Address | 111 | import Network.BitTorrent.Address |
110 | import Network.BitTorrent.DHT.ContactInfo (PeerStore) | 112 | import Network.BitTorrent.DHT.ContactInfo (PeerStore) |
111 | import qualified Network.BitTorrent.DHT.ContactInfo as P | 113 | import qualified Network.BitTorrent.DHT.ContactInfo as P |
@@ -312,7 +314,7 @@ instance MonadLogger (DHT ip) where | |||
312 | logger <- asks loggerFun | 314 | logger <- asks loggerFun |
313 | liftIO $ logger loc src lvl (toLogStr msg) | 315 | liftIO $ logger loc src lvl (toLogStr msg) |
314 | 316 | ||
315 | type NodeHandler ip = Handler (DHT ip) | 317 | type NodeHandler ip = Handler (DHT ip) KMessageOf BValue |
316 | 318 | ||
317 | -- | Run DHT session. You /must/ properly close session using | 319 | -- | Run DHT session. You /must/ properly close session using |
318 | -- 'closeNode' function, otherwise socket or other scarce resources may | 320 | -- 'closeNode' function, otherwise socket or other scarce resources may |