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