summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-24 04:55:58 -0500
committerjoe <joe@jerkface.net>2017-01-24 04:55:58 -0500
commitc0256fa5a3bb7374eb5aade5f46b0a586c8d314a (patch)
tree49ef0fc8309dfe26d0b98d34b4e25bcf611e4a74 /examples
parent6272d0ab121d3a964f49dc7df34802b5f6540b9c (diff)
New "closest" command, find nearby routing nodes to an info hash.
Diffstat (limited to 'examples')
-rw-r--r--examples/dhtd.hs19
1 files changed, 18 insertions, 1 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 337e7d0d..1790d10a 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -7,7 +7,7 @@
7{-# LANGUAGE RecordWildCards #-} 7{-# LANGUAGE RecordWildCards #-}
8{-# LANGUAGE CPP #-} 8{-# LANGUAGE CPP #-}
9 9
10import Control.Arrow; 10import Control.Arrow
11import Control.Monad 11import Control.Monad
12import Control.Monad.Logger 12import Control.Monad.Logger
13import Control.Monad.Reader 13import Control.Monad.Reader
@@ -27,6 +27,7 @@ import Text.Read
27import Control.Monad.Reader.Class 27import Control.Monad.Reader.Class
28import System.Posix.Process (getProcessID) 28import System.Posix.Process (getProcessID)
29 29
30import Data.Torrent (InfoHash)
30import Network.BitTorrent.Address 31import Network.BitTorrent.Address
31import Network.BitTorrent.DHT 32import Network.BitTorrent.DHT
32import qualified Network.BitTorrent.DHT.Routing as R 33import qualified Network.BitTorrent.DHT.Routing as R
@@ -183,6 +184,22 @@ clientSession st signalQuit sock n h = do
183 let r = map (\PerThread{..} -> (show lbl,show (diffUTCTime tm startTime))) ts 184 let r = map (\PerThread{..} -> (show lbl,show (diffUTCTime tm startTime))) ts
184 hPutClient h $ showReport r 185 hPutClient h $ showReport r
185#endif 186#endif
187 ("closest", s) -> cmd $ do
188 let (ns,hs) = second (dropWhile isSpace) $ break isSpace s
189 parse | null hs = do
190 ih <- readEither ns
191 return (8 :: Int, ih :: InfoHash)
192 | otherwise = do
193 n <- readEither ns
194 ih <- readEither hs
195 return (n :: Int, ih :: InfoHash)
196 case parse of
197 Right (n,ih) -> do
198 tbl <- getTable
199 let nodes = R.kclosest n ih tbl
200 return $ do
201 hPutClient h $ unlines $ map (showEnry . (flip (,) 0)) nodes
202 Left er -> return $ hPutClient h er
186 203
187 _ -> cmd0 $ hPutClient h "error." 204 _ -> cmd0 $ hPutClient h "error."
188 205