Skip to content

Commit

Permalink
Simplify TURN data retrieval API.
Browse files Browse the repository at this point in the history
Receivers no longer must implement the rather large "Sender" interface and
a complete "Session" object also is no longer required to get TURN data.
  • Loading branch information
fancycode committed Feb 16, 2017
1 parent c9be334 commit 927124e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go/channelling/api/handle_self.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (api *channellingAPI) HandleSelf(sender channelling.Sender, session *channe
Token: token,
Version: api.config.Version,
ApiVersion: apiVersion,
Turn: api.TurnDataCreator.CreateTurnData(sender, session),
Turn: api.TurnDataCreator.CreateTurnData(session.Id, sender),
Stun: api.config.StunURIs,
}
api.BusManager.Trigger(channelling.BusManagerSession, session.Id, session.Userid(), nil, nil)
Expand Down
10 changes: 9 additions & 1 deletion go/channelling/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
)

type Sender interface {
Index() uint64
TurnDataReceiver

Send(buffercache.Buffer)
Outgoing(interface{})
}
Expand Down Expand Up @@ -106,3 +107,10 @@ func (client *Client) ReplaceAndClose(oldClient *Client) {
oldClient.Close()
}()
}

func (client *Client) TurnDataAvailable(turn *DataTurn) {
client.Outgoing(&DataTurnUpdate{
Type: "TurnUpdate",
Turn: turn,
})
}
4 changes: 2 additions & 2 deletions go/channelling/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ func (h *hub) ClientInfo(details bool) (clientCount int, sessions map[string]*Da
return
}

func (h *hub) CreateTurnData(sender Sender, session *Session) *DataTurn {
func (h *hub) CreateTurnData(sessionId string, receiver TurnDataReceiver) *DataTurn {
if len(h.turnSecret) > 0 {
// Create turn data credentials for shared secret auth with TURN
// server. See http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
// and https://code.google.com/p/rfc5766-turn-server/ REST API auth
// and set shared secret in TURN server with static-auth-secret.
id := session.Id
id := sessionId
bar := sha256.New()
bar.Write([]byte(id))
id = base64.StdEncoding.EncodeToString(bar.Sum(nil))
Expand Down
7 changes: 7 additions & 0 deletions go/channelling/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ func (pipeline *Pipeline) attach(sink Sink) error {
pipeline.sink = sink
return nil
}

func (pipeline *Pipeline) TurnDataAvailable(turn *DataTurn) {
pipeline.Outgoing(&DataTurnUpdate{
Type: "TurnUpdate",
Turn: turn,
})
}
14 changes: 13 additions & 1 deletion go/channelling/turndata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@

package channelling

type TurnDataReceiver interface {
Index() uint64

TurnDataAvailable(*DataTurn)
}

type TurnDataCreator interface {
CreateTurnData(Sender, *Session) *DataTurn
/*
First parameter is the id of the session that requests the TURN data.
Second parameter will be notified when TURN data become available if none
were ready at the time the method was called (can be nil if no notification
is required).
*/
CreateTurnData(string, TurnDataReceiver) *DataTurn
}
21 changes: 10 additions & 11 deletions go/channelling/turnservice_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type TURNServiceManager interface {

type turnServiceManager struct {
sync.Mutex
pleaders map[uint64]Sender // Mapping of clients waiting to receive TURN data.
pleaders map[uint64]TurnDataReceiver // Mapping of clients waiting to receive TURN data.

uri string
accessToken string
Expand All @@ -51,7 +51,7 @@ func NewTURNServiceManager(uri string, accessToken string, clientID string) TURN
clientID: clientID,

turnService: turnService,
pleaders: make(map[uint64]Sender),
pleaders: make(map[uint64]TurnDataReceiver),
}

turnService.Open(accessToken, clientID, "")
Expand All @@ -71,14 +71,16 @@ func NewTURNServiceManager(uri string, accessToken string, clientID string) TURN
return mgr
}

func (mgr *turnServiceManager) CreateTurnData(sender Sender, session *Session) *DataTurn {
func (mgr *turnServiceManager) CreateTurnData(sessionId string, receiver TurnDataReceiver) *DataTurn {
credentials := mgr.turnService.Credentials(false)
turn, err := mgr.turnData(credentials)
if err != nil || turn.Ttl == 0 {
// When no data was return from service, refresh quickly.
mgr.Lock()
mgr.pleaders[sender.Index()] = sender
mgr.Unlock()
if receiver != nil {
mgr.Lock()
mgr.pleaders[receiver.Index()] = receiver
mgr.Unlock()
}

// Have client come back early.
turn.Ttl = 300
Expand Down Expand Up @@ -129,12 +131,9 @@ func (mgr *turnServiceManager) onCredentials(credentials *turnservicecli.CachedC
mgr.Lock()
for _, sender := range mgr.pleaders {
if turn, err := mgr.turnData(credentials); err == nil {
sender.Outgoing(&DataTurnUpdate{
Type: "TurnUpdate",
Turn: turn,
})
sender.TurnDataAvailable(turn)
}
}
mgr.pleaders = make(map[uint64]Sender) // Clear.
mgr.pleaders = make(map[uint64]TurnDataReceiver) // Clear.
mgr.Unlock()
}

0 comments on commit 927124e

Please sign in to comment.