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

Example with mdns discovery #282

Closed
deckbsd opened this issue Feb 24, 2018 · 3 comments
Closed

Example with mdns discovery #282

deckbsd opened this issue Feb 24, 2018 · 3 comments

Comments

@deckbsd
Copy link

deckbsd commented Feb 24, 2018

Hello everybody,

I'm working on sample of how to use mDNS to find other peers. And well it didn't work very much. I don't know what i'm doing wrong. I'have followed some guidelines from here or here i'm kind of new in network programming, but i really want to know how it works because i think that what you are doing with p2plib , ipfs and so on is really great. here is my code :

package main

import (
	"context"
	"fmt"
	"time"

	crypto "github.com/libp2p/go-libp2p-crypto"
	peer "github.com/libp2p/go-libp2p-peer"
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"

	host "github.com/libp2p/go-libp2p-host"

	pstore "github.com/libp2p/go-libp2p-peerstore"
	swarm "github.com/libp2p/go-libp2p-swarm"
	disco "github.com/libp2p/go-libp2p/p2p/discovery"
	ma "github.com/multiformats/go-multiaddr"
)

type DiscoveryNotifee struct {
	h host.Host
}

func (n *DiscoveryNotifee) HandlePeerFound(pi pstore.PeerInfo) {
	fmt.Printf("discovered :")
	fmt.Println(pi)
}

func makeRandomNode(port int, ctx context.Context) *bhost.BasicHost {
	// Ignoring most errors for brevity
	// See echo example for more details and better implementation
	priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
	pid, _ := peer.IDFromPublicKey(pub)
	listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
	peerStore := pstore.NewPeerstore()
	peerStore.AddPrivKey(pid, priv)
	peerStore.AddPubKey(pid, pub)
	n, _ := swarm.NewNetwork(ctx, []ma.Multiaddr{listen}, pid, peerStore, nil)
	host := bhost.New(n)

	fmt.Println(host)
	return host
}

func main() {

	ctxa := context.Background()
	ctxb := context.Background()

	a := makeRandomNode(9000, ctxa)
	b := makeRandomNode(9001, ctxb)

	peersNode1 := len(a.Peerstore().Peers())
	peersNode2 := len(b.Peerstore().Peers())

	_ = peersNode1
	_ = peersNode2

	sa, err := disco.NewMdnsService(ctxa, a, time.Second/4, "findme")
	if err != nil {
		panic(err)
	}

	sb, err := disco.NewMdnsService(ctxb, b, time.Second/4, "findme")
	if err != nil {
		panic(err)
	}

	_ = sb

	n := &DiscoveryNotifee{a}

	sa.RegisterNotifee(n)

	time.Sleep(time.Second * 10)

	peersNodea := len(a.Peerstore().Peers())
	peersNodeb := len(b.Peerstore().Peers())

	_ = peersNodea
	_ = peersNodeb

	err = a.Connect(context.Background(), pstore.PeerInfo{ID: b.ID()})
	if err != nil {
		panic(err)
	}
}

do i need a routed host or something ? i'm a little bit confuse. Sorry for boring you probably with my little problem :-)

Edit : I just saw #257, maybe it's linked if nobody found nothing wrong in my code ? I 'll have a look at zeroconf and i ll try to implement it in discovery instead of mdns from whyrusleeping, but again, i'm a very newbie in that domain :-) and in golang too.

@deckbsd
Copy link
Author

deckbsd commented Feb 26, 2018

Actually, i tested the zeroconf library, it works really well. So i'm would like to replace it, in case if nobody did it during this time is there a doc to learn how to upload code on this project ? (i'm ok with git practise , but not in open-source oriented project).

@Stebalien
Copy link
Member

There's ongoing work here: #285

@deckbsd
Copy link
Author

deckbsd commented Mar 3, 2018

Hi @Stebalien , yep i saw that already, that's why i didn't talk more about it. i'll flag it as resolved. thanks 👍

@deckbsd deckbsd closed this as completed Mar 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants