diff options
author | joe <joe@jerkface.net> | 2018-06-16 03:17:55 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-16 03:17:55 -0400 |
commit | 5f60c316724df294c48bc4d99d73f2b3d6b9a23d (patch) | |
tree | 4bea067058ab937c6a73399dce3852feeb9c2f50 /Connection/Tox/Threads.hs | |
parent | 8d6a50dde334a2dc4039c0c718a992e4df36402f (diff) |
tox: persueContact, fixed build errors.
Diffstat (limited to 'Connection/Tox/Threads.hs')
-rw-r--r-- | Connection/Tox/Threads.hs | 105 |
1 files changed, 54 insertions, 51 deletions
diff --git a/Connection/Tox/Threads.hs b/Connection/Tox/Threads.hs index 3cf9486d..b3527ed2 100644 --- a/Connection/Tox/Threads.hs +++ b/Connection/Tox/Threads.hs | |||
@@ -29,6 +29,8 @@ import Control.Concurrent.STM | |||
29 | import Control.Monad | 29 | import Control.Monad |
30 | import Data.Function | 30 | import Data.Function |
31 | import Data.Functor.Identity | 31 | import Data.Functor.Identity |
32 | import Data.Time.Clock.POSIX | ||
33 | import System.IO | ||
32 | import System.Timeout | 34 | import System.Timeout |
33 | 35 | ||
34 | 36 | ||
@@ -96,11 +98,7 @@ data PersueContactMethods params = PersueContactMethods | |||
96 | } | 98 | } |
97 | 99 | ||
98 | retryUntilJust :: TVar (Maybe a) -> STM a | 100 | retryUntilJust :: TVar (Maybe a) -> STM a |
99 | retryUntilJust tvar = do | 101 | retryUntilJust tvar = maybe retry return =<< readTVar tvar |
100 | mb <- readTVar tvar | ||
101 | case mb of | ||
102 | mempty -> retry | ||
103 | Just x -> return x | ||
104 | 102 | ||
105 | -- | Continuously attempt to send handshake packets until a connection is | 103 | -- | Continuously attempt to send handshake packets until a connection is |
106 | -- established. | 104 | -- established. |
@@ -112,52 +110,57 @@ persueContact :: STM Policy | |||
112 | -> PersueContactMethods a | 110 | -> PersueContactMethods a |
113 | -> (Status ToxProgress -> STM ()) | 111 | -> (Status ToxProgress -> STM ()) |
114 | -> IO () | 112 | -> IO () |
115 | persueContact getPolicy getStatus PersueContactMethods{..} writeStatus | 113 | persueContact getPolicy getStatus PersueContactMethods{..} writeStatus = do |
116 | = whileTryingAndNotEstablished getPolicy getStatus writeStatus | 114 | -- AwaitingDHTKey |
117 | $ \retryAfterTimeout -> do | 115 | atomically $ writeStatus (InProgress AwaitingDHTKey) |
118 | -- AwaitingDHTKey | 116 | whileTryingAndNotEstablished getPolicy getStatus writeStatus |
119 | atomically $ writeStatus (InProgress AwaitingDHTKey) | 117 | $ \retryAfterTimeout -> |
120 | keypkt <- atomically $ retryUntilJust (contactKeyPacket contact) | 118 | orElse (do |
121 | let theirDhtKey = DHT.dhtpk keypkt | 119 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) |
122 | -- AcquiringIPAddress | 120 | -- We don't have an IP address yet. |
123 | atomically $ writeStatus (InProgress AcquiringIPAddress) | 121 | maybe (return ()) (const retry) =<< readTVar (contactLastSeenAddr contact) |
124 | ni <- atomically $ do | 122 | return $ do -- AcquiringIPAddress |
125 | saddr <- retryUntilJust (contactLastSeenAddr contact) | 123 | atomically $ writeStatus (InProgress AcquiringIPAddress) |
126 | either retry return $ nodeInfo (key2id theirDhtKey) saddr | 124 | retryAfterTimeout 0) |
127 | let mykeyAsId = key2id (toPublic myseckey) | 125 | (do |
128 | theirkeyAsId = key2id theirpubkey | 126 | theirDhtKey <- DHT.dhtpk <$> retryUntilJust (contactKeyPacket contact) |
129 | crypto = transportCrypto allsessions | 127 | saddr <- retryUntilJust (contactLastSeenAddr contact) |
130 | -- AcquiringCookie | 128 | ni <- either (const retry) return $ nodeInfo (key2id theirDhtKey) saddr |
131 | atomically $ writeStatus (InProgress AcquiringCookie) | 129 | return $ do |
132 | mbCookie <- -- TODO: Check for recent cached cookie. | 130 | -- AcquiringCookie |
133 | DHT.cookieRequest crypto client (toPublic myseckey) ni | 131 | atomically $ writeStatus (InProgress AcquiringCookie) |
134 | interval <- case mbCookie of | 132 | let mykeyAsId = key2id (toPublic myseckey) |
135 | Nothing -> do | 133 | theirkeyAsId = key2id theirpubkey |
136 | hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").") | 134 | crypto = transportCrypto allsessions |
137 | hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy") | 135 | mbCookie <- -- TODO: Check for recent cached cookie. |
138 | return longRetryInterval | 136 | DHT.cookieRequest crypto client (toPublic myseckey) ni |
139 | Just cookie -> do | 137 | interval <- case mbCookie of |
140 | hPutStrLn stderr "Have cookie, creating handshake packet..." | 138 | Nothing -> do |
141 | let hp = HParam { hpOtherCookie = cookie | 139 | hPutStrLn stderr ("persueContact: (" ++ show mykeyAsId ++") <--> (" ++ show theirkeyAsId ++ ").") |
142 | , hpMySecretKey = myseckey | 140 | hPutStrLn stderr ("persueContact: CookieRequest failed. TODO: dhtpkNodes thingy") |
143 | , hpCookieRemotePubkey = theirpubkey | 141 | return longRetryInterval |
144 | , hpCookieRemoteDhtkey = theirDhtKey | 142 | Just cookie -> do |
145 | , hpTheirBaseNonce = Nothing | 143 | hPutStrLn stderr "Have cookie, creating handshake packet..." |
146 | , hpTheirSessionKeyPublic = Nothing | 144 | let hp = HParam { hpOtherCookie = cookie |
147 | } | 145 | , hpMySecretKey = myseckey |
148 | newsession <- generateSecretKey | 146 | , hpCookieRemotePubkey = theirpubkey |
149 | timestamp <- getPOSIXTime | 147 | , hpCookieRemoteDhtkey = theirDhtKey |
150 | (myhandshake,ioAction) | 148 | , hpTheirBaseNonce = Nothing |
151 | <- atomically $ freshCryptoSession allsessions saddr newsession timestamp hp | 149 | , hpTheirSessionKeyPublic = Nothing |
152 | ioAction | 150 | } |
153 | -- send handshake | 151 | newsession <- generateSecretKey |
154 | forM myhandshake $ \response_handshake -> do | 152 | timestamp <- getPOSIXTime |
155 | sendHandshake (toxCryptoSessions tox) saddr response_handshake | 153 | (myhandshake,ioAction) |
156 | atomically $ writeStatus $ InProgress AwaitingHandshake | 154 | <- atomically $ freshCryptoSession allsessions saddr newsession timestamp hp |
157 | return shortRetryInterval | 155 | ioAction |
158 | -- AwaitingHandshake | 156 | -- send handshake |
159 | -- AwaitingSessionPacket | 157 | forM myhandshake $ \response_handshake -> do |
160 | retryAfterTimeout interval | 158 | sendHandshake allsessions saddr response_handshake |
159 | atomically $ writeStatus $ InProgress AwaitingHandshake | ||
160 | return shortRetryInterval | ||
161 | -- AwaitingHandshake | ||
162 | -- AwaitingSessionPacket | ||
163 | retryAfterTimeout interval) | ||
161 | 164 | ||
162 | data FreshenContactMethods = FreshenContactMethods | 165 | data FreshenContactMethods = FreshenContactMethods |
163 | { dhtkeyInterval :: Int | 166 | { dhtkeyInterval :: Int |