summaryrefslogtreecommitdiff
path: root/exec/ebus-dump-layer2.hs
blob: c8fd1b5a0d64ba21e5a445f16d5b571737f540a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Main(main) where
  
import Network.EBus.Layer2
import Data.Enumerator (Enumerator, Iteratee, run, ($$))
import Data.Enumerator.Binary (enumHandle)
import System.IO (hSetBinaryMode,stdin)
import Data.ByteString (ByteString)
import Data.Attoparsec.Enumerator (iterParser)

main = do
  -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.) 
  hSetBinaryMode stdin True  
  -- * 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. 
  
  loop 100

loop :: Int -> IO()
loop 0 = do
  print "finished"
loop n = do
  readAndDumpPacket
  loop (n - 1)

readAndDumpPacket :: IO()
readAndDumpPacket = do
  maybePacket <- run( enumSource $$ runParser )
  case maybePacket of  
    Right result -> print result
    Left error -> print error
    

enumSource :: Enumerator ByteString IO a
enumSource = enumHandle 1 stdin

runParser :: Iteratee ByteString IO EbusPacket
runParser = do
  p <- iterParser ebusParserLayer2
  return p