diff options
author | joe <joe@jerkface.net> | 2017-11-19 22:59:49 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-11-19 22:59:49 -0500 |
commit | eaaf761dfb6af3673d9f064a791afadbbdc60e29 (patch) | |
tree | a8b927e0dc497f91298c2d43e85bba9dff48e0b9 /examples | |
parent | 0e10db03f6d8f60b4afa58c39765ac2d78e2f859 (diff) |
Conduit stubs to convert between Tox and XMPP messages.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dhtd.hs | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index e9b365cb..527af7e7 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -27,6 +27,7 @@ import Control.Monad.Trans.Control | |||
27 | import Control.Monad.Trans.Resource (runResourceT) | 27 | import Control.Monad.Trans.Resource (runResourceT) |
28 | import Data.Bool | 28 | import Data.Bool |
29 | import Data.Char | 29 | import Data.Char |
30 | import Data.Conduit as C | ||
30 | import Data.Function | 31 | import Data.Function |
31 | import Data.Hashable | 32 | import Data.Hashable |
32 | import Data.List | 33 | import Data.List |
@@ -35,6 +36,7 @@ import qualified Data.Map.Strict as Map | |||
35 | import Data.Maybe | 36 | import Data.Maybe |
36 | import qualified Data.Set as Set | 37 | import qualified Data.Set as Set |
37 | import Data.Time.Clock | 38 | import Data.Time.Clock |
39 | import qualified Data.XML.Types as XML | ||
38 | import GHC.Conc (threadStatus,ThreadStatus(..)) | 40 | import GHC.Conc (threadStatus,ThreadStatus(..)) |
39 | import GHC.Stats | 41 | import GHC.Stats |
40 | import Network.Socket | 42 | import Network.Socket |
@@ -85,6 +87,7 @@ import qualified Network.Tox.DHT.Transport as Tox | |||
85 | import qualified Network.Tox.DHT.Handlers as Tox | 87 | import qualified Network.Tox.DHT.Handlers as Tox |
86 | import qualified Network.Tox.Onion.Transport as Tox | 88 | import qualified Network.Tox.Onion.Transport as Tox |
87 | import qualified Network.Tox.Onion.Handlers as Tox | 89 | import qualified Network.Tox.Onion.Handlers as Tox |
90 | import qualified Network.Tox.Crypto.Transport as Tox (CryptoMessage) | ||
88 | import qualified Network.Tox.Crypto.Handlers as Tox | 91 | import qualified Network.Tox.Crypto.Handlers as Tox |
89 | import Data.Typeable | 92 | import Data.Typeable |
90 | import Roster | 93 | import Roster |
@@ -95,6 +98,8 @@ import ConsoleWriter | |||
95 | import Presence | 98 | import Presence |
96 | import XMPPServer | 99 | import XMPPServer |
97 | import Connection | 100 | import Connection |
101 | import ToxToXMPP | ||
102 | import qualified Server (ConnectionEvent(..)) | ||
98 | 103 | ||
99 | 104 | ||
100 | showReport :: [(String,String)] -> String | 105 | showReport :: [(String,String)] -> String |
@@ -982,6 +987,22 @@ noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r) | |||
982 | noArgPing f [] x = f x | 987 | noArgPing f [] x = f x |
983 | noArgPing _ _ _ = return Nothing | 988 | noArgPing _ _ _ = return Nothing |
984 | 989 | ||
990 | announceToxConnection :: TChan ((ConnectionKey,SockAddr), Server.ConnectionEvent XML.Event) | ||
991 | -> SockAddr | ||
992 | -> SockAddr | ||
993 | -> STM Bool | ||
994 | -> C.Source IO Tox.CryptoMessage | ||
995 | -> C.Sink (Flush Tox.CryptoMessage) IO () | ||
996 | -> IO () | ||
997 | announceToxConnection echan laddr saddr pingflag tsrc tsnk | ||
998 | = atomically $ writeTChan echan | ||
999 | ( (PeerKey saddr, laddr ) | ||
1000 | , Server.Connection pingflag xsrc xsnk ) | ||
1001 | where | ||
1002 | xsrc = tsrc =$= toxToXmpp | ||
1003 | xsnk = flushPassThrough xmppToTox =$= tsnk | ||
1004 | |||
1005 | |||
985 | main :: IO () | 1006 | main :: IO () |
986 | main = runResourceT $ liftBaseWith $ \resT -> do | 1007 | main = runResourceT $ liftBaseWith $ \resT -> do |
987 | args <- getArgs | 1008 | args <- getArgs |
@@ -1007,7 +1028,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1007 | -- We now have a server object but it's not ready to use until | 1028 | -- We now have a server object but it's not ready to use until |
1008 | -- we put it into the 'server' field of our /state/ record. | 1029 | -- we put it into the 'server' field of our /state/ record. |
1009 | 1030 | ||
1010 | conns <- xmppConnections sv | 1031 | conns <- xmppConnections sv |
1032 | |||
1033 | atomically $ do | ||
1034 | putTMVar serverVar (sv,conns) -- Okay, now it's ready. :) | ||
1035 | -- FIXME: This is error prone. | ||
1036 | |||
1011 | 1037 | ||
1012 | (quitBt,btdhts,btips,baddrs) <- case portbt opts of | 1038 | (quitBt,btdhts,btips,baddrs) <- case portbt opts of |
1013 | "" -> return (return (), Map.empty,return [],[]) | 1039 | "" -> return (return (), Map.empty,return [],[]) |
@@ -1106,7 +1132,11 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1106 | toxport -> do | 1132 | toxport -> do |
1107 | addrTox <- getBindAddress toxport (ip6tox opts) | 1133 | addrTox <- getBindAddress toxport (ip6tox opts) |
1108 | hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) | 1134 | hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) |
1109 | tox <- Tox.newTox keysdb addrTox (Just netCryptoSessionsState) (dhtkey opts) | 1135 | tox <- Tox.newTox keysdb |
1136 | addrTox | ||
1137 | (Just netCryptoSessionsState) | ||
1138 | (dhtkey opts) | ||
1139 | (announceToxConnection (xmppEventChannel sv) addrTox) | ||
1110 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox | 1140 | (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox |
1111 | 1141 | ||
1112 | toxSearches <- atomically $ newTVar Map.empty | 1142 | toxSearches <- atomically $ newTVar Map.empty |
@@ -1329,10 +1359,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1329 | bootstrap btSaved fallbackNodes | 1359 | bootstrap btSaved fallbackNodes |
1330 | return () | 1360 | return () |
1331 | 1361 | ||
1332 | atomically $ do | ||
1333 | putTMVar serverVar (sv,conns) -- Okay, now it's ready. :) | ||
1334 | -- FIXME: This is error prone. | ||
1335 | |||
1336 | forkIO $ do | 1362 | forkIO $ do |
1337 | myThreadId >>= flip labelThread "XMPP.stanzas" | 1363 | myThreadId >>= flip labelThread "XMPP.stanzas" |
1338 | let console = cwPresenceChan <$> consoleWriter state | 1364 | let console = cwPresenceChan <$> consoleWriter state |