diff options
-rw-r--r-- | examples/atox.hs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/examples/atox.hs b/examples/atox.hs index 561e85f9..7845d911 100644 --- a/examples/atox.hs +++ b/examples/atox.hs | |||
@@ -15,6 +15,8 @@ import qualified Data.Conduit.Binary as Conduit | |||
15 | import Data.Conduit.Cereal | 15 | import Data.Conduit.Cereal |
16 | import Data.Function | 16 | import Data.Function |
17 | import qualified Data.Map.Strict as Map | 17 | import qualified Data.Map.Strict as Map |
18 | import qualified Data.Sequence as Seq | ||
19 | import Data.Sequence (Seq(..),(|>)) | ||
18 | import Data.Monoid | 20 | import Data.Monoid |
19 | import qualified Data.Serialize as S | 21 | import qualified Data.Serialize as S |
20 | import Data.Serialize (Get(..), Put(..)) | 22 | import Data.Serialize (Get(..), Put(..)) |
@@ -54,6 +56,10 @@ sThem = unsafePerformIO $ newTVarIO zero | |||
54 | sMap :: TVar (Map.Map Key ViewSnapshot) | 56 | sMap :: TVar (Map.Map Key ViewSnapshot) |
55 | sMap = unsafePerformIO $ newTVarIO (Map.empty) | 57 | sMap = unsafePerformIO $ newTVarIO (Map.empty) |
56 | 58 | ||
59 | {-# NOINLINE sScroll #-} | ||
60 | sScroll :: TVar (Map.Map Key (Seq CryptoMessage)) | ||
61 | sScroll = unsafePerformIO $ newTVarIO (Map.empty) | ||
62 | |||
57 | ----------------------- | 63 | ----------------------- |
58 | 64 | ||
59 | 65 | ||
@@ -82,6 +88,7 @@ pattern IPC = Padding | |||
82 | data SetCmd = SetME | 88 | data SetCmd = SetME |
83 | | SetTHEM | 89 | | SetTHEM |
84 | | SetView | 90 | | SetView |
91 | | AppendMsg | ||
85 | deriving (Eq,Bounded,Ord,Enum,Show) | 92 | deriving (Eq,Bounded,Ord,Enum,Show) |
86 | 93 | ||
87 | forkToxInputThread myRead = forkIO $ do | 94 | forkToxInputThread myRead = forkIO $ do |
@@ -105,6 +112,19 @@ forkToxInputThread myRead = forkIO $ do | |||
105 | let key = Key me them | 112 | let key = Key me them |
106 | modifyTVar' sMap (Map.insert key view) | 113 | modifyTVar' sMap (Map.insert key view) |
107 | 114 | ||
115 | updateState AppendMsg arg | ||
116 | = case S.decode arg of | ||
117 | Left str -> puts (packUtf8 str) | ||
118 | Right msg -> liftIO . atomically $ do | ||
119 | me <- readTVar sMe | ||
120 | them <- readTVar sThem | ||
121 | let key = Key me them | ||
122 | scroll <- readTVar sScroll | ||
123 | let mbCurrentMsgs = Map.lookup key scroll | ||
124 | case mbCurrentMsgs of | ||
125 | Nothing -> modifyTVar' sScroll (Map.insert key (Seq.singleton msg)) | ||
126 | Just history -> modifyTVar' sScroll (Map.insert key (history |> msg)) | ||
127 | |||
108 | doit :: Fd -> Fd -> IO () | 128 | doit :: Fd -> Fd -> IO () |
109 | doit myReadFd myWriteFd = do | 129 | doit myReadFd myWriteFd = do |
110 | myRead <- fdToHandle myReadFd | 130 | myRead <- fdToHandle myReadFd |