diff options
author | joe <joe@jerkface.net> | 2018-06-20 16:08:42 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-20 17:24:19 -0400 |
commit | c8f7f30885f14b6239fac2a8b3157866494ae775 (patch) | |
tree | 78436d01af46d61515cf380a65460e899ad5ada5 /Presence | |
parent | 9ee162744e9f03f8a3c1ab0e5b3688f9dc534241 (diff) |
Export non-ResourceT interface to xmppServer.
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/Presence.hs | 1 | ||||
-rw-r--r-- | Presence/XMPPServer.hs | 29 |
2 files changed, 17 insertions, 13 deletions
diff --git a/Presence/Presence.hs b/Presence/Presence.hs index adae567a..2f59a52f 100644 --- a/Presence/Presence.hs +++ b/Presence/Presence.hs | |||
@@ -12,7 +12,6 @@ import System.Posix.Signals | |||
12 | import Control.Concurrent (threadDelay,forkIO,forkOS,killThread,throwTo) | 12 | import Control.Concurrent (threadDelay,forkIO,forkOS,killThread,throwTo) |
13 | import Control.Concurrent.STM | 13 | import Control.Concurrent.STM |
14 | import Control.Concurrent.STM.TMVar | 14 | import Control.Concurrent.STM.TMVar |
15 | import Control.Monad.Trans.Resource (runResourceT) | ||
16 | import Control.Monad.Trans | 15 | import Control.Monad.Trans |
17 | import Control.Monad.IO.Class (MonadIO, liftIO) | 16 | import Control.Monad.IO.Class (MonadIO, liftIO) |
18 | import Network.Socket ( SockAddr(..), PortNumber ) | 17 | import Network.Socket ( SockAddr(..), PortNumber ) |
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 774fe886..a1221b6c 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -3,8 +3,10 @@ | |||
3 | {-# LANGUAGE RankNTypes #-} | 3 | {-# LANGUAGE RankNTypes #-} |
4 | {-# LANGUAGE FlexibleInstances #-} -- instance for TChan Event | 4 | {-# LANGUAGE FlexibleInstances #-} -- instance for TChan Event |
5 | {-# LANGUAGE DoAndIfThenElse #-} | 5 | {-# LANGUAGE DoAndIfThenElse #-} |
6 | {-# LANGUAGE ExistentialQuantification #-} | ||
6 | module XMPPServer | 7 | module XMPPServer |
7 | ( xmppServer | 8 | ( xmppServer |
9 | , quitXmpp | ||
8 | , ConnectionKey(..) | 10 | , ConnectionKey(..) |
9 | , XMPPServerParameters(..) | 11 | , XMPPServerParameters(..) |
10 | , XMPPServer | 12 | , XMPPServer |
@@ -47,7 +49,6 @@ import Blaze.ByteString.Builder (Builder) | |||
47 | 49 | ||
48 | import Debug.Trace | 50 | import Debug.Trace |
49 | import System.IO (hFlush,stdout) | 51 | import System.IO (hFlush,stdout) |
50 | import Control.Monad.Trans.Resource | ||
51 | import Control.Monad.Trans (lift) | 52 | import Control.Monad.Trans (lift) |
52 | import Control.Monad.IO.Class (MonadIO, liftIO) | 53 | import Control.Monad.IO.Class (MonadIO, liftIO) |
53 | import Control.Monad.Fix (fix) | 54 | import Control.Monad.Fix (fix) |
@@ -72,6 +73,7 @@ import Data.Conduit | |||
72 | import qualified Data.Conduit.List as CL | 73 | import qualified Data.Conduit.List as CL |
73 | import qualified Data.Conduit.Binary as CB | 74 | import qualified Data.Conduit.Binary as CB |
74 | import Data.Conduit.Blaze (builderToByteStringFlush) | 75 | import Data.Conduit.Blaze (builderToByteStringFlush) |
76 | import Control.Monad.Catch (MonadThrow) | ||
75 | 77 | ||
76 | import qualified Text.XML.Stream.Render as XML hiding (content) | 78 | import qualified Text.XML.Stream.Render as XML hiding (content) |
77 | import qualified Text.XML.Stream.Parse as XML | 79 | import qualified Text.XML.Stream.Parse as XML |
@@ -895,7 +897,7 @@ makePong namespace mid to from = | |||
895 | ] | 897 | ] |
896 | 898 | ||
897 | 899 | ||
898 | xmppInbound :: Server ConnectionKey SockAddr ReleaseKey XML.Event | 900 | xmppInbound :: Server ConnectionKey SockAddr releaseKey XML.Event |
899 | -> XMPPServerParameters | 901 | -> XMPPServerParameters |
900 | -> ConnectionKey | 902 | -> ConnectionKey |
901 | -> SockAddr | 903 | -> SockAddr |
@@ -1243,7 +1245,7 @@ slotsToSource slots nesting lastStanza needsFlush rdone = | |||
1243 | ,readTMVar rdone >> return (return ()) | 1245 | ,readTMVar rdone >> return (return ()) |
1244 | ] | 1246 | ] |
1245 | 1247 | ||
1246 | forkConnection :: Server ConnectionKey SockAddr ReleaseKey XML.Event | 1248 | forkConnection :: Server ConnectionKey SockAddr releaseKey XML.Event |
1247 | -> XMPPServerParameters | 1249 | -> XMPPServerParameters |
1248 | -> ConnectionKey | 1250 | -> ConnectionKey |
1249 | -> SockAddr | 1251 | -> SockAddr |
@@ -1481,7 +1483,7 @@ sendRoster query xmpp clientKey replyto = do | |||
1481 | -} | 1483 | -} |
1482 | 1484 | ||
1483 | 1485 | ||
1484 | socketFromKey :: Server ConnectionKey SockAddr ReleaseKey XML.Event -> ConnectionKey -> IO SockAddr | 1486 | socketFromKey :: Server ConnectionKey SockAddr releaseKey XML.Event -> ConnectionKey -> IO SockAddr |
1485 | socketFromKey sv k = do | 1487 | socketFromKey sv k = do |
1486 | map <- atomically $ readTVar (conmap sv) | 1488 | map <- atomically $ readTVar (conmap sv) |
1487 | let mcd = Map.lookup k map | 1489 | let mcd = Map.lookup k map |
@@ -1651,7 +1653,7 @@ makeErrorStanza stanza = do | |||
1651 | ] | 1653 | ] |
1652 | 1654 | ||
1653 | monitor :: | 1655 | monitor :: |
1654 | Server ConnectionKey SockAddr ReleaseKey XML.Event | 1656 | Server ConnectionKey SockAddr releaseKey XML.Event |
1655 | -> ConnectionParameters ConnectionKey SockAddr | 1657 | -> ConnectionParameters ConnectionKey SockAddr |
1656 | -> XMPPServerParameters | 1658 | -> XMPPServerParameters |
1657 | -> IO b | 1659 | -> IO b |
@@ -1817,7 +1819,8 @@ monitor sv params xmpp = do | |||
1817 | _ = str :: String | 1819 | _ = str :: String |
1818 | 1820 | ||
1819 | data XMPPServer | 1821 | data XMPPServer |
1820 | = XMPPServer { _xmpp_sv :: Server ConnectionKey SockAddr ReleaseKey XML.Event | 1822 | = forall releaseKey. |
1823 | XMPPServer { _xmpp_sv :: Server ConnectionKey SockAddr releaseKey XML.Event | ||
1821 | , _xmpp_peer_params :: ConnectionParameters ConnectionKey SockAddr | 1824 | , _xmpp_peer_params :: ConnectionParameters ConnectionKey SockAddr |
1822 | } | 1825 | } |
1823 | 1826 | ||
@@ -1825,18 +1828,20 @@ grokPeer :: XMPPServer -> ConnectionKey -> (SockAddr, ConnectionParameters Conne | |||
1825 | grokPeer sv (PeerKey addr) = (addr, _xmpp_peer_params sv, 10000) | 1828 | grokPeer sv (PeerKey addr) = (addr, _xmpp_peer_params sv, 10000) |
1826 | 1829 | ||
1827 | xmppConnections :: XMPPServer -> IO (Connection.Manager TCPStatus Text) | 1830 | xmppConnections :: XMPPServer -> IO (Connection.Manager TCPStatus Text) |
1828 | xmppConnections sv = tcpManager (grokPeer sv) (Just . Text.pack) resolvPeer (_xmpp_sv sv) | 1831 | xmppConnections xsv@XMPPServer{_xmpp_sv=sv} = tcpManager (grokPeer xsv) (Just . Text.pack) resolvPeer sv |
1829 | where | 1832 | where |
1830 | resolvPeer :: Text -> IO (Maybe ConnectionKey) | 1833 | resolvPeer :: Text -> IO (Maybe ConnectionKey) |
1831 | resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str | 1834 | resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str |
1832 | 1835 | ||
1833 | xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, SockAddr), ConnectionEvent Event) | 1836 | xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, SockAddr), ConnectionEvent Event) |
1834 | xmppEventChannel sv = serverEvent $ _xmpp_sv sv | 1837 | xmppEventChannel XMPPServer{_xmpp_sv=sv} = serverEvent sv |
1835 | 1838 | ||
1836 | xmppServer :: ( MonadResource m | 1839 | quitXmpp :: XMPPServer -> IO () |
1837 | , MonadIO m | 1840 | quitXmpp XMPPServer{_xmpp_sv=sv} = control sv Quit |
1838 | ) => XMPPServerParameters -> m XMPPServer | 1841 | |
1839 | xmppServer xmpp = do | 1842 | xmppServer :: MonadIO m => |
1843 | Allocate releaseKey m -> XMPPServerParameters -> m XMPPServer | ||
1844 | xmppServer allocate xmpp = do | ||
1840 | sv <- server allocate xmlStream | 1845 | sv <- server allocate xmlStream |
1841 | -- some fuzz helps avoid simultaneity | 1846 | -- some fuzz helps avoid simultaneity |
1842 | pingfuzz <- liftIO $ do | 1847 | pingfuzz <- liftIO $ do |