aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2016-02-16 20:35:29 +0000
committerGregor Kleen <gkleen@yggdrasil.li>2016-02-16 20:35:29 +0000
commite9c92d233ba470d060f1ee0276120bc746b806ac (patch)
tree89aac68a6fbb912562c3979f7d278268695dad79
parent8e8394452565dbd338427c6304c324d5fb51b908 (diff)
downloadthermoprint-e9c92d233ba470d060f1ee0276120bc746b806ac.tar
thermoprint-e9c92d233ba470d060f1ee0276120bc746b806ac.tar.gz
thermoprint-e9c92d233ba470d060f1ee0276120bc746b806ac.tar.bz2
thermoprint-e9c92d233ba470d060f1ee0276120bc746b806ac.tar.xz
thermoprint-e9c92d233ba470d060f1ee0276120bc746b806ac.zip
rate limiting output to generic printer
-rw-r--r--server/src/Thermoprint/Server/Printer/Generic.hs11
1 files changed, 10 insertions, 1 deletions
diff --git a/server/src/Thermoprint/Server/Printer/Generic.hs b/server/src/Thermoprint/Server/Printer/Generic.hs
index 2c88b55..2945dd0 100644
--- a/server/src/Thermoprint/Server/Printer/Generic.hs
+++ b/server/src/Thermoprint/Server/Printer/Generic.hs
@@ -52,6 +52,8 @@ import Data.Monoid
52 52
53import Data.Int (Int64) 53import Data.Int (Int64)
54 54
55import Control.Concurrent (threadDelay)
56
55import Prelude hiding (mapM_, sequence_, lines) 57import Prelude hiding (mapM_, sequence_, lines)
56 58
57genericPrint :: FilePath -> PrinterMethod 59genericPrint :: FilePath -> PrinterMethod
@@ -65,10 +67,17 @@ genericPrint' path = flip catches handlers . withFile path . print
65 , Handler $ return . Just . EncError 67 , Handler $ return . Just . EncError
66 , Handler $ return . Just 68 , Handler $ return . Just
67 ] 69 ]
68 print printout handle = $(logDebug) (T.pack $ show printout') >> liftIO (LBS.hPutStr handle printout' >> return Nothing) 70 print printout handle = $(logDebug) (T.pack $ show printout') >> liftIO (slowPut handle printout' >> return Nothing)
69 where 71 where
70 printout' = runPut $ initialize >> render printout >> finalize 72 printout' = runPut $ initialize >> render printout >> finalize
71 73
74slowPut :: Handle -> Lazy.ByteString -> IO ()
75slowPut h = slowPut' . LBS.split (LBS.last newl)
76 where
77 slowPut' [] = return ()
78 slowPut' [t] = LBS.hPutStr h t
79 slowPut' (x:xs) = slowPut' [x] >> LBS.hPutStr h (LBS.singleton $ LBS.last newl) >> threadDelay (50 * 10^3) >> slowPut' xs
80
72encode' :: ByteSink m => String -> m () 81encode' :: ByteSink m => String -> m ()
73encode' = encode CP437 82encode' = encode CP437
74 83