summaryrefslogtreecommitdiff
path: root/examples/dht.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-02-01 15:27:34 -0500
committerjoe <joe@jerkface.net>2017-02-01 15:27:34 -0500
commit713cee07450697e40811e74059739da02dd604c7 (patch)
treec30014b323d207f2b9aaf07ca819dc1ad7e31f30 /examples/dht.hs
parent38c23ccf93a7715babc2f99b2b98acd695159aca (diff)
Show peers as soon as they're found.
Diffstat (limited to 'examples/dht.hs')
-rw-r--r--examples/dht.hs17
1 files changed, 11 insertions, 6 deletions
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 @@
1{-# LANGUAGE NondecreasingIndentation #-} 1{-# LANGUAGE NondecreasingIndentation #-}
2import Control.Monad 2import Control.Monad
3import Control.Monad.Fix 3import Data.Function
4import Control.Monad.IO.Class 4import Control.Monad.IO.Class
5import Data.Char 5import Data.Char
6import Network.Socket as Socket 6import Network.Socket as Socket
@@ -34,16 +34,21 @@ hReadInt h = do
34-- and /.../ is the sequence of characters 34-- and /.../ is the sequence of characters
35-- 35--
36-- Note: The first byte after the count is ignored and discarded. 36-- Note: The first byte after the count is ignored and discarded.
37readResponse :: Handle -> IO String 37readResponse :: Handle -> IO (Char, String)
38readResponse h = do 38readResponse h = do
39 c <- hGetChar h
39 n <- hReadInt h 40 n <- hReadInt h
40 sequence $ replicate n (hGetChar h) 41 s <- sequence $ replicate n (hGetChar h)
42 return (c,s)
41 43
42-- | Send a command to the dhtd daemon and then print the response. 44-- | Send a command to the dhtd daemon and then print the response.
43sendCommand :: Handle -> String -> InputT IO () 45sendCommand :: Handle -> String -> InputT IO ()
44sendCommand h cmd = do resp <- liftIO $ do hPutStrLn h cmd 46sendCommand h cmd = do liftIO $ hPutStrLn h cmd
45 readResponse h 47 fix $ \again -> do
46 outputStrLn resp 48 (c, resp) <- liftIO $ readResponse h
49 if c /= '.'
50 then outputStr resp >> again
51 else outputStrLn resp
47 52
48-- | Get one line of input and send it to the daemon, then run the 53-- | Get one line of input and send it to the daemon, then run the
49-- passed continuation if it wasn't "quit". 54-- passed continuation if it wasn't "quit".