Skip to content

Commit

Permalink
Now sending an MsgError in case of error.
Browse files Browse the repository at this point in the history
  • Loading branch information
baldo committed Oct 12, 2010
1 parent b2f61f9 commit 58aa748
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
6 changes: 1 addition & 5 deletions demo/Message.hs
@@ -1,20 +1,16 @@
module Message
( Message (..)
, Event (..)
, Command (..)
)
where

import Control.Concurrent.Mailbox.Wrapper

data Message = MsgEvent Event
data Message = MsgError String
| MsgCommand Command
| M Int
deriving (Read, Show)

data Event = EvKey Char
deriving (Read, Show)

data Command = CmdQuit
deriving (Read, Show)

Expand Down
18 changes: 10 additions & 8 deletions demo/TestClient.hs
Expand Up @@ -15,9 +15,9 @@ main = do
hFlush hdl

inBox <- wrapReadHandle hdl
(\inBox e -> inBox <! (error $ "Handled: " ++ show e))
(\ inBox e -> inBox <! (MsgError $ show e))
outBox <- wrapWriteHandle hdl
(\_ e -> inBox <! (error $ "Handled: " ++ show e))
(\ _ e -> inBox <! (MsgError $ show e))

loop inBox outBox 1
mapM close [inBox, outBox]
Expand All @@ -29,11 +29,13 @@ loop inBox outBox n = do
outBox <! M n

receive inBox
[ \(M 10) -> handler $ do
putStrLn "received 10"
loop inBox outBox (n + 1)
, \m -> handler $ do
print m
loop inBox outBox (n + 1)
[ \ (MsgError e) -> handler $
putStrLn $ "received error: " ++ e
, \ (M 10) -> handler $ do
putStrLn "received 10"
loop inBox outBox (n + 1)
, \ m -> handler $ do
print m
loop inBox outBox (n + 1)
]

18 changes: 11 additions & 7 deletions demo/TestServer.hs
Expand Up @@ -13,9 +13,9 @@ main = do
(hdl, _, _) <- accept sock

inBox <- wrapReadHandle hdl
(\inBox e -> inBox <! (error $ "Handled: " ++ show e))
(\ inBox e -> inBox <! (MsgError $ show e))
outBox <- wrapWriteHandle hdl
(\_ e -> inBox <! (error $ "Handled: " ++ show e))
(\ _ e -> inBox <! (MsgError $ show e))

loop inBox outBox
mapM close [inBox, outBox]
Expand All @@ -24,13 +24,17 @@ main = do
loop :: MailboxClass mb => mb Message -> mb Message -> IO ()
loop inBox outBox = do
receiveNonBlocking inBox
[ \ (MsgCommand CmdQuit) -> handler $ return ()
[ \ (MsgError e) -> handler $
putStrLn $ "received error: " ++ e
, \ (MsgCommand CmdQuit) -> handler $ return ()
, \ m -> handler $ do
putStrLn $ "Matched " ++ show m ++ " non-blocking."
outBox <! M (-1)
loop inBox outBox
putStrLn $ "Matched " ++ show m ++ " non-blocking."
outBox <! M (-1)
loop inBox outBox
] $ receiveTimeout inBox 1000
[ \ (m@(M (n + 1))) -> handler $ do
[ \ (MsgError e) -> handler $
putStrLn $ "received error: " ++ e
, \ (m@(M (n + 1))) -> handler $ do
putStrLn $ "Matched " ++ show m ++ " within timeout."
outBox <! M (n * 2)
loop inBox outBox
Expand Down

0 comments on commit 58aa748

Please sign in to comment.