summaryrefslogtreecommitdiff
path: root/KikiD/Message.hs
diff options
context:
space:
mode:
Diffstat (limited to 'KikiD/Message.hs')
-rw-r--r--KikiD/Message.hs35
1 files changed, 25 insertions, 10 deletions
diff --git a/KikiD/Message.hs b/KikiD/Message.hs
index 5b642f3..cd3ee71 100644
--- a/KikiD/Message.hs
+++ b/KikiD/Message.hs
@@ -1,22 +1,37 @@
1{-# LANGUAGE DoAndIfThenElse #-}
1module KikiD.Message where 2module KikiD.Message where
2 3
3import Data.Serialize 4import Data.Serialize as Cereal
4import qualified KikiD.GetLine
5import qualified Data.ByteString.Char8 as B 5import qualified Data.ByteString.Char8 as B
6import Data.Monoid 6import Data.Monoid
7import Text.Read 7import Text.Read
8import Data.Char (ord) 8import Data.Char (ord,chr)
9import Control.Monad 9import Control.Monad
10import Data.Bytes.Serial as R
11import Data.Bytes.Put as Put
12import Data.Bytes.Get as Get
13import Codec.LineReady
14import Control.Monad.Loops
10 15
11data KikiDMessage = TODO deriving (Show,Read) 16data KikiDMessage = TODO deriving (Show,Read)
12 17
13instance Serialize KikiDMessage where 18instance Serialize KikiDMessage where
14 put m = mapM_ (putWord8 . fromIntegral . ord) "TODO" 19 put m = mapM_ (Cereal.putWord8 . fromIntegral . ord) "TO\nO"
15 -- putByteString . B.pack $ show m ++ "\n" 20 -- putByteString . B.pack $ show m ++ "\n"
16 get = do 21 get = do
17 t <- getWord8 22 t <- Cereal.getWord8
18 o <- getWord8 23 o <- Cereal.getWord8
19 d <- getWord8 24 d <- Cereal.getWord8
20 o <- getWord8 25 o <- Cereal.getWord8
21 return TODO 26 let s = map (chr . fromIntegral) [t,o,d,o]
22 27 if "TO\nO" == s
28 then return TODO
29 else fail ("Could not decode message: " ++ show s)
30
31instance Serial KikiDMessage where
32 serialize m = Put.putByteString . toLineReady . Cereal.encode $ m
33 deserialize = do
34 xs <- unfoldWhileM (/= '\n') (fmap (chr . fromIntegral) Get.getWord8)
35 case (Cereal.decode . fromLineReady $ B.pack xs) of
36 Left str -> fail str
37 Right x -> return x