summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-22 21:26:22 -0500
committerjoe <joe@jerkface.net>2017-01-22 21:26:22 -0500
commitbe0436e4d5c301fa643799cc41b204459d696f17 (patch)
tree17bae90fceaecc1928179580ef7bc51c31048d6e
parent655efe0e7e1b25e2b4d333cf7551998ed69a4dfa (diff)
Added "peers" command to cli.
-rw-r--r--examples/dhtd.hs7
-rw-r--r--src/Network/BitTorrent/DHT/ContactInfo.hs7
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs6
3 files changed, 17 insertions, 3 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 96c31dfe..bc5e9eda 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -15,6 +15,7 @@ import Data.Char
15import Data.Default 15import Data.Default
16import Data.List as L 16import Data.List as L
17import Data.Maybe 17import Data.Maybe
18import Data.String
18import qualified Data.ByteString as B (ByteString,writeFile,readFile) 19import qualified Data.ByteString as B (ByteString,writeFile,readFile)
19 ; import Data.ByteString (ByteString) 20 ; import Data.ByteString (ByteString)
20import System.IO 21import System.IO
@@ -140,6 +141,12 @@ clientSession st signalQuit sock n h = do
140 return $ do 141 return $ do
141 hPutClient h $ showReport r 142 hPutClient h $ showReport r
142 143
144 s | "peers " `isPrefixOf` s -> cmd $ do
145 let ih = fromString (drop 6 s)
146 ps <- allPeers ih
147 return $ do
148 hPutClient h $ showReport $ map (((,) "") . show . pPrint) ps
149
143 _ -> cmd0 $ hPutClient h "error." 150 _ -> cmd0 $ hPutClient h "error."
144 151
145main :: IO () 152main :: IO ()
diff --git a/src/Network/BitTorrent/DHT/ContactInfo.hs b/src/Network/BitTorrent/DHT/ContactInfo.hs
index 823982d4..4302288c 100644
--- a/src/Network/BitTorrent/DHT/ContactInfo.hs
+++ b/src/Network/BitTorrent/DHT/ContactInfo.hs
@@ -1,5 +1,6 @@
1module Network.BitTorrent.DHT.ContactInfo 1module Network.BitTorrent.DHT.ContactInfo
2 ( PeerStore 2 ( PeerStore
3 , Network.BitTorrent.DHT.ContactInfo.lookup
3 , Network.BitTorrent.DHT.ContactInfo.freshPeers 4 , Network.BitTorrent.DHT.ContactInfo.freshPeers
4 , Network.BitTorrent.DHT.ContactInfo.insertPeer 5 , Network.BitTorrent.DHT.ContactInfo.insertPeer
5 , knownSwarms 6 , knownSwarms
@@ -156,9 +157,9 @@ instance Serialize (PeerStore a) where
156 get = undefined 157 get = undefined
157 put = undefined 158 put = undefined
158 159
159-- | Used in 'get_peers' DHT queries. 160-- | Returns all peers associated with a given info hash.
160-- lookup :: Ord a => InfoHash -> PeerStore a -> [PeerAddr a] 161lookup :: Ord a => InfoHash -> PeerStore a -> [PeerAddr a]
161-- lookup ih (PeerStore m) = maybe [] (PSQ.keys . peers) $ HM.lookup ih m 162lookup ih (PeerStore m) = maybe [] (PSQ.keys . peers) $ HM.lookup ih m
162 163
163batchSize = 64 164batchSize = 64
164 165
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 4f861a1e..bc9fda91 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -55,6 +55,7 @@ module Network.BitTorrent.DHT.Session
55 , getTable 55 , getTable
56 , getClosest 56 , getClosest
57 , getSwarms 57 , getSwarms
58 , allPeers
58 59
59 -- ** Peer storage 60 -- ** Peer storage
60 , insertPeer 61 , insertPeer
@@ -408,6 +409,11 @@ getSwarms = do
408 store <- asks contactInfo >>= liftIO . atomically . readTVar 409 store <- asks contactInfo >>= liftIO . atomically . readTVar
409 return $ P.knownSwarms store 410 return $ P.knownSwarms store
410 411
412allPeers :: Ord ip => InfoHash -> DHT ip [ PeerAddr ip ]
413allPeers ih = do
414 store <- asks contactInfo >>= liftIO . atomically . readTVar
415 return $ P.lookup ih store
416
411-- | Find a set of closest nodes from routing table of this node. (in 417-- | Find a set of closest nodes from routing table of this node. (in
412-- no particular order) 418-- no particular order)
413-- 419--