parse address spec, create websocket. a bit more to iron out (issue #5)

This commit is contained in:
Serene Han 2016-01-11 18:19:09 -08:00
parent d735c0fbf9
commit cfd87d1798
2 changed files with 35 additions and 11 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ datadir/
client/client client/client
server/server server/server
snowflake.log snowflake.log
proxy/test

View file

@ -10,7 +10,7 @@ this must always act as the answerer.
TODO(keroserene): Complete the websocket + webrtc ProxyPair TODO(keroserene): Complete the websocket + webrtc ProxyPair
### ###
if 'undefined' == typeof module || 'undefined' != typeof module.exports if 'undefined' != typeof module && 'undefined' != typeof module.exports
console.log 'not in browser.' console.log 'not in browser.'
else else
window.PeerConnection = window.RTCPeerConnection || window.PeerConnection = window.RTCPeerConnection ||
@ -81,6 +81,22 @@ Params =
result[name] = value if !(name in result) result[name] = value if !(name in result)
result result
# Parse an address in the form "host:port". Returns an Object with keys "host"
# (String) and "port" (int). Returns null on error.
parseAddress: (spec) ->
m = null
# IPv6 syntax.
m = spec.match(/^\[([\0-9a-fA-F:.]+)\]:([0-9]+)$/) if !m
# IPv4 syntax.
m = spec.match(/^([0-9.]+):([0-9]+)$/) if !m
return null if !m
host = m[1]
port = parseInt(m[2], 10)
if isNaN(port) || port < 0 || port > 65535
return null
{ host: host, port: port }
# repr = (x) -> # repr = (x) ->
# return 'null' if null == x # return 'null' if null == x
# return 'undefined' if 'undefined' == typeof x # return 'undefined' if 'undefined' == typeof x
@ -150,11 +166,17 @@ class Snowflake
@$badgem = @badge.elem @$badgem = @badge.elem
@$badge.setAttribute("id", "snowflake-badge") if (@$badge) @$badge.setAttribute("id", "snowflake-badge") if (@$badge)
setRelayAddr: (relayAddr) ->
# TODO: User-supplied for now, but should fetch from facilitator later. # TODO: User-supplied for now, but should fetch from facilitator later.
@relayAddr = relayAddr setRelayAddr: (relayAddr) ->
addr = Params.parseAddress relayAddr
if !addr
log 'Invalid address spec. Try again.'
return false
@relayAddr = addr
log 'Using ' + relayAddr + ' as Relay.'
log "Input offer from the snowflake client:" log "Input offer from the snowflake client:"
@beginWebRTC() @beginWebRTC()
return true
# Initialize WebRTC PeerConnection # Initialize WebRTC PeerConnection
beginWebRTC: -> beginWebRTC: ->
@ -186,8 +208,9 @@ class Snowflake
channel.onopen = => channel.onopen = =>
log "Data channel opened!" log "Data channel opened!"
@state = MODE.WEBRTC_READY @state = MODE.WEBRTC_READY
# TODO: Prepare ProxyPair onw. # This is the point when the WebRTC datachannel is done, so the next step
@beginProxy() # is to establish websocket to the server.
@beginProxy(null, @relayAddr)
channel.onclose = => channel.onclose = =>
log "Data channel closed." log "Data channel closed."
@state = MODE.INIT; @state = MODE.INIT;
@ -311,10 +334,10 @@ buildUrl = (scheme, host, port, path, params) ->
makeWebsocket = (addr) -> makeWebsocket = (addr) ->
url = buildUrl 'ws', addr.host, addr.port, '/' url = buildUrl 'ws', addr.host, addr.port, '/'
if have_websocket_binary_frames() # if have_websocket_binary_frames()
ws = new WebSocket url ws = new WebSocket url
else # else
ws = new WebSocket url 'base64' # ws = new WebSocket url 'base64'
### ###
"User agents can use this as a hint for how to handle incoming binary data: if "User agents can use this as a hint for how to handle incoming binary data: if
the attribute is set to 'blob', it is safe to spool it to disk, and if it is the attribute is set to 'blob', it is safe to spool it to disk, and if it is
@ -370,7 +393,7 @@ class ProxyPair
isOpen: (ws) -> undefined != ws && WebSocket.OPEN == ws.readyState isOpen: (ws) -> undefined != ws && WebSocket.OPEN == ws.readyState
isClosed: (ws) -> undefined == ws || WebSocket.CLOSED == ws.readyState isClosed: (ws) -> undefined == ws || WebSocket.CLOSED == ws.readyState
maybe_cleanup: -> maybeCleanup: ->
if @running && @isClosed(client) && @isClosed @relay if @running && @isClosed(client) && @isClosed @relay
@running = false @running = false
@cleanup_callback() @cleanup_callback()
@ -378,8 +401,8 @@ class ProxyPair
false false
# Send as much data as the rate limit currently allows. # Send as much data as the rate limit currently allows.
###
flush: -> flush: ->
###
clearTimeout @flush_timeout_id if @flush_timeout_id clearTimeout @flush_timeout_id if @flush_timeout_id
@flush_timeout_id = null @flush_timeout_id = null
busy = true busy = true