2023-04-12 10:30:12 +00:00
|
|
|
package pairing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
2023-04-12 13:36:38 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-04-12 10:30:12 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils"
|
|
|
|
"github.com/status-im/status-go/server"
|
|
|
|
"github.com/status-im/status-go/server/pairing/peers"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PeerNotifier struct {
|
2023-04-12 13:36:38 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
stop chan struct{}
|
|
|
|
terminator sync.Once
|
2023-04-12 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPeerNotifier() *PeerNotifier {
|
|
|
|
logger := logutils.ZapLogger().Named("PeerNotifier")
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
|
|
|
return &PeerNotifier{
|
|
|
|
logger: logger,
|
|
|
|
stop: stop,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 13:36:38 +00:00
|
|
|
func (p *PeerNotifier) terminateIn(d time.Duration) {
|
|
|
|
p.terminator.Do(func() {
|
|
|
|
time.Sleep(d)
|
|
|
|
p.stop <- struct{}{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:30:12 +00:00
|
|
|
func (p *PeerNotifier) handler(hello *peers.LocalPairingPeerHello) {
|
|
|
|
signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: hello})
|
|
|
|
p.logger.Debug("received peers.LocalPairingPeerHello message", zap.Any("hello message", hello))
|
2023-04-12 13:36:38 +00:00
|
|
|
p.terminateIn(5 * time.Second)
|
2023-04-12 10:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PeerNotifier) Search() error {
|
|
|
|
dn, err := server.GetDeviceName()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers.Search(dn, runtime.GOOS, p.handler, p.stop, p.logger)
|
|
|
|
}
|