forked from learning-at-home/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
151 lines (122 loc) · 2.95 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package p2pclient
import (
"context"
"fmt"
"github.com/chiangmaioneluv/go-libp2p/core/peer"
ggio "github.com/gogo/protobuf/io"
pb "github.com/learning-at-home/go-libp2p-daemon/pb"
)
func newPubsubReq(req *pb.PSRequest) *pb.Request {
return &pb.Request{
Type: pb.Request_PUBSUB.Enum(),
Pubsub: req,
}
}
func (c *Client) doPubsub(psReq *pb.PSRequest) (*pb.PSResponse, error) {
control, err := c.newControlConn()
if err != nil {
return nil, err
}
defer control.Close()
w := ggio.NewDelimitedWriter(control)
req := newPubsubReq(psReq)
if err = w.WriteMsg(req); err != nil {
return nil, err
}
r := ggio.NewDelimitedReader(control, MessageSizeMax)
msg := &pb.Response{}
if err = r.ReadMsg(msg); err != nil {
return nil, err
}
if msg.GetType() == pb.Response_ERROR {
err := fmt.Errorf("error from daemon in %s response: %s", req.GetType().String(), msg.GetError())
log.Errorf(err.Error())
return nil, err
}
return msg.GetPubsub(), nil
}
func (c *Client) streamPubsubRequest(ctx context.Context, psReq *pb.PSRequest) (<-chan *pb.PSMessage, error) {
control, err := c.newControlConn()
if err != nil {
return nil, err
}
w := ggio.NewDelimitedWriter(control)
req := newPubsubReq(psReq)
if err = w.WriteMsg(req); err != nil {
control.Close()
return nil, err
}
r := ggio.NewDelimitedReader(control, MessageSizeMax)
msg := &pb.Response{}
if err = r.ReadMsg(msg); err != nil {
control.Close()
return nil, err
}
if msg.GetType() == pb.Response_ERROR {
err := fmt.Errorf("error from daemon in %s response: %s", req.GetType().String(), msg.GetError())
log.Errorf(err.Error())
return nil, err
}
go func() {
<-ctx.Done()
control.Close()
}()
out := make(chan *pb.PSMessage)
go func() {
defer close(out)
defer control.Close()
for {
msg := &pb.PSMessage{}
if err := r.ReadMsg(msg); err != nil {
log.Errorw("reading pubsub message", "error", err)
return
}
out <- msg
}
}()
return out, nil
}
func (c *Client) GetTopics() ([]string, error) {
req := &pb.PSRequest{
Type: pb.PSRequest_GET_TOPICS.Enum(),
}
res, err := c.doPubsub(req)
if err != nil {
return nil, err
}
return res.GetTopics(), nil
}
func (c *Client) ListPeers() ([]peer.ID, error) {
req := &pb.PSRequest{
Type: pb.PSRequest_LIST_PEERS.Enum(),
}
res, err := c.doPubsub(req)
if err != nil {
return nil, err
}
ids := make([]peer.ID, len(res.GetPeerIDs()))
for i, idbytes := range res.GetPeerIDs() {
id, err := peer.IDFromBytes(idbytes)
if err != nil {
return nil, err
}
ids[i] = id
}
return ids, nil
}
func (c *Client) Publish(topic string, data []byte) error {
req := &pb.PSRequest{
Type: pb.PSRequest_PUBLISH.Enum(),
Topic: &topic,
Data: data,
}
_, err := c.doPubsub(req)
return err
}
func (c *Client) Subscribe(ctx context.Context, topic string) (<-chan *pb.PSMessage, error) {
req := &pb.PSRequest{
Type: pb.PSRequest_SUBSCRIBE.Enum(),
Topic: &topic,
}
return c.streamPubsubRequest(ctx, req)
}