summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/atox.hs20
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
15import Data.Conduit.Cereal 15import Data.Conduit.Cereal
16import Data.Function 16import Data.Function
17import qualified Data.Map.Strict as Map 17import qualified Data.Map.Strict as Map
18import qualified Data.Sequence as Seq
19import Data.Sequence (Seq(..),(|>))
18import Data.Monoid 20import Data.Monoid
19import qualified Data.Serialize as S 21import qualified Data.Serialize as S
20import Data.Serialize (Get(..), Put(..)) 22import Data.Serialize (Get(..), Put(..))
@@ -54,6 +56,10 @@ sThem = unsafePerformIO $ newTVarIO zero
54sMap :: TVar (Map.Map Key ViewSnapshot) 56sMap :: TVar (Map.Map Key ViewSnapshot)
55sMap = unsafePerformIO $ newTVarIO (Map.empty) 57sMap = unsafePerformIO $ newTVarIO (Map.empty)
56 58
59{-# NOINLINE sScroll #-}
60sScroll :: TVar (Map.Map Key (Seq CryptoMessage))
61sScroll = unsafePerformIO $ newTVarIO (Map.empty)
62
57----------------------- 63-----------------------
58 64
59 65
@@ -82,6 +88,7 @@ pattern IPC = Padding
82data SetCmd = SetME 88data 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
87forkToxInputThread myRead = forkIO $ do 94forkToxInputThread 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
108doit :: Fd -> Fd -> IO () 128doit :: Fd -> Fd -> IO ()
109doit myReadFd myWriteFd = do 129doit myReadFd myWriteFd = do
110 myRead <- fdToHandle myReadFd 130 myRead <- fdToHandle myReadFd