Skip to content

Commit

Permalink
pass notifees to the mDNS constructor, add dedicated Start method
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 5, 2021
1 parent 64693c6 commit 077325c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 58 deletions.
11 changes: 6 additions & 5 deletions examples/chat-with-mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {

//Initialize the MDNS service
func initMDNS(peerhost host.Host, rendezvous string) chan peer.AddrInfo {
// An hour might be a long long period in practical applications. But this is fine for us
ser := mdns.NewMdnsService(peerhost, rendezvous)

//register with service so that we get notified about peer discovery
// register with service so that we get notified about peer discovery
n := &discoveryNotifee{}
n.PeerChan = make(chan peer.AddrInfo)

ser.RegisterNotifee(n)
// An hour might be a long long period in practical applications. But this is fine for us
ser := mdns.NewMdnsService(peerhost, rendezvous, n)
if err := ser.Start(); err != nil {
panic(err)
}
return n.PeerChan
}
6 changes: 4 additions & 2 deletions examples/ipfs-camp-2019/06-Pubsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ func main() {
fmt.Println("Connected to", targetInfo.ID)
}

mdns := mdns.NewMdnsService(host, "")
notifee := &discoveryNotifee{h: host, ctx: ctx}
mdns.RegisterNotifee(notifee)
mdns := mdns.NewMdnsService(host, "", notifee)
if err := mdns.Start(); err != nil {
panic(err)
}

err = dht.Bootstrap(ctx)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions examples/ipfs-camp-2019/07-Messaging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ func main() {

fmt.Println("Connected to", targetInfo.ID)

mdns := mdns.NewMdnsService(host, "")
mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx})
mdns := mdns.NewMdnsService(host, "", &mdnsNotifee{h: host, ctx: ctx})
if err := mdns.Start(); err != nil {
panic(err)
}

err = dht.Bootstrap(ctx)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions examples/ipfs-camp-2019/08-End/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ func main() {

fmt.Println("Connected to", targetInfo.ID)

mdns := mdns.NewMdnsService(host, "")
mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx})
mdns := mdns.NewMdnsService(host, "", &mdnsNotifee{h: host, ctx: ctx})
if err := mdns.Start(); err != nil {
panic(err)
}

err = dht.Bootstrap(ctx)
if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions examples/pubsub/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func main() {
}

// setup local mDNS discovery
err = setupDiscovery(ctx, h)
if err != nil {
if err := setupDiscovery(h); err != nil {
panic(err)
}

Expand Down Expand Up @@ -103,11 +102,8 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {

// setupDiscovery creates an mDNS discovery service and attaches it to the libp2p Host.
// This lets us automatically discover peers on the same LAN and connect to them.
func setupDiscovery(ctx context.Context, h host.Host) error {
func setupDiscovery(h host.Host) error {
// setup mDNS discovery to find local peers
disc := mdns.NewMdnsService(h, DiscoveryServiceTag)

n := discoveryNotifee{h: h}
disc.RegisterNotifee(&n)
return nil
s := mdns.NewMdnsService(h, DiscoveryServiceTag, &discoveryNotifee{h: h})
return s.Start()
}
51 changes: 15 additions & 36 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ const (
var log = logging.Logger("mdns")

type Service interface {
Start() error
io.Closer
RegisterNotifee(Notifee)
UnregisterNotifee(Notifee)
}

type Notifee interface {
Expand All @@ -40,30 +39,36 @@ type mdnsService struct {
serviceName string

// The context is canceled when Close() is called.
ctx context.Context
ctxCancel context.CancelFunc

resolverWG sync.WaitGroup
server *zeroconf.Server

mutex sync.Mutex
notifees []Notifee
notifee Notifee
}

func NewMdnsService(host host.Host, serviceName string) *mdnsService {
ctx, cancel := context.WithCancel(context.Background())
func NewMdnsService(host host.Host, serviceName string, notifee Notifee) *mdnsService {
if serviceName == "" {
serviceName = ServiceName
}
s := &mdnsService{
ctxCancel: cancel,
host: host,
serviceName: serviceName,
notifee: notifee,
}
s.startServer()
s.startResolver(ctx)
s.ctx, s.ctxCancel = context.WithCancel(context.Background())
return s
}

func (s *mdnsService) Start() error {
if err := s.startServer(); err != nil {
return err
}
s.startResolver(s.ctx)
return nil
}

func (s *mdnsService) Close() error {
s.ctxCancel()
if s.server != nil {
Expand Down Expand Up @@ -176,13 +181,9 @@ func (s *mdnsService) startResolver(ctx context.Context) {
log.Debugf("failed to get peer info: %s", err)
continue
}
s.mutex.Lock()
for _, info := range infos {
for _, notif := range s.notifees {
go notif.HandlePeerFound(info)
}
go s.notifee.HandlePeerFound(info)
}
s.mutex.Unlock()
}
}()
go func() {
Expand All @@ -192,25 +193,3 @@ func (s *mdnsService) startResolver(ctx context.Context) {
}
}()
}

func (s *mdnsService) RegisterNotifee(n Notifee) {
s.mutex.Lock()
s.notifees = append(s.notifees, n)
s.mutex.Unlock()
}

func (s *mdnsService) UnregisterNotifee(n Notifee) {
s.mutex.Lock()
defer s.mutex.Unlock()

found := -1
for i, notif := range s.notifees {
if notif == n {
found = i
break
}
}
if found != -1 {
s.notifees = append(s.notifees[:found], s.notifees[found+1:]...)
}
}
4 changes: 2 additions & 2 deletions p2p/discovery/mdns/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func setupMDNS(t *testing.T, notifee Notifee) peer.ID {
t.Helper()
host, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
require.NoError(t, err)
s := NewMdnsService(host, "")
s.RegisterNotifee(notifee)
s := NewMdnsService(host, "", notifee)
require.NoError(t, s.Start())
t.Cleanup(func() {
host.Close()
s.Close()
Expand Down
8 changes: 7 additions & 1 deletion p2p/discovery/mdns_legacy/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mdns_legacy
import (
"context"
"errors"
"io"
"net"
"sync"
"time"
Expand All @@ -26,7 +27,12 @@ var log = logging.Logger("mdns_legacy")

const ServiceTag = "_ipfs-discovery._udp"

type Service = mdnsnew.Service
type Service interface {
io.Closer
RegisterNotifee(Notifee)
UnregisterNotifee(Notifee)
}

type Notifee = mdnsnew.Notifee

type mdnsService struct {
Expand Down

0 comments on commit 077325c

Please sign in to comment.