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 (
"runtime"
"sync"
"time"
"go.uber.org/zap"
@ -12,8 +14,9 @@ import (
)
type PeerNotifier struct {
logger *zap.Logger
stop chan struct{}
logger *zap.Logger
stop chan struct{}
terminator sync.Once
}
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) {
signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: 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
// peer to discover us.
p.terminateIn(5 * time.Second)
}
func (p *PeerNotifier) Search() error {

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"encoding/json"
udpp2p "github.com/schollz/peerdiscovery"
@ -30,6 +31,22 @@ func NewLocalPairingPeerHello(id []byte, name, deviceType string, k *ecdsa.Priva
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 {
dHash := sha256.Sum256(append(h.PeerId, []byte(h.DeviceName+h.DeviceType)...))
return dHash[:]

View File

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

View File

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