-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gossipsub router, with not much success.
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |