Browse Source

handle StreamError in withConnection

master
Philipp Balzarek 14 years ago
parent
commit
ee4d62d7c7
  1. 15
      src/Network/XMPP/Concurrent/Monad.hs
  2. 1
      src/Network/XMPP/Concurrent/Threads.hs

15
src/Network/XMPP/Concurrent/Monad.hs

@ -153,7 +153,7 @@ waitForPresence f = do
-- and resumed with the new session details once the action returns. -- and resumed with the new session details once the action returns.
-- The Action will run in the calling thread/ -- The Action will run in the calling thread/
-- Any uncaught exceptions will be interpreted as connection failure -- Any uncaught exceptions will be interpreted as connection failure
withConnection :: XMPPConMonad a -> XMPP a withConnection :: XMPPConMonad a -> XMPP (Either StreamError a)
withConnection a = do withConnection a = do
readerId <- asks readerThread readerId <- asks readerThread
stateRef <- asks conStateRef stateRef <- asks conStateRef
@ -171,17 +171,18 @@ withConnection a = do
>> Ex.throwIO (e :: Ex.SomeException) >> Ex.throwIO (e :: Ex.SomeException)
-- No MVar taken -- No MVar taken
) )
Ex.catch ( do Ex.catches ( do
(res, s') <- runStateT a s (res, s') <- runStateT a s
atomically $ do atomically $ do
putTMVar write (sConPushBS s') putTMVar write (sConPushBS s')
putTMVar stateRef s' putTMVar stateRef s'
return res return $ Right res
) )
-- we treat all Exceptions as fatal -- we treat all Exceptions as fatal
(\e -> runStateT xmppKillConnection s [ Ex.Handler $ \e -> return $ Left (e :: StreamError)
, Ex.Handler $ \e -> runStateT xmppKillConnection s
>> Ex.throwIO (e :: Ex.SomeException) >> Ex.throwIO (e :: Ex.SomeException)
) ]
-- | Send a presence Stanza -- | Send a presence Stanza
sendPresence :: Presence -> XMPP () sendPresence :: Presence -> XMPP ()
@ -216,11 +217,11 @@ runHandler h = do
-- | End the current xmpp session -- | End the current xmpp session
endSession :: XMPP () endSession :: XMPP ()
endSession = do -- TODO: This has to be idempotent (is it?) endSession = do -- TODO: This has to be idempotent (is it?)
withConnection xmppKillConnection void $ withConnection xmppKillConnection
liftIO =<< asks stopThreads liftIO =<< asks stopThreads
runHandler sessionEndHandler runHandler sessionEndHandler
-- | Close the connection to the server -- | Close the connection to the server
closeConnection :: XMPP () closeConnection :: XMPP ()
closeConnection = withConnection xmppKillConnection closeConnection = void $ withConnection xmppKillConnection

1
src/Network/XMPP/Concurrent/Threads.hs

@ -166,7 +166,6 @@ startThreads = do
where where
killConnection writeLock threads = liftIO $ do killConnection writeLock threads = liftIO $ do
_ <- atomically $ takeTMVar writeLock -- Should we put it back? _ <- atomically $ takeTMVar writeLock -- Should we put it back?
liftIO $ putStrLn "killing threads #"
_ <- forM threads killThread _ <- forM threads killThread
return() return()

Loading…
Cancel
Save