summaryrefslogtreecommitdiff
path: root/xmppServer.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-02-12 02:00:47 -0500
committerjoe <joe@jerkface.net>2014-02-12 02:00:47 -0500
commit5e3db9b189fa518f2f103ad15c0a3c18d348ec82 (patch)
tree300412f829687dc207e70728598648444e858845 /xmppServer.hs
parentc8f8f643d5e4ea35eba36e86c3055700e61cbb1e (diff)
Slightly less picky handshake for peer (nextElement rather than withXML)
Diffstat (limited to 'xmppServer.hs')
-rw-r--r--xmppServer.hs40
1 files changed, 25 insertions, 15 deletions
diff --git a/xmppServer.hs b/xmppServer.hs
index cbd30bda..a6cf2a2b 100644
--- a/xmppServer.hs
+++ b/xmppServer.hs
@@ -87,24 +87,19 @@ xmppInbound :: ConnectionKey -> FlagCommand
87xmppInbound k pingflag src stanzas = doNestingXML $ do 87xmppInbound k pingflag src stanzas = doNestingXML $ do
88 withXML $ \begindoc -> do 88 withXML $ \begindoc -> do
89 when (begindoc==EventBeginDocument) $ do 89 when (begindoc==EventBeginDocument) $ do
90 -- liftIO . wlog $ "begin-doc" 90 whenJust nextElement $ \xml -> do
91 withXML $ \xml -> do
92 withJust (elementAttrs "stream" xml) $ \stream_attrs -> do 91 withJust (elementAttrs "stream" xml) $ \stream_attrs -> do
93 -- liftIO . wlog $ "stream: " ++ show (getStreamName xml)
94 -- liftIO . wlog $ "stream atributes: " ++ show stream_attrs
95 fix $ \loop -> do 92 fix $ \loop -> do
96 -- liftIO . wlog $ "waiting for stanza." 93 -- liftIO . wlog $ "waiting for stanza."
97 chan <- liftIO $ atomically newTChan 94 chan <- liftIO $ atomically newTChan
98 whenJust nextElement $ \stanza -> do 95 whenJust nextElement $ \stanza -> do
99 stanza_lvl <- nesting 96 stanza_lvl <- nesting
100 liftIO . atomically $ writeTChan chan (Just stanza) 97 liftIO . atomically $ writeTChan chan (Just stanza)
101 -- liftIO . wlog $ "stanza: "++show stanza
102 98
103 liftIO . atomically $ writeTChan stanzas $ 99 liftIO . atomically $ writeTChan stanzas $
104 UnrecognizedStanza chan 100 UnrecognizedStanza chan
105 doUntilCloser stanza_lvl $ \xml -> do 101 doUntilCloser stanza_lvl $ \xml -> do
106 liftIO . atomically $ writeTChan chan (Just xml) 102 liftIO . atomically $ writeTChan chan (Just xml)
107 -- liftIO . wlog $ "-stanza: " ++ show xml
108 liftIO . atomically $ writeTChan chan Nothing 103 liftIO . atomically $ writeTChan chan Nothing
109 loop 104 loop
110 105
@@ -122,6 +117,15 @@ chanContents ch = do
122 return (x:xs)) 117 return (x:xs))
123 x 118 x
124 119
120readUntilNothing :: TChan (Maybe x) -> IO [x]
121readUntilNothing ch = do
122 x <- atomically $ readTChan ch
123 maybe (return [])
124 (\x -> do
125 xs <- readUntilNothing ch
126 return (x:xs))
127 x
128
125 129
126greetPeer = 130greetPeer =
127 [ EventBeginDocument 131 [ EventBeginDocument
@@ -136,6 +140,9 @@ goodbyePeer =
136 , EventEndDocument 140 , EventEndDocument
137 ] 141 ]
138 142
143data XMPPState
144 = PingSlot
145 deriving (Eq,Ord)
139 146
140 147
141forkConnection :: ConnectionKey 148forkConnection :: ConnectionKey
@@ -152,11 +159,12 @@ forkConnection k pingflag src snk stanzas = do
152 atomically $ putTMVar rdone () 159 atomically $ putTMVar rdone ()
153 wlog $ "end reader fork: " ++ show k 160 wlog $ "end reader fork: " ++ show k
154 slots <- atomically $ Slotted.new isEventBeginElement isEventEndElement 161 slots <- atomically $ Slotted.new isEventBeginElement isEventEndElement
155 let _ = slots :: Slotted.UpdateStream () XML.Event 162 needsFlush <- atomically $ newTVar False
156 let slot_src = do 163 let _ = slots :: Slotted.UpdateStream XMPPState XML.Event
164 let greet_src = do
157 CL.sourceList greetPeer =$= CL.map Chunk 165 CL.sourceList greetPeer =$= CL.map Chunk
158 yield Flush 166 yield Flush
159 needsFlush <- lift . atomically $ newTVar False 167 slot_src = do
160 what <- lift . atomically $ foldr1 orElse 168 what <- lift . atomically $ foldr1 orElse
161 [Slotted.pull slots >>= \x -> do 169 [Slotted.pull slots >>= \x -> do
162 writeTVar needsFlush True 170 writeTVar needsFlush True
@@ -172,7 +180,7 @@ forkConnection k pingflag src snk stanzas = do
172 ,readTMVar rdone >> return (return ()) 180 ,readTMVar rdone >> return (return ())
173 ] 181 ]
174 what 182 what
175 forkIO $ do slot_src $$ snk 183 forkIO $ do (greet_src >> slot_src) $$ snk
176 wlog $ "end post-queue fork: " ++ show k 184 wlog $ "end post-queue fork: " ++ show k
177 output <- atomically newTChan 185 output <- atomically newTChan
178 forkIO $ do 186 forkIO $ do
@@ -193,7 +201,9 @@ forkConnection k pingflag src snk stanzas = do
193 what 201 what
194 ,do pingflag >>= check 202 ,do pingflag >>= check
195 return $ do 203 return $ do
196 wlog $ "TODO: send ping" 204 atomically $ Slotted.push slots (Just PingSlot) $ EventBeginElement "ping" []
205 atomically $ Slotted.push slots (Just PingSlot) $ EventEndElement "ping"
206 -- TODO: fix ping
197 loop 207 loop
198 ,readTMVar rdone >> return (return ()) 208 ,readTMVar rdone >> return (return ())
199 ] 209 ]
@@ -220,9 +230,9 @@ monitor sv params = do
220 RequiresPing -> wlog $ tomsg k "RequiresPing" 230 RequiresPing -> wlog $ tomsg k "RequiresPing"
221 _ -> return () 231 _ -> return ()
222 , readTChan stanzas >>= \stanza -> return $ do 232 , readTChan stanzas >>= \stanza -> return $ do
223 xs <- chanContents (stanzaChan stanza) 233 xs <- readUntilNothing (stanzaChan stanza)
224 wlog "" 234 wlog ""
225 prettyPrint "STANZA: " (catMaybes xs) 235 prettyPrint "STANZA: " xs
226 ] 236 ]
227 action 237 action
228 loop 238 loop
@@ -249,8 +259,8 @@ main = runResourceT $ do
249 sv <- server 259 sv <- server
250 lift $ do 260 lift $ do
251 peer_params <- return (connectionDefaults peerKey) 261 peer_params <- return (connectionDefaults peerKey)
252 { pingInterval = 5000 262 { pingInterval = 10000
253 , timeout = 5000 263 , timeout = 10000
254 , duplex = False } 264 , duplex = False }
255 client_params <- return $ connectionDefaults clientKey 265 client_params <- return $ connectionDefaults clientKey
256 forkIO $ monitor sv peer_params 266 forkIO $ monitor sv peer_params