diff options
author | joe <joe@jerkface.net> | 2014-02-09 21:57:30 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-02-09 21:57:30 -0500 |
commit | 1b0182eae555aeb2952e40a522bd5215ae0fc6d9 (patch) | |
tree | dc3fa54ad0930309d39b6c7b0f6d2eb211f951a2 /xmppServer.hs | |
parent | 716dffcc5f0c21d39c08e512f0dd51950d1bc482 (diff) |
new Server code, xmppServer demo
Diffstat (limited to 'xmppServer.hs')
-rw-r--r-- | xmppServer.hs | 60 |
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 @@ | |||
1 | import Control.Monad.Trans.Resource (runResourceT) | ||
2 | import Control.Monad.Trans (lift) | ||
3 | import Control.Monad.Fix (fix) | ||
4 | import Control.Concurrent (forkIO) | ||
5 | import Control.Concurrent.STM | ||
6 | -- import Control.Concurrent.STM.TChan | ||
7 | import Network.Socket | ||
8 | import XMPPTypes (withPort) | ||
9 | import Text.Printf | ||
10 | |||
11 | import Server | ||
12 | |||
13 | wlog s = putStrLn s | ||
14 | |||
15 | control sv = atomically . putTMVar (serverCommand sv) | ||
16 | |||
17 | monitor 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 | |||
34 | data ConnectionKey | ||
35 | = PeerKey { callBackAddress :: SockAddr } | ||
36 | | ClientKey { localAddress :: SockAddr } | ||
37 | deriving (Show, Ord, Eq) | ||
38 | |||
39 | peerKey (sock,addr) = do | ||
40 | peer <- getPeerName sock | ||
41 | return $ PeerKey (peer `withPort` fromIntegral peerport) | ||
42 | |||
43 | clientKey (sock,addr) = return $ ClientKey addr | ||
44 | |||
45 | peerport = 5269 | ||
46 | clientport = 5222 | ||
47 | |||
48 | main = 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 () | ||