summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/dhtd.hs14
-rw-r--r--src/Network/BitTorrent/DHT.hs8
-rw-r--r--src/Network/BitTorrent/DHT/Query.hs71
-rw-r--r--src/Network/DHT/Mainline.hs8
-rw-r--r--src/Network/KRPC/Method.hs43
5 files changed, 75 insertions, 69 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 99ff7218..de1f11ab 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -151,8 +151,8 @@ godht ::
151 , SerializableTo raw (Response dht (NodeFound dht IPv4)) 151 , SerializableTo raw (Response dht (NodeFound dht IPv4))
152 , SerializableTo raw (Query dht (Ping dht)) 152 , SerializableTo raw (Query dht (Ping dht))
153 , SerializableTo raw (Response dht (Ping dht)) 153 , SerializableTo raw (Response dht (Ping dht))
154 , KRPC (Query dht (FindNode dht IPv4)) (Response dht (NodeFound dht IPv4)) 154 , KRPC dht (Query dht (FindNode dht IPv4)) (Response dht (NodeFound dht IPv4))
155 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 155 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
156 , Ord (NodeId dht) 156 , Ord (NodeId dht)
157 , FiniteBits (NodeId dht) 157 , FiniteBits (NodeId dht)
158 , Serialize (NodeId dht) 158 , Serialize (NodeId dht)
@@ -195,8 +195,8 @@ data GenericDHT ip a
195 , SerializableTo raw (Response dht (NodeFound dht ip)) 195 , SerializableTo raw (Response dht (NodeFound dht ip))
196 , SerializableTo raw (Query dht (Ping dht)) 196 , SerializableTo raw (Query dht (Ping dht))
197 , SerializableTo raw (Response dht (Ping dht)) 197 , SerializableTo raw (Response dht (Ping dht))
198 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 198 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
199 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 199 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
200 , Ord (NodeId dht) 200 , Ord (NodeId dht)
201 , FiniteBits (NodeId dht) 201 , FiniteBits (NodeId dht)
202 , Serialize (NodeId dht) 202 , Serialize (NodeId dht)
@@ -249,10 +249,12 @@ instance Serialize (Response Tox.Message (NodeFound Tox.Message IPv4)) where
249instance Serialize (Response Tox.Message (Ping Tox.Message)) where -- TODO 249instance Serialize (Response Tox.Message (Ping Tox.Message)) where -- TODO
250 get = error "TODO get" 250 get = error "TODO get"
251 put = error "TODO put" 251 put = error "TODO put"
252instance KRPC (Query Tox.Message (FindNode Tox.Message IPv4)) 252instance KRPC Tox.Message (Query Tox.Message (FindNode Tox.Message IPv4))
253 (Response Tox.Message (NodeFound Tox.Message IPv4)) where 253 (Response Tox.Message (NodeFound Tox.Message IPv4)) where
254instance KRPC (Query Tox.Message (Ping Tox.Message )) 254 method = error "TODO method"
255instance KRPC Tox.Message (Query Tox.Message (Ping Tox.Message ))
255 (Response Tox.Message (Ping Tox.Message )) where 256 (Response Tox.Message (Ping Tox.Message )) where
257 method = error "TODO method"
256instance DataHandlers ByteString Tox.Message where 258instance DataHandlers ByteString Tox.Message where
257 259
258 260
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index 6d31eab2..fa8071d5 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -119,8 +119,8 @@ dht ::
119 , Show (QueryMethod dht) 119 , Show (QueryMethod dht)
120 , Pretty (NodeInfo dht ip u) 120 , Pretty (NodeInfo dht ip u)
121 , Kademlia dht 121 , Kademlia dht
122 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 122 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
123 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 123 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
124 , DataHandlers raw dht 124 , DataHandlers raw dht
125 , WireFormat raw dht 125 , WireFormat raw dht
126 , Show u 126 , Show u
@@ -227,8 +227,8 @@ bootstrap :: forall raw dht u ip.
227 , Show (QueryMethod dht) 227 , Show (QueryMethod dht)
228 , Pretty (NodeInfo dht ip u) 228 , Pretty (NodeInfo dht ip u)
229 , Kademlia dht 229 , Kademlia dht
230 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 230 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
231 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 231 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
232 , DataHandlers raw dht 232 , DataHandlers raw dht
233 , WireFormat raw dht 233 , WireFormat raw dht
234 , Show u 234 , Show u
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs
index 67dc4541..4c980e22 100644
--- a/src/Network/BitTorrent/DHT/Query.hs
+++ b/src/Network/BitTorrent/DHT/Query.hs
@@ -123,7 +123,7 @@ import Data.String
123 123
124{- 124{-
125nodeHandler :: ( Address ip 125nodeHandler :: ( Address ip
126 , KRPC (Query KMessageOf a) (Response KMessageOf b) 126 , KRPC dht (Query KMessageOf a) (Response KMessageOf b)
127 ) 127 )
128 => (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) -> (NodeAddr ip -> IO (NodeId KMessageOf)) -> (Char -> String -> Text -> IO ()) -> QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler 128 => (NodeInfo KMessageOf ip () -> Maybe ReflectedIP -> IO ()) -> (NodeAddr ip -> IO (NodeId KMessageOf)) -> (Char -> String -> Text -> IO ()) -> QueryMethod KMessageOf -> (NodeAddr ip -> a -> IO b) -> NodeHandler
129-} 129-}
@@ -197,10 +197,10 @@ kademliaHandlers :: forall raw dht u ip. (Eq ip, Ord ip, Address ip
197 , Serialize (TransactionID dht) 197 , Serialize (TransactionID dht)
198 , WireFormat raw dht 198 , WireFormat raw dht
199 , Kademlia dht 199 , Kademlia dht
200 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 200 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
201 , Functor dht 201 , Functor dht
202 , Pretty (NodeInfo dht ip u) 202 , Pretty (NodeInfo dht ip u)
203 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 203 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
204 , SerializableTo raw (Response dht (NodeFound dht ip)) 204 , SerializableTo raw (Response dht (NodeFound dht ip))
205 , SerializableTo raw (Query dht (FindNode dht ip)) 205 , SerializableTo raw (Query dht (FindNode dht ip))
206 ) => LogFun -> DHT raw dht u ip [Handler IO dht raw] 206 ) => LogFun -> DHT raw dht u ip [Handler IO dht raw]
@@ -208,7 +208,7 @@ kademliaHandlers :: forall raw dht u ip. (Eq ip, Ord ip, Address ip
208kademliaHandlers logger = do 208kademliaHandlers logger = do
209 groknode <- insertNode1 209 groknode <- insertNode1
210 mynid <- myNodeIdAccordingTo1 210 mynid <- myNodeIdAccordingTo1
211 let handler :: ( KRPC (Query dht a) (Response dht b) 211 let handler :: ( KRPC dht (Query dht a) (Response dht b)
212 , SerializableTo raw (Response dht b) 212 , SerializableTo raw (Response dht b)
213 , SerializableTo raw (Query dht a) 213 , SerializableTo raw (Query dht a)
214 ) => QueryMethod dht -> (NodeAddr ip -> a -> IO b) -> Handler IO dht raw 214 ) => QueryMethod dht -> (NodeAddr ip -> a -> IO b) -> Handler IO dht raw
@@ -264,10 +264,10 @@ defaultHandlers :: forall raw dht u ip.
264 , Serialize (TransactionID dht) 264 , Serialize (TransactionID dht)
265 , WireFormat raw dht 265 , WireFormat raw dht
266 , Kademlia dht 266 , Kademlia dht
267 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 267 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
268 , Functor dht 268 , Functor dht
269 , Pretty (NodeInfo dht ip u) 269 , Pretty (NodeInfo dht ip u)
270 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 270 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
271 , SerializableTo raw (Response dht (NodeFound dht ip)) 271 , SerializableTo raw (Response dht (NodeFound dht ip))
272 , SerializableTo raw (Query dht (FindNode dht ip)) 272 , SerializableTo raw (Query dht (FindNode dht ip))
273 , Eq ip, Ord ip, Address ip, DataHandlers raw dht 273 , Eq ip, Ord ip, Address ip, DataHandlers raw dht
@@ -293,7 +293,7 @@ type Iteration raw dht u ip o = NodeInfo dht ip u -> DHT raw dht u ip (Either [N
293pingQ :: forall raw dht u ip. 293pingQ :: forall raw dht u ip.
294 ( DHT.Kademlia dht 294 ( DHT.Kademlia dht
295 , Address ip 295 , Address ip
296 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 296 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
297 , Default u 297 , Default u
298 , Show u 298 , Show u
299 , Ord (TransactionID dht) 299 , Ord (TransactionID dht)
@@ -361,7 +361,7 @@ ioGetPeers ih = do
361 361
362ioFindNode :: ( DHT.Kademlia dht 362ioFindNode :: ( DHT.Kademlia dht
363 , WireFormat raw dht 363 , WireFormat raw dht
364 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 364 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
365 , Address ip 365 , Address ip
366 , Default u 366 , Default u
367 , Show u 367 , Show u
@@ -371,7 +371,7 @@ ioFindNode :: ( DHT.Kademlia dht
371 , Ord (NodeId dht) 371 , Ord (NodeId dht)
372 , FiniteBits (NodeId dht) 372 , FiniteBits (NodeId dht)
373 , Show (NodeId dht) 373 , Show (NodeId dht)
374 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 374 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
375 , Ord (TransactionID dht) 375 , Ord (TransactionID dht)
376 , Serialize (TransactionID dht) 376 , Serialize (TransactionID dht)
377 , SerializableTo raw (Response dht (NodeFound dht ip)) 377 , SerializableTo raw (Response dht (NodeFound dht ip))
@@ -390,7 +390,7 @@ ioFindNode ih = do
390-- | Like ioFindNode, but considers all found nodes to be 'Right' results. 390-- | Like ioFindNode, but considers all found nodes to be 'Right' results.
391ioFindNodes :: ( DHT.Kademlia dht 391ioFindNodes :: ( DHT.Kademlia dht
392 , WireFormat raw dht 392 , WireFormat raw dht
393 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 393 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
394 , Address ip 394 , Address ip
395 , Default u 395 , Default u
396 , Show u 396 , Show u
@@ -400,7 +400,7 @@ ioFindNodes :: ( DHT.Kademlia dht
400 , Ord (NodeId dht) 400 , Ord (NodeId dht)
401 , FiniteBits (NodeId dht) 401 , FiniteBits (NodeId dht)
402 , Show (NodeId dht) 402 , Show (NodeId dht)
403 , KRPC (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip)) 403 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
404 , Ord (TransactionID dht) 404 , Ord (TransactionID dht)
405 , Serialize (TransactionID dht) 405 , Serialize (TransactionID dht)
406 , SerializableTo raw (Response dht (NodeFound dht ip)) 406 , SerializableTo raw (Response dht (NodeFound dht ip))
@@ -481,7 +481,7 @@ publish = error "todo"
481 481
482probeNode :: ( Default u 482probeNode :: ( Default u
483 , Show u 483 , Show u
484 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 484 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
485 , DHT.Kademlia dht 485 , DHT.Kademlia dht
486 , Address ip 486 , Address ip
487 , Ord (TransactionID dht) 487 , Ord (TransactionID dht)
@@ -507,9 +507,22 @@ refreshNodes :: forall raw dht u ip.
507 , Default u 507 , Default u
508 , FiniteBits (NodeId dht) 508 , FiniteBits (NodeId dht)
509 , Pretty (NodeId dht) 509 , Pretty (NodeId dht)
510 , DHT.Kademlia dht ) => NodeId dht -> DHT raw dht u ip () -- [NodeInfo KMessageOf ip ()] 510 , DHT.Kademlia dht
511refreshNodes _ = return () -- TODO 511 , Ord ip
512#if 0 512 , Ord (TransactionID dht)
513 , SerializableTo raw (Response dht (NodeFound dht ip))
514 , SerializableTo raw (Query dht (FindNode dht ip))
515 , SerializableTo raw (Response dht (Ping dht))
516 , SerializableTo raw (Query dht (Ping dht))
517 , Pretty (NodeInfo dht ip u)
518 , Show (NodeId dht)
519 , Show u
520 , Show (QueryMethod dht)
521 , Serialize (TransactionID dht)
522 , WireFormat raw dht
523 , KRPC dht (Query dht (FindNode dht ip)) (Response dht (NodeFound dht ip))
524 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
525 ) => NodeId dht -> DHT raw dht u ip () -- [NodeInfo KMessageOf ip ()]
513-- FIXME do not use getClosest sinse we should /refresh/ them 526-- FIXME do not use getClosest sinse we should /refresh/ them
514refreshNodes nid = do 527refreshNodes nid = do
515 $(logDebugS) "routing.refresh_bucket" (T.pack (render (pPrint nid))) 528 $(logDebugS) "routing.refresh_bucket" (T.pack (render (pPrint nid)))
@@ -520,15 +533,15 @@ refreshNodes nid = do
520 -- Expected type: ConduitM [NodeAddr ip] [NodeInfo KMessageOf ip ()] (DHT ip) () 533 -- Expected type: ConduitM [NodeAddr ip] [NodeInfo KMessageOf ip ()] (DHT ip) ()
521 -- Actual type: ConduitM [NodeInfo KMessageOf ip ()] [NodeInfo KMessageOf ip ()] (DHT ip) () 534 -- Actual type: ConduitM [NodeInfo KMessageOf ip ()] [NodeInfo KMessageOf ip ()] (DHT ip) ()
522 -- nss <- sourceList [[addr]] \$= search nid (findNodeQ nid) $$ C.consume 535 -- nss <- sourceList [[addr]] \$= search nid (findNodeQ nid) $$ C.consume
523 nss <- sourceList [nodes] $= search nid (findNodeQ (Proxy :: Proxy dht) nid) $$ C.consume 536 -- nss <- sourceList [nodes] \$= search nid (findNodeQ (Proxy :: Proxy dht) nid) $$ C.consume
524 $(logWarnS) "refreshNodes" $ "received " <> T.pack (show (L.length (L.concat nss))) <> " nodes." 537 ns <- bgsearch ioFindNodes nid
525 _ <- queryParallel $ flip L.map (L.concat nss) $ \n -> do 538 $(logWarnS) "refreshNodes" $ "received " <> T.pack (show (L.length ns)) <> " nodes."
539 _ <- queryParallel $ flip L.map ns $ \n -> do
526 $(logWarnS) "refreshNodes" $ "received node: " <> T.pack (show (pPrint n)) 540 $(logWarnS) "refreshNodes" $ "received node: " <> T.pack (show (pPrint n))
527 pingQ (nodeAddr n) 541 pingQ (nodeAddr n)
528 -- pingQ takes care of inserting the node. 542 -- pingQ takes care of inserting the node.
529 return () 543 return ()
530 return () -- \$ L.concat nss 544 return () -- \$ L.concat nss
531#endif
532 545
533logc :: Char -> String -> DHT raw dht u ip () 546logc :: Char -> String -> DHT raw dht u ip ()
534logc 'D' = $(logDebugS) "insertNode" . T.pack 547logc 'D' = $(logDebugS) "insertNode" . T.pack
@@ -546,7 +559,7 @@ insertNode :: forall raw dht u ip.
546 , Default u 559 , Default u
547 , Show u 560 , Show u
548 , DHT.Kademlia dht 561 , DHT.Kademlia dht
549 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 562 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
550 , Ord (TransactionID dht) 563 , Ord (TransactionID dht)
551 , WireFormat raw dht 564 , WireFormat raw dht
552 , Serialize (TransactionID dht) 565 , Serialize (TransactionID dht)
@@ -567,7 +580,7 @@ insertNode1 :: forall raw dht u ip.
567 , Ord (NodeId dht) 580 , Ord (NodeId dht)
568 , FiniteBits (NodeId dht) 581 , FiniteBits (NodeId dht)
569 , Show (NodeId dht) 582 , Show (NodeId dht)
570 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 583 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
571 , DHT.Kademlia dht 584 , DHT.Kademlia dht
572 , Ord (TransactionID dht) 585 , Ord (TransactionID dht)
573 , WireFormat raw dht 586 , WireFormat raw dht
@@ -611,8 +624,8 @@ insertNode1 = do
611-- | Throws exception if node is not responding. 624-- | Throws exception if node is not responding.
612queryNode :: forall raw dht u a b ip. 625queryNode :: forall raw dht u a b ip.
613 ( Address ip 626 ( Address ip
614 , KRPC (Query dht a) (Response dht b) 627 , KRPC dht (Query dht a) (Response dht b)
615 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 628 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
616 , Default u 629 , Default u
617 , Show u 630 , Show u
618 , DHT.Kademlia dht 631 , DHT.Kademlia dht
@@ -635,8 +648,8 @@ queryNode' :: forall raw dht u a b ip.
635 , Default u 648 , Default u
636 , Show u 649 , Show u
637 , DHT.Kademlia dht 650 , DHT.Kademlia dht
638 , KRPC (Query dht a) (Response dht b) 651 , KRPC dht (Query dht a) (Response dht b)
639 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 652 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
640 , Ord (TransactionID dht) 653 , Ord (TransactionID dht)
641 , Serialize (TransactionID dht) 654 , Serialize (TransactionID dht)
642 , WireFormat raw dht 655 , WireFormat raw dht
@@ -652,9 +665,9 @@ queryNode' :: forall raw dht u a b ip.
652queryNode' addr q = do 665queryNode' addr q = do
653 nid <- myNodeIdAccordingTo addr 666 nid <- myNodeIdAccordingTo addr
654 let read_only = False -- TODO: check for NAT issues. (BEP 43) 667 let read_only = False -- TODO: check for NAT issues. (BEP 43)
655 let KRPC.Method name = KRPC.method :: KRPC.Method (Query dht a) (Response dht b) 668 let KRPC.Method name = KRPC.method :: KRPC.Method dht (Query dht a) (Response dht b)
656 mgr <- asks manager 669 mgr <- asks manager
657 (Response remoteId r, witnessed_ip) <- liftIO $ query' mgr (error "TODO: name") (toSockAddr addr) (Query nid read_only q) 670 (Response remoteId r, witnessed_ip) <- liftIO $ query' mgr name (toSockAddr addr) (Query nid read_only q)
658 -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip) 671 -- \$(logDebugS) "queryNode" $ "Witnessed address: " <> T.pack (show witnessed_ip)
659 -- <> " by " <> T.pack (show (toSockAddr addr)) 672 -- <> " by " <> T.pack (show (toSockAddr addr))
660 _ <- insertNode (NodeInfo remoteId addr def) witnessed_ip 673 _ <- insertNode (NodeInfo remoteId addr def) witnessed_ip
@@ -662,8 +675,8 @@ queryNode' addr q = do
662 675
663-- | Infix version of 'queryNode' function. 676-- | Infix version of 'queryNode' function.
664(<@>) :: ( Address ip 677(<@>) :: ( Address ip
665 , KRPC (Query dht a) (Response dht b) 678 , KRPC dht (Query dht a) (Response dht b)
666 , KRPC (Query dht (Ping dht)) (Response dht (Ping dht)) 679 , KRPC dht (Query dht (Ping dht)) (Response dht (Ping dht))
667 , Default u 680 , Default u
668 , Show u 681 , Show u
669 , Show (QueryMethod dht) 682 , Show (QueryMethod dht)
diff --git a/src/Network/DHT/Mainline.hs b/src/Network/DHT/Mainline.hs
index aefd7742..b756ff6a 100644
--- a/src/Network/DHT/Mainline.hs
+++ b/src/Network/DHT/Mainline.hs
@@ -217,7 +217,7 @@ instance Serialize (Response Ping) where
217#endif 217#endif
218 218
219-- | \"q\" = \"ping\" 219-- | \"q\" = \"ping\"
220instance KRPC (Query KMessageOf (Ping KMessageOf)) (Response KMessageOf (Ping KMessageOf)) where 220instance KRPC KMessageOf (Query KMessageOf (Ping KMessageOf)) (Response KMessageOf (Ping KMessageOf)) where
221#ifdef VERSION_bencoding 221#ifdef VERSION_bencoding
222 method = "ping" 222 method = "ping"
223#else 223#else
@@ -301,7 +301,7 @@ instance Serialize (Response KMessageOf (NodeFound KMessageOf ip)) where
301 301
302-- | \"q\" == \"find_node\" 302-- | \"q\" == \"find_node\"
303instance (Address ip, Typeable ip) 303instance (Address ip, Typeable ip)
304 => KRPC (Query KMessageOf (FindNode KMessageOf ip)) (Response KMessageOf (NodeFound KMessageOf ip)) where 304 => KRPC KMessageOf (Query KMessageOf (FindNode KMessageOf ip)) (Response KMessageOf (NodeFound KMessageOf ip)) where
305#ifdef VERSION_bencoding 305#ifdef VERSION_bencoding
306 method = "find_node" 306 method = "find_node"
307#else 307#else
@@ -370,7 +370,7 @@ instance (Typeable ip, Serialize ip) => BEncode (GotPeers ip) where
370 370
371-- | \"q" = \"get_peers\" 371-- | \"q" = \"get_peers\"
372instance (Typeable ip, Serialize ip) => 372instance (Typeable ip, Serialize ip) =>
373 KRPC (Query KMessageOf (GetPeers ip)) (Response KMessageOf (GotPeers ip)) where 373 KRPC KMessageOf (Query KMessageOf (GetPeers ip)) (Response KMessageOf (GotPeers ip)) where
374 method = "get_peers" 374 method = "get_peers"
375 375
376{----------------------------------------------------------------------- 376{-----------------------------------------------------------------------
@@ -442,7 +442,7 @@ instance BEncode Announced where
442 fromBEncode _ = pure Announced 442 fromBEncode _ = pure Announced
443 443
444-- | \"q" = \"announce\" 444-- | \"q" = \"announce\"
445instance KRPC (Query KMessageOf Announce) (Response KMessageOf Announced) where 445instance KRPC KMessageOf (Query KMessageOf Announce) (Response KMessageOf Announced) where
446 method = "announce_peer" 446 method = "announce_peer"
447 447
448-- endif VERSION_bencoding 448-- endif VERSION_bencoding
diff --git a/src/Network/KRPC/Method.hs b/src/Network/KRPC/Method.hs
index d0eb136a..84c7fe4c 100644
--- a/src/Network/KRPC/Method.hs
+++ b/src/Network/KRPC/Method.hs
@@ -9,12 +9,14 @@
9-- 9--
10{-# LANGUAGE CPP #-} 10{-# LANGUAGE CPP #-}
11{-# LANGUAGE DefaultSignatures #-} 11{-# LANGUAGE DefaultSignatures #-}
12{-# LANGUAGE FlexibleContexts #-}
13{-# LANGUAGE FunctionalDependencies #-}
12{-# LANGUAGE GeneralizedNewtypeDeriving #-} 14{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13{-# LANGUAGE MultiParamTypeClasses #-} 15{-# LANGUAGE MultiParamTypeClasses #-}
14{-# LANGUAGE RankNTypes #-} 16{-# LANGUAGE RankNTypes #-}
15{-# LANGUAGE ScopedTypeVariables #-} 17{-# LANGUAGE ScopedTypeVariables #-}
18{-# LANGUAGE StandaloneDeriving #-}
16{-# LANGUAGE TypeFamilies #-} 19{-# LANGUAGE TypeFamilies #-}
17{-# LANGUAGE FunctionalDependencies #-}
18module Network.KRPC.Method 20module Network.KRPC.Method
19 ( Method (..) 21 ( Method (..)
20 , KRPC (..) 22 , KRPC (..)
@@ -32,6 +34,7 @@ import Data.List as L
32import Data.String 34import Data.String
33import Data.Typeable 35import Data.Typeable
34import Network.DatagramServer.Mainline 36import Network.DatagramServer.Mainline
37import Network.DatagramServer.Types
35 38
36 39
37-- | Method datatype used to describe method name, parameters and 40-- | Method datatype used to describe method name, parameters and
@@ -44,28 +47,24 @@ import Network.DatagramServer.Mainline
44-- 47--
45-- * result: Type of return value of the method. 48-- * result: Type of return value of the method.
46-- 49--
47newtype Method param result = Method { methodName :: MethodName } 50newtype Method dht param result = Method { methodName :: QueryMethod dht }
48 deriving ( Eq, Ord 51
49#ifdef VERSION_bencoding 52deriving instance Eq (QueryMethod dht) => Eq (Method dht param result)
50 , IsString 53deriving instance Ord (QueryMethod dht) => Ord (Method dht param result)
51 , BEncode 54deriving instance IsString (QueryMethod dht) => IsString (Method dht param result)
52#endif 55deriving instance BEncode (QueryMethod dht) => BEncode (Method dht param result)
53 )
54 56
55-- | Example: 57-- | Example:
56-- 58--
57-- @show (Method \"concat\" :: [Int] Int) == \"concat :: [Int] -> Int\"@ 59-- @show (Method \"concat\" :: [Int] Int) == \"concat :: [Int] -> Int\"@
58-- 60--
59instance (Typeable a, Typeable b) => Show (Method a b) where 61instance (Show (QueryMethod dht), Typeable a, Typeable b) => Show (Method dht a b) where
60 showsPrec _ = showsMethod 62 showsPrec _ = showsMethod
61 63
62showsMethod :: forall a b. ( Typeable a , Typeable b ) => Method a b -> ShowS 64showsMethod :: forall dht a b. ( Show (QueryMethod dht), Typeable a , Typeable b ) => Method dht a b -> ShowS
63showsMethod (Method name) = 65showsMethod (Method name) =
64#ifdef VERSION_bencoding 66 -- showString (BC.unpack name) <>
65 showString (BC.unpack name) <>
66#else
67 shows (show name) <> 67 shows (show name) <>
68#endif
69 showString " :: " <> 68 showString " :: " <>
70 shows paramsTy <> 69 shows paramsTy <>
71 showString " -> " <> 70 showString " -> " <>
@@ -88,24 +87,16 @@ showsMethod (Method name) =
88-- method = \"ping\" 87-- method = \"ping\"
89-- @ 88-- @
90-- 89--
91class ( Typeable req, Typeable resp 90class ( Typeable req, Typeable resp)
92-- #ifdef VERSION_bencoding 91 => KRPC dht req resp | req -> resp, resp -> req where
93 -- , BEncode req, BEncode resp
94-- #else
95 -- , Serialize req, Serialize resp
96-- #endif
97 )
98 => KRPC req resp | req -> resp, resp -> req where
99 92
100 -- | Method name. Default implementation uses lowercased @req@ 93 -- | Method name. Default implementation uses lowercased @req@
101 -- datatype name. 94 -- datatype name.
102 -- 95 --
103 method :: Method req resp 96 method :: Method dht req resp
104 97
105#ifdef VERSION_bencoding
106 -- TODO add underscores 98 -- TODO add underscores
107 default method :: Typeable req => Method req resp 99 default method :: (IsString (QueryMethod dht), Typeable req) => Method dht req resp
108 method = Method $ fromString $ L.map toLower $ show $ typeOf hole 100 method = Method $ fromString $ L.map toLower $ show $ typeOf hole
109 where 101 where
110 hole = error "krpc.method: impossible" :: req 102 hole = error "krpc.method: impossible" :: req
111#endif