diff options
author | joe <joe@jerkface.net> | 2018-05-19 22:26:46 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-05-19 22:26:46 -0400 |
commit | f2016c7a77c077ec342a687a172a16bf64311709 (patch) | |
tree | 8ce526d89dff01d9f3c660bfed6d08c1d50a9261 | |
parent | c3c89c536cd7524eaa510356b393e2d60fefdba6 (diff) |
Auto-schedule DHT announce for toxmpp sessions.
-rw-r--r-- | examples/dhtd.hs | 75 |
1 files changed, 53 insertions, 22 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 0439ed7e..b3c60015 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -1067,27 +1067,61 @@ newXmmpSink session@(Tox.NCrypto { ncOutgoingQueue, ncPacketQueue }) = C.awaitFo | |||
1067 | sendit session Flush = return () | 1067 | sendit session Flush = return () |
1068 | liftIO $ sendit session flush_cyptomessage | 1068 | liftIO $ sendit session flush_cyptomessage |
1069 | 1069 | ||
1070 | -- | TODO | 1070 | |
1071 | toxAnnounceSendData :: Tox.Tox -> PublicKey | ||
1072 | -> Nonce32 | ||
1073 | -> Maybe Tox.NodeInfo | ||
1074 | -> IO (Maybe (Tox.Rendezvous, Tox.AnnounceResponse)) | ||
1075 | toxAnnounceSendData tox pubkey token = \case | ||
1076 | Just ni -> Tox.putRendezvous (Tox.onionTimeout tox) | ||
1077 | (Tox.toxCryptoKeys tox) | ||
1078 | (Tox.toxOnion tox) | ||
1079 | (pubkey :: PublicKey) | ||
1080 | (token :: Nonce32) | ||
1081 | ni | ||
1082 | Nothing -> return Nothing | ||
1083 | |||
1084 | toxQSearch :: Tox.Tox -> Search Tox.NodeId (IP, PortNumber) Nonce32 Tox.NodeInfo Tox.Rendezvous | ||
1085 | toxQSearch tox = Tox.toxidSearch (Tox.onionTimeout tox) (Tox.toxCryptoKeys tox) (Tox.toxOnion tox) | ||
1086 | |||
1087 | toxAnnounceInterval :: POSIXTime | ||
1088 | toxAnnounceInterval = 15 | ||
1089 | |||
1090 | -- | | ||
1071 | -- | 1091 | -- |
1072 | -- These hooks will be invoked in order to connect to *.tox hosts in a user's | 1092 | -- These hooks will be invoked in order to connect to *.tox hosts in a user's |
1073 | -- XMPP roster. | 1093 | -- XMPP roster. |
1074 | toxman :: Tox.Tox -> ToxManager ConnectionKey | 1094 | toxman :: Announcer -> [(String,TVar (BucketList Tox.NodeInfo))] -> Tox.Tox -> ToxManager ConnectionKey |
1075 | toxman tox = ToxManager | 1095 | toxman announcer toxbkts tox = ToxManager |
1076 | { activateAccount = \k pubname seckey -> do | 1096 | { activateAccount = \k pubname seckey -> do |
1077 | hPutStrLn stderr $ "toxman ACTIVATE " ++ show (Tox.key2id $ toPublic seckey) | 1097 | hPutStrLn stderr $ "toxman ACTIVATE " ++ show (Tox.key2id $ toPublic seckey) |
1078 | atomically $ do | ||
1079 | let ContactInfo{ accounts } = Tox.toxContactInfo tox | 1098 | let ContactInfo{ accounts } = Tox.toxContactInfo tox |
1080 | pub = toPublic seckey | 1099 | pub = toPublic seckey |
1081 | pubid = Tox.key2id pub | 1100 | pubid = Tox.key2id pub |
1082 | refs <- do | 1101 | newlyActive <- atomically $ do |
1083 | macnt <- HashMap.lookup pubid <$> readTVar accounts | 1102 | macnt <- HashMap.lookup pubid <$> readTVar accounts |
1084 | acnt <- maybe (newAccount seckey) return macnt | 1103 | acnt <- maybe (newAccount seckey) return macnt |
1085 | rs <- readTVar $ clientRefs acnt | 1104 | rs <- readTVar $ clientRefs acnt |
1086 | writeTVar (clientRefs acnt) $! Set.insert k rs | 1105 | writeTVar (clientRefs acnt) $! Set.insert k rs |
1087 | modifyTVar accounts (HashMap.insert pubid acnt) | 1106 | modifyTVar accounts (HashMap.insert pubid acnt) |
1088 | return rs | 1107 | if not (Set.null rs) |
1089 | when (Set.null refs) $ do | 1108 | then return [] |
1109 | else do | ||
1110 | forM toxbkts $ \(nm,bkts) -> do | ||
1111 | akey <- packAnnounceKey announcer (nm ++ "id:" ++ show pubid) | ||
1112 | return (akey,bkts) | ||
1113 | forM_ newlyActive $ \(akey,bkts) -> do | ||
1090 | -- Schedule recurring announce. | 1114 | -- Schedule recurring announce. |
1115 | -- | ||
1116 | schedule announcer | ||
1117 | akey | ||
1118 | (AnnounceMethod (toxQSearch tox) | ||
1119 | (Right $ toxAnnounceSendData tox) | ||
1120 | bkts | ||
1121 | pubid | ||
1122 | toxAnnounceInterval) | ||
1123 | pub | ||
1124 | -- | ||
1091 | -- Schedule recurring search for all non-connected contacts. | 1125 | -- Schedule recurring search for all non-connected contacts. |
1092 | return () | 1126 | return () |
1093 | , deactivateAccount = \k pubname -> atomically $ do | 1127 | , deactivateAccount = \k pubname -> atomically $ do |
@@ -1293,9 +1327,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1293 | , qshowTok = (const Nothing) | 1327 | , qshowTok = (const Nothing) |
1294 | }) | 1328 | }) |
1295 | , ("toxid", DHTQuery | 1329 | , ("toxid", DHTQuery |
1296 | { qsearch = (Tox.toxidSearch (Tox.onionTimeout tox) | 1330 | { qsearch = toxQSearch tox |
1297 | (Tox.toxCryptoKeys tox) | ||
1298 | (Tox.toxOnion tox)) | ||
1299 | , qhandler = -- qhandler :: ni -> nid -> IO ([ni], [r], tok) | 1331 | , qhandler = -- qhandler :: ni -> nid -> IO ([ni], [r], tok) |
1300 | (\ni nid -> | 1332 | (\ni nid -> |
1301 | Tox.unwrapAnnounceResponse Nothing | 1333 | Tox.unwrapAnnounceResponse Nothing |
@@ -1313,16 +1345,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1313 | , dhtSearches = toxSearches | 1345 | , dhtSearches = toxSearches |
1314 | , dhtFallbackNodes = return [] | 1346 | , dhtFallbackNodes = return [] |
1315 | , dhtAnnouncables = Map.fromList | 1347 | , dhtAnnouncables = Map.fromList |
1316 | [ ("toxid", DHTAnnouncable { announceSendData = Right $ \pubkey token -> \case | 1348 | [ ("toxid", DHTAnnouncable { announceSendData = Right (toxAnnounceSendData tox) |
1317 | Just ni -> | ||
1318 | Tox.putRendezvous | ||
1319 | (Tox.onionTimeout tox) | ||
1320 | (Tox.toxCryptoKeys tox) | ||
1321 | (Tox.toxOnion tox) | ||
1322 | (pubkey :: PublicKey) | ||
1323 | (token :: Nonce32) | ||
1324 | ni | ||
1325 | Nothing -> return Nothing | ||
1326 | , announceParseAddress = readEither | 1349 | , announceParseAddress = readEither |
1327 | , announceParseToken = const $ readEither | 1350 | , announceParseToken = const $ readEither |
1328 | , announceParseData = fmap Tox.id2key . readEither | 1351 | , announceParseData = fmap Tox.id2key . readEither |
@@ -1340,7 +1363,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1340 | -- 15 seconds later. Toxcore sends every announce packet | 1363 | -- 15 seconds later. Toxcore sends every announce packet |
1341 | -- with the `ping_id` previously received from that peer | 1364 | -- with the `ping_id` previously received from that peer |
1342 | -- with the same path (if possible). | 1365 | -- with the same path (if possible). |
1343 | , announceInterval = 15 | 1366 | , announceInterval = toxAnnounceInterval |
1344 | 1367 | ||
1345 | }) | 1368 | }) |
1346 | -- dhtkey parameters: | 1369 | -- dhtkey parameters: |
@@ -1451,7 +1474,15 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1451 | -- XMPP initialization | 1474 | -- XMPP initialization |
1452 | cw <- newConsoleWriter | 1475 | cw <- newConsoleWriter |
1453 | serverVar <- atomically $ newEmptyTMVar | 1476 | serverVar <- atomically $ newEmptyTMVar |
1454 | state <- newPresenceState cw (toxman <$> mbtox) serverVar | 1477 | let lookupBkts :: String -> Map.Map String DHT -> Maybe (String,TVar (BucketList Tox.NodeInfo)) |
1478 | lookupBkts name m = case Map.lookup name m of | ||
1479 | Nothing -> Nothing | ||
1480 | Just DHT{dhtBuckets} -> cast (name, dhtBuckets) | ||
1481 | let toxbkts = catMaybes | ||
1482 | [ lookupBkts "tox4" toxdhts | ||
1483 | , lookupBkts "tox6" toxdhts | ||
1484 | ] | ||
1485 | state <- newPresenceState cw (toxman announcer toxbkts <$> mbtox) serverVar | ||
1455 | 1486 | ||
1456 | sv <- resT $ xmppServer (presenceHooks state (verbosity opts) Nothing) | 1487 | sv <- resT $ xmppServer (presenceHooks state (verbosity opts) Nothing) |
1457 | -- We now have a server object but it's not ready to use until | 1488 | -- We now have a server object but it's not ready to use until |