github.com/status-im/status-go@v1.1.0/server/pairing/peers.go (about)

     1  package pairing
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"time"
     7  
     8  	"go.uber.org/zap"
     9  
    10  	"github.com/status-im/status-go/common"
    11  	"github.com/status-im/status-go/logutils"
    12  	"github.com/status-im/status-go/server"
    13  	"github.com/status-im/status-go/server/pairing/peers"
    14  	"github.com/status-im/status-go/signal"
    15  )
    16  
    17  type PeerNotifier struct {
    18  	logger     *zap.Logger
    19  	stop       chan struct{}
    20  	terminator sync.Once
    21  }
    22  
    23  func NewPeerNotifier() *PeerNotifier {
    24  	logger := logutils.ZapLogger().Named("PeerNotifier")
    25  	stop := make(chan struct{})
    26  
    27  	return &PeerNotifier{
    28  		logger: logger,
    29  		stop:   stop,
    30  	}
    31  }
    32  
    33  func (p *PeerNotifier) terminateIn(d time.Duration) {
    34  	p.terminator.Do(func() {
    35  		time.Sleep(d)
    36  		p.stop <- struct{}{}
    37  	})
    38  }
    39  
    40  func (p *PeerNotifier) handler(hello *peers.LocalPairingPeerHello) {
    41  	signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: hello})
    42  	p.logger.Debug("received peers.LocalPairingPeerHello message", zap.Any("hello message", hello))
    43  	p.terminateIn(5 * time.Second)
    44  }
    45  
    46  func (p *PeerNotifier) Search() error {
    47  	// TODO until we can resolve Android errors when calling net.Interfaces() just noop. Sorry Android
    48  	if common.OperatingSystemIs(common.AndroidPlatform) {
    49  		return nil
    50  	}
    51  
    52  	dn, err := server.GetDeviceName()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	return peers.Search(dn, runtime.GOOS, p.handler, p.stop, p.logger)
    58  }