summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r--examples/dhtd.hs38
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
27import Control.Monad.Trans.Resource (runResourceT) 27import Control.Monad.Trans.Resource (runResourceT)
28import Data.Bool 28import Data.Bool
29import Data.Char 29import Data.Char
30import Data.Conduit as C
30import Data.Function 31import Data.Function
31import Data.Hashable 32import Data.Hashable
32import Data.List 33import Data.List
@@ -35,6 +36,7 @@ import qualified Data.Map.Strict as Map
35import Data.Maybe 36import Data.Maybe
36import qualified Data.Set as Set 37import qualified Data.Set as Set
37import Data.Time.Clock 38import Data.Time.Clock
39import qualified Data.XML.Types as XML
38import GHC.Conc (threadStatus,ThreadStatus(..)) 40import GHC.Conc (threadStatus,ThreadStatus(..))
39import GHC.Stats 41import GHC.Stats
40import Network.Socket 42import Network.Socket
@@ -85,6 +87,7 @@ import qualified Network.Tox.DHT.Transport as Tox
85import qualified Network.Tox.DHT.Handlers as Tox 87import qualified Network.Tox.DHT.Handlers as Tox
86import qualified Network.Tox.Onion.Transport as Tox 88import qualified Network.Tox.Onion.Transport as Tox
87import qualified Network.Tox.Onion.Handlers as Tox 89import qualified Network.Tox.Onion.Handlers as Tox
90import qualified Network.Tox.Crypto.Transport as Tox (CryptoMessage)
88import qualified Network.Tox.Crypto.Handlers as Tox 91import qualified Network.Tox.Crypto.Handlers as Tox
89import Data.Typeable 92import Data.Typeable
90import Roster 93import Roster
@@ -95,6 +98,8 @@ import ConsoleWriter
95import Presence 98import Presence
96import XMPPServer 99import XMPPServer
97import Connection 100import Connection
101import ToxToXMPP
102import qualified Server (ConnectionEvent(..))
98 103
99 104
100showReport :: [(String,String)] -> String 105showReport :: [(String,String)] -> String
@@ -982,6 +987,22 @@ noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r)
982noArgPing f [] x = f x 987noArgPing f [] x = f x
983noArgPing _ _ _ = return Nothing 988noArgPing _ _ _ = return Nothing
984 989
990announceToxConnection :: 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 ()
997announceToxConnection 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
985main :: IO () 1006main :: IO ()
986main = runResourceT $ liftBaseWith $ \resT -> do 1007main = 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