go-libp2p-pubsub/floodsub.go

81 lines
1.7 KiB
Go
Raw Normal View History

2016-09-10 03:13:50 +00:00
package floodsub
import (
2016-09-10 23:03:53 +00:00
"context"
2016-09-13 02:59:24 +00:00
pb "github.com/libp2p/go-floodsub/pb"
2016-09-10 15:14:17 +00:00
2016-10-05 19:47:20 +00:00
host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
protocol "github.com/libp2p/go-libp2p-protocol"
)
const (
FloodSubID = protocol.ID("/floodsub/1.0.0")
)
2018-06-04 21:14:54 +00:00
// NewFloodsubWithProtocols returns a new floodsub-enabled PubSub objecting using the protocols specified in ps
func NewFloodsubWithProtocols(ctx context.Context, h host.Host, ps []protocol.ID, opts ...Option) (*PubSub, error) {
rt := &FloodSubRouter{
protocols: ps,
}
return NewPubSub(ctx, h, rt, opts...)
}
// NewFloodSub returns a new PubSub object using the FloodSubRouter
func NewFloodSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, error) {
2018-06-04 21:14:54 +00:00
return NewFloodsubWithProtocols(ctx, h, []protocol.ID{FloodSubID}, opts...)
}
2016-09-10 03:13:50 +00:00
type FloodSubRouter struct {
p *PubSub
protocols []protocol.ID
2016-10-19 23:01:06 +00:00
}
func (fs *FloodSubRouter) Protocols() []protocol.ID {
return fs.protocols
2016-09-10 03:13:50 +00:00
}
func (fs *FloodSubRouter) Attach(p *PubSub) {
fs.p = p
}
func (fs *FloodSubRouter) AddPeer(peer.ID, protocol.ID) {}
func (fs *FloodSubRouter) RemovePeer(peer.ID) {}
2018-01-27 07:52:35 +00:00
func (fs *FloodSubRouter) HandleRPC(rpc *RPC) {}
2018-01-27 07:52:35 +00:00
func (fs *FloodSubRouter) Publish(from peer.ID, msg *pb.Message) {
tosend := make(map[peer.ID]struct{})
for _, topic := range msg.GetTopicIDs() {
tmap, ok := fs.p.topics[topic]
if !ok {
continue
}
2016-09-11 03:47:12 +00:00
2017-11-23 13:39:14 +00:00
for p := range tmap {
tosend[p] = struct{}{}
}
}
out := rpcWithMessages(msg)
for pid := range tosend {
2016-09-11 03:47:12 +00:00
if pid == from || pid == peer.ID(msg.GetFrom()) {
continue
}
mch, ok := fs.p.peers[pid]
if !ok {
continue
}
select {
case mch <- out:
default:
2017-08-30 02:42:33 +00:00
log.Infof("dropping message to peer %s: queue full", pid)
// Drop it. The peer is too slow.
}
}
}