From 927124e732599ae2e2e3a56b195bbc7a58d3eef5 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Thu, 16 Feb 2017 12:33:07 +0100 Subject: [PATCH] Simplify TURN data retrieval API. Receivers no longer must implement the rather large "Sender" interface and a complete "Session" object also is no longer required to get TURN data. --- go/channelling/api/handle_self.go | 2 +- go/channelling/client.go | 10 +++++++++- go/channelling/hub.go | 4 ++-- go/channelling/pipeline.go | 7 +++++++ go/channelling/turndata.go | 14 +++++++++++++- go/channelling/turnservice_manager.go | 21 ++++++++++----------- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/go/channelling/api/handle_self.go b/go/channelling/api/handle_self.go index 10a87575f..60c48ec63 100644 --- a/go/channelling/api/handle_self.go +++ b/go/channelling/api/handle_self.go @@ -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) diff --git a/go/channelling/client.go b/go/channelling/client.go index 168b14897..1f5838edb 100644 --- a/go/channelling/client.go +++ b/go/channelling/client.go @@ -28,7 +28,8 @@ import ( ) type Sender interface { - Index() uint64 + TurnDataReceiver + Send(buffercache.Buffer) Outgoing(interface{}) } @@ -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, + }) +} diff --git a/go/channelling/hub.go b/go/channelling/hub.go index 068e1e834..d40fa1fd5 100644 --- a/go/channelling/hub.go +++ b/go/channelling/hub.go @@ -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)) diff --git a/go/channelling/pipeline.go b/go/channelling/pipeline.go index edca9580a..863c77047 100644 --- a/go/channelling/pipeline.go +++ b/go/channelling/pipeline.go @@ -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, + }) +} diff --git a/go/channelling/turndata.go b/go/channelling/turndata.go index c6991ee97..93cc6608b 100644 --- a/go/channelling/turndata.go +++ b/go/channelling/turndata.go @@ -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 } diff --git a/go/channelling/turnservice_manager.go b/go/channelling/turnservice_manager.go index 3119c0c5a..f0db7debc 100644 --- a/go/channelling/turnservice_manager.go +++ b/go/channelling/turnservice_manager.go @@ -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 @@ -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, "") @@ -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 @@ -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() }