2021-11-01 14:42:55 +00:00
|
|
|
package v2
|
2021-04-15 02:19:31 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
2021-04-15 02:19:31 +00:00
|
|
|
)
|
|
|
|
|
2021-10-09 18:18:53 +00:00
|
|
|
// Adapted from https://github.com/dustin/go-broadcast/commit/f664265f5a662fb4d1df7f3533b1e8d0e0277120
|
|
|
|
// by Dustin Sallings (c) 2013, which was released under MIT license
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2022-02-23 15:08:27 +00:00
|
|
|
type doneCh chan struct{}
|
|
|
|
|
|
|
|
type chOperation struct {
|
2022-04-25 19:31:26 +00:00
|
|
|
ch chan<- *protocol.Envelope
|
|
|
|
topic *string
|
|
|
|
done doneCh
|
2022-02-23 15:08:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
type broadcastOutputs map[chan<- *protocol.Envelope]struct{}
|
|
|
|
|
2021-04-15 02:19:31 +00:00
|
|
|
type broadcaster struct {
|
2021-04-22 00:09:37 +00:00
|
|
|
input chan *protocol.Envelope
|
2022-02-23 15:08:27 +00:00
|
|
|
reg chan chOperation
|
|
|
|
unreg chan chOperation
|
2021-04-15 02:19:31 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
outputs broadcastOutputs
|
|
|
|
outputsPerTopic map[string]broadcastOutputs
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The Broadcaster interface describes the main entry points to
|
|
|
|
// broadcasters.
|
|
|
|
type Broadcaster interface {
|
2022-04-25 19:31:26 +00:00
|
|
|
// Register a new channel to receive broadcasts from a pubsubtopic
|
|
|
|
Register(topic *string, newch chan<- *protocol.Envelope)
|
|
|
|
// Register a new channel to receive broadcasts from a pubsub topic and return a channel to wait until this operation is complete
|
|
|
|
WaitRegister(topic *string, newch chan<- *protocol.Envelope) doneCh
|
|
|
|
// Unregister a channel so that it no longer receives broadcasts from a pubsub topic
|
|
|
|
Unregister(topic *string, newch chan<- *protocol.Envelope)
|
2022-02-23 15:08:27 +00:00
|
|
|
// Unregister a subscriptor channel and return a channel to wait until this operation is done
|
2022-04-25 19:31:26 +00:00
|
|
|
WaitUnregister(topic *string, newch chan<- *protocol.Envelope) doneCh
|
2021-04-15 02:19:31 +00:00
|
|
|
// Shut this broadcaster down.
|
2021-11-01 14:42:55 +00:00
|
|
|
Close()
|
2021-04-15 02:19:31 +00:00
|
|
|
// Submit a new object to all subscribers
|
2021-04-22 00:09:37 +00:00
|
|
|
Submit(*protocol.Envelope)
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 00:09:37 +00:00
|
|
|
func (b *broadcaster) broadcast(m *protocol.Envelope) {
|
2021-04-15 02:19:31 +00:00
|
|
|
for ch := range b.outputs {
|
|
|
|
ch <- m
|
|
|
|
}
|
2022-04-25 19:31:26 +00:00
|
|
|
|
|
|
|
outputs, ok := b.outputsPerTopic[m.PubsubTopic()]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for ch := range outputs {
|
|
|
|
ch <- m
|
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *broadcaster) run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-b.input:
|
|
|
|
b.broadcast(m)
|
2022-02-23 15:08:27 +00:00
|
|
|
case broadcastee, ok := <-b.reg:
|
2021-04-15 02:19:31 +00:00
|
|
|
if ok {
|
2022-04-25 19:31:26 +00:00
|
|
|
if broadcastee.topic != nil {
|
|
|
|
topicOutputs, ok := b.outputsPerTopic[*broadcastee.topic]
|
|
|
|
if !ok {
|
|
|
|
b.outputsPerTopic[*broadcastee.topic] = make(broadcastOutputs)
|
|
|
|
topicOutputs = b.outputsPerTopic[*broadcastee.topic]
|
|
|
|
}
|
|
|
|
|
|
|
|
topicOutputs[broadcastee.ch] = struct{}{}
|
|
|
|
b.outputsPerTopic[*broadcastee.topic] = topicOutputs
|
|
|
|
} else {
|
|
|
|
b.outputs[broadcastee.ch] = struct{}{}
|
|
|
|
}
|
2022-02-23 15:08:27 +00:00
|
|
|
if broadcastee.done != nil {
|
|
|
|
broadcastee.done <- struct{}{}
|
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
} else {
|
2022-02-23 15:08:27 +00:00
|
|
|
if broadcastee.done != nil {
|
|
|
|
broadcastee.done <- struct{}{}
|
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-23 15:08:27 +00:00
|
|
|
case broadcastee := <-b.unreg:
|
2022-04-25 19:31:26 +00:00
|
|
|
if broadcastee.topic != nil {
|
|
|
|
topicOutputs, ok := b.outputsPerTopic[*broadcastee.topic]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(topicOutputs, broadcastee.ch)
|
|
|
|
b.outputsPerTopic[*broadcastee.topic] = topicOutputs
|
|
|
|
} else {
|
|
|
|
delete(b.outputs, broadcastee.ch)
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:08:27 +00:00
|
|
|
if broadcastee.done != nil {
|
|
|
|
broadcastee.done <- struct{}{}
|
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// NewBroadcaster creates a Broadcaster with an specified length
|
|
|
|
// It's used to register subscriptors that will need to receive
|
|
|
|
// an Envelope containing a WakuMessage
|
2021-04-15 02:19:31 +00:00
|
|
|
func NewBroadcaster(buflen int) Broadcaster {
|
|
|
|
b := &broadcaster{
|
2022-04-25 19:31:26 +00:00
|
|
|
input: make(chan *protocol.Envelope, buflen),
|
|
|
|
reg: make(chan chOperation),
|
|
|
|
unreg: make(chan chOperation),
|
|
|
|
outputs: make(broadcastOutputs),
|
|
|
|
outputsPerTopic: make(map[string]broadcastOutputs),
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go b.run()
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:08:27 +00:00
|
|
|
// Register a subscriptor channel and return a channel to wait until this operation is done
|
2022-04-25 19:31:26 +00:00
|
|
|
func (b *broadcaster) WaitRegister(topic *string, newch chan<- *protocol.Envelope) doneCh {
|
2022-02-23 15:08:27 +00:00
|
|
|
d := make(doneCh)
|
|
|
|
b.reg <- chOperation{
|
2022-04-25 19:31:26 +00:00
|
|
|
ch: newch,
|
|
|
|
topic: topic,
|
|
|
|
done: d,
|
2022-02-23 15:08:27 +00:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// Register a subscriptor channel
|
2022-04-25 19:31:26 +00:00
|
|
|
func (b *broadcaster) Register(topic *string, newch chan<- *protocol.Envelope) {
|
2022-02-23 15:08:27 +00:00
|
|
|
b.reg <- chOperation{
|
2022-04-25 19:31:26 +00:00
|
|
|
ch: newch,
|
|
|
|
topic: topic,
|
|
|
|
done: nil,
|
2022-02-23 15:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister a subscriptor channel and return a channel to wait until this operation is done
|
2022-04-25 19:31:26 +00:00
|
|
|
func (b *broadcaster) WaitUnregister(topic *string, newch chan<- *protocol.Envelope) doneCh {
|
2022-02-23 15:08:27 +00:00
|
|
|
d := make(doneCh)
|
|
|
|
b.unreg <- chOperation{
|
2022-04-25 19:31:26 +00:00
|
|
|
ch: newch,
|
|
|
|
topic: topic,
|
|
|
|
done: d,
|
2022-02-23 15:08:27 +00:00
|
|
|
}
|
|
|
|
return d
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// Unregister a subscriptor channel
|
2022-04-25 19:31:26 +00:00
|
|
|
func (b *broadcaster) Unregister(topic *string, newch chan<- *protocol.Envelope) {
|
2022-02-23 15:08:27 +00:00
|
|
|
b.unreg <- chOperation{
|
2022-04-25 19:31:26 +00:00
|
|
|
ch: newch,
|
|
|
|
topic: topic,
|
|
|
|
done: nil,
|
2022-02-23 15:08:27 +00:00
|
|
|
}
|
2021-04-15 02:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// Closes the broadcaster. Used to stop receiving new subscribers
|
2021-11-01 14:42:55 +00:00
|
|
|
func (b *broadcaster) Close() {
|
2021-04-15 02:19:31 +00:00
|
|
|
close(b.reg)
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:49:52 +00:00
|
|
|
// Submits an Envelope to be broadcasted among all registered subscriber channels
|
2021-04-22 00:09:37 +00:00
|
|
|
func (b *broadcaster) Submit(m *protocol.Envelope) {
|
2021-04-15 02:19:31 +00:00
|
|
|
if b != nil {
|
|
|
|
b.input <- m
|
|
|
|
}
|
|
|
|
}
|