diff options
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r-- | Presence/XMPPServer.hs | 48 |
1 files changed, 19 insertions, 29 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 76c14f52..4f61646f 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -38,7 +38,6 @@ import Text.XML.HaXml.Parse (XParser,xmlParseWith,element,{-doctypedecl,-}proce | |||
38 | import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) | 38 | import Text.XML.HaXml.Types as Hax hiding (Element) -- (ElemTag,QName(..),Namespace(..),Element(..),Content(..),AttValue(..)) |
39 | import qualified Text.XML.HaXml.Types as Hax (Element(..)) | 39 | import qualified Text.XML.HaXml.Types as Hax (Element(..)) |
40 | import Text.XML.HaXml.Posn (Posn, posnLine, posnColumn) | 40 | import Text.XML.HaXml.Posn (Posn, posnLine, posnColumn) |
41 | import Text.XML.HaXml.Lex (TokenT) | ||
42 | import qualified Text.XML.HaXml.Pretty as PP | 41 | import qualified Text.XML.HaXml.Pretty as PP |
43 | import Text.PrettyPrint | 42 | import Text.PrettyPrint |
44 | import Data.Maybe | 43 | import Data.Maybe |
@@ -432,13 +431,25 @@ connect_to_server chan peer = (>> return ()) . runMaybeT $ do | |||
432 | 431 | ||
433 | connected <- liftIO . async $ connect' (unpack peer) port | 432 | connected <- liftIO . async $ connect' (unpack peer) port |
434 | 433 | ||
434 | -- We'll cache Presence notifications until the socket | ||
435 | -- is ready. | ||
436 | cached <- liftIO $ newIORef Map.empty | ||
437 | |||
435 | sock <- MaybeT . fix $ \loop -> do | 438 | sock <- MaybeT . fix $ \loop -> do |
436 | e <- atomically $ orElse | 439 | e <- atomically $ orElse |
437 | (fmap Right $ waitSTM connected) | 440 | (fmap Right $ waitSTM connected) |
438 | (fmap Left $ readTChan chan) | 441 | (fmap Left $ readTChan chan) |
439 | case e of | 442 | case e of |
443 | Left (OutBoundPresence (Presence jid Offline)) -> do | ||
444 | cached_map <- readIORef cached | ||
445 | writeIORef cached (Map.delete jid cached_map) | ||
446 | loop | ||
447 | Left (OutBoundPresence p@(Presence jid st)) -> do | ||
448 | cached_map <- readIORef cached | ||
449 | writeIORef cached (Map.insert jid st cached_map) | ||
450 | loop | ||
440 | Left event -> do | 451 | Left event -> do |
441 | L.putStrLn $ "REMOTE-OUT NOT READY: " <++> bshow event | 452 | L.putStrLn $ "REMOTE-OUT DISCARDED: " <++> bshow event |
442 | loop | 453 | loop |
443 | Right sock -> return sock | 454 | Right sock -> return sock |
444 | 455 | ||
@@ -447,6 +458,12 @@ connect_to_server chan peer = (>> return ()) . runMaybeT $ do | |||
447 | hSetBuffering h NoBuffering | 458 | hSetBuffering h NoBuffering |
448 | hPutStrLn h "<stream>" | 459 | hPutStrLn h "<stream>" |
449 | L.putStrLn $ "REMOTE-OUT: <stream>" | 460 | L.putStrLn $ "REMOTE-OUT: <stream>" |
461 | cache <- fmap Map.assocs . readIORef $ cached | ||
462 | writeIORef cached Map.empty -- hint garbage collector: we're done with this | ||
463 | forM_ cache $ \(jid,st) -> do | ||
464 | let r = xmlifyPresence (Presence jid st) | ||
465 | hPutStrLn h r | ||
466 | L.putStrLn $ "REMOTE-OUT (cache):\n" <++> r <++> "\n" | ||
450 | fix $ \loop -> do | 467 | fix $ \loop -> do |
451 | event <- atomically $ readTChan chan | 468 | event <- atomically $ readTChan chan |
452 | case event of | 469 | case event of |
@@ -459,33 +476,6 @@ connect_to_server chan peer = (>> return ()) . runMaybeT $ do | |||
459 | L.putStrLn $ "REMOTE-OUT: </stream>" | 476 | L.putStrLn $ "REMOTE-OUT: </stream>" |
460 | 477 | ||
461 | 478 | ||
462 | {- | ||
463 | pending <- newTVarIO True | ||
464 | thread <- forkIO $ | ||
465 | runMaybeT $ do | ||
466 | let port = "5269" | ||
467 | sock <- MaybeT $ connect' (unpack peer) port | ||
468 | liftIO $ do | ||
469 | h <- socketToHandle sock ReadWriteMode | ||
470 | hSetBuffering h NoBuffering | ||
471 | hPutStrLn h "<stream>" | ||
472 | atomically $ writeTVar pending False | ||
473 | fix $ \loop -> do | ||
474 | event <- atomically $ readTChan chan | ||
475 | case event of | ||
476 | OutBoundPresence p -> do | ||
477 | let r = xmlifyPresence p | ||
478 | hPutStrLn h r | ||
479 | L.putStrLn $ "REMOTE:\n" <++> r <++> "\n" | ||
480 | loop | ||
481 | hPutStrLn h "</stream>" | ||
482 | fix $ \loop -> do | ||
483 | event <- atomically $ readTChan chan | ||
484 | when (readTVarIO pending) loop | ||
485 | joinThread thread | ||
486 | return () | ||
487 | -} | ||
488 | |||
489 | parseJID :: ByteString -> JID | 479 | parseJID :: ByteString -> JID |
490 | parseJID bjid = | 480 | parseJID bjid = |
491 | let xs = L.splitWith (=='@') bjid | 481 | let xs = L.splitWith (=='@') bjid |