Skip to content

Commit

Permalink
Added gossipsub router, with not much success.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 17, 2024
1 parent 9951ae1 commit 77a490e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
35 changes: 35 additions & 0 deletions p2p/pubsub/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pubsub

import (
p2pubsub "github.com/libp2p/go-libp2p-pubsub"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)

const PUBSUB_PROTOCOL = "/meshsub/1.1.0"

var r *pubsub.GossipSubRouter

func newRouter(h host.Host) *pubsub.GossipSubRouter {

r = p2pubsub.DefaultGossipSubRouter(h)

return r
}

// Adds a peer ID to the GossipSubRouter
func AddPeer(id peer.ID) {
r.AddPeer(id, PUBSUB_PROTOCOL)
r.AcceptFrom(id)

}

// Removes a peer ID from the GossipSubRouter
func RemovePeer(id peer.ID) {
r.RemovePeer(id)
}

func SetEoughPeers(t string, peerno int) {
r.EnoughPeers(t, peerno)
}
55 changes: 55 additions & 0 deletions ui/pubsub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ui

import (
"context"
"time"

"github.com/bahner/go-ma-actor/p2p/peer"
"github.com/bahner/go-ma-actor/p2p/pubsub"
p2peer "github.com/libp2p/go-libp2p/core/peer"
log "github.com/sirupsen/logrus"
)

const DISCOVERY_INTERVAL = time.Minute

func (ui *ChatUI) pubsubPeersLoop(ctx context.Context) {

ticker := time.NewTicker(DISCOVERY_INTERVAL)
defer ticker.Stop()

for {
select {
case <-ticker.C:
log.Debugf("Checking for new peers to add to pubsub")
for _, p := range ui.p.ConnectedProtectedPeersAddrInfo() {
err := ui.addPeerToPubsub(p)
if err == peer.ErrAddrInfoAddrsEmpty || err == peer.ErrAlreadyConnected {
continue
}
log.Debugf("Added peer %s to pubsub", p.ID)
}
case <-ctx.Done():
log.Warn("pubsubPeersLoop: context done")
return
}
}
}

func (ui *ChatUI) addPeerToPubsub(pai p2peer.AddrInfo) error {

if len(pai.Addrs) == 0 {
return peer.ErrAddrInfoAddrsEmpty
}

// Don't add already connected peers
for _, p := range ui.e.Topic.ListPeers() {
if p == pai.ID {
return peer.ErrAlreadyConnected
}
}

pubsub.AddPeer(pai.ID)

return nil

}

0 comments on commit 77a490e

Please sign in to comment.