1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
module Network.Tox.RelayPinger where
import Announcer
import Control.Arrow
import Control.Concurrent.STM
import Control.Concurrent.ThreadUtil
import Control.Monad
import Data.Ord
import qualified Data.Serialize as S
import Data.Time.Clock (NominalDiffTime)
import Data.Time.Clock.POSIX
import qualified Data.Wrapper.PSQ as PSQ
;import Data.Wrapper.PSQ (pattern (:->), PSQ)
import Network.Kademlia.Routing
import Network.QueryResponse
import Network.Tox.TCP as TCP
data RelayPriority = RelayPriority
{ tcpPortRank :: Int
, xorDeltaDistance :: NodeId
}
deriving (Eq,Ord)
data RelayPinger = RelayPinger
{ relayAnnouncer :: Announcer
, relayKademliaSpace :: KademliaSpace NodeId NodeInfo
, relayClient :: RelayClient
, relayQueue :: TVar (Int, PSQ NodeInfo (Down RelayPriority))
, relaySelf :: NodeId
}
forkRelayPinger :: KademliaSpace NodeId NodeInfo -> RelayClient -> IO RelayPinger
forkRelayPinger kad cli = do
a <- forkAnnouncer
q <- newTVarIO (0,PSQ.empty)
self <- kademliaLocation kad <$> clientAddress cli Nothing
return RelayPinger
{ relayAnnouncer = a
, relayKademliaSpace = kad
, relayClient = cli
, relayQueue = q
, relaySelf = self
}
pinginterval :: NominalDiffTime
pinginterval = 120.0 -- 2 minutes
rankTCPPort :: NodeInfo -> Int
rankTCPPort NodeInfo{tcpPort = 443} = 1
rankTCPPort NodeInfo{tcpPort = 80} = 1
rankTCPPort NodeInfo{tcpPort = 3389} = 2
rankTCPPort _ = 3
addRelay :: RelayPinger -> TCP.NodeInfo -> STM ()
addRelay (RelayPinger a kad cli que self) ni = do
let nid = kademliaLocation kad ni
nidk = decodeAnnounceKey a $ S.encode nid
pingit = ScheduledItem $ \a nidk tm -> do
scheduleRel a nidk pingit pinginterval -- Reschedule the next ping.
return $ do
_ <- tcpPing cli ni
-- TODO: Remove after ping timeout?
return ()
modifyTVar' que $ \(n,q) ->
( (,) $! (if PSQ.member ni q then n else n+1) )
$ PSQ.insert
ni
(Down $ RelayPriority (rankTCPPort ni) (kademliaXor kad self nid))
q
scheduleRel a nidk pingit pinginterval
delRelay :: RelayPinger -> TCP.NodeInfo -> STM ()
delRelay (RelayPinger a kad _ que _) ni = do
let nid = kademliaLocation kad ni
nidk = decodeAnnounceKey a $ S.encode nid
(n,q) <- readTVar que
writeTVar que $ ( (,) $! (if PSQ.member ni q then n - 1 else n) )
$ PSQ.delete ni q
unschedule a nidk
bumpRelay :: RelayPinger -> TCP.NodeInfo -> STM ()
bumpRelay rp@(RelayPinger a kad cli que self) ni = do
let nid = kademliaLocation kad ni
(cnt,q) <- readTVar que
if cnt < 4
then addRelay rp ni
else case PSQ.minView q of
Nothing -> addRelay rp ni
Just (r :-> Down p, q')
| let pnew = RelayPriority (rankTCPPort ni) (kademliaXor kad self nid)
, pnew < p
-> delRelay rp r >> addRelay rp ni
_ -> return ()
currentRelays :: RelayPinger -> STM (Int,[TCP.NodeInfo])
currentRelays rp = second PSQ.keys <$> readTVar (relayQueue rp)
|