diff options
-rw-r--r-- | Connection/Tox/Threads.hs | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs index b3527ed2..f3357215 100644 --- a/Connection/Tox/Threads.hs +++ b/Connection/Tox/Threads.hs | |||
@@ -65,13 +65,13 @@ acceptContact getPolicy AcceptContactMethods{..} writeState = fix $ \loop -> do | |||
65 | 65 | ||
66 | whileTryingAndNotEstablished :: STM Policy | 66 | whileTryingAndNotEstablished :: STM Policy |
67 | -> STM (Status t) | 67 | -> STM (Status t) |
68 | -> (Status ToxProgress -> STM ()) | 68 | -> TVar (Status ToxProgress) |
69 | -> ((Int -> IO ()) -> STM (IO ())) | 69 | -> ((Int -> IO ()) -> STM (IO ())) |
70 | -> IO () | 70 | -> IO () |
71 | whileTryingAndNotEstablished getPolicy getStatus writeStatus body = fix $ \loop -> do | 71 | whileTryingAndNotEstablished getPolicy getStatus statusVar body = fix $ \loop -> do |
72 | let retryWhileTrying k = getPolicy >>= \case | 72 | let retryWhileTrying k = getPolicy >>= \case |
73 | TryingToConnect -> retry | 73 | TryingToConnect -> retry |
74 | _ -> do writeStatus Dormant | 74 | _ -> do writeTVar statusVar Dormant |
75 | return k | 75 | return k |
76 | ifEstablished t e = getStatus >>= \case | 76 | ifEstablished t e = getStatus >>= \case |
77 | Established -> t | 77 | Established -> t |
@@ -87,7 +87,7 @@ whileTryingAndNotEstablished getPolicy getStatus writeStatus body = fix $ \loop | |||
87 | (ifEstablished retry | 87 | (ifEstablished retry |
88 | (body retryAfterTimeout)) | 88 | (body retryAfterTimeout)) |
89 | 89 | ||
90 | data PersueContactMethods params = PersueContactMethods | 90 | data PersueContactMethods = PersueContactMethods |
91 | { allsessions :: NetCryptoSessions | 91 | { allsessions :: NetCryptoSessions |
92 | , myseckey :: SecretKey | 92 | , myseckey :: SecretKey |
93 | , theirpubkey :: PublicKey | 93 | , theirpubkey :: PublicKey |
@@ -107,28 +107,32 @@ retryUntilJust tvar = maybe retry return =<< readTVar tvar | |||
107 | -- connection, this function will continue. | 107 | -- connection, this function will continue. |
108 | persueContact :: STM Policy | 108 | persueContact :: STM Policy |
109 | -> STM (Status t) | 109 | -> STM (Status t) |
110 | -> PersueContactMethods a | 110 | -> PersueContactMethods |
111 | -> (Status ToxProgress -> STM ()) | 111 | -> TVar (Status ToxProgress) |
112 | -> IO () | 112 | -> IO () |
113 | persueContact getPolicy getStatus PersueContactMethods{..} writeStatus = do | 113 | persueContact getPolicy getStatus PersueContactMethods{..} statusVar = do |
114 | -- AwaitingDHTKey | 114 | -- AwaitingDHTKey |
115 | atomically $ writeStatus (InProgress AwaitingDHTKey) | 115 | atomically $ writeTVar statusVar (InProgress AwaitingDHTKey) |
116 | whileTryingAndNotEstablished getPolicy getStatus writeStatus | 116 | whileTryingAndNotEstablished getPolicy getStatus statusVar |
117 | $ \retryAfterTimeout -> | 117 | $ \retryAfterTimeout -> |
118 | orElse (do | 118 | orElse (do |
119 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) | 119 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) |
120 | -- We don't have an IP address yet. | 120 | -- We don't have an IP address yet. |
121 | maybe (return ()) (const retry) =<< readTVar (contactLastSeenAddr contact) | 121 | maybe (return ()) (const retry) =<< readTVar (contactLastSeenAddr contact) |
122 | return $ do -- AcquiringIPAddress | 122 | return $ do -- AcquiringIPAddress |
123 | atomically $ writeStatus (InProgress AcquiringIPAddress) | 123 | atomically $ do |
124 | retryAfterTimeout 0) | 124 | status <- readTVar statusVar |
125 | if status == InProgress AcquiringIPAddress | ||
126 | then retry | ||
127 | else writeTVar statusVar (InProgress AcquiringIPAddress) | ||
128 | retryAfterTimeout 0) | ||
125 | (do | 129 | (do |
126 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) | 130 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) |
127 | saddr <- retryUntilJust (contactLastSeenAddr contact) | 131 | saddr <- retryUntilJust (contactLastSeenAddr contact) |
128 | ni <- either (const retry) return $ nodeInfo (key2id theirDhtKey) saddr | 132 | ni <- either (const retry) return $ nodeInfo (key2id theirDhtKey) saddr |
129 | return $ do | 133 | return $ do |
130 | -- AcquiringCookie | 134 | -- AcquiringCookie |
131 | atomically $ writeStatus (InProgress AcquiringCookie) | 135 | atomically $ writeTVar statusVar (InProgress AcquiringCookie) |
132 | let mykeyAsId = key2id (toPublic myseckey) | 136 | let mykeyAsId = key2id (toPublic myseckey) |
133 | theirkeyAsId = key2id theirpubkey | 137 | theirkeyAsId = key2id theirpubkey |
134 | crypto = transportCrypto allsessions | 138 | crypto = transportCrypto allsessions |
@@ -156,7 +160,7 @@ persueContact getPolicy getStatus PersueContactMethods{..} writeStatus = do | |||
156 | -- send handshake | 160 | -- send handshake |
157 | forM myhandshake $ \response_handshake -> do | 161 | forM myhandshake $ \response_handshake -> do |
158 | sendHandshake allsessions saddr response_handshake | 162 | sendHandshake allsessions saddr response_handshake |
159 | atomically $ writeStatus $ InProgress AwaitingHandshake | 163 | atomically $ writeTVar statusVar $ InProgress AwaitingHandshake |
160 | return shortRetryInterval | 164 | return shortRetryInterval |
161 | -- AwaitingHandshake | 165 | -- AwaitingHandshake |
162 | -- AwaitingSessionPacket | 166 | -- AwaitingSessionPacket |
@@ -192,10 +196,10 @@ data FreshenContactMethods = FreshenContactMethods | |||
192 | -- As long as getPolicy is TryingToConnect and there is no established | 196 | -- As long as getPolicy is TryingToConnect and there is no established |
193 | -- connection, this function will continue. | 197 | -- connection, this function will continue. |
194 | freshenContact :: STM Policy -> STM (Status t) -> FreshenContactMethods | 198 | freshenContact :: STM Policy -> STM (Status t) -> FreshenContactMethods |
195 | -> (Status ToxProgress -> STM ()) | 199 | -> TVar (Status ToxProgress) |
196 | -> IO () | 200 | -> IO () |
197 | freshenContact getPolicy getStatus FreshenContactMethods{..} writeStatus | 201 | freshenContact getPolicy getStatus FreshenContactMethods{..} statusVar |
198 | = whileTryingAndNotEstablished getPolicy getStatus writeStatus | 202 | = whileTryingAndNotEstablished getPolicy getStatus statusVar |
199 | -- retryAfterTimeout :: Int -> IO () | 203 | -- retryAfterTimeout :: Int -> IO () |
200 | $ \retryAfterTimeout -> | 204 | $ \retryAfterTimeout -> |
201 | getDHTKey >>= \case | 205 | getDHTKey >>= \case |
@@ -203,7 +207,7 @@ freshenContact getPolicy getStatus FreshenContactMethods{..} writeStatus | |||
203 | retry | 207 | retry |
204 | Just dk -> getSockAddr >>= \case | 208 | Just dk -> getSockAddr >>= \case |
205 | Nothing -> do -- AcquiringIPAddress | 209 | Nothing -> do -- AcquiringIPAddress |
206 | writeStatus (InProgress AcquiringIPAddress) | 210 | writeTVar statusVar (InProgress AcquiringIPAddress) |
207 | return $ | 211 | return $ |
208 | do bkts <- atomically $ getBuckets | 212 | do bkts <- atomically $ getBuckets |
209 | st <- search nodeSch bkts dk $ | 213 | st <- search nodeSch bkts dk $ |
@@ -213,7 +217,7 @@ freshenContact getPolicy getStatus FreshenContactMethods{..} writeStatus | |||
213 | -- TODO: searchCancel on stop condition | 217 | -- TODO: searchCancel on stop condition |
214 | retryAfterTimeout sockAddrInterval | 218 | retryAfterTimeout sockAddrInterval |
215 | Just a -> do | 219 | Just a -> do |
216 | writeStatus (InProgress AcquiringCookie) | 220 | writeTVar statusVar (InProgress AcquiringCookie) |
217 | return $ | 221 | return $ |
218 | -- AcquiringCookie | 222 | -- AcquiringCookie |
219 | -- AwaitingHandshake | 223 | -- AwaitingHandshake |