summaryrefslogtreecommitdiff
path: root/Connection/Tox
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-13 16:11:11 -0400
committerjoe <joe@jerkface.net>2018-06-13 16:43:54 -0400
commitbc8360faf51a058f3c1dd90145d4b87506e2ddfe (patch)
tree7dbf64254ae6690b124d11df9fd8fd5e28ef166e /Connection/Tox
parentfd3f604f8961d7c7db48015c9ed7be40ea872a7c (diff)
tox: Associate a state with each connection management thread.
Diffstat (limited to 'Connection/Tox')
-rw-r--r--Connection/Tox/Threads.hs50
1 files changed, 35 insertions, 15 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs
index dcee37d1..2ff058b3 100644
--- a/Connection/Tox/Threads.hs
+++ b/Connection/Tox/Threads.hs
@@ -102,26 +102,33 @@ data AcceptContactMethods = AcceptContactMethods
102-- | Invokes an STM action on each incoming handshake. 102-- | Invokes an STM action on each incoming handshake.
103-- 103--
104-- Does not return until getPolicy yields RefusingToConnect. 104-- Does not return until getPolicy yields RefusingToConnect.
105acceptContact :: STM Policy -> AcceptContactMethods -> IO () 105acceptContact :: STM Policy -> AcceptContactMethods -> (Status ToxProgress -> STM ()) -> IO ()
106acceptContact getPolicy AcceptContactMethods{..} = fix $ \loop -> do 106acceptContact getPolicy AcceptContactMethods{..} writeState = fix $ \loop -> do
107 join $ atomically $ do 107 join $ atomically $ do
108 orElse 108 orElse
109 (getPolicy >>= \case 109 (getPolicy >>= \case
110 RefusingToConnect -> return $ return () -- QUIT Dormant/Established 110 RefusingToConnect -> do writeState Dormant
111 return $ return () -- QUIT Dormant/Established
111 _ -> retry) 112 _ -> retry)
112 (do hs <- getHandshake 113 (do hs <- getHandshake
113 handshakeIsSuitable hs >>= \case 114 handshakeIsSuitable hs >>= \case
114 True -> do 115 True -> do
115 -- Here we allocate a NetCrypto session for handling CryptoPacket. 116 -- Here we allocate a NetCrypto session for handling CryptoPacket.
117 writeState (InProgress AwaitingSessionPacket)
116 transitionToState (InProgress AwaitingSessionPacket) 118 transitionToState (InProgress AwaitingSessionPacket)
117 return loop 119 return loop
118 False -> return loop) 120 False -> return loop)
119 121
120whileTryingAndNotEstablished :: STM Policy -> STM (Status t) -> ((Int -> IO ()) -> STM (IO ())) -> IO () 122whileTryingAndNotEstablished :: STM Policy
121whileTryingAndNotEstablished getPolicy getStatus body = fix $ \loop -> do 123 -> STM (Status t)
124 -> (Status ToxProgress -> STM ())
125 -> ((Int -> IO ()) -> STM (IO ()))
126 -> IO ()
127whileTryingAndNotEstablished getPolicy getStatus writeStatus body = fix $ \loop -> do
122 let retryWhileTrying k = getPolicy >>= \case 128 let retryWhileTrying k = getPolicy >>= \case
123 TryingToConnect -> retry 129 TryingToConnect -> retry
124 _ -> return k 130 _ -> do writeStatus Dormant
131 return k
125 ifEstablished t e = getStatus >>= \case 132 ifEstablished t e = getStatus >>= \case
126 Established -> t 133 Established -> t
127 _ -> e 134 _ -> e
@@ -147,17 +154,23 @@ data PersueContactMethods params = PersueContactMethods
147-- 154--
148-- As long as getPolicy is TryingToConnect and there is no established 155-- As long as getPolicy is TryingToConnect and there is no established
149-- connection, this function will continue. 156-- connection, this function will continue.
150persueContact :: STM Policy -> STM (Status t) -> PersueContactMethods a -> IO () 157persueContact :: STM Policy
151persueContact getPolicy getStatus PersueContactMethods{..} 158 -> STM (Status t)
152 = whileTryingAndNotEstablished getPolicy getStatus 159 -> PersueContactMethods a
160 -> (Status ToxProgress -> STM ())
161 -> IO ()
162persueContact getPolicy getStatus PersueContactMethods{..} writeStatus
163 = whileTryingAndNotEstablished getPolicy getStatus writeStatus
153 $ \retryAfterTimeout -> do 164 $ \retryAfterTimeout -> do
154 -- AwaitingDHTKey 165 -- AwaitingDHTKey
155 -- AcquiringIPAddress 166 -- AcquiringIPAddress
156 params <- getHandshakeParams 167 params <- getHandshakeParams
168 writeStatus (InProgress AcquiringCookie)
157 return $ do -- AcquiringCookie 169 return $ do -- AcquiringCookie
158 -- AwaitingHandshake 170 -- AwaitingHandshake
159 -- AwaitingSessionPacket 171 -- AwaitingSessionPacket
160 sendHandshake params 172 sendHandshake params
173 atomically $ writeStatus $ InProgress AwaitingHandshake
161 retryAfterTimeout retryInterval 174 retryAfterTimeout retryInterval
162 175
163data FreshenContactMethods = FreshenContactMethods 176data FreshenContactMethods = FreshenContactMethods
@@ -189,16 +202,20 @@ data FreshenContactMethods = FreshenContactMethods
189-- 202--
190-- As long as getPolicy is TryingToConnect and there is no established 203-- As long as getPolicy is TryingToConnect and there is no established
191-- connection, this function will continue. 204-- connection, this function will continue.
192freshenContact :: STM Policy -> STM (Status t) -> FreshenContactMethods -> IO () 205freshenContact :: STM Policy -> STM (Status t) -> FreshenContactMethods
193freshenContact getPolicy getStatus FreshenContactMethods{..} 206 -> (Status ToxProgress -> STM ())
194 = whileTryingAndNotEstablished getPolicy getStatus 207 -> IO ()
208freshenContact getPolicy getStatus FreshenContactMethods{..} writeStatus
209 = whileTryingAndNotEstablished getPolicy getStatus writeStatus
195 -- retryAfterTimeout :: Int -> IO () 210 -- retryAfterTimeout :: Int -> IO ()
196 $ \retryAfterTimeout -> 211 $ \retryAfterTimeout ->
197 getDHTKey >>= \case 212 getDHTKey >>= \case
198 Nothing -> -- AwaitingDHTKey 213 Nothing -> -- AwaitingDHTKey
199 retry 214 retry
200 Just dk -> getSockAddr >>= return . \case 215 Just dk -> getSockAddr >>= \case
201 Nothing -> -- AcquiringIPAddress 216 Nothing -> do -- AcquiringIPAddress
217 writeStatus (InProgress AcquiringIPAddress)
218 return $
202 do bkts <- atomically $ getBuckets 219 do bkts <- atomically $ getBuckets
203 st <- search nodeSch bkts dk $ 220 st <- search nodeSch bkts dk $
204 \r -> do -- TODO: store saddr, check for finish 221 \r -> do -- TODO: store saddr, check for finish
@@ -206,7 +223,10 @@ freshenContact getPolicy getStatus FreshenContactMethods{..}
206 atomically $ searchIsFinished st >>= check 223 atomically $ searchIsFinished st >>= check
207 -- TODO: searchCancel on stop condition 224 -- TODO: searchCancel on stop condition
208 retryAfterTimeout sockAddrInterval 225 retryAfterTimeout sockAddrInterval
209 Just a -> -- AcquiringCookie 226 Just a -> do
227 writeStatus (InProgress AcquiringCookie)
228 return $
229 -- AcquiringCookie
210 -- AwaitingHandshake 230 -- AwaitingHandshake
211 -- AwaitingSessionPacket 231 -- AwaitingSessionPacket
212 do _todo_search_toxid_send_dhtkey -- 123 _todo_search_toxid_send_dhtkey :: IO a0 232 do _todo_search_toxid_send_dhtkey -- 123 _todo_search_toxid_send_dhtkey :: IO a0