module Main(main) where import Control.Exception import Data.Enumerator (Enumerator, Iteratee, run, ($$)) import Data.Enumerator.Binary (enumHandle) import Data.ByteString (ByteString) import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts) import System.Exit import System.IO (hSetBinaryMode,stdin) import Network.EBus.Layer2 main :: IO () main = do -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.) hSetBinaryMode stdin True loop 900 loop :: Int -> IO() loop 0 = do print "finished" loop n = do readAndDumpPacket loop (n - 1) readAndDumpPacket :: IO() readAndDumpPacket = do -- * run -- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed). -- * run_ -- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases. maybePacket <- run( enumSource $$ runParser ) case maybePacket of Right result -> do { print "Result"; print result} Left exc -> case fromException (exc::SomeException) of Just e | any (\ctx -> ctx == "demandInput") (errorContexts (e::ParseError)) -> do putStrLn "EOF reached" exitWith $ ExitFailure 1 | otherwise -> do putStr "Other Error: "; print e _ -> putStr "" enumSource :: Enumerator ByteString IO a enumSource = enumHandle 1 stdin runParser :: Iteratee ByteString IO EbusPacket runParser = do p <- iterParser ebusParserLayer2 return p