This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
pubsub.go
95 lines (81 loc) · 2.14 KB
/
pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ipfs
import (
"context"
"io"
"strings"
"sync"
"time"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
peer "github.com/libp2p/go-libp2p-core/peer"
)
const PublishTimeout = time.Second * 5
// Publish publishes data to a topic
func Publish(node *core.IpfsNode, topic string, data []byte) error {
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return err
}
ctx, pcancel := context.WithTimeout(node.Context(), PublishTimeout)
defer pcancel()
log.Debugf("publishing to topic %s", topic)
return api.PubSub().Publish(ctx, topic, data)
}
// Subscribe subscribes to a topic
func Subscribe(node *core.IpfsNode, ctx context.Context, topic string, discover bool, msgs chan iface.PubSubMessage) error {
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return err
}
sub, err := api.PubSub().Subscribe(ctx, topic, options.PubSub.Discover(discover))
if err != nil {
return err
}
defer sub.Close()
for {
msg, err := sub.Next(node.Context())
if err == io.EOF || err == context.Canceled {
return nil
} else if err != nil {
return err
}
msgs <- msg
}
}
// connectToTopicReceiver attempts to connect with a pubsub topic's receiver
func connectToTopicReceiver(node *core.IpfsNode, ctx context.Context, topic string) error {
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return err
}
blk, err := api.Block().Put(ctx, strings.NewReader("floodsub:"+topic))
if err != nil {
return err
}
fctx, cancel := context.WithCancel(ctx)
defer cancel()
log.Debugf("looking for peers for topic %s", topic)
provs := node.Routing.FindProvidersAsync(fctx, blk.Path().Cid(), 10)
var wg sync.WaitGroup
for p := range provs {
log.Debugf("found topic provider %s", p.ID.Pretty())
if !strings.Contains(topic, p.ID.Pretty()) {
continue
}
wg.Add(1)
go func(pi peer.AddrInfo) {
defer wg.Done()
err := node.PeerHost.Connect(ctx, pi)
if err != nil {
log.Info("pubsub discover: ", err)
return
}
log.Info("connected to pubsub peer:", pi.ID)
cancel()
}(p)
}
wg.Wait()
return nil
}