Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove {Un}RegisterNotifee functions from mDNS service #1220

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/chat-with-mdns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 9 additions & 13 deletions examples/chat-with-mdns/mdns.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -19,17 +17,15 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
}

//Initialize the MDNS service
func initMDNS(ctx context.Context, 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)
}

//register with service so that we get notified about peer discovery
func initMDNS(peerhost host.Host, rendezvous string) chan peer.AddrInfo {
// 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
}
1 change: 0 additions & 1 deletion examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
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