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.'
snowflake = null
@ -80,7 +74,7 @@ init = () ->
# Notification of closing tab with active proxy.
window.onbeforeunload = ->
if !silenceNotifications && MODE.WEBRTC_READY == snowflake.state
if !silenceNotifications && Snowflake.MODE.WEBRTC_READY == snowflake.state
return CONFIRMATION_MESSAGE
null

View file

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

View file

@ -14,11 +14,18 @@ class Snowflake
relayAddr: null
proxyPairs: []
rateLimit: null
state: MODE.INIT
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.
constructor: (@broker, @ui) ->
@state = Snowflake.MODE.INIT
rateLimitBytes = undefined
if 'off' != query['ratelimit']
rateLimitBytes = Params.getByteCount(query, 'ratelimit',
@ -41,7 +48,7 @@ class Snowflake
# Initialize WebRTC PeerConnection, which requires beginning the signalling
# process. |pollBroker| automatically arranges signalling.
beginWebRTC: ->
@state = MODE.WEBRTC_CONNECTING
@state = Snowflake.MODE.WEBRTC_CONNECTING
for i in [1..CONNECTIONS_PER_CLIENT]
@makeProxyPair @relayAddr
log 'ProxyPair Slots: ' + @proxyPairs.length

View file

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