diff --git a/examples/chat-with-mdns/mdns.go b/examples/chat-with-mdns/mdns.go index f0fd9abb10..dbcb964fcf 100644 --- a/examples/chat-with-mdns/mdns.go +++ b/examples/chat-with-mdns/mdns.go @@ -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 } diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index 436aa531a2..bcbdff134e 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -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 { diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index fb716a5159..f04f898103 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -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 { diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index 0c8c7a265d..0d55176a61 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -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 { diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index cec945478a..47e7bf856b 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -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) } @@ -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() } diff --git a/p2p/discovery/mdns/mdns.go b/p2p/discovery/mdns/mdns.go index e4e68c115c..bec06128b8 100644 --- a/p2p/discovery/mdns/mdns.go +++ b/p2p/discovery/mdns/mdns.go @@ -26,9 +26,8 @@ const ( var log = logging.Logger("mdns") type Service interface { + Start() error io.Closer - RegisterNotifee(Notifee) - UnregisterNotifee(Notifee) } type Notifee interface { @@ -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 { @@ -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() { @@ -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:]...) - } -} diff --git a/p2p/discovery/mdns/mdns_test.go b/p2p/discovery/mdns/mdns_test.go index d599e721db..8b5e000ab9 100644 --- a/p2p/discovery/mdns/mdns_test.go +++ b/p2p/discovery/mdns/mdns_test.go @@ -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() diff --git a/p2p/discovery/mdns_legacy/mdns.go b/p2p/discovery/mdns_legacy/mdns.go index 7472aea4af..e5283b5294 100644 --- a/p2p/discovery/mdns_legacy/mdns.go +++ b/p2p/discovery/mdns_legacy/mdns.go @@ -3,6 +3,7 @@ package mdns_legacy import ( "context" "errors" + "io" "net" "sync" "time" @@ -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 {