diff options
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r-- | Presence/XMPPServer.hs | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 02c33635..24cfd055 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -65,7 +65,7 @@ import Control.Monad.IO.Class (MonadIO, liftIO) | |||
65 | import Control.Monad.Fix (fix) | 65 | import Control.Monad.Fix (fix) |
66 | import Control.Monad | 66 | import Control.Monad |
67 | #ifdef THREAD_DEBUG | 67 | #ifdef THREAD_DEBUG |
68 | import Control.Concurrent.Lifted.Instrument (forkIO,myThreadId,labelThread,ThreadId) | 68 | import Control.Concurrent.Lifted.Instrument (forkIO,myThreadId,labelThread,ThreadId,MVar,putMVar,takeMVar,newMVar) |
69 | #else | 69 | #else |
70 | import Control.Concurrent.Lifted (forkIO,myThreadId,ThreadId) | 70 | import Control.Concurrent.Lifted (forkIO,myThreadId,ThreadId) |
71 | import GHC.Conc (labelThread) | 71 | import GHC.Conc (labelThread) |
@@ -1258,8 +1258,9 @@ forkConnection :: Server PeerAddress ConnectionData releaseKey XML.Event | |||
1258 | -> Source IO XML.Event | 1258 | -> Source IO XML.Event |
1259 | -> Sink (Flush XML.Event) IO () | 1259 | -> Sink (Flush XML.Event) IO () |
1260 | -> TChan Stanza | 1260 | -> TChan Stanza |
1261 | -> MVar () | ||
1261 | -> IO (TChan Stanza) | 1262 | -> IO (TChan Stanza) |
1262 | forkConnection sv xmpp saddr cdta pingflag src snk stanzas = do | 1263 | forkConnection sv xmpp saddr cdta pingflag src snk stanzas pp_mvar = do |
1263 | let auxAddr = cdAddr cdta | 1264 | let auxAddr = cdAddr cdta |
1264 | clientOrServer@(namespace,tellmyname,telltheirname,_) = case auxAddr of | 1265 | clientOrServer@(namespace,tellmyname,telltheirname,_) = case auxAddr of |
1265 | Right _ -> ("jabber:client", xmppTellMyNameToClient xmpp (ClientAddress $ peerAddress saddr) | 1266 | Right _ -> ("jabber:client", xmppTellMyNameToClient xmpp (ClientAddress $ peerAddress saddr) |
@@ -1285,8 +1286,9 @@ forkConnection sv xmpp saddr cdta pingflag src snk stanzas = do | |||
1285 | CL.sourceList (greet' namespace me) =$= CL.map Chunk | 1286 | CL.sourceList (greet' namespace me) =$= CL.map Chunk |
1286 | yield Flush | 1287 | yield Flush |
1287 | slot_src = slotsToSource slots nesting lastStanza needsFlush rdone | 1288 | slot_src = slotsToSource slots nesting lastStanza needsFlush rdone |
1289 | -- client.PeerAddress {peerAddress = [::1]:5222} | ||
1288 | let lbl n = concat [ n | 1290 | let lbl n = concat [ n |
1289 | , Text.unpack (Text.drop 7 namespace) | 1291 | , Text.unpack (Text.drop 7 namespace) -- "client" or "server" |
1290 | , "." | 1292 | , "." |
1291 | , case cdProfile cdta of | 1293 | , case cdProfile cdta of |
1292 | _ | Right _ <- cdAddr cdta -> show saddr | 1294 | _ | Right _ <- cdAddr cdta -> show saddr |
@@ -1341,11 +1343,13 @@ forkConnection sv xmpp saddr cdta pingflag src snk stanzas = do | |||
1341 | fix $ \loop -> do | 1343 | fix $ \loop -> do |
1342 | what <- atomically $ foldr1 orElse | 1344 | what <- atomically $ foldr1 orElse |
1343 | [readTChan output >>= \stanza -> return $ do | 1345 | [readTChan output >>= \stanza -> return $ do |
1346 | wantStanzas <- getVerbose XJabber | ||
1344 | let notping f | 1347 | let notping f |
1345 | | (verbosity==1) = case stanzaType stanza of Pong -> return () | 1348 | | not wantStanzas = return () |
1346 | _ -> f | 1349 | | (verbosity==1) = case stanzaType stanza of Pong -> return () |
1347 | | (verbosity>=2) = f | 1350 | _ -> f |
1348 | | otherwise = return () | 1351 | | (verbosity>=2) = f |
1352 | | otherwise = return () | ||
1349 | -- isempty <- atomically $ isEmptyTChan (stanzaChan stanza) | 1353 | -- isempty <- atomically $ isEmptyTChan (stanzaChan stanza) |
1350 | -- kwlog $ "queuing: "++show (isempty, stanzaId stanza) | 1354 | -- kwlog $ "queuing: "++show (isempty, stanzaId stanza) |
1351 | notping $ do | 1355 | notping $ do |
@@ -1355,7 +1359,9 @@ forkConnection sv xmpp saddr cdta pingflag src snk stanzas = do | |||
1355 | Right _ -> "C" | 1359 | Right _ -> "C" |
1356 | Left _ -> "P" | 1360 | Left _ -> "P" |
1357 | wlog "" | 1361 | wlog "" |
1362 | liftIO $ takeMVar pp_mvar | ||
1358 | stanzaToConduit dup $$ prettyPrint typ | 1363 | stanzaToConduit dup $$ prettyPrint typ |
1364 | liftIO $ putMVar pp_mvar () | ||
1359 | -- wlog $ "hacks: "++show (stanzaId stanza) | 1365 | -- wlog $ "hacks: "++show (stanzaId stanza) |
1360 | case stanzaType stanza of | 1366 | case stanzaType stanza of |
1361 | InternalEnableHack hack -> do | 1367 | InternalEnableHack hack -> do |
@@ -1441,6 +1447,7 @@ peerKey bind_addr sock = do | |||
1441 | -- to distinguish fake address connection keys. | 1447 | -- to distinguish fake address connection keys. |
1442 | return p | 1448 | return p |
1443 | rname <- atomically $ newTVar Nothing | 1449 | rname <- atomically $ newTVar Nothing |
1450 | -- dput XMan $ "peerKey " ++ show (PeerAddress $ raddr `withPort` peerport,laddr) | ||
1444 | return $ ( PeerAddress $ raddr `withPort` peerport | 1451 | return $ ( PeerAddress $ raddr `withPort` peerport |
1445 | , ConnectionData { cdAddr = Left (Local laddr) | 1452 | , ConnectionData { cdAddr = Left (Local laddr) |
1446 | , cdType = XMPP | 1453 | , cdType = XMPP |
@@ -1449,16 +1456,17 @@ peerKey bind_addr sock = do | |||
1449 | 1456 | ||
1450 | clientKey :: SocketLike sock => sock -> IO (PeerAddress,ConnectionData) | 1457 | clientKey :: SocketLike sock => sock -> IO (PeerAddress,ConnectionData) |
1451 | clientKey sock = do | 1458 | clientKey sock = do |
1452 | laddr <- getSocketName sock | 1459 | laddr <- getSocketName sock -- [::1]:5222 bind address, same for all clients |
1453 | raddr <- getPeerName sock | 1460 | raddr <- getPeerName sock -- [::1]:????? unique key |
1454 | when (Just 0 == sockAddrPort laddr) $ do | 1461 | when (Just 0 == sockAddrPort raddr) $ do |
1455 | dput XMan $ unwords [ "BUG: XMPP Client" | 1462 | dput XMan $ unwords [ "BUG: XMPP Client" |
1456 | , show (laddr,raddr) | 1463 | , show (laddr,raddr) |
1457 | , "is using port zero. This could interfere" | 1464 | , "is using port zero. This could interfere" |
1458 | , "with Tox peer sessions." ] | 1465 | , "with Tox peer sessions." ] |
1459 | rname <- atomically $ newTVar Nothing | 1466 | rname <- atomically $ newTVar Nothing |
1460 | return $ ( PeerAddress laddr | 1467 | -- dput XMan $ "clientKey " ++ show (PeerAddress laddr,raddr) |
1461 | , ConnectionData { cdAddr = Right (Remote raddr) | 1468 | return $ ( PeerAddress raddr -- Actually a ClientAddress, but _xmpp_sv conkey type is PeerAddress. |
1469 | , ConnectionData { cdAddr = Right (Remote raddr) -- FIXME: This is a bad way to detect client/peer. | ||
1462 | , cdType = XMPP | 1470 | , cdType = XMPP |
1463 | , cdProfile = "." | 1471 | , cdProfile = "." |
1464 | , cdRemoteName = rname } ) | 1472 | , cdRemoteName = rname } ) |
@@ -1705,13 +1713,14 @@ monitor sv params xmpp = do | |||
1705 | chan <- return $ serverEvent sv | 1713 | chan <- return $ serverEvent sv |
1706 | stanzas <- atomically newTChan | 1714 | stanzas <- atomically newTChan |
1707 | quitVar <- atomically newEmptyTMVar | 1715 | quitVar <- atomically newEmptyTMVar |
1716 | pp_mvar <- newMVar () -- Lock for synchronous pretty-printing of stanzas in log. | ||
1708 | fix $ \loop -> do | 1717 | fix $ \loop -> do |
1709 | action <- atomically $ foldr1 orElse | 1718 | action <- atomically $ foldr1 orElse |
1710 | [ readTChan chan >>= \((addr,u),e) -> return $ do | 1719 | [ readTChan chan >>= \((addr,u),e) -> return $ do |
1711 | case e of | 1720 | case e of |
1712 | Connection pingflag xsrc xsnk | 1721 | Connection pingflag xsrc xsnk |
1713 | -> do wlog $ tomsg addr "Connection" | 1722 | -> do wlog $ tomsg addr "Connection" |
1714 | outs <- forkConnection sv xmpp addr u pingflag xsrc xsnk stanzas | 1723 | outs <- forkConnection sv xmpp addr u pingflag xsrc xsnk stanzas pp_mvar |
1715 | -- /addr/ may be a peer or a client. So we'll strip off | 1724 | -- /addr/ may be a peer or a client. So we'll strip off |
1716 | -- the PeerAddress constructor before exposing it. | 1725 | -- the PeerAddress constructor before exposing it. |
1717 | xmppNewConnection xmpp (peerAddress addr) u outs | 1726 | xmppNewConnection xmpp (peerAddress addr) u outs |
@@ -1848,11 +1857,13 @@ monitor sv params xmpp = do | |||
1848 | PeerOrigin _ replyto -> deliver replyto | 1857 | PeerOrigin _ replyto -> deliver replyto |
1849 | _ -> return () | 1858 | _ -> return () |
1850 | -- We need to clone in the case the stanza is passed on as for Message. | 1859 | -- We need to clone in the case the stanza is passed on as for Message. |
1860 | wantStanzas <- getVerbose XJabber | ||
1851 | verbosity <- xmppVerbosity xmpp | 1861 | verbosity <- xmppVerbosity xmpp |
1852 | let notping f | (verbosity==1) = case stanzaType stanza of Pong -> return () | 1862 | let notping f | not wantStanzas = return () |
1853 | _ -> f | 1863 | | (verbosity==1) = case stanzaType stanza of Pong -> return () |
1854 | | (verbosity>=2) = f | 1864 | _ -> f |
1855 | | otherwise = return () | 1865 | | (verbosity>=2) = f |
1866 | | otherwise = return () | ||
1856 | notping $ do | 1867 | notping $ do |
1857 | let typ = Strict8.pack $ c ++ "->"++(concat . take 1 . words $ show (stanzaType stanza))++" " | 1868 | let typ = Strict8.pack $ c ++ "->"++(concat . take 1 . words $ show (stanzaType stanza))++" " |
1858 | c = case stanzaOrigin stanza of | 1869 | c = case stanzaOrigin stanza of |
@@ -1860,8 +1871,9 @@ monitor sv params xmpp = do | |||
1860 | ClientOrigin {} -> "C" | 1871 | ClientOrigin {} -> "C" |
1861 | PeerOrigin {} -> "P" | 1872 | PeerOrigin {} -> "P" |
1862 | wlog "" | 1873 | wlog "" |
1874 | liftIO $ takeMVar pp_mvar | ||
1863 | stanzaToConduit dup $$ prettyPrint typ | 1875 | stanzaToConduit dup $$ prettyPrint typ |
1864 | 1876 | liftIO $ putMVar pp_mvar () | |
1865 | ] | 1877 | ] |
1866 | action | 1878 | action |
1867 | loop | 1879 | loop |
@@ -1875,7 +1887,7 @@ data ConnectionType = XMPP | Tox | |||
1875 | 1887 | ||
1876 | data ConnectionData = ConnectionData | 1888 | data ConnectionData = ConnectionData |
1877 | { cdAddr :: Either (Local SockAddr) -- Peer connection local address | 1889 | { cdAddr :: Either (Local SockAddr) -- Peer connection local address |
1878 | (Remote SockAddr) -- Client connection remote address | 1890 | (Remote SockAddr) -- unused, todo:remove. (was client connection remote address). |
1879 | , cdType :: ConnectionType | 1891 | , cdType :: ConnectionType |
1880 | , cdProfile :: Text -- Currently ignored for clients. Instead, see | 1892 | , cdProfile :: Text -- Currently ignored for clients. Instead, see |
1881 | -- 'clientProfile' field of 'ClientState'. | 1893 | -- 'clientProfile' field of 'ClientState'. |