summaryrefslogtreecommitdiff
path: root/Connection/Tox.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-16 16:15:13 -0400
committerjoe <joe@jerkface.net>2018-06-16 16:15:13 -0400
commit2f3dc306c9416655666df8e627dabbb40fca88ab (patch)
treea0712da353b568626ea10445dae575aac311cd1e /Connection/Tox.hs
parentcec17482641390147d509ad288fcc288813e4a70 (diff)
tox: freshenContact WIP.
Diffstat (limited to 'Connection/Tox.hs')
-rw-r--r--Connection/Tox.hs38
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
9import qualified Connection as G 9import qualified Connection as G
10 ;import Connection (Manager (..), Policy (..)) 10 ;import Connection (Manager (..), Policy (..))
11import Connection.Tox.Threads 11import Connection.Tox.Threads
12import Control.Concurrent.STM 12import Control.Concurrent.STM
13import Control.Monad 13import Control.Monad
14import Crypto.Tox 14import Crypto.Tox
15import qualified Data.HashMap.Strict as HashMap 15import qualified Data.HashMap.Strict as HashMap
16import qualified Data.Map as Map 16import qualified Data.Map as Map
17import Data.Maybe 17import Data.Maybe
18import Network.Kademlia.Routing as R
19import Network.Kademlia.Search
18import Network.Tox.ContactInfo 20import Network.Tox.ContactInfo
19import Network.Tox.Crypto.Handlers 21import Network.Tox.Crypto.Handlers
20import Network.Tox.DHT.Handlers as DHT 22import Network.Tox.DHT.Handlers as DHT
21import Network.Tox.DHT.Transport as DHT 23import Network.Tox.DHT.Transport as DHT
22import PingMachine 24import PingMachine
23import Text.Read 25import 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
225stringToKey_ :: String -> Maybe Key 239stringToKey_ :: String -> Maybe Key