diff options
author | joe <joe@jerkface.net> | 2014-02-17 00:11:40 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-02-17 00:11:40 -0500 |
commit | 0748137553fcb230cf33a6765538238d7dae6066 (patch) | |
tree | 2cb4936245de5b261f58e19d1bf8cb97181a4050 | |
parent | 8bb04df557c3b78ff47ffca7facb06ac7f076110 (diff) |
xmppServer method now returns opague data structure XMPPServer
-rw-r--r-- | Presence/XMPPServer.hs | 20 | ||||
-rw-r--r-- | xmppServer.hs | 13 |
2 files changed, 24 insertions, 9 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 96998198..421648fb 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -3,6 +3,8 @@ module XMPPServer | |||
3 | ( xmppServer | 3 | ( xmppServer |
4 | , ConnectionKey(..) | 4 | , ConnectionKey(..) |
5 | , XMPPServerParameters(..) | 5 | , XMPPServerParameters(..) |
6 | , XMPPServer | ||
7 | , addPeer | ||
6 | , Stanza(..) | 8 | , Stanza(..) |
7 | , StanzaType(..) | 9 | , StanzaType(..) |
8 | , StanzaOrigin(..) | 10 | , StanzaOrigin(..) |
@@ -210,6 +212,7 @@ prettyPrint prefix = | |||
210 | =$= CB.lines | 212 | =$= CB.lines |
211 | =$ CL.mapM_ (wlogb . (prefix <>)) | 213 | =$ CL.mapM_ (wlogb . (prefix <>)) |
212 | 214 | ||
215 | -- id,to, and from are taken as-is from reply list | ||
213 | sendReply donevar stype reply replychan = do | 216 | sendReply donevar stype reply replychan = do |
214 | if null reply then return () | 217 | if null reply then return () |
215 | else do | 218 | else do |
@@ -222,8 +225,8 @@ sendReply donevar stype reply replychan = do | |||
222 | replyClsrs <- newTVar (Just []) | 225 | replyClsrs <- newTVar (Just []) |
223 | return Stanza { stanzaType = stype | 226 | return Stanza { stanzaType = stype |
224 | , stanzaId = mid | 227 | , stanzaId = mid |
225 | , stanzaTo = mto -- todo: should this be reversed? | 228 | , stanzaTo = mto -- as-is from reply list |
226 | , stanzaFrom = mfrom -- todo: should this be reversed? | 229 | , stanzaFrom = mfrom -- as-is from reply list |
227 | , stanzaChan = replyChan | 230 | , stanzaChan = replyChan |
228 | , stanzaClosers = replyClsrs | 231 | , stanzaClosers = replyClsrs |
229 | , stanzaInterrupt = donevar | 232 | , stanzaInterrupt = donevar |
@@ -884,9 +887,18 @@ monitor sv params xmpp = do | |||
884 | where | 887 | where |
885 | _ = str :: String | 888 | _ = str :: String |
886 | 889 | ||
890 | data XMPPServer | ||
891 | = XMPPServer { _xmpp_sv :: Server ConnectionKey SockAddr | ||
892 | , _xmpp_peer_params :: ConnectionParameters ConnectionKey SockAddr | ||
893 | } | ||
894 | |||
895 | addPeer :: XMPPServer -> SockAddr -> IO () | ||
896 | addPeer sv addr = do | ||
897 | control (_xmpp_sv sv) (ConnectWithEndlessRetry addr (_xmpp_peer_params sv) 10000) | ||
898 | |||
887 | xmppServer :: ( MonadResource m | 899 | xmppServer :: ( MonadResource m |
888 | , MonadIO m | 900 | , MonadIO m |
889 | ) => XMPPServerParameters -> m (Server ConnectionKey SockAddr,ConnectionParameters ConnectionKey SockAddr) | 901 | ) => XMPPServerParameters -> m XMPPServer |
890 | xmppServer xmpp = do | 902 | xmppServer xmpp = do |
891 | sv <- server | 903 | sv <- server |
892 | -- some fuzz helps avoid simultaneity | 904 | -- some fuzz helps avoid simultaneity |
@@ -906,4 +918,4 @@ xmppServer xmpp = do | |||
906 | forkIO $ monitor sv peer_params xmpp | 918 | forkIO $ monitor sv peer_params xmpp |
907 | control sv (Listen peerport peer_params) | 919 | control sv (Listen peerport peer_params) |
908 | control sv (Listen clientport client_params) | 920 | control sv (Listen clientport client_params) |
909 | return (sv,peer_params) | 921 | return XMPPServer { _xmpp_sv = sv, _xmpp_peer_params = peer_params } |
diff --git a/xmppServer.hs b/xmppServer.hs index d67b7552..464d3b1d 100644 --- a/xmppServer.hs +++ b/xmppServer.hs | |||
@@ -11,18 +11,21 @@ import Network.Socket | |||
11 | , AddrInfoFlag(AI_CANONNAME) | 11 | , AddrInfoFlag(AI_CANONNAME) |
12 | ) | 12 | ) |
13 | import Data.Monoid ( (<>) ) | 13 | import Data.Monoid ( (<>) ) |
14 | import qualified Data.Text as Text | ||
14 | import qualified Data.Text.IO as Text | 15 | import qualified Data.Text.IO as Text |
15 | import Control.Monad | 16 | import Control.Monad |
17 | import qualified Network.BSD as BSD | ||
16 | 18 | ||
17 | 19 | ||
18 | import XMPPServer | 20 | import XMPPServer |
19 | import Server | 21 | import Server |
20 | 22 | ||
21 | main = runResourceT $ do | 23 | main = runResourceT $ do |
22 | (sv,peer_params) <- xmppServer | 24 | hostname <- fmap Text.pack $ liftIO BSD.getHostName |
25 | sv <- xmppServer | ||
23 | XMPPServerParameters | 26 | XMPPServerParameters |
24 | { xmppChooseResourceName = \k sock desired -> return "nobody@localhost/tty666" | 27 | { xmppChooseResourceName = \k sock desired -> return $ "nobody@" <> hostname <> "/tty666" |
25 | , xmppTellMyNameToClient = return "localhost" | 28 | , xmppTellMyNameToClient = return hostname |
26 | , xmppTellMyNameToPeer = \addr -> return "localhost" | 29 | , xmppTellMyNameToPeer = \addr -> return "localhost" |
27 | , xmppNewConnection = \k outchan -> return () | 30 | , xmppNewConnection = \k outchan -> return () |
28 | , xmppEOF = \k -> return () | 31 | , xmppEOF = \k -> return () |
@@ -32,7 +35,7 @@ main = runResourceT $ do | |||
32 | , xmppRosterOthers = \k -> return [] | 35 | , xmppRosterOthers = \k -> return [] |
33 | , xmppSubscribeToRoster = \k -> return () | 36 | , xmppSubscribeToRoster = \k -> return () |
34 | , xmppLookupPeerName = \k -> return "localhost" | 37 | , xmppLookupPeerName = \k -> return "localhost" |
35 | , xmppLookupClientJID = \k -> return "nobody@localhost/tty666" | 38 | , xmppLookupClientJID = \k -> return $ "nobody@" <> hostname <> "/tty666" |
36 | , xmppDeliverMessage = \fail msg -> do | 39 | , xmppDeliverMessage = \fail msg -> do |
37 | let msgs = msgLangMap (stanzaType msg) | 40 | let msgs = msgLangMap (stanzaType msg) |
38 | body = fmap (maybe "" id . msgBody . snd) $ take 1 msgs | 41 | body = fmap (maybe "" id . msgBody . snd) $ take 1 msgs |
@@ -49,7 +52,7 @@ main = runResourceT $ do | |||
49 | (Just testaddr0) | 52 | (Just testaddr0) |
50 | (Just "5269") | 53 | (Just "5269") |
51 | putStrLn $ "Connecting to "++show testaddr | 54 | putStrLn $ "Connecting to "++show testaddr |
52 | control sv (ConnectWithEndlessRetry testaddr peer_params 10000) | 55 | addPeer sv testaddr |
53 | 56 | ||
54 | quitVar <- newEmptyTMVarIO | 57 | quitVar <- newEmptyTMVarIO |
55 | installHandler sigTERM (CatchOnce (atomically $ putTMVar quitVar True)) Nothing | 58 | installHandler sigTERM (CatchOnce (atomically $ putTMVar quitVar True)) Nothing |