From 713cee07450697e40811e74059739da02dd604c7 Mon Sep 17 00:00:00 2001 From: joe Date: Wed, 1 Feb 2017 15:27:34 -0500 Subject: Show peers as soon as they're found. --- examples/dht.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'examples/dht.hs') diff --git a/examples/dht.hs b/examples/dht.hs index feeee9ff..16b12678 100644 --- a/examples/dht.hs +++ b/examples/dht.hs @@ -1,6 +1,6 @@ {-# LANGUAGE NondecreasingIndentation #-} import Control.Monad -import Control.Monad.Fix +import Data.Function import Control.Monad.IO.Class import Data.Char import Network.Socket as Socket @@ -34,16 +34,21 @@ hReadInt h = do -- and /.../ is the sequence of characters -- -- Note: The first byte after the count is ignored and discarded. -readResponse :: Handle -> IO String +readResponse :: Handle -> IO (Char, String) readResponse h = do + c <- hGetChar h n <- hReadInt h - sequence $ replicate n (hGetChar h) + s <- sequence $ replicate n (hGetChar h) + return (c,s) -- | Send a command to the dhtd daemon and then print the response. sendCommand :: Handle -> String -> InputT IO () -sendCommand h cmd = do resp <- liftIO $ do hPutStrLn h cmd - readResponse h - outputStrLn resp +sendCommand h cmd = do liftIO $ hPutStrLn h cmd + fix $ \again -> do + (c, resp) <- liftIO $ readResponse h + if c /= '.' + then outputStr resp >> again + else outputStrLn resp -- | Get one line of input and send it to the daemon, then run the -- passed continuation if it wasn't "quit". -- cgit v1.2.3