diff options
-rw-r--r-- | examples/dhtd.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 1790d10a..f8ca8575 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -30,6 +30,9 @@ import System.Posix.Process (getProcessID) | |||
30 | import Data.Torrent (InfoHash) | 30 | import Data.Torrent (InfoHash) |
31 | import Network.BitTorrent.Address | 31 | import Network.BitTorrent.Address |
32 | import Network.BitTorrent.DHT | 32 | import Network.BitTorrent.DHT |
33 | import Network.BitTorrent.DHT.Query | ||
34 | import Network.KRPC.Manager (QueryFailure(..)) | ||
35 | import Network.KRPC.Message (ReflectedIP(..)) | ||
33 | import qualified Network.BitTorrent.DHT.Routing as R | 36 | import qualified Network.BitTorrent.DHT.Routing as R |
34 | import Network.BitTorrent.DHT.Session | 37 | import Network.BitTorrent.DHT.Session |
35 | import Network.SocketLike | 38 | import Network.SocketLike |
@@ -201,10 +204,25 @@ clientSession st signalQuit sock n h = do | |||
201 | hPutClient h $ unlines $ map (showEnry . (flip (,) 0)) nodes | 204 | hPutClient h $ unlines $ map (showEnry . (flip (,) 0)) nodes |
202 | Left er -> return $ hPutClient h er | 205 | Left er -> return $ hPutClient h er |
203 | 206 | ||
207 | ("ping", s) -> cmd $ do | ||
208 | case readEither s of | ||
209 | Right addr -> do result <- try $ pingQ addr | ||
210 | let rs = either (pure . showQueryFail) reportPong result | ||
211 | return $ do | ||
212 | hPutClient h $ unlines rs | ||
213 | Left er -> return $ hPutClient h er | ||
214 | |||
204 | _ -> cmd0 $ hPutClient h "error." | 215 | _ -> cmd0 $ hPutClient h "error." |
205 | 216 | ||
206 | defaultPort = error "TODO defaultPort" | 217 | defaultPort = error "TODO defaultPort" |
207 | 218 | ||
219 | showQueryFail :: QueryFailure -> String | ||
220 | showQueryFail e = show e | ||
221 | |||
222 | reportPong (info,myip) = maybe id consip myip [show $ pPrint info] | ||
223 | where | ||
224 | consip (ReflectedIP ip) xs = ("(external-ip " ++ show ip ++ ")") : xs | ||
225 | |||
208 | main :: IO () | 226 | main :: IO () |
209 | main = do | 227 | main = do |
210 | args <- getArgs | 228 | args <- getArgs |