2021-11-22 14:48:32 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2022-11-09 19:53:01 +00:00
|
|
|
v2 "github.com/waku-org/go-waku/waku/v2"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2021-11-22 14:48:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Adder func(msg *protocol.Envelope)
|
|
|
|
|
|
|
|
type runnerService struct {
|
|
|
|
broadcaster v2.Broadcaster
|
|
|
|
ch chan *protocol.Envelope
|
|
|
|
quit chan bool
|
|
|
|
adder Adder
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRunnerService(broadcaster v2.Broadcaster, adder Adder) *runnerService {
|
|
|
|
return &runnerService{
|
|
|
|
broadcaster: broadcaster,
|
|
|
|
quit: make(chan bool),
|
|
|
|
adder: adder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runnerService) Start() {
|
|
|
|
r.ch = make(chan *protocol.Envelope, 1024)
|
2022-04-25 19:31:26 +00:00
|
|
|
r.broadcaster.Register(nil, r.ch)
|
2021-11-22 14:48:32 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.quit:
|
|
|
|
return
|
|
|
|
case envelope := <-r.ch:
|
|
|
|
r.adder(envelope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runnerService) Stop() {
|
|
|
|
r.quit <- true
|
2022-04-25 19:31:26 +00:00
|
|
|
r.broadcaster.Unregister(nil, r.ch)
|
2021-11-22 14:48:32 +00:00
|
|
|
close(r.ch)
|
|
|
|
}
|