2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-08-17 13:01:48 +00:00
|
|
|
|
|
|
|
package beacon
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
import (
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2019-08-22 10:30:57 +00:00
|
|
|
"fmt"
|
2015-09-20 13:30:25 +00:00
|
|
|
"net"
|
2019-08-22 10:30:57 +00:00
|
|
|
"time"
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
"github.com/thejerf/suture/v4"
|
2019-08-22 10:30:57 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/util"
|
2015-09-20 13:30:25 +00:00
|
|
|
)
|
2014-08-17 13:01:48 +00:00
|
|
|
|
|
|
|
type recv struct {
|
|
|
|
data []byte
|
|
|
|
src net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
type Interface interface {
|
2015-09-20 13:30:25 +00:00
|
|
|
suture.Service
|
2019-08-22 10:30:57 +00:00
|
|
|
fmt.Stringer
|
2014-08-17 13:01:48 +00:00
|
|
|
Send(data []byte)
|
|
|
|
Recv() ([]byte, net.Addr)
|
2015-09-20 13:30:25 +00:00
|
|
|
Error() error
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
2019-08-22 10:30:57 +00:00
|
|
|
|
|
|
|
type cast struct {
|
|
|
|
*suture.Supervisor
|
|
|
|
name string
|
|
|
|
reader util.ServiceWithError
|
|
|
|
writer util.ServiceWithError
|
|
|
|
outbox chan recv
|
|
|
|
inbox chan []byte
|
|
|
|
stopped chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCast creates a base object for multi- or broadcasting. Afterwards the
|
|
|
|
// caller needs to set reader and writer with the addReader and addWriter
|
|
|
|
// methods to get a functional implementation of Interface.
|
|
|
|
func newCast(name string) *cast {
|
2020-11-17 12:19:04 +00:00
|
|
|
spec := util.Spec()
|
|
|
|
// Don't retry too frenetically: an error to open a socket or
|
|
|
|
// whatever is usually something that is either permanent or takes
|
|
|
|
// a while to get solved...
|
|
|
|
spec.FailureThreshold = 2
|
|
|
|
spec.FailureBackoff = 60 * time.Second
|
|
|
|
// Only log restarts in debug mode.
|
|
|
|
spec.EventHook = func(e suture.Event) {
|
|
|
|
l.Debugln(e)
|
2019-08-22 10:30:57 +00:00
|
|
|
}
|
2020-11-17 12:19:04 +00:00
|
|
|
c := &cast{
|
|
|
|
Supervisor: suture.New(name, spec),
|
|
|
|
name: name,
|
|
|
|
inbox: make(chan []byte),
|
|
|
|
outbox: make(chan recv, 16),
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
}
|
|
|
|
util.OnSupervisorDone(c.Supervisor, func() { close(c.stopped) })
|
|
|
|
return c
|
2019-08-22 10:30:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (c *cast) addReader(svc func(context.Context) error) {
|
2019-08-22 10:30:57 +00:00
|
|
|
c.reader = c.createService(svc, "reader")
|
|
|
|
c.Add(c.reader)
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (c *cast) addWriter(svc func(ctx context.Context) error) {
|
2019-08-22 10:30:57 +00:00
|
|
|
c.writer = c.createService(svc, "writer")
|
|
|
|
c.Add(c.writer)
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func (c *cast) createService(svc func(context.Context) error, suffix string) util.ServiceWithError {
|
2020-11-17 12:19:04 +00:00
|
|
|
return util.AsService(svc, fmt.Sprintf("%s/%s", c, suffix))
|
2019-08-22 10:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cast) String() string {
|
|
|
|
return fmt.Sprintf("%s@%p", c.name, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cast) Send(data []byte) {
|
|
|
|
select {
|
|
|
|
case c.inbox <- data:
|
|
|
|
case <-c.stopped:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cast) Recv() ([]byte, net.Addr) {
|
|
|
|
select {
|
|
|
|
case recv := <-c.outbox:
|
|
|
|
return recv.data, recv.src
|
|
|
|
case <-c.stopped:
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cast) Error() error {
|
|
|
|
if err := c.reader.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.writer.Error()
|
|
|
|
}
|