summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Message.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-06-05 03:21:25 -0400
committerjoe <joe@jerkface.net>2017-06-05 03:31:23 -0400
commit24df9a12a9240aaed8741d60e4b0b9cbf59a9fd9 (patch)
tree04791746bb576c40851f441ebc851c9d0d8da777 /src/Network/BitTorrent/DHT/Message.hs
parent219d72ebde4bab5a516a86608dcb3aede75c1611 (diff)
WIP: Adapting DHT to Tox network (part 2).
Diffstat (limited to 'src/Network/BitTorrent/DHT/Message.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Message.hs29
1 files changed, 21 insertions, 8 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 #-}
64module Network.BitTorrent.DHT.Message 65module 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\"
218instance KRPC (Query Ping) (Response Ping) where 219instance 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
232newtype FindNode = FindNode NodeId 236newtype FindNode ip = FindNode NodeId
233#else 237#else
234data FindNode = FindNode NodeId Tox.Nonce8 -- Tox: Get Nodes 238data 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
239target_key = "target" 243target_key = "target"
240 244
241#ifdef VERSION_bencoding 245#ifdef VERSION_bencoding
242instance BEncode FindNode where 246instance 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
246instance Serialize (Query FindNode) where 250instance 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\"
306instance (Address ip, Typeable ip) 310instance (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.
320newtype GetPeers = GetPeers InfoHash 327newtype GetPeers ip = GetPeers InfoHash
321 deriving (Show, Eq, Typeable) 328 deriving (Show, Eq, Typeable)
322 329
323info_hash_key :: BKey 330info_hash_key :: BKey
324info_hash_key = "info_hash" 331info_hash_key = "info_hash"
325 332
326instance BEncode GetPeers where 333instance 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\"
375instance (Typeable ip, Serialize ip) => 382instance (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\"
448instance KRPC (Query Announce) (Response Announced) where 458instance 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