diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dhtd.hs | 86 |
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 | |||
21 | import Control.Arrow | 21 | import Control.Arrow |
22 | import Control.Applicative | 22 | import Control.Applicative |
23 | import Control.Concurrent.STM | 23 | import Control.Concurrent.STM |
24 | import Control.DeepSeq | ||
25 | import Control.Exception | 24 | import Control.Exception |
26 | import Control.Monad | 25 | import Control.Monad |
26 | import Control.Monad.IO.Class (liftIO) | ||
27 | import Control.Monad.Trans.Resource (runResourceT) | ||
27 | import Data.Bool | 28 | import Data.Bool |
28 | import Data.Char | 29 | import Data.Char |
30 | import Data.Function | ||
29 | import Data.Hashable | 31 | import Data.Hashable |
30 | import Data.List | 32 | import Data.List |
31 | import qualified Data.IntMap.Strict as IntMap | 33 | import qualified Data.IntMap.Strict as IntMap |
@@ -53,6 +55,8 @@ import qualified Data.HashMap.Strict as HashMap | |||
53 | import qualified Data.Vector as V | 55 | import qualified Data.Vector as V |
54 | import qualified Data.Text as T | 56 | import qualified Data.Text as T |
55 | import qualified Data.Text.Encoding as T | 57 | import qualified Data.Text.Encoding as T |
58 | import System.Posix.Signals | ||
59 | |||
56 | 60 | ||
57 | import Announcer | 61 | import Announcer |
58 | import Crypto.Tox -- (zeros32,SecretKey,PublicKey, generateSecretKey, toPublic, encodeSecret, decodeSecret, userKeys) | 62 | import Crypto.Tox -- (zeros32,SecretKey,PublicKey, generateSecretKey, toPublic, encodeSecret, decodeSecret, userKeys) |
@@ -60,8 +64,6 @@ import Network.UPNP as UPNP | |||
60 | import Network.Address hiding (NodeId, NodeInfo(..)) | 64 | import Network.Address hiding (NodeId, NodeInfo(..)) |
61 | import Network.QueryResponse | 65 | import Network.QueryResponse |
62 | import Network.StreamServer | 66 | import Network.StreamServer |
63 | import Network.Kademlia | ||
64 | import Network.Kademlia.Bootstrap | ||
65 | import Network.Kademlia.Search | 67 | import Network.Kademlia.Search |
66 | import qualified Network.BitTorrent.MainlineDHT as Mainline | 68 | import qualified Network.BitTorrent.MainlineDHT as Mainline |
67 | import qualified Network.Tox as Tox | 69 | import qualified Network.Tox as Tox |
@@ -88,6 +90,12 @@ import Data.Typeable | |||
88 | import Roster | 90 | import Roster |
89 | import OnionRouter | 91 | import OnionRouter |
90 | 92 | ||
93 | -- Presence imports. | ||
94 | import ConsoleWriter | ||
95 | import Presence | ||
96 | import XMPPServer | ||
97 | |||
98 | |||
91 | showReport :: [(String,String)] -> String | 99 | showReport :: [(String,String)] -> String |
92 | showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs | 100 | showReport 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 | ||
392 | exceptionsToClient :: ClientHandle -> IO () -> IO () | 400 | exceptionsToClient :: 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 | ||
959 | data Options = Options | 967 | data 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 | ||
968 | sensibleDefaults :: Options | 984 | sensibleDefaults :: Options |
969 | sensibleDefaults = Options | 985 | sensibleDefaults = 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 |