diff options
author | joe <joe@jerkface.net> | 2018-06-16 16:15:13 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-16 16:15:13 -0400 |
commit | 2f3dc306c9416655666df8e627dabbb40fca88ab (patch) | |
tree | a0712da353b568626ea10445dae575aac311cd1e /Connection/Tox.hs | |
parent | cec17482641390147d509ad288fcc288813e4a70 (diff) |
tox: freshenContact WIP.
Diffstat (limited to 'Connection/Tox.hs')
-rw-r--r-- | Connection/Tox.hs | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/Connection/Tox.hs b/Connection/Tox.hs index 2b39ef1c..42a64a6d 100644 --- a/Connection/Tox.hs +++ b/Connection/Tox.hs | |||
@@ -6,19 +6,21 @@ module Connection.Tox | |||
6 | , ToxProgress(..) | 6 | , ToxProgress(..) |
7 | ) where | 7 | ) where |
8 | 8 | ||
9 | import qualified Connection as G | 9 | import qualified Connection as G |
10 | ;import Connection (Manager (..), Policy (..)) | 10 | ;import Connection (Manager (..), Policy (..)) |
11 | import Connection.Tox.Threads | 11 | import Connection.Tox.Threads |
12 | import Control.Concurrent.STM | 12 | import Control.Concurrent.STM |
13 | import Control.Monad | 13 | import Control.Monad |
14 | import Crypto.Tox | 14 | import Crypto.Tox |
15 | import qualified Data.HashMap.Strict as HashMap | 15 | import qualified Data.HashMap.Strict as HashMap |
16 | import qualified Data.Map as Map | 16 | import qualified Data.Map as Map |
17 | import Data.Maybe | 17 | import Data.Maybe |
18 | import Network.Kademlia.Routing as R | ||
19 | import Network.Kademlia.Search | ||
18 | import Network.Tox.ContactInfo | 20 | import Network.Tox.ContactInfo |
19 | import Network.Tox.Crypto.Handlers | 21 | import Network.Tox.Crypto.Handlers |
20 | import Network.Tox.DHT.Handlers as DHT | 22 | import Network.Tox.DHT.Handlers as DHT |
21 | import Network.Tox.DHT.Transport as DHT | 23 | import Network.Tox.DHT.Transport as DHT |
22 | import PingMachine | 24 | import PingMachine |
23 | import Text.Read | 25 | import Text.Read |
24 | #ifdef THREAD_DEBUG | 26 | #ifdef THREAD_DEBUG |
@@ -147,14 +149,20 @@ setToxPolicy params conmap k@(Key me them) policy = case policy of | |||
147 | , longRetryInterval = _todo | 149 | , longRetryInterval = _todo |
148 | , contact = c | 150 | , contact = c |
149 | } | 151 | } |
152 | sch = nodeSearch (dhtClient params) (nodesOfInterest $ dhtRouting params) | ||
150 | freshen_methods = FreshenContactMethods | 153 | freshen_methods = FreshenContactMethods |
151 | { dhtkeyInterval = _todo :: Int | 154 | { dhtkeyInterval = _todo :: Int |
152 | , sockAddrInterval = _todo :: Int | 155 | , sockAddrInterval = _todo :: Int |
153 | , nodeSch = nodeSearch (dhtClient params) | 156 | , nodeSch = sch |
154 | (nodesOfInterest $ dhtRouting params) | ||
155 | , getDHTKey = retry :: STM (Maybe NodeId) | 157 | , getDHTKey = retry :: STM (Maybe NodeId) |
156 | , getSockAddr = retry -- :: STM (Maybe SockAddr) | 158 | , getSockAddr = retry -- :: STM (Maybe SockAddr) |
157 | , getBuckets = retry -- :: STM (BucketList NodeInfo) | 159 | , nearestNodes = \nid -> do |
160 | bkts4 <- readTVar $ routing4 $ dhtRouting params | ||
161 | bkts6 <- readTVar $ routing6 $ dhtRouting params | ||
162 | let interweave [] ys = ys | ||
163 | interweave (x:xs) ys = x : interweave ys xs | ||
164 | return $ interweave (R.kclosest (searchSpace sch) searchK nid bkts4) | ||
165 | (R.kclosest (searchSpace sch) searchK nid bkts6) | ||
158 | } | 166 | } |
159 | get_status = do | 167 | get_status = do |
160 | sbk <- readTVar $ netCryptoSessionsByKey (sessions params) | 168 | sbk <- readTVar $ netCryptoSessionsByKey (sessions params) |
@@ -187,7 +195,7 @@ setToxPolicy params conmap k@(Key me them) policy = case policy of | |||
187 | return () | 195 | return () |
188 | RefusingToConnect -> do -- disconnect or cancel any pending connection | 196 | RefusingToConnect -> do -- disconnect or cancel any pending connection |
189 | mst <- lookupForPolicyChange conmap k policy | 197 | mst <- lookupForPolicyChange conmap k policy |
190 | -- Since the 3 connection threads poll the current policy, they should | 198 | -- Since the connection threads poll the current policy, they should |
191 | -- all terminate on their own. | 199 | -- all terminate on their own. |
192 | -- | 200 | -- |
193 | -- Here we block until they finish. | 201 | -- Here we block until they finish. |
@@ -205,6 +213,8 @@ setToxPolicy params conmap k@(Key me them) policy = case policy of | |||
205 | _ -> retry | 213 | _ -> retry |
206 | OpenToConnect -> do -- passively accept connections if they initiate. | 214 | OpenToConnect -> do -- passively accept connections if they initiate. |
207 | mst <- lookupForPolicyChange conmap k policy | 215 | mst <- lookupForPolicyChange conmap k policy |
216 | r <- atomically $ lookupContact k (roster params) | ||
217 | forM_ r $ \(sec,c) -> do | ||
208 | forM_ mst $ \st -> do | 218 | forM_ mst $ \st -> do |
209 | {- | 219 | {- |
210 | let getPolicy = readTVar $ connPolicy st | 220 | let getPolicy = readTVar $ connPolicy st |
@@ -218,8 +228,12 @@ setToxPolicy params conmap k@(Key me them) policy = case policy of | |||
218 | registerNodeCallback routing $ NodeInfoCallback | 228 | registerNodeCallback routing $ NodeInfoCallback |
219 | { interestingNodeId = nid | 229 | { interestingNodeId = nid |
220 | , listenerId = callbackId | 230 | , listenerId = callbackId |
221 | , observedAddress = \ni -> return () -- TODO | 231 | , observedAddress = \ni -> writeTVar (contactLastSeenAddr c) (Just $ nodeAddr ni) |
222 | , rumoredAddress = \saddr ni -> return () -- TODO | 232 | , rumoredAddress = \saddr ni -> do |
233 | m <- readTVar (contactLastSeenAddr c) | ||
234 | -- TODO remember information source and handle multiple rumors. | ||
235 | case m of Just _ -> return () | ||
236 | Nothing -> writeTVar (contactLastSeenAddr c) (Just $ nodeAddr ni) | ||
223 | } | 237 | } |
224 | 238 | ||
225 | stringToKey_ :: String -> Maybe Key | 239 | stringToKey_ :: String -> Maybe Key |