Added functionality to slow stop UDP beacon multicast/listen

This commit is contained in:
Samuel Hawksby-Robinson 2023-04-12 14:36:38 +01:00
parent 552c58bb9c
commit 5a993e8f98
5 changed files with 36 additions and 9 deletions

View File

@ -1 +1 @@
0.145.2 0.146.0

View File

@ -2,6 +2,8 @@ package pairing
import ( import (
"runtime" "runtime"
"sync"
"time"
"go.uber.org/zap" "go.uber.org/zap"
@ -14,6 +16,7 @@ import (
type PeerNotifier struct { type PeerNotifier struct {
logger *zap.Logger logger *zap.Logger
stop chan struct{} stop chan struct{}
terminator sync.Once
} }
func NewPeerNotifier() *PeerNotifier { func NewPeerNotifier() *PeerNotifier {
@ -26,11 +29,17 @@ func NewPeerNotifier() *PeerNotifier {
} }
} }
func (p *PeerNotifier) terminateIn(d time.Duration) {
p.terminator.Do(func() {
time.Sleep(d)
p.stop <- struct{}{}
})
}
func (p *PeerNotifier) handler(hello *peers.LocalPairingPeerHello) { func (p *PeerNotifier) handler(hello *peers.LocalPairingPeerHello) {
signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: hello}) signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: hello})
p.logger.Debug("received peers.LocalPairingPeerHello message", zap.Any("hello message", hello)) p.logger.Debug("received peers.LocalPairingPeerHello message", zap.Any("hello message", hello))
// TODO p.stop <- struct{}{} Don't do this immediately start a countdown to kill after 5 seconds to allow the p.terminateIn(5 * time.Second)
// peer to discover us.
} }
func (p *PeerNotifier) Search() error { func (p *PeerNotifier) Search() error {

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/json"
udpp2p "github.com/schollz/peerdiscovery" udpp2p "github.com/schollz/peerdiscovery"
@ -30,6 +31,22 @@ func NewLocalPairingPeerHello(id []byte, name, deviceType string, k *ecdsa.Priva
return h, nil return h, nil
} }
func (h *LocalPairingPeerHello) MarshalJSON() ([]byte, error) {
alias := struct {
PeerID []byte
DeviceName string
DeviceType string
Address string
}{
PeerID: h.PeerId,
DeviceName: h.DeviceName,
DeviceType: h.DeviceType,
Address: h.Discovered.Address,
}
return json.Marshal(alias)
}
func (h *LocalPairingPeerHello) hash() []byte { func (h *LocalPairingPeerHello) hash() []byte {
dHash := sha256.Sum256(append(h.PeerId, []byte(h.DeviceName+h.DeviceType)...)) dHash := sha256.Sum256(append(h.PeerId, []byte(h.DeviceName+h.DeviceType)...))
return dHash[:] return dHash[:]

View File

@ -19,15 +19,15 @@ type UDPNotifier struct {
} }
func NewUDPNotifier(logger *zap.Logger, outputFunc NotifyHandler) (*UDPNotifier, error) { func NewUDPNotifier(logger *zap.Logger, outputFunc NotifyHandler) (*UDPNotifier, error) {
randId := make([]byte, 32) randID := make([]byte, 32)
_, err := rand.Read(randId) _, err := rand.Read(randID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
n := new(UDPNotifier) n := new(UDPNotifier)
n.logger = logger n.logger = logger
n.id = randId n.id = randID
n.notifyOutput = outputFunc n.notifyOutput = outputFunc
return n, nil return n, nil
} }

View File

@ -5,7 +5,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"encoding/asn1" "encoding/asn1"
"github.com/status-im/status-go/logutils"
"math/big" "math/big"
"testing" "testing"
"time" "time"
@ -13,6 +12,8 @@ import (
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/status-im/status-go/logutils"
) )
const ( const (