Browse Source

correction of last patch: actually closed the stream following the stream errors

provided a single threaded counterpart for closeConnection
master
Jon Kristensen 14 years ago
parent
commit
581f088aee
  1. 5
      source/Network/Xmpp.hs
  2. 25
      source/Network/Xmpp/Monad.hs

5
source/Network/Xmpp.hs

@ -179,23 +179,28 @@ connect address hostname = do
Left (StreamNotStreamElement _name) -> do Left (StreamNotStreamElement _name) -> do
_ <- pushElement $ pickleElem xpStreamError $ _ <- pushElement $ pickleElem xpStreamError $
XmppStreamError StreamInvalidXml Nothing Nothing XmppStreamError StreamInvalidXml Nothing Nothing
xmppCloseStreams
return () return ()
Left (StreamInvalidStreamNamespace _ns) -> do Left (StreamInvalidStreamNamespace _ns) -> do
_ <- pushElement $ pickleElem xpStreamError $ _ <- pushElement $ pickleElem xpStreamError $
XmppStreamError StreamInvalidNamespace Nothing Nothing XmppStreamError StreamInvalidNamespace Nothing Nothing
xmppCloseStreams
return () return ()
Left (StreamInvalidStreamPrefix _prefix) -> do Left (StreamInvalidStreamPrefix _prefix) -> do
_ <- pushElement $ pickleElem xpStreamError $ _ <- pushElement $ pickleElem xpStreamError $
XmppStreamError StreamBadNamespacePrefix Nothing Nothing XmppStreamError StreamBadNamespacePrefix Nothing Nothing
xmppCloseStreams
return () return ()
-- TODO: Catch remaining xmppStartStream errors. -- TODO: Catch remaining xmppStartStream errors.
Left (StreamWrongVersion _ver) -> do Left (StreamWrongVersion _ver) -> do
_ <- pushElement $ pickleElem xpStreamError $ _ <- pushElement $ pickleElem xpStreamError $
XmppStreamError StreamUnsupportedVersion Nothing Nothing XmppStreamError StreamUnsupportedVersion Nothing Nothing
xmppCloseStreams
return () return ()
Left (StreamWrongLangTag _lang) -> do Left (StreamWrongLangTag _lang) -> do
_ <- pushElement $ pickleElem xpStreamError $ _ <- pushElement $ pickleElem xpStreamError $
XmppStreamError StreamInvalidXml Nothing Nothing XmppStreamError StreamInvalidXml Nothing Nothing
xmppCloseStreams
return () return ()
Right () -> Right () ->
return () return ()

25
source/Network/Xmpp/Monad.hs

@ -4,6 +4,7 @@
module Network.Xmpp.Monad where module Network.Xmpp.Monad where
import Control.Applicative((<$>)) import Control.Applicative((<$>))
import Control.Concurrent (forkIO, threadDelay)
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Trans.Class import Control.Monad.Trans.Class
@ -185,3 +186,27 @@ xmppSendIQ' iqID to tp lang body = do
("In xmppSendIQ' IDs don't match: " ++ show iqID ++ " /= " ++ ("In xmppSendIQ' IDs don't match: " ++ show iqID ++ " /= " ++
show (iqResultID iq') ++ " .") show (iqResultID iq') ++ " .")
return $ Right iq' return $ Right iq'
-- | Send "</stream:stream>" and wait for the server to finish processing and to
-- close the connection. Any remaining elements from the server and whether or
-- not we received a </stream:stream> element from the server is returned.
xmppCloseStreams :: XmppConMonad ([Element], Bool)
xmppCloseStreams = do
send <- gets sConPushBS
cc <- gets sCloseConnection
liftIO $ send "</stream:stream>"
void $ liftIO $ forkIO $ do
threadDelay 3000000
(Ex.try cc) :: IO (Either Ex.SomeException ())
return ()
collectElems []
where
-- Pulls elements from the stream until the stream ends, or an error is
-- raised.
collectElems :: [Element] -> XmppConMonad ([Element], Bool)
collectElems elems = do
result <- Ex.try pullElement
case result of
Left StreamStreamEnd -> return (elems, True)
Left _ -> return (elems, False)
Right elem -> collectElems (elem:elems)
Loading…
Cancel
Save