diff options
Diffstat (limited to 'dht/src/Network/Tox/RelayPinger.hs')
-rw-r--r-- | dht/src/Network/Tox/RelayPinger.hs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/dht/src/Network/Tox/RelayPinger.hs b/dht/src/Network/Tox/RelayPinger.hs index 00c6f65a..5803c89f 100644 --- a/dht/src/Network/Tox/RelayPinger.hs +++ b/dht/src/Network/Tox/RelayPinger.hs | |||
@@ -58,12 +58,15 @@ addRelay :: RelayPinger -> TCP.NodeInfo -> STM () | |||
58 | addRelay (RelayPinger a kad cli que self) ni = do | 58 | addRelay (RelayPinger a kad cli que self) ni = do |
59 | let nid = kademliaLocation kad ni | 59 | let nid = kademliaLocation kad ni |
60 | nidk = decodeAnnounceKey a $ S.encode nid | 60 | nidk = decodeAnnounceKey a $ S.encode nid |
61 | pingit = ScheduledItem $ \a nidk tm -> do | 61 | pingit = ScheduledItem $ \a nidk tm -> return $ do |
62 | scheduleRel a nidk pingit pinginterval -- Reschedule the next ping. | 62 | m <- tcpPing cli ni |
63 | return $ do | 63 | case m of |
64 | _ <- tcpPing cli ni | 64 | Just () -> atomically $ scheduleRel a nidk pingit pinginterval -- Reschedule the next ping. |
65 | -- TODO: Remove after ping timeout? | 65 | Nothing -> do |
66 | return () | 66 | -- dput XMisc $ "relay-ping: Ping timeout for " ++ show ni |
67 | atomically $ modifyTVar' que $ | ||
68 | \(n,q) -> ( (,) $! (if PSQ.member ni q then n - 1 else n) ) | ||
69 | $ PSQ.delete ni q | ||
67 | modifyTVar' que $ \(n,q) -> | 70 | modifyTVar' que $ \(n,q) -> |
68 | ( (,) $! (if PSQ.member ni q then n else n+1) ) | 71 | ( (,) $! (if PSQ.member ni q then n else n+1) ) |
69 | $ PSQ.insert | 72 | $ PSQ.insert |
@@ -85,14 +88,17 @@ bumpRelay :: RelayPinger -> TCP.NodeInfo -> STM () | |||
85 | bumpRelay rp@(RelayPinger a kad cli que self) ni = do | 88 | bumpRelay rp@(RelayPinger a kad cli que self) ni = do |
86 | let nid = kademliaLocation kad ni | 89 | let nid = kademliaLocation kad ni |
87 | (cnt,q) <- readTVar que | 90 | (cnt,q) <- readTVar que |
88 | if cnt < 4 | 91 | if cnt < 4 || PSQ.member ni q |
89 | then addRelay rp ni | 92 | then addRelay rp ni |
90 | else case PSQ.minView q of | 93 | else case PSQ.minView q of |
91 | Nothing -> addRelay rp ni | 94 | Nothing -> addRelay rp ni |
92 | Just (r :-> Down p, q') | 95 | Just (r :-> Down p, q') |
93 | | let pnew = RelayPriority (rankTCPPort ni) (kademliaXor kad self nid) | 96 | | let pnew = RelayPriority (rankTCPPort ni) (kademliaXor kad self nid) |
94 | , pnew < p | 97 | , pnew < p |
95 | -> delRelay rp r >> addRelay rp ni | 98 | -> do |
99 | writeTVar que (3,q') | ||
100 | unschedule a (decodeAnnounceKey a $ S.encode $ kademliaLocation kad r) | ||
101 | addRelay rp ni | ||
96 | _ -> return () | 102 | _ -> return () |
97 | 103 | ||
98 | currentRelays :: RelayPinger -> STM (Int,[TCP.NodeInfo]) | 104 | currentRelays :: RelayPinger -> STM (Int,[TCP.NodeInfo]) |