From 64693c60746fc5c0de44b8e6de997fff8ca8d0c8 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 5 Oct 2021 12:54:12 +0100 Subject: [PATCH 1/2] use the new mDNS service in the chat-with-mdns example --- examples/chat-with-mdns/main.go | 2 +- examples/chat-with-mdns/mdns.go | 13 ++++--------- examples/go.sum | 1 - 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/examples/chat-with-mdns/main.go b/examples/chat-with-mdns/main.go index 6e3a4ebca0..c75be25d7d 100644 --- a/examples/chat-with-mdns/main.go +++ b/examples/chat-with-mdns/main.go @@ -113,7 +113,7 @@ func main() { fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, host.ID().Pretty()) - peerChan := initMDNS(ctx, host, cfg.RendezvousString) + peerChan := initMDNS(host, cfg.RendezvousString) peer := <-peerChan // will block untill we discover a peer fmt.Println("Found peer:", peer, ", connecting") diff --git a/examples/chat-with-mdns/mdns.go b/examples/chat-with-mdns/mdns.go index 178f7c2042..f0fd9abb10 100644 --- a/examples/chat-with-mdns/mdns.go +++ b/examples/chat-with-mdns/mdns.go @@ -1,12 +1,10 @@ package main import ( - "context" - "time" - "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns_legacy" + + "github.com/libp2p/go-libp2p/p2p/discovery/mdns" ) type discoveryNotifee struct { @@ -19,12 +17,9 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { } //Initialize the MDNS service -func initMDNS(ctx context.Context, peerhost host.Host, rendezvous string) chan peer.AddrInfo { +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, err := mdns.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) - if err != nil { - panic(err) - } + ser := mdns.NewMdnsService(peerhost, rendezvous) //register with service so that we get notified about peer discovery n := &discoveryNotifee{} diff --git a/examples/go.sum b/examples/go.sum index 4ff26db643..ef7c8e244f 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -875,7 +875,6 @@ github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a/go.mod h1:x6AKhvS github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= From 077325cde758a28de93aafeb703aff9cf5a3588e Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 5 Oct 2021 12:25:21 +0100 Subject: [PATCH 2/2] pass notifees to the mDNS constructor, add dedicated Start method --- examples/chat-with-mdns/mdns.go | 11 +++-- examples/ipfs-camp-2019/06-Pubsub/main.go | 6 ++- examples/ipfs-camp-2019/07-Messaging/main.go | 6 ++- examples/ipfs-camp-2019/08-End/main.go | 6 ++- examples/pubsub/chat/main.go | 12 ++--- p2p/discovery/mdns/mdns.go | 51 ++++++-------------- p2p/discovery/mdns/mdns_test.go | 4 +- p2p/discovery/mdns_legacy/mdns.go | 8 ++- 8 files changed, 46 insertions(+), 58 deletions(-) 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 {