summaryrefslogtreecommitdiff
path: root/xmppServer.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-02-09 21:57:30 -0500
committerjoe <joe@jerkface.net>2014-02-09 21:57:30 -0500
commit1b0182eae555aeb2952e40a522bd5215ae0fc6d9 (patch)
treedc3fa54ad0930309d39b6c7b0f6d2eb211f951a2 /xmppServer.hs
parent716dffcc5f0c21d39c08e512f0dd51950d1bc482 (diff)
new Server code, xmppServer demo
Diffstat (limited to 'xmppServer.hs')
-rw-r--r--xmppServer.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/xmppServer.hs b/xmppServer.hs
new file mode 100644
index 00000000..50818dd6
--- /dev/null
+++ b/xmppServer.hs
@@ -0,0 +1,60 @@
1import Control.Monad.Trans.Resource (runResourceT)
2import Control.Monad.Trans (lift)
3import Control.Monad.Fix (fix)
4import Control.Concurrent (forkIO)
5import Control.Concurrent.STM
6-- import Control.Concurrent.STM.TChan
7import Network.Socket
8import XMPPTypes (withPort)
9import Text.Printf
10
11import Server
12
13wlog s = putStrLn s
14
15control sv = atomically . putTMVar (serverCommand sv)
16
17monitor sv params = do
18 chan <- return $ serverEvent sv
19 fix $ \loop -> do
20 (k,e) <- atomically $ readTChan chan
21 case e of
22 Connection -> wlog $ tomsg k "Connection"
23 EOF -> wlog $ tomsg k "EOF"
24 HalfConnection In -> do
25 wlog $ tomsg k "ReadOnly"
26 control sv (Connect (callBackAddress k) params)
27 HalfConnection Out -> wlog $ tomsg k "WriteOnly"
28 RequiresPing -> wlog $ tomsg k "RequiresPing"
29 _ -> return ()
30 loop
31 where
32 tomsg k str = printf "%12s %s" str (show k)
33
34data ConnectionKey
35 = PeerKey { callBackAddress :: SockAddr }
36 | ClientKey { localAddress :: SockAddr }
37 deriving (Show, Ord, Eq)
38
39peerKey (sock,addr) = do
40 peer <- getPeerName sock
41 return $ PeerKey (peer `withPort` fromIntegral peerport)
42
43clientKey (sock,addr) = return $ ClientKey addr
44
45peerport = 5269
46clientport = 5222
47
48main = runResourceT $ do
49 sv <- server
50 lift $ do
51 peer_params <- return (connectionDefaults peerKey)
52 { duplex = False }
53 client_params <- return $ connectionDefaults clientKey
54 forkIO $ monitor sv peer_params
55 control sv (Listen peerport peer_params)
56 control sv (Listen clientport client_params)
57
58 atomically $ newEmptyTMVar >>= readTMVar -- Wait for control-c
59
60 return ()