diff options
-rw-r--r-- | examples/dhtd.hs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 2abaecdd..6477fac4 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -29,6 +29,7 @@ import System.Environment | |||
29 | import System.IO | 29 | import System.IO |
30 | import System.Mem | 30 | import System.Mem |
31 | import System.Posix.Process | 31 | import System.Posix.Process |
32 | import Text.PrettyPrint.HughesPJClass | ||
32 | import Text.Printf | 33 | import Text.Printf |
33 | import Text.Read | 34 | import Text.Read |
34 | #ifdef THREAD_DEBUG | 35 | #ifdef THREAD_DEBUG |
@@ -50,6 +51,8 @@ import qualified Data.Aeson as J | |||
50 | import qualified Data.ByteString.Lazy as L | 51 | import qualified Data.ByteString.Lazy as L |
51 | import Control.Concurrent.Async.Pool | 52 | import Control.Concurrent.Async.Pool |
52 | import System.IO.Error | 53 | import System.IO.Error |
54 | import qualified Data.Serialize as S | ||
55 | import Network.BitTorrent.DHT.ContactInfo as Peers | ||
53 | 56 | ||
54 | showReport :: [(String,String)] -> String | 57 | showReport :: [(String,String)] -> String |
55 | showReport kvs = do | 58 | showReport kvs = do |
@@ -156,6 +159,7 @@ data Session = Session | |||
156 | { netname :: String | 159 | { netname :: String |
157 | , dhts :: Map.Map String DHT | 160 | , dhts :: Map.Map String DHT |
158 | , externalAddresses :: IO [SockAddr] | 161 | , externalAddresses :: IO [SockAddr] |
162 | , swarms :: Mainline.SwarmsDatabase | ||
159 | , signalQuit :: MVar () | 163 | , signalQuit :: MVar () |
160 | } | 164 | } |
161 | 165 | ||
@@ -177,7 +181,7 @@ clientSession s@Session{..} sock cnum h = do | |||
177 | pid <- getProcessID | 181 | pid <- getProcessID |
178 | hPutClient h (show pid) | 182 | hPutClient h (show pid) |
179 | ("external-ip", _) -> cmd0 $ do | 183 | ("external-ip", _) -> cmd0 $ do |
180 | unlines . map show <$> externalAddresses | 184 | unlines . map (either show show . Mainline.either4or6) <$> externalAddresses |
181 | >>= hPutClient h | 185 | >>= hPutClient h |
182 | #ifdef THREAD_DEBUG | 186 | #ifdef THREAD_DEBUG |
183 | ("threads", _) -> cmd0 $ do | 187 | ("threads", _) -> cmd0 $ do |
@@ -269,6 +273,23 @@ clientSession s@Session{..} sock cnum h = do | |||
269 | b <- pingNodes netname dht | 273 | b <- pingNodes netname dht |
270 | if b then hPutClient h $ "Pinging " ++ nodesFileName netname ++ "." | 274 | if b then hPutClient h $ "Pinging " ++ nodesFileName netname ++ "." |
271 | else hPutClient h $ "Failed: " ++ nodesFileName netname ++ "." | 275 | else hPutClient h $ "Failed: " ++ nodesFileName netname ++ "." |
276 | |||
277 | ("swarms", s) -> cmd0 $ do | ||
278 | let fltr = case s of | ||
279 | ('-':'v':cs) | all isSpace (take 1 cs) | ||
280 | -> const True | ||
281 | _ -> (\(h,c,n) -> c/=0 ) | ||
282 | ss <- atomically $ Peers.knownSwarms <$> readTVar (Mainline.contactInfo swarms) | ||
283 | let r = map (\(h,c,n) -> (unwords [show h,show c], maybe "" show n)) | ||
284 | $ filter fltr ss | ||
285 | hPutClient h $ showReport r | ||
286 | |||
287 | ("peers", s) -> cmd0 $ case readEither s of | ||
288 | Right ih -> do | ||
289 | ps <- atomically $ Peers.lookup ih <$> readTVar (Mainline.contactInfo swarms) | ||
290 | hPutClient h $ showReport $ map (((,) "") . show . pPrint) ps | ||
291 | Left er -> hPutClient h er | ||
292 | |||
272 | (n, _) | n `elem` Map.keys dhts -> switchNetwork n | 293 | (n, _) | n `elem` Map.keys dhts -> switchNetwork n |
273 | 294 | ||
274 | _ -> cmd0 $ hPutClient h "error." | 295 | _ -> cmd0 $ hPutClient h "error." |
@@ -291,6 +312,13 @@ main = do | |||
291 | addr <- getBindAddress p True{- ipv6 -} | 312 | addr <- getBindAddress p True{- ipv6 -} |
292 | 313 | ||
293 | (bt,btR,swarms) <- Mainline.newClient addr | 314 | (bt,btR,swarms) <- Mainline.newClient addr |
315 | |||
316 | -- Restore peer database before forking the listener thread. | ||
317 | peerdb <- left show <$> tryIOError (L.readFile "bt-peers.dat") | ||
318 | either (hPutStrLn stderr . ("bt-peers.dat: "++)) | ||
319 | (atomically . writeTVar (Mainline.contactInfo swarms)) | ||
320 | (peerdb >>= S.decodeLazy) | ||
321 | |||
294 | quitBt <- forkListener bt | 322 | quitBt <- forkListener bt |
295 | 323 | ||
296 | tox <- return $ error "TODO: Tox.newClient" | 324 | tox <- return $ error "TODO: Tox.newClient" |
@@ -327,6 +355,7 @@ main = do | |||
327 | { netname = "bt4" -- initial default DHT | 355 | { netname = "bt4" -- initial default DHT |
328 | , dhts = dhts -- all DHTs | 356 | , dhts = dhts -- all DHTs |
329 | , signalQuit = signalQuit | 357 | , signalQuit = signalQuit |
358 | , swarms = swarms | ||
330 | , externalAddresses = readExternals | 359 | , externalAddresses = readExternals |
331 | [ Mainline.routing4 btR | 360 | [ Mainline.routing4 btR |
332 | , Mainline.routing6 btR | 361 | , Mainline.routing6 btR |
@@ -362,3 +391,6 @@ main = do | |||
362 | 391 | ||
363 | quitBt | 392 | quitBt |
364 | quitTox | 393 | quitTox |
394 | |||
395 | swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms) | ||
396 | L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb | ||