summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-16 13:42:19 -0400
committerjoe <joe@jerkface.net>2018-06-16 13:42:19 -0400
commitef58d2af90f387960e188365b0dfdd507fcbebc8 (patch)
tree4f38729ea319e52775b975afc9ac143c41e06fef
parentc65f59c83c787e14efb5a58d32807b3bcb300de5 (diff)
Fill in needed parameters to toxManager.
-rw-r--r--Connection/Tox.hs48
-rw-r--r--src/Network/Tox.hs37
2 files changed, 57 insertions, 28 deletions
diff --git a/Connection/Tox.hs b/Connection/Tox.hs
index 2c485edd..436e7599 100644
--- a/Connection/Tox.hs
+++ b/Connection/Tox.hs
@@ -1,21 +1,25 @@
1{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
2{-# LANGUAGE GADTs #-} 2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE NamedFieldPuns #-}
3module Connection.Tox 4module Connection.Tox
4 ( module Connection.Tox 5 ( module Connection.Tox
5 , ToxProgress(..) 6 , ToxProgress(..)
6 ) where 7 ) where
7 8
8import qualified Connection as G 9import qualified Connection as G
9 ;import Connection (Manager (..), Policy (..)) 10 ;import Connection (Manager (..), Policy (..))
11import Connection.Tox.Threads
10import Control.Concurrent.STM 12import Control.Concurrent.STM
11import Control.Monad 13import Control.Monad
12-- import Data.Dependent.Sum 14import Crypto.Tox
13import Data.Functor.Identity 15import Data.Functor.Identity
14import qualified Data.Map as Map 16import qualified Data.HashMap.Strict as HashMap
15import Connection.Tox.Threads 17import qualified Data.Map as Map
16import Network.Tox.NodeId 18import Network.Tox.ContactInfo
17import Network.Tox.DHT.Handlers
18import Network.Tox.Crypto.Handlers 19import Network.Tox.Crypto.Handlers
20import Network.Tox.DHT.Handlers as DHT
21import Network.Tox.DHT.Transport as DHT
22import Network.Tox.NodeId
19import PingMachine 23import PingMachine
20import Text.Read 24import Text.Read
21#ifdef THREAD_DEBUG 25#ifdef THREAD_DEBUG
@@ -24,7 +28,7 @@ import Control.Concurrent.Lifted.Instrument
24import Control.Concurrent.Lifted 28import Control.Concurrent.Lifted
25import GHC.Conc (labelThread) 29import GHC.Conc (labelThread)
26#endif 30#endif
27import GHC.Conc (threadStatus,ThreadStatus(..)) 31import GHC.Conc (ThreadStatus (..), threadStatus)
28 32
29 33
30 34
@@ -32,6 +36,9 @@ import GHC.Conc (threadStatus,ThreadStatus(..))
32data Parameters = Parameters 36data Parameters = Parameters
33 { -- | Various Tox transports and clients. 37 { -- | Various Tox transports and clients.
34 dhtRouting :: Routing 38 dhtRouting :: Routing
39 , roster :: ContactInfo
40 , sessions :: NetCryptoSessions
41 , dhtClient :: DHT.Client
35 -- | Thread to be forked when a connection is established. 42 -- | Thread to be forked when a connection is established.
36 -- TODO: this function should accept relevant parameters. 43 -- TODO: this function should accept relevant parameters.
37 , onToxSession :: IO () 44 , onToxSession :: IO ()
@@ -113,22 +120,38 @@ lookupForPolicyChange conmap k policy = atomically $ do
113callbackId :: Int 120callbackId :: Int
114callbackId = 1 121callbackId = 1
115 122
123lookupContact :: Key -> ContactInfo -> STM (Maybe (SecretKey,Contact))
124lookupContact (Key me them) ContactInfo{accounts} = do
125 acnts <- readTVar accounts
126 fmap join $ forM (HashMap.lookup me acnts) $ \Account{userSecret,contacts} -> do
127 cs <- readTVar contacts
128 forM (HashMap.lookup them cs) $ \c -> do
129 return (userSecret,c)
130
116-- | This function will fork threads as necessary. 131-- | This function will fork threads as necessary.
117setToxPolicy :: Parameters 132setToxPolicy :: Parameters
118 -> TVar (Map.Map Key SessionState) 133 -> TVar (Map.Map Key SessionState)
119 -> Key 134 -> Key
120 -> Policy 135 -> Policy
121 -> IO () 136 -> IO ()
122setToxPolicy params conmap k policy = case policy of 137setToxPolicy params conmap k@(Key me them) policy = case policy of
123 TryingToConnect -> do 138 TryingToConnect -> do
124 mst <- lookupForPolicyChange conmap k policy 139 mst <- lookupForPolicyChange conmap k policy
140 r <- atomically $ lookupContact k (roster params)
141 forM_ r $ \(sec,c) -> do
125 let accept_methods = AcceptContactMethods 142 let accept_methods = AcceptContactMethods
126 { getHandshake = retry -- :: STM (Handshake Identity) 143 { getHandshake = retry -- :: STM (Handshake Identity)
127 , handshakeIsSuitable = (\_ -> return False) -- :: Handshake Identity -> STM Bool 144 , handshakeIsSuitable = (\_ -> return False) -- :: Handshake Identity -> STM Bool
128 , transitionToState = (\_ -> return ()) :: G.Status ToxProgress -> STM () 145 , transitionToState = (\_ -> return ()) :: G.Status ToxProgress -> STM ()
129 } 146 }
130 persue_methods = PersueContactMethods 147 persue_methods = PersueContactMethods
131 { -- TODO 148 { allsessions = sessions params
149 , myseckey = sec
150 , theirpubkey = id2key them
151 , client = dhtClient params
152 , shortRetryInterval = _todo
153 , longRetryInterval = _todo
154 , contact = c
132 } 155 }
133 freshen_methods = FreshenContactMethods 156 freshen_methods = FreshenContactMethods
134 { dhtkeyInterval = _todo :: Int 157 { dhtkeyInterval = _todo :: Int
@@ -138,6 +161,7 @@ setToxPolicy params conmap k policy = case policy of
138 , getSockAddr = retry -- :: STM (Maybe SockAddr) 161 , getSockAddr = retry -- :: STM (Maybe SockAddr)
139 , getBuckets = retry -- :: STM (BucketList NodeInfo) 162 , getBuckets = retry -- :: STM (BucketList NodeInfo)
140 } 163 }
164 get_status = readTVar (ncState _todo)
141 forM_ mst $ \st -> do 165 forM_ mst $ \st -> do
142 let getPolicy = readTVar $ connPolicy st 166 let getPolicy = readTVar $ connPolicy st
143 tasks <- atomically $ readTVar (sessionTasks st) 167 tasks <- atomically $ readTVar (sessionTasks st)
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 091c268d..29b980b7 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -1,4 +1,3 @@
1{-# LANGUAGE ViewPatterns #-}
2{-# LANGUAGE CPP #-} 1{-# LANGUAGE CPP #-}
3{-# LANGUAGE DeriveDataTypeable #-} 2{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE DeriveFoldable #-} 3{-# LANGUAGE DeriveFoldable #-}
@@ -12,8 +11,10 @@
12{-# LANGUAGE NamedFieldPuns #-} 11{-# LANGUAGE NamedFieldPuns #-}
13{-# LANGUAGE PatternSynonyms #-} 12{-# LANGUAGE PatternSynonyms #-}
14{-# LANGUAGE RankNTypes #-} 13{-# LANGUAGE RankNTypes #-}
14{-# LANGUAGE RecursiveDo #-}
15{-# LANGUAGE ScopedTypeVariables #-} 15{-# LANGUAGE ScopedTypeVariables #-}
16{-# LANGUAGE TupleSections #-} 16{-# LANGUAGE TupleSections #-}
17{-# LANGUAGE ViewPatterns #-}
17module Network.Tox where 18module Network.Tox where
18 19
19import Debug.Trace 20import Debug.Trace
@@ -446,21 +447,25 @@ newTox keydb addr mbSessionsState suppliedDHTKey = do
446 tbl6 = DHT.routing6 $ mkrouting (error "missing client") 447 tbl6 = DHT.routing6 $ mkrouting (error "missing client")
447 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id 448 dhtclient <- newClient drg dhtnet0 DHT.classify (myAddr tbl4 tbl6) (DHT.handlers crypto . mkrouting) id
448 $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) orouter) net 449 $ \client net -> onInbound (DHT.updateRouting client (mkrouting client) orouter) net
449 mgr <- toxManager (Parameters { dhtRouting = mkrouting dhtclient 450 -- TODO: Refactor so this recursive do is unnecessary.
450 , onToxSession = return () -- TODO 451 rec (mgr,sessionsState) <- do
451 }) 452 mgr <- toxManager (Parameters { dhtRouting = mkrouting dhtclient
452 453 , roster = roster
453 let policylookup key = do 454 , sessions = sessionsState
454 mp <- connections mgr 455 , dhtClient = dhtclient
455 case Map.lookup key mp of 456 , onToxSession = return () -- TODO
456 Nothing -> return OpenToConnect 457 })
457 Just conn -> Connection.connPolicy conn 458 let policylookup key = do
458 459 mp <- connections mgr
459 let sessionsState = sessionsState0 { sendHandshake = sendMessage handshakes 460 case Map.lookup key mp of
460 , sendSessionPacket = sendMessage cryptonet 461 Nothing -> return OpenToConnect
461 , transportCrypto = crypto 462 Just conn -> Connection.connPolicy conn
462 , netCryptoPolicyByKey = policylookup 463
463 } 464 return (mgr, sessionsState0 { sendHandshake = sendMessage handshakes
465 , sendSessionPacket = sendMessage cryptonet
466 , transportCrypto = crypto
467 , netCryptoPolicyByKey = policylookup
468 })
464 469
465 orouter' <- forkRouteBuilder orouter 470 orouter' <- forkRouteBuilder orouter
466 $ \nid ni -> fmap (\(_,ns,_)->ns) 471 $ \nid ni -> fmap (\(_,ns,_)->ns)