forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpubsub.go
143 lines (116 loc) · 3.49 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
package p2pd
import (
pb "github.com/learning-at-home/go-libp2p-daemon/pb"
"github.com/libp2p/go-libp2p/core/peer"
ggio "github.com/gogo/protobuf/io"
ps "github.com/libp2p/go-libp2p-pubsub"
)
func (d *Daemon) doPubsub(req *pb.Request) (*pb.Response, *ps.Subscription) {
if d.pubsub == nil {
return errorResponseString("PubSub not enabled"), nil
}
if req.Pubsub == nil {
return errorResponseString("Malformed request; missing parameters"), nil
}
switch req.Pubsub.GetType() {
case pb.PSRequest_GET_TOPICS:
return d.doPubsubGetTopics(req.Pubsub)
case pb.PSRequest_LIST_PEERS:
return d.doPubsubListPeers(req.Pubsub)
case pb.PSRequest_PUBLISH:
return d.doPubsubPublish(req.Pubsub)
case pb.PSRequest_SUBSCRIBE:
return d.doPubsubSubscribe(req.Pubsub)
default:
log.Debugw("unexpected pubsub request type", "type", req.Pubsub.GetType())
return errorResponseString("Unexpected request"), nil
}
}
func (d *Daemon) doPubsubGetTopics(req *pb.PSRequest) (*pb.Response, *ps.Subscription) {
topics := d.pubsub.GetTopics()
return psOkResponse(psResponseTopics(topics)), nil
}
func (d *Daemon) doPubsubListPeers(req *pb.PSRequest) (*pb.Response, *ps.Subscription) {
if req.Topic == nil {
return errorResponseString("Malformed request; missing topic parameter"), nil
}
peers := d.pubsub.ListPeers(*req.Topic)
return psOkResponse(psResponsePeers(peers)), nil
}
func (d *Daemon) doPubsubPublish(req *pb.PSRequest) (*pb.Response, *ps.Subscription) {
if req.Topic == nil {
return errorResponseString("Malformed request; missing topic parameter"), nil
}
//lint:ignore SA1019 requires API changes
err := d.pubsub.Publish(*req.Topic, req.Data)
if err != nil {
return errorResponse(err), nil
}
return okResponse(), nil
}
func (d *Daemon) doPubsubSubscribe(req *pb.PSRequest) (*pb.Response, *ps.Subscription) {
if req.Topic == nil {
return errorResponseString("Malformed request; missing topic parameter"), nil
}
//lint:ignore SA1019 requires API changes
sub, err := d.pubsub.Subscribe(*req.Topic)
if err != nil {
return errorResponse(err), nil
}
return okResponse(), sub
}
func (d *Daemon) doPubsubPipe(sub *ps.Subscription, r ggio.ReadCloser, w ggio.WriteCloser) {
go func() {
// read something until the client closes the connection
// at which point we cancel the subscription
for {
var req pb.Request
err := r.ReadMsg(&req)
if err != nil {
sub.Cancel()
return
}
log.Warnw("unexpected message", "type", req.GetType())
}
}()
for {
msg, err := sub.Next(d.ctx)
if err != nil {
log.Warnw("subscription error", "error", err)
// goroutine will cancel the subscription once the connection is closed on return
return
}
psmsg := psMessage(msg)
err = w.WriteMsg(psmsg)
if err != nil {
log.Warnw("error writing pubsub message", "error", err)
// goroutine will cancel the subscription once the connection is closed on return
return
}
}
}
func psResponseTopics(topics []string) *pb.PSResponse {
return &pb.PSResponse{Topics: topics}
}
func psResponsePeers(peers []peer.ID) *pb.PSResponse {
xpeers := make([][]byte, len(peers))
for x, p := range peers {
xpeers[x] = []byte(p)
}
return &pb.PSResponse{PeerIDs: xpeers}
}
func psMessage(msg *ps.Message) *pb.PSMessage {
return &pb.PSMessage{
From: msg.From,
Data: msg.Data,
Seqno: msg.Seqno,
TopicIDs: []string{*msg.Topic},
Signature: msg.Signature,
Key: msg.Key,
}
}
func psOkResponse(r *pb.PSResponse) *pb.Response {
res := okResponse()
res.Pubsub = r
return res
}