diff options
Diffstat (limited to 'examples/dht.hs')
-rw-r--r-- | examples/dht.hs | 17 |
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 #-} |
2 | import Control.Monad | 2 | import Control.Monad |
3 | import Control.Monad.Fix | 3 | import Data.Function |
4 | import Control.Monad.IO.Class | 4 | import Control.Monad.IO.Class |
5 | import Data.Char | 5 | import Data.Char |
6 | import Network.Socket as Socket | 6 | import 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. |
37 | readResponse :: Handle -> IO String | 37 | readResponse :: Handle -> IO (Char, String) |
38 | readResponse h = do | 38 | readResponse 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. |
43 | sendCommand :: Handle -> String -> InputT IO () | 45 | sendCommand :: Handle -> String -> InputT IO () |
44 | sendCommand h cmd = do resp <- liftIO $ do hPutStrLn h cmd | 46 | sendCommand 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". |