summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-09 21:11:27 -0500
committerjoe <joe@jerkface.net>2017-11-09 21:11:27 -0500
commit57b68ee93bd7a2c6d619ebafbe081703e3c3b8cc (patch)
tree1ea47d159f0decdf7f92a1031b512ca07c3e4b24 /examples/dhtd.hs
parent4809caedadd1832f2fd363f72cab941b425934ac (diff)
Combined XMPP daemon into examples/dhtd.
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r--examples/dhtd.hs86
1 files changed, 64 insertions, 22 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 837cb210..025f957f 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -21,11 +21,13 @@ module Main where
21import Control.Arrow 21import Control.Arrow
22import Control.Applicative 22import Control.Applicative
23import Control.Concurrent.STM 23import Control.Concurrent.STM
24import Control.DeepSeq
25import Control.Exception 24import Control.Exception
26import Control.Monad 25import Control.Monad
26import Control.Monad.IO.Class (liftIO)
27import Control.Monad.Trans.Resource (runResourceT)
27import Data.Bool 28import Data.Bool
28import Data.Char 29import Data.Char
30import Data.Function
29import Data.Hashable 31import Data.Hashable
30import Data.List 32import Data.List
31import qualified Data.IntMap.Strict as IntMap 33import qualified Data.IntMap.Strict as IntMap
@@ -53,6 +55,8 @@ import qualified Data.HashMap.Strict as HashMap
53import qualified Data.Vector as V 55import qualified Data.Vector as V
54import qualified Data.Text as T 56import qualified Data.Text as T
55import qualified Data.Text.Encoding as T 57import qualified Data.Text.Encoding as T
58import System.Posix.Signals
59
56 60
57import Announcer 61import Announcer
58import Crypto.Tox -- (zeros32,SecretKey,PublicKey, generateSecretKey, toPublic, encodeSecret, decodeSecret, userKeys) 62import Crypto.Tox -- (zeros32,SecretKey,PublicKey, generateSecretKey, toPublic, encodeSecret, decodeSecret, userKeys)
@@ -60,8 +64,6 @@ import Network.UPNP as UPNP
60import Network.Address hiding (NodeId, NodeInfo(..)) 64import Network.Address hiding (NodeId, NodeInfo(..))
61import Network.QueryResponse 65import Network.QueryResponse
62import Network.StreamServer 66import Network.StreamServer
63import Network.Kademlia
64import Network.Kademlia.Bootstrap
65import Network.Kademlia.Search 67import Network.Kademlia.Search
66import qualified Network.BitTorrent.MainlineDHT as Mainline 68import qualified Network.BitTorrent.MainlineDHT as Mainline
67import qualified Network.Tox as Tox 69import qualified Network.Tox as Tox
@@ -88,6 +90,12 @@ import Data.Typeable
88import Roster 90import Roster
89import OnionRouter 91import OnionRouter
90 92
93-- Presence imports.
94import ConsoleWriter
95import Presence
96import XMPPServer
97
98
91showReport :: [(String,String)] -> String 99showReport :: [(String,String)] -> String
92showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs 100showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs
93 101
@@ -386,7 +394,7 @@ data Session = Session
386 , roster :: Roster 394 , roster :: Roster
387 , onionRouter :: OnionRouter 395 , onionRouter :: OnionRouter
388 , announcer :: Announcer 396 , announcer :: Announcer
389 , signalQuit :: MVar () 397 , signalQuit :: IO ()
390 } 398 }
391 399
392exceptionsToClient :: ClientHandle -> IO () -> IO () 400exceptionsToClient :: ClientHandle -> IO () -> IO ()
@@ -481,7 +489,7 @@ clientSession s@Session{..} sock cnum h = do
481 489
482 ("stop", _) -> do hPutClient h "Terminating DHT Daemon." 490 ("stop", _) -> do hPutClient h "Terminating DHT Daemon."
483 hCloseClient h 491 hCloseClient h
484 putMVar signalQuit () 492 signalQuit
485 493
486 ("throw", er) -> cmd0 $ do 494 ("throw", er) -> cmd0 $ do
487 throwIO $ userError er 495 throwIO $ userError er
@@ -957,21 +965,30 @@ readExternals nodeAddr vars = do
957 return $ filter (not . unspecified) as 965 return $ filter (not . unspecified) as
958 966
959data Options = Options 967data Options = Options
960 { portbt :: String 968 { portbt :: String
961 , porttox :: String 969 , porttox :: String
962 , ip6bt :: Bool 970 , ip6bt :: Bool
963 , ip6tox :: Bool 971 , ip6tox :: Bool
964 , dhtkey :: Maybe SecretKey 972 , dhtkey :: Maybe SecretKey
973 -- | Currently only relevant to XMPP server code.
974 --
975 -- [ 0 ] Don't log XMPP stanzas.
976 --
977 -- [ 1 ] Log non-ping stanzas.
978 --
979 -- [ 2 ] Log all stanzas, even pings.
980 , verbosity :: Int
965 } 981 }
966 deriving (Eq,Show) 982 deriving (Eq,Show)
967 983
968sensibleDefaults :: Options 984sensibleDefaults :: Options
969sensibleDefaults = Options 985sensibleDefaults = Options
970 { portbt = "6881" 986 { portbt = "6881"
971 , porttox = "33445" 987 , porttox = "33445"
972 , ip6bt = True 988 , ip6bt = True
973 , ip6tox = True 989 , ip6tox = True
974 , dhtkey = Nothing 990 , dhtkey = Nothing
991 , verbosity = 0
975 } 992 }
976 993
977-- bt=<port>,tox=<port> 994-- bt=<port>,tox=<port>
@@ -1283,8 +1300,11 @@ main = do
1283 1300
1284 let dhts = Map.union btdhts toxdhts 1301 let dhts = Map.union btdhts toxdhts
1285 1302
1286 waitForSignal <- do 1303 (waitForSignal, checkQuit) <- do
1287 signalQuit <- newEmptyMVar 1304 signalQuit <- atomically $ newTVar False
1305 let quitCommand = atomically $ writeTVar signalQuit True
1306 installHandler sigTERM (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
1307 installHandler sigINT (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
1288 let defaultToxData = do 1308 let defaultToxData = do
1289 toxids <- atomically $ newTVar [] 1309 toxids <- atomically $ newTVar []
1290 rster <- newRoster 1310 rster <- newRoster
@@ -1296,7 +1316,7 @@ main = do
1296 let session = clientSession0 $ Session 1316 let session = clientSession0 $ Session
1297 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT 1317 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT
1298 , dhts = dhts -- all DHTs 1318 , dhts = dhts -- all DHTs
1299 , signalQuit = signalQuit 1319 , signalQuit = quitCommand
1300 , swarms = swarms 1320 , swarms = swarms
1301 , cryptosessions = netCryptoSessionsState 1321 , cryptosessions = netCryptoSessionsState
1302 , toxkeys = keysdb 1322 , toxkeys = keysdb
@@ -1307,9 +1327,10 @@ main = do
1307 , announcer = announcer 1327 , announcer = announcer
1308 } 1328 }
1309 srv <- streamServer (withSession session) (SockAddrUnix "dht.sock") 1329 srv <- streamServer (withSession session) (SockAddrUnix "dht.sock")
1310 return $ do 1330 return ( do atomically $ readTVar signalQuit >>= check
1311 () <- takeMVar signalQuit 1331 quitListening srv
1312 quitListening srv 1332 , readTVar signalQuit
1333 )
1313 1334
1314 1335
1315 forM_ (Map.toList dhts) 1336 forM_ (Map.toList dhts)
@@ -1331,7 +1352,28 @@ main = do
1331 bootstrap btSaved fallbackNodes 1352 bootstrap btSaved fallbackNodes
1332 return () 1353 return ()
1333 1354
1334 waitForSignal 1355 -- XMPP initialization
1356 cw <- newConsoleWriter
1357 state <- newPresenceState cw
1358
1359 -- XMPP stanza handling
1360 runResourceT $ do
1361 sv <- xmppServer (presenceHooks state (verbosity opts))
1362
1363 fork $ liftIO $ do
1364 myThreadId >>= flip labelThread "XMPP.stanzas"
1365 let console = cwPresenceChan $ consoleWriter state
1366 fix $ \loop -> do
1367 what <- atomically
1368 $ orElse (do (client,stanza) <- takeTMVar console
1369 return $ do informClientPresence0 state Nothing client stanza
1370 loop)
1371 (checkQuit >> return (return ()))
1372 what
1373
1374 -- Wait for DHT and XMPP threads to finish.
1375 -- Use ResourceT to clean-up XMPP server.
1376 liftIO waitForSignal
1335 1377
1336 stopAnnouncer announcer 1378 stopAnnouncer announcer
1337 quitBt 1379 quitBt