Refactor: slightly simplify relay.Svc

This commit is contained in:
Jakob Borg 2015-08-23 09:39:53 +02:00
parent aec143b882
commit 21adf752c8

View File

@ -54,9 +54,10 @@ func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config, conns chan<- model.Intermed
tlsCfg: tlsCfg, tlsCfg: tlsCfg,
conns: conns, conns: conns,
invitations: svc.invitations, invitations: svc.invitations,
stop: make(chan struct{}),
} }
svc.receiverToken = svc.Add(receiver) svc.Add(receiver)
return svc return svc
} }
@ -66,11 +67,10 @@ type Svc struct {
cfg *config.Wrapper cfg *config.Wrapper
tlsCfg *tls.Config tlsCfg *tls.Config
receiverToken suture.ServiceToken tokens map[string]suture.ServiceToken
tokens map[string]suture.ServiceToken clients map[string]*client.ProtocolClient
clients map[string]*client.ProtocolClient mut sync.RWMutex
mut sync.RWMutex invitations chan protocol.SessionInvitation
invitations chan protocol.SessionInvitation
} }
func (s *Svc) VerifyConfiguration(from, to config.Configuration) error { func (s *Svc) VerifyConfiguration(from, to config.Configuration) error {
@ -123,6 +123,7 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
} }
continue continue
} }
for _, relayAnn := range ann.Relays { for _, relayAnn := range ann.Relays {
ruri, err := url.Parse(relayAnn.URL) ruri, err := url.Parse(relayAnn.URL)
if err != nil { if err != nil {
@ -138,6 +139,8 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
} }
} }
s.mut.Lock()
for key, uri := range existing { for key, uri := range existing {
_, ok := s.tokens[key] _, ok := s.tokens[key]
if !ok { if !ok {
@ -146,9 +149,7 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
} }
c := client.NewProtocolClient(uri, s.tlsCfg.Certificates, s.invitations) c := client.NewProtocolClient(uri, s.tlsCfg.Certificates, s.invitations)
s.tokens[key] = s.Add(c) s.tokens[key] = s.Add(c)
s.mut.Lock()
s.clients[key] = c s.clients[key] = c
s.mut.Unlock()
} }
} }
@ -157,15 +158,15 @@ func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
if !ok { if !ok {
err := s.Remove(token) err := s.Remove(token)
delete(s.tokens, key) delete(s.tokens, key)
s.mut.Lock()
delete(s.clients, key) delete(s.clients, key)
s.mut.Unlock()
if debug { if debug {
l.Debugln("Disconnecting from relay", key, err) l.Debugln("Disconnecting from relay", key, err)
} }
} }
} }
s.mut.Unlock()
return true return true
} }
@ -187,11 +188,6 @@ type invitationReceiver struct {
} }
func (r *invitationReceiver) Serve() { func (r *invitationReceiver) Serve() {
if r.stop != nil {
return
}
r.stop = make(chan struct{})
for { for {
select { select {
case inv := <-r.invitations: case inv := <-r.invitations:
@ -227,6 +223,7 @@ func (r *invitationReceiver) Serve() {
r.conns <- model.IntermediateConnection{ r.conns <- model.IntermediateConnection{
tc, model.ConnectionTypeRelayAccept, tc, model.ConnectionTypeRelayAccept,
} }
case <-r.stop: case <-r.stop:
return return
} }
@ -234,17 +231,13 @@ func (r *invitationReceiver) Serve() {
} }
func (r *invitationReceiver) Stop() { func (r *invitationReceiver) Stop() {
if r.stop == nil { close(r.stop)
return
}
r.stop <- struct{}{}
r.stop = nil
} }
// This is the announcement recieved from the relay server;
// {"relays": [{"url": "relay://10.20.30.40:5060"}, ...]}
type dynamicAnnouncement struct { type dynamicAnnouncement struct {
Relays []relayAnnouncement `json:"relays"` Relays []struct {
} URL string
}
type relayAnnouncement struct {
URL string `json:"url"`
} }