Make MODE a class constant

This commit is contained in:
Arlo Breault 2019-05-08 13:24:37 -04:00
parent 6b002c5f22
commit 9df66b15b1
4 changed files with 20 additions and 19 deletions

View file

@ -29,12 +29,6 @@ config = {
] ]
} }
# Janky state machine
MODE =
INIT: 0
WEBRTC_CONNECTING: 1
WEBRTC_READY: 2
CONFIRMATION_MESSAGE = 'You\'re currently serving a Tor user via Snowflake.' CONFIRMATION_MESSAGE = 'You\'re currently serving a Tor user via Snowflake.'
snowflake = null snowflake = null
@ -80,7 +74,7 @@ init = () ->
# Notification of closing tab with active proxy. # Notification of closing tab with active proxy.
window.onbeforeunload = -> window.onbeforeunload = ->
if !silenceNotifications && MODE.WEBRTC_READY == snowflake.state if !silenceNotifications && Snowflake.MODE.WEBRTC_READY == snowflake.state
return CONFIRMATION_MESSAGE return CONFIRMATION_MESSAGE
null null

View file

@ -68,7 +68,7 @@ class ProxyPair
prepareDataChannel: (channel) => prepareDataChannel: (channel) =>
channel.onopen = => channel.onopen = =>
log 'WebRTC DataChannel opened!' log 'WebRTC DataChannel opened!'
snowflake.state = MODE.WEBRTC_READY snowflake.state = Snowflake.MODE.WEBRTC_READY
snowflake.ui?.setActive true snowflake.ui?.setActive true
# This is the point when the WebRTC datachannel is done, so the next step # This is the point when the WebRTC datachannel is done, so the next step
# is to establish websocket to the server. # is to establish websocket to the server.
@ -77,7 +77,7 @@ class ProxyPair
log 'WebRTC DataChannel closed.' log 'WebRTC DataChannel closed.'
snowflake.ui?.setStatus 'disconnected by webrtc.' snowflake.ui?.setStatus 'disconnected by webrtc.'
snowflake.ui?.setActive false snowflake.ui?.setActive false
snowflake.state = MODE.INIT snowflake.state = Snowflake.MODE.INIT
@flush() @flush()
@close() @close()
# TODO: Change this for multiplexing. # TODO: Change this for multiplexing.
@ -114,7 +114,7 @@ class ProxyPair
log @relay.label + ' closed.' log @relay.label + ' closed.'
snowflake.ui?.setStatus 'disconnected.' snowflake.ui?.setStatus 'disconnected.'
snowflake.ui?.setActive false snowflake.ui?.setActive false
snowflake.state = MODE.INIT snowflake.state = Snowflake.MODE.INIT
@flush() @flush()
@close() @close()
@relay.onerror = @onError @relay.onerror = @onError

View file

@ -14,11 +14,18 @@ class Snowflake
relayAddr: null relayAddr: null
proxyPairs: [] proxyPairs: []
rateLimit: null rateLimit: null
state: MODE.INIT
retries: 0 retries: 0
# Janky state machine
@MODE =
INIT: 0
WEBRTC_CONNECTING: 1
WEBRTC_READY: 2
# Prepare the Snowflake with a Broker (to find clients) and optional UI. # Prepare the Snowflake with a Broker (to find clients) and optional UI.
constructor: (@broker, @ui) -> constructor: (@broker, @ui) ->
@state = Snowflake.MODE.INIT
rateLimitBytes = undefined rateLimitBytes = undefined
if 'off' != query['ratelimit'] if 'off' != query['ratelimit']
rateLimitBytes = Params.getByteCount(query, 'ratelimit', rateLimitBytes = Params.getByteCount(query, 'ratelimit',
@ -41,7 +48,7 @@ class Snowflake
# Initialize WebRTC PeerConnection, which requires beginning the signalling # Initialize WebRTC PeerConnection, which requires beginning the signalling
# process. |pollBroker| automatically arranges signalling. # process. |pollBroker| automatically arranges signalling.
beginWebRTC: -> beginWebRTC: ->
@state = MODE.WEBRTC_CONNECTING @state = Snowflake.MODE.WEBRTC_CONNECTING
for i in [1..CONNECTIONS_PER_CLIENT] for i in [1..CONNECTIONS_PER_CLIENT]
@makeProxyPair @relayAddr @makeProxyPair @relayAddr
log 'ProxyPair Slots: ' + @proxyPairs.length log 'ProxyPair Slots: ' + @proxyPairs.length

View file

@ -25,7 +25,7 @@ snowflake =
ui: fakeUI ui: fakeUI
broker: broker:
sendAnswer: -> sendAnswer: ->
state: MODE.INIT state: Snowflake.MODE.INIT
describe 'Snowflake', -> describe 'Snowflake', ->
@ -72,19 +72,19 @@ describe 'Snowflake', ->
it 'gives a dialog when closing, only while active', -> it 'gives a dialog when closing, only while active', ->
silenceNotifications = false silenceNotifications = false
snowflake.state = MODE.WEBRTC_READY snowflake.state = Snowflake.MODE.WEBRTC_READY
msg = window.onbeforeunload() msg = window.onbeforeunload()
expect(snowflake.state).toBe MODE.WEBRTC_READY expect(snowflake.state).toBe Snowflake.MODE.WEBRTC_READY
expect(msg).toBe CONFIRMATION_MESSAGE expect(msg).toBe CONFIRMATION_MESSAGE
snowflake.state = MODE.INIT snowflake.state = Snowflake.MODE.INIT
msg = window.onbeforeunload() msg = window.onbeforeunload()
expect(snowflake.state).toBe MODE.INIT expect(snowflake.state).toBe Snowflake.MODE.INIT
expect(msg).toBe null expect(msg).toBe null
it 'does not give a dialog when silent flag is on', -> it 'does not give a dialog when silent flag is on', ->
silenceNotifications = true silenceNotifications = true
snowflake.state = MODE.WEBRTC_READY snowflake.state = Snowflake.MODE.WEBRTC_READY
msg = window.onbeforeunload() msg = window.onbeforeunload()
expect(snowflake.state).toBe MODE.WEBRTC_READY expect(snowflake.state).toBe Snowflake.MODE.WEBRTC_READY
expect(msg).toBe null expect(msg).toBe null