123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /*
- Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package discovery
- import (
- "context"
- "github.com/ipfs/go-log"
- "github.com/libp2p/go-libp2p"
- "github.com/libp2p/go-libp2p/core/host"
- "github.com/libp2p/go-libp2p/core/peer"
- "github.com/libp2p/go-libp2p/p2p/discovery/mdns"
- )
- type MDNS struct {
- DiscoveryServiceTag string
- }
- // discoveryNotifee gets notified when we find a new peer via mDNS discovery
- type discoveryNotifee struct {
- h host.Host
- c log.StandardLogger
- }
- // HandlePeerFound connects to peers discovered via mDNS. Once they're connected,
- // the PubSub system will automatically start interacting with them if they also
- // support PubSub.
- func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
- //n.c.Infof("mDNS: discovered new peer %s\n", pi.ID.String())
- err := n.h.Connect(context.Background(), pi)
- if err != nil {
- n.c.Debugf("mDNS: error connecting to peer %s: %s\n", pi.ID.String(), err)
- }
- }
- func (d *MDNS) Option(ctx context.Context) func(c *libp2p.Config) error {
- return func(*libp2p.Config) error { return nil }
- }
- func (d *MDNS) Run(l log.StandardLogger, ctx context.Context, host host.Host) error {
- // setup mDNS discovery to find local peers
- disc := mdns.NewMdnsService(host, d.DiscoveryServiceTag, &discoveryNotifee{h: host, c: l})
- return disc.Start()
- }
|