WIP trying dialing instead connect

This commit is contained in:
2024-10-08 18:14:16 +02:00
parent 06c4c6492e
commit 9fce13c6db

View File

@@ -18,12 +18,12 @@ import (
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/control"
"github.com/libp2p/go-libp2p/core/network"
//"github.com/libp2p/go-libp2p/core/peerstore"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
ds "github.com/ipfs/go-datastore"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/libp2p/go-libp2p/core/network"
"github.com/google/uuid"
multiaddr "github.com/multiformats/go-multiaddr"
@@ -41,7 +41,7 @@ import (
)
var (
topicNameFlag = "afbjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd3"
topicNameFlag = "pentapass"
logger = logging.Logger("globaldb")
Listen = libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0")
)
@@ -623,6 +623,8 @@ func initDHT(ctx context.Context, h host.Host) *dht.IpfsDHT {
defer wg.Done()
if err := h.Connect(ctx, *peerinfo); err != nil {
fmt.Println("Bootstrap warning:", err)
} else {
fmt.Printf("Connection established with bootstrap node: %q\n", *peerinfo)
}
}()
}
@@ -632,33 +634,6 @@ func initDHT(ctx context.Context, h host.Host) *dht.IpfsDHT {
}
func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
//cfg := NewConfig()
//for _, v := range cfg {
// for _, p := range v.Peers {
// peerID, err := peer.Decode(p.Id)
// if err != nil {
// logger.Fatal(err)
// }
// peerInfo, err := dht.FindPeer(ctx, peerID)
// if err != nil {
// fmt.Println(err)
// }
// h.Peerstore().AddAddrs(peerInfo.ID, peerInfo.Addrs, peerstore.PermanentAddrTTL)
// err = h.Connect(ctx, peerInfo)
// if err != nil {
// fmt.Printf("Failed connecting to %s, error: %s\n", peerInfo.ID, err)
// } else {
// fmt.Println("Connected to:", peerInfo.ID)
// }
// }
//}
routingDiscovery := drouting.NewRoutingDiscovery(dht)
dutil.Advertise(ctx, routingDiscovery, topicNameFlag)
@@ -667,7 +642,7 @@ func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
fmt.Printf("Own Id: %s\n", h.ID())
for !anyConnected {
time.Sleep(2 * time.Second)
//debug fmt.Println("Searching for peers...")
fmt.Println("Searching for peers...")
peerChan, err := routingDiscovery.FindPeers(ctx, topicNameFlag)
if err != nil {
panic(err)
@@ -676,13 +651,26 @@ func DiscoverPeers(ctx context.Context, h host.Host, dht *dht.IpfsDHT) {
if peer.ID == h.ID() {
continue // No self connection
}
err := h.Connect(ctx, peer)
if err != nil {
//debug fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
} else {
fmt.Println("Connected to:", peer.ID)
anyConnected = true
fmt.Printf("Trying to connect to a peer with id %s", peer.ID.String())
if h.Network().Connectedness(peer.ID) != network.Connected {
_, err = h.Network().DialPeer(ctx, peer.ID)
if err != nil {
fmt.Println("Connected to:", peer.ID)
continue
}
fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
}
//fmt.Printf("Trying to connect to a peer with id %s", peer.ID.String())
//err := h.Connect(ctx, peer)
//if err != nil {
// fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
//} else {
// fmt.Println("Connected to:", peer.ID)
// anyConnected = true
//}
}
}
fmt.Println("Peer discovery complete")