Use reconnect timeout on client (#12)

This commit is contained in:
Serene Han 2016-02-17 19:53:11 -08:00
parent f205a0be59
commit 8a2e7a6041
3 changed files with 36 additions and 25 deletions

View file

@ -39,6 +39,13 @@ func TestConnect(t *testing.T) {
So(mock.destination.Bytes(), ShouldResemble, []byte("test")) So(mock.destination.Bytes(), ShouldResemble, []byte("test"))
}) })
Convey("Receive answer fails on nil answer", func() {
c.reset = make(chan struct{})
c.ReceiveAnswer()
answerChannel <- nil
<-c.reset
})
Convey("Connect Loop", func() { Convey("Connect Loop", func() {
// TODO // TODO
}) })

View file

@ -5,6 +5,7 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -66,6 +67,9 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Printf("BrokerChannel Response:\n%s\n\n", resp.Status) log.Printf("BrokerChannel Response:\n%s\n\n", resp.Status)
if http.StatusOK != resp.StatusCode {
return nil, errors.New("no answer from broker.")
}
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if nil != err { if nil != err {
return nil, err return nil, err

View file

@ -58,7 +58,7 @@ type SnowflakeChannel interface {
// Implements net.Conn interface // Implements net.Conn interface
type webRTCConn struct { type webRTCConn struct {
config *webrtc.Configuration config *webrtc.Configuration
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
snowflake SnowflakeChannel // Interface holding the WebRTC DataChannel. snowflake SnowflakeChannel // Interface holding the WebRTC DataChannel.
broker *BrokerChannel broker *BrokerChannel
@ -108,7 +108,6 @@ func (c *webRTCConn) SetWriteDeadline(t time.Time) error {
func (c *webRTCConn) PreparePeerConnection() { func (c *webRTCConn) PreparePeerConnection() {
if nil != c.pc { if nil != c.pc {
log.Printf("PeerConnection already exists.")
c.pc.Close() c.pc.Close()
c.pc = nil c.pc = nil
} }
@ -135,12 +134,12 @@ func (c *webRTCConn) PreparePeerConnection() {
}() }()
} }
pc.OnIceCandidate = func(candidate webrtc.IceCandidate) { pc.OnIceCandidate = func(candidate webrtc.IceCandidate) {
log.Printf("OnIceCandidate %s", candidate.Serialize()) log.Printf("WebRTC: OnIceCandidate %s", candidate.Serialize())
// Allow candidates to accumulate until OnIceComplete. // Allow candidates to accumulate until OnIceComplete.
} }
// TODO: This may soon be deprecated, consider OnIceGatheringStateChange. // TODO: This may soon be deprecated, consider OnIceGatheringStateChange.
pc.OnIceComplete = func() { pc.OnIceComplete = func() {
log.Printf("OnIceComplete") log.Printf("WebRTC: OnIceComplete")
c.offerChannel <- pc.LocalDescription() c.offerChannel <- pc.LocalDescription()
} }
// This callback is not expected, as the Client initiates the creation // This callback is not expected, as the Client initiates the creation
@ -150,6 +149,7 @@ func (c *webRTCConn) PreparePeerConnection() {
panic("Unexpected OnDataChannel!") panic("Unexpected OnDataChannel!")
} }
c.pc = pc c.pc = pc
log.Println("WebRTC: PeerConnection created.")
} }
// Create a WebRTC DataChannel locally. // Create a WebRTC DataChannel locally.
@ -189,6 +189,7 @@ func (c *webRTCConn) EstablishDataChannel() error {
panic("short write") panic("short write")
} }
} }
log.Println("WebRTC: DataChannel created.")
return nil return nil
} }
@ -209,15 +210,9 @@ func (c *webRTCConn) SendOffer() error {
log.Println("Sending offer via BrokerChannel...\nTarget URL: ", brokerURL, log.Println("Sending offer via BrokerChannel...\nTarget URL: ", brokerURL,
"\nFront URL: ", frontDomain) "\nFront URL: ", frontDomain)
answer, err := c.broker.Negotiate(c.pc.LocalDescription()) answer, err := c.broker.Negotiate(c.pc.LocalDescription())
if nil != err { if nil != err || nil == answer {
log.Printf("BrokerChannel signaling error: %s", err) log.Printf("BrokerChannel error: %s", err)
return answer = nil
}
if nil == answer {
log.Printf("BrokerChannel: No answer received.")
// TODO: Should try again here.
c.reset <- struct{}{}
return
} }
answerChannel <- answer answerChannel <- answer
}() }()
@ -228,17 +223,21 @@ func (c *webRTCConn) SendOffer() error {
return nil return nil
} }
func (c *webRTCConn) ReceiveAnswer() error { func (c *webRTCConn) ReceiveAnswer() {
log.Printf("waiting for answer...") go func() {
answer, ok := <-answerChannel answer, ok := <-answerChannel
if !ok { if !ok || nil == answer {
// TODO: Don't just fail, try again! log.Printf("Failed to retrieve answer. Retrying in %d seconds", ReconnectTimeout)
c.pc.Close() <-time.After(time.Second * ReconnectTimeout)
// connection.errorChannel <- errors.New("Bad answer") c.reset <- struct{}{}
return errors.New("Bad answer") return
} }
log.Printf("Received Answer:\n\n%s\n", answer.Sdp) log.Printf("Received Answer:\n\n%s\n", answer.Sdp)
return c.pc.SetRemoteDescription(answer) err := c.pc.SetRemoteDescription(answer)
if nil != err {
c.errorChannel <- err
}
}()
} }
func (c *webRTCConn) sendData(data []byte) { func (c *webRTCConn) sendData(data []byte) {
@ -259,10 +258,11 @@ func (c *webRTCConn) ConnectLoop() {
// TODO: When go-webrtc is more stable, it's possible that a new // TODO: When go-webrtc is more stable, it's possible that a new
// PeerConnection won't need to be recreated each time. // PeerConnection won't need to be recreated each time.
// called once. // called once.
c.PreparePeerConnection() c.PreparePeerConnection()
c.EstablishDataChannel() c.EstablishDataChannel()
c.SendOffer() c.SendOffer()
c.ReceiveAnswer() c.ReceiveAnswer()
<-c.reset <-c.reset
log.Println(" --- snowflake connection reset ---") log.Println(" --- snowflake connection reset ---")
} }