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 | |
parent | cec17482641390147d509ad288fcc288813e4a70 (diff) |
tox: freshenContact WIP.
Diffstat (limited to 'Connection')
-rw-r--r-- | Connection/Tox.hs | 38 | ||||
-rw-r--r-- | Connection/Tox/Threads.hs | 20 |
2 files changed, 40 insertions, 18 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 |
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs index 73c83338..5602fa40 100644 --- a/Connection/Tox/Threads.hs +++ b/Connection/Tox/Threads.hs | |||
@@ -8,6 +8,7 @@ | |||
8 | -- | 8 | -- |
9 | -- * 'freshenContact' | 9 | -- * 'freshenContact' |
10 | -- | 10 | -- |
11 | {-# LANGUAGE CPP #-} | ||
11 | {-# LANGUAGE LambdaCase #-} | 12 | {-# LANGUAGE LambdaCase #-} |
12 | module Connection.Tox.Threads where | 13 | module Connection.Tox.Threads where |
13 | 14 | ||
@@ -24,6 +25,12 @@ import Network.Tox.DHT.Transport as DHT (dhtpk) | |||
24 | import Network.Socket | 25 | import Network.Socket |
25 | import Network.Kademlia.Search | 26 | import Network.Kademlia.Search |
26 | import Network.Kademlia.Routing (BucketList) | 27 | import Network.Kademlia.Routing (BucketList) |
28 | #ifdef THREAD_DEBUG | ||
29 | import Control.Concurrent.Lifted.Instrument | ||
30 | #else | ||
31 | import Control.Concurrent.Lifted | ||
32 | import GHC.Conc (labelThread) | ||
33 | #endif | ||
27 | 34 | ||
28 | import Control.Concurrent.STM | 35 | import Control.Concurrent.STM |
29 | import Control.Monad | 36 | import Control.Monad |
@@ -169,7 +176,7 @@ data FreshenContactMethods = FreshenContactMethods | |||
169 | , nodeSch :: NodeSearch | 176 | , nodeSch :: NodeSearch |
170 | , getDHTKey :: STM (Maybe NodeId) | 177 | , getDHTKey :: STM (Maybe NodeId) |
171 | , getSockAddr :: STM (Maybe SockAddr) | 178 | , getSockAddr :: STM (Maybe SockAddr) |
172 | , getBuckets :: STM (BucketList NodeInfo) | 179 | , nearestNodes :: NodeId -> STM [NodeInfo] |
173 | } | 180 | } |
174 | 181 | ||
175 | -- send my dht key | 182 | -- send my dht key |
@@ -206,12 +213,13 @@ freshenContact getPolicy getStatus FreshenContactMethods{..} statusVar | |||
206 | Nothing -> do -- AcquiringIPAddress | 213 | Nothing -> do -- AcquiringIPAddress |
207 | writeTVar statusVar (InProgress AcquiringIPAddress) | 214 | writeTVar statusVar (InProgress AcquiringIPAddress) |
208 | return $ | 215 | return $ |
209 | do bkts <- atomically $ getBuckets | 216 | do st <- atomically $ do |
210 | st <- search nodeSch bkts dk $ | 217 | ns <- nearestNodes dk |
211 | \r -> do -- TODO: store saddr, check for finish | 218 | newSearch nodeSch dk ns |
212 | return True | 219 | -- forked simply to avoid relabeling this thread. |
220 | forkIO $ searchLoop nodeSch dk (const $ return True) st | ||
221 | -- TODO: searchCancel on stop condition | ||
213 | atomically $ searchIsFinished st >>= check | 222 | atomically $ searchIsFinished st >>= check |
214 | -- TODO: searchCancel on stop condition | ||
215 | retryAfterTimeout sockAddrInterval | 223 | retryAfterTimeout sockAddrInterval |
216 | Just a -> do | 224 | Just a -> do |
217 | writeTVar statusVar (InProgress AcquiringCookie) | 225 | writeTVar statusVar (InProgress AcquiringCookie) |