summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-07 23:58:28 -0500
committerjoe <joe@jerkface.net>2017-11-08 02:30:43 -0500
commit65a2d2fbeee67dc400f683eaedf88153f0ba9bab (patch)
tree093426196ec7d65c3fc3509b5807161daffb185c /examples/dhtd.hs
parent83650b62724ae8deb90fc4659b3ead47d25ead53 (diff)
dhtBootstrap
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r--examples/dhtd.hs33
1 files changed, 15 insertions, 18 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 7d3661e6..837cb210 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -195,6 +195,7 @@ data DHT = forall nid ni. ( Show ni
195 , dhtParseId :: String -> Either String nid 195 , dhtParseId :: String -> Either String nid
196 , dhtSearches :: TVar (Map.Map (String,nid) (DHTSearch nid ni)) 196 , dhtSearches :: TVar (Map.Map (String,nid) (DHTSearch nid ni))
197 , dhtFallbackNodes :: IO [ni] 197 , dhtFallbackNodes :: IO [ni]
198 , dhtBootstrap :: [ni] -> [ni] -> IO ()
198 } 199 }
199 200
200nodesFileName :: String -> String 201nodesFileName :: String -> String
@@ -1013,7 +1014,7 @@ main = do
1013 "" -> return (return (), Map.empty,return [],[]) 1014 "" -> return (return (), Map.empty,return [],[])
1014 p -> do 1015 p -> do
1015 addr <- getBindAddress p (ip6bt opts) 1016 addr <- getBindAddress p (ip6bt opts)
1016 (bt,btR) <- Mainline.newClient swarms addr 1017 (bt,btR,btBootstrap4, btBootstrap6) <- Mainline.newClient swarms addr
1017 quitBt <- forkListener "bt" (clientNet bt) 1018 quitBt <- forkListener "bt" (clientNet bt)
1018 mainlineSearches <- atomically $ newTVar Map.empty 1019 mainlineSearches <- atomically $ newTVar Map.empty
1019 peerPort <- atomically $ newTVar 6881 -- BitTorrent client TCP port. 1020 peerPort <- atomically $ newTVar 6881 -- BitTorrent client TCP port.
@@ -1083,6 +1084,9 @@ main = do
1083 [ {- TODO -} 1084 [ {- TODO -}
1084 ] 1085 ]
1085 , dhtSecretKey = return Nothing 1086 , dhtSecretKey = return Nothing
1087 , dhtBootstrap = case wantip of
1088 Want_IP4 -> btBootstrap4
1089 Want_IP6 -> btBootstrap6
1086 } 1090 }
1087 dhts = Map.fromList $ 1091 dhts = Map.fromList $
1088 ("bt4", mainlineDHT Mainline.routing4 Want_IP4) 1092 ("bt4", mainlineDHT Mainline.routing4 Want_IP4)
@@ -1107,11 +1111,11 @@ main = do
1107 addrTox <- getBindAddress toxport (ip6tox opts) 1111 addrTox <- getBindAddress toxport (ip6tox opts)
1108 hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) 1112 hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts))
1109 tox <- Tox.newTox keysdb addrTox (Just netCryptoSessionsState) (dhtkey opts) 1113 tox <- Tox.newTox keysdb addrTox (Just netCryptoSessionsState) (dhtkey opts)
1110 quitTox <- Tox.forkTox tox 1114 (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox
1111 1115
1112 toxSearches <- atomically $ newTVar Map.empty 1116 toxSearches <- atomically $ newTVar Map.empty
1113 1117
1114 let toxDHT bkts = DHT 1118 let toxDHT bkts wantip = DHT
1115 { dhtBuckets = bkts (Tox.toxRouting tox) 1119 { dhtBuckets = bkts (Tox.toxRouting tox)
1116 , dhtPing = Map.fromList 1120 , dhtPing = Map.fromList
1117 [ ("ping", DHTPing 1121 [ ("ping", DHTPing
@@ -1262,11 +1266,14 @@ main = do
1262 [ {- TODO -} 1266 [ {- TODO -}
1263 ] 1267 ]
1264 , dhtSecretKey = return $ Just $ transportSecret (Tox.toxCryptoKeys tox) 1268 , dhtSecretKey = return $ Just $ transportSecret (Tox.toxCryptoKeys tox)
1269 , dhtBootstrap = case wantip of
1270 Want_IP4 -> toxStrap4
1271 Want_IP6 -> toxStrap6
1265 } 1272 }
1266 dhts = Map.fromList $ 1273 dhts = Map.fromList $
1267 ("tox4", toxDHT Tox.routing4) 1274 ("tox4", toxDHT Tox.routing4 Want_IP4)
1268 : if ip6tox opts 1275 : if ip6tox opts
1269 then [ ("tox6", toxDHT Tox.routing6) ] 1276 then [ ("tox6", toxDHT Tox.routing6 Want_IP6) ]
1270 else [] 1277 else []
1271 ips :: IO [SockAddr] 1278 ips :: IO [SockAddr]
1272 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox 1279 ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox
@@ -1309,7 +1316,8 @@ main = do
1309 $ \(netname, dht@DHT { dhtBuckets = bkts 1316 $ \(netname, dht@DHT { dhtBuckets = bkts
1310 , dhtQuery = qrys 1317 , dhtQuery = qrys
1311 , dhtPing = pings 1318 , dhtPing = pings
1312 , dhtFallbackNodes = getBootstrapNodes }) -> do 1319 , dhtFallbackNodes = getBootstrapNodes
1320 , dhtBootstrap = bootstrap }) -> do
1313 btSaved <- loadNodes netname -- :: IO [Mainline.NodeInfo] 1321 btSaved <- loadNodes netname -- :: IO [Mainline.NodeInfo]
1314 putStrLn $ "Loaded "++show (length btSaved)++" nodes for "++netname++"." 1322 putStrLn $ "Loaded "++show (length btSaved)++" nodes for "++netname++"."
1315 fallbackNodes <- getBootstrapNodes 1323 fallbackNodes <- getBootstrapNodes
@@ -1320,18 +1328,7 @@ main = do
1320 $ Map.lookup "ping" pings 1328 $ Map.lookup "ping" pings
1321 fork $ do 1329 fork $ do
1322 myThreadId >>= flip labelThread ("bootstrap."++netname) 1330 myThreadId >>= flip labelThread ("bootstrap."++netname)
1323 case Map.lookup "node" qrys of 1331 bootstrap btSaved fallbackNodes
1324 Just DHTQuery { qsearch = srch } -> do
1325 case eqT of
1326 Just witness -> let strapper = BucketRefresher
1327 { refreshSearch = isNodesSearch witness srch
1328 , refreshBuckets = bkts
1329 , refreshPing = ping
1330 }
1331 in bootstrap strapper btSaved fallbackNodes
1332 _ -> error $ "Missing node-search for "++netname++"."
1333 saveNodes netname dht
1334 Nothing -> return ()
1335 return () 1332 return ()
1336 1333
1337 waitForSignal 1334 waitForSignal