mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Refactored poll loop
This enables breaking the loop from the outside and cleans up the countdown
This commit is contained in:
parent
eeae741262
commit
ead579a6e9
2 changed files with 20 additions and 33 deletions
|
@ -60,7 +60,6 @@ class ProxyPair
|
||||||
log 'Invalid SDP message.'
|
log 'Invalid SDP message.'
|
||||||
return false
|
return false
|
||||||
dbg 'SDP ' + offer.type + ' successfully received.'
|
dbg 'SDP ' + offer.type + ' successfully received.'
|
||||||
@active = true
|
|
||||||
true
|
true
|
||||||
|
|
||||||
# Given a WebRTC DataChannel, prepare callbacks.
|
# Given a WebRTC DataChannel, prepare callbacks.
|
||||||
|
|
|
@ -13,6 +13,7 @@ TODO: More documentation
|
||||||
class Snowflake
|
class Snowflake
|
||||||
relayAddr: null
|
relayAddr: null
|
||||||
rateLimit: null
|
rateLimit: null
|
||||||
|
pollInterval: null
|
||||||
retries: 0
|
retries: 0
|
||||||
|
|
||||||
# Janky state machine
|
# Janky state machine
|
||||||
|
@ -54,43 +55,29 @@ class Snowflake
|
||||||
@makeProxyPair @relayAddr
|
@makeProxyPair @relayAddr
|
||||||
log 'ProxyPair Slots: ' + @proxyPairs.length
|
log 'ProxyPair Slots: ' + @proxyPairs.length
|
||||||
log 'Snowflake IDs: ' + (@proxyPairs.map (p) -> p.id).join ' | '
|
log 'Snowflake IDs: ' + (@proxyPairs.map (p) -> p.id).join ' | '
|
||||||
@pollBroker()
|
@pollInterval = setInterval((=> @pollBroker()), config.defaultBrokerPollInterval)
|
||||||
|
log @pollInterval
|
||||||
|
|
||||||
# Regularly poll Broker for clients to serve until this snowflake is
|
# Regularly poll Broker for clients to serve until this snowflake is
|
||||||
# serving at capacity, at which point stop polling.
|
# serving at capacity, at which point stop polling.
|
||||||
pollBroker: ->
|
pollBroker: ->
|
||||||
# Temporary countdown. TODO: Simplify
|
|
||||||
countdown = (msg, sec, skip) =>
|
|
||||||
if not skip then dbg msg
|
|
||||||
if sec > 0
|
|
||||||
@ui.setStatus msg + ' (Polling in ' + sec + ' seconds...)'
|
|
||||||
sec--
|
|
||||||
setTimeout((-> countdown(msg, sec, true)), 1000)
|
|
||||||
else
|
|
||||||
@ui.setStatus msg
|
|
||||||
findClients()
|
|
||||||
# Poll broker for clients.
|
# Poll broker for clients.
|
||||||
findClients = =>
|
|
||||||
pair = @nextAvailableProxyPair()
|
pair = @nextAvailableProxyPair()
|
||||||
if !pair
|
if !pair
|
||||||
log 'At client capacity.'
|
log 'At client capacity.'
|
||||||
# Do nothing until a new proxyPair is available.
|
# Do nothing until a new proxyPair is available.
|
||||||
return
|
return
|
||||||
|
pair.active = true
|
||||||
msg = 'Polling for client ... '
|
msg = 'Polling for client ... '
|
||||||
msg += '[retries: ' + @retries + ']' if @retries > 0
|
msg += '[retries: ' + @retries + ']' if @retries > 0
|
||||||
@ui.setStatus msg
|
@ui.setStatus msg
|
||||||
recv = @broker.getClientOffer pair.id
|
recv = @broker.getClientOffer pair.id
|
||||||
recv.then (desc) =>
|
recv.then (desc) =>
|
||||||
@receiveOffer pair, desc
|
@receiveOffer pair, desc
|
||||||
countdown(
|
|
||||||
'Serving 1 new client.',
|
|
||||||
@config.defaultBrokerPollInterval / 1000
|
|
||||||
)
|
|
||||||
, (err) =>
|
, (err) =>
|
||||||
countdown(err, @config.defaultBrokerPollInterval / 1000)
|
pair.active = false
|
||||||
@retries++
|
@retries++
|
||||||
|
|
||||||
findClients()
|
|
||||||
|
|
||||||
# Returns the first ProxyPair that's available to connect.
|
# Returns the first ProxyPair that's available to connect.
|
||||||
nextAvailableProxyPair: ->
|
nextAvailableProxyPair: ->
|
||||||
|
@ -99,7 +86,6 @@ class Snowflake
|
||||||
# Receive an SDP offer from some client assigned by the Broker,
|
# Receive an SDP offer from some client assigned by the Broker,
|
||||||
# |pair| - an available ProxyPair.
|
# |pair| - an available ProxyPair.
|
||||||
receiveOffer: (pair, desc) =>
|
receiveOffer: (pair, desc) =>
|
||||||
console.assert !pair.active
|
|
||||||
try
|
try
|
||||||
offer = JSON.parse desc
|
offer = JSON.parse desc
|
||||||
dbg 'Received:\n\n' + offer.sdp + '\n'
|
dbg 'Received:\n\n' + offer.sdp + '\n'
|
||||||
|
@ -131,6 +117,8 @@ class Snowflake
|
||||||
cease: ->
|
cease: ->
|
||||||
while @proxyPairs.length > 0
|
while @proxyPairs.length > 0
|
||||||
@proxyPairs.pop().close()
|
@proxyPairs.pop().close()
|
||||||
|
log @pollInterval
|
||||||
|
clearInterval(@pollInterval)
|
||||||
|
|
||||||
disable: ->
|
disable: ->
|
||||||
log 'Disabling Snowflake.'
|
log 'Disabling Snowflake.'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue