summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Connection/Tox.hs37
1 files changed, 29 insertions, 8 deletions
diff --git a/Connection/Tox.hs b/Connection/Tox.hs
index b0da494a..0156806e 100644
--- a/Connection/Tox.hs
+++ b/Connection/Tox.hs
@@ -80,6 +80,8 @@ launch lbl st f = do
80 stvar <- newTVarIO st 80 stvar <- newTVarIO st
81 tid <- forkIO (f stvar) 81 tid <- forkIO (f stvar)
82 labelThread tid lbl 82 labelThread tid lbl
83 stat <- threadStatus tid
84 hPutStrLn stderr $ "launch "++lbl++" "++show stat
83 return $ StatefulTask tid stvar 85 return $ StatefulTask tid stvar
84 86
85 87
@@ -95,6 +97,20 @@ data SessionState = SessionState
95 -- , transient :: TVar (DSum Transient Identity) 97 -- , transient :: TVar (DSum Transient Identity)
96 } 98 }
97 99
100newSessionState :: IO SessionState
101newSessionState = do
102 pings <- forkPingMachine "SessionState"
103 25000 -- 25 ms send ping
104 30000 -- 30 ms timed out
105 a <- fork $ return ()
106 b <- fork $ return ()
107 atomically $ do
108 av <- newTVar G.Dormant
109 bv <- newTVar G.Dormant
110 let tasks = SessionTasks (StatefulTask a av) (StatefulTask b bv)
111 SessionState <$> newTVar G.RefusingToConnect <*> pure pings <*> newTVar tasks
112
113
98sessionStatus :: SessionState -> G.Connection ToxProgress 114sessionStatus :: SessionState -> G.Connection ToxProgress
99sessionStatus st = G.Connection 115sessionStatus st = G.Connection
100 { G.connStatus = combinedStatus =<< readTVar (sessionTasks st) 116 { G.connStatus = combinedStatus =<< readTVar (sessionTasks st)
@@ -110,14 +126,17 @@ combinedStatus tasks = do
110 126
111lookupForPolicyChange :: TVar (Map.Map Key SessionState) 127lookupForPolicyChange :: TVar (Map.Map Key SessionState)
112 -> Key -> Policy -> IO (Maybe SessionState) 128 -> Key -> Policy -> IO (Maybe SessionState)
113lookupForPolicyChange conmap k policy = atomically $ do 129lookupForPolicyChange conmap k policy = do
114 cons <- readTVar conmap 130 cons <- atomically $ readTVar conmap
115 fmap join $ forM (Map.lookup k cons) $ \st -> do 131 st <- case Map.lookup k cons of
116 p <- readTVar (connPolicy st) 132 Nothing -> newSessionState
117 writeTVar (connPolicy st) policy 133 Just st -> return st
118 return $ do 134 atomically $ do
119 guard $ p /= policy 135 p <- readTVar (connPolicy st)
120 return st 136 writeTVar (connPolicy st) policy
137 return $ do
138 guard $ p /= policy
139 return st
121 140
122callbackId :: Int 141callbackId :: Int
123callbackId = 1 142callbackId = 1
@@ -142,6 +161,7 @@ setToxPolicy params conmap k@(Key me them) policy = do
142 TryingToConnect -> do 161 TryingToConnect -> do
143 mst <- lookupForPolicyChange conmap k policy 162 mst <- lookupForPolicyChange conmap k policy
144 r <- atomically $ lookupContact k (roster params) 163 r <- atomically $ lookupContact k (roster params)
164 hPutStrLn stderr $ "C.r="++show (fmap (const ()) r)
145 forM_ r $ \(sec,c) -> do 165 forM_ r $ \(sec,c) -> do
146 let persue_methods = PersueContactMethods 166 let persue_methods = PersueContactMethods
147 { allsessions = sessions params 167 { allsessions = sessions params
@@ -172,6 +192,7 @@ setToxPolicy params conmap k@(Key me them) policy = do
172 fmap (fromMaybe G.Dormant) $ forM (Map.lookup (id2key them) sbk) $ \ss -> do 192 fmap (fromMaybe G.Dormant) $ forM (Map.lookup (id2key them) sbk) $ \ss -> do
173 stats <- mapM (readTVar . ncState) ss 193 stats <- mapM (readTVar . ncState) ss
174 return $ maximum stats 194 return $ maximum stats
195 hPutStrLn stderr $ "C.mst="++show (fmap (const ()) mst)
175 forM_ mst $ \st -> do 196 forM_ mst $ \st -> do
176 let getPolicy = readTVar $ connPolicy st 197 let getPolicy = readTVar $ connPolicy st
177 tasks <- atomically $ readTVar (sessionTasks st) 198 tasks <- atomically $ readTVar (sessionTasks st)