@@ -3,23 +3,16 @@ package commands
3
3
import (
4
4
"context"
5
5
"encoding/binary"
6
- "errors"
7
6
"fmt"
8
7
"io"
9
8
"net/http"
10
9
"sort"
11
- "sync"
12
- "time"
13
10
14
- core "github.com/ipfs/go-ipfs/core"
15
11
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
16
12
e "github.com/ipfs/go-ipfs/core/commands/e"
13
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
17
14
18
- cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
19
- blocks "gx/ipfs/QmRcHuYzAyswytBuMF78rj3LTChYszomRFXNg4685ZN1WM/go-block-format"
20
- pstore "gx/ipfs/QmSJ36wcYQyEViJUWUEhJU81tw1KdakTKqLLHbvYbA9zDv/go-libp2p-peerstore"
21
15
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
22
- floodsub "gx/ipfs/QmUK4h113Hh7bR2gPpsMcbUEbbzc7hspocmPi91Bmi69nH/go-libp2p-floodsub"
23
16
cmds "gx/ipfs/QmXTmUCBtDUrzDYVzASogLiNph7EBuYqEgPL7QoHNMzUnz/go-ipfs-cmds"
24
17
)
25
18
@@ -48,6 +41,13 @@ const (
48
41
pubsubDiscoverOptionName = "discover"
49
42
)
50
43
44
+ type pubsubMessage struct {
45
+ From []byte `json:"from,omitempty"`
46
+ Data []byte `json:"data,omitempty"`
47
+ Seqno []byte `json:"seqno,omitempty"`
48
+ TopicIDs []string `json:"topicIDs,omitempty"`
49
+ }
50
+
51
51
var PubsubSubCmd = & cmds.Command {
52
52
Helptext : cmdkit.HelpText {
53
53
Tagline : "Subscribe to messages on a given topic." ,
@@ -79,40 +79,19 @@ This command outputs data in the following encodings:
79
79
cmdkit .BoolOption (pubsubDiscoverOptionName , "try to discover other peers subscribed to the same topic" ),
80
80
},
81
81
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
82
- n , err := cmdenv .GetNode (env )
82
+ api , err := cmdenv .GetApi (env )
83
83
if err != nil {
84
84
return err
85
85
}
86
86
87
- // Must be online!
88
- if ! n .OnlineMode () {
89
- return cmdkit .Errorf (cmdkit .ErrClient , ErrNotOnline .Error ())
90
- }
91
-
92
- if n .Floodsub == nil {
93
- return fmt .Errorf ("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use" )
94
- }
95
-
96
87
topic := req .Arguments [0 ]
97
- sub , err := n .Floodsub .Subscribe (topic )
88
+ discover , _ := req .Options [pubsubDiscoverOptionName ].(bool )
89
+
90
+ sub , err := api .PubSub ().Subscribe (req .Context , topic , options .PubSub .Discover (discover ))
98
91
if err != nil {
99
92
return err
100
93
}
101
- defer sub .Cancel ()
102
-
103
- discover , _ := req .Options [pubsubDiscoverOptionName ].(bool )
104
- if discover {
105
- go func () {
106
- blk := blocks .NewBlock ([]byte ("floodsub:" + topic ))
107
- err := n .Blocks .AddBlock (blk )
108
- if err != nil {
109
- log .Error ("pubsub discovery: " , err )
110
- return
111
- }
112
-
113
- connectToPubSubPeers (req .Context , n , blk .Cid ())
114
- }()
115
- }
94
+ defer sub .Close ()
116
95
117
96
if f , ok := res .(http.Flusher ); ok {
118
97
f .Flush ()
@@ -126,15 +105,17 @@ This command outputs data in the following encodings:
126
105
return err
127
106
}
128
107
129
- err = res .Emit (msg )
130
- if err != nil {
131
- return err
132
- }
108
+ res .Emit (& pubsubMessage {
109
+ Data : msg .Data (),
110
+ From : []byte (msg .From ()),
111
+ Seqno : msg .Seq (),
112
+ TopicIDs : msg .Topics (),
113
+ })
133
114
}
134
115
},
135
116
Encoders : cmds.EncoderMap {
136
117
cmds .Text : cmds .MakeEncoder (func (req * cmds.Request , w io.Writer , v interface {}) error {
137
- m , ok := v .(* floodsub. Message )
118
+ m , ok := v .(* pubsubMessage )
138
119
if ! ok {
139
120
return fmt .Errorf ("unexpected type: %T" , v )
140
121
}
@@ -143,7 +124,7 @@ This command outputs data in the following encodings:
143
124
return err
144
125
}),
145
126
"ndpayload" : cmds .MakeEncoder (func (req * cmds.Request , w io.Writer , v interface {}) error {
146
- m , ok := v .(* floodsub. Message )
127
+ m , ok := v .(* pubsubMessage )
147
128
if ! ok {
148
129
return fmt .Errorf ("unexpected type: %T" , v )
149
130
}
@@ -153,7 +134,7 @@ This command outputs data in the following encodings:
153
134
return err
154
135
}),
155
136
"lenpayload" : cmds .MakeEncoder (func (req * cmds.Request , w io.Writer , v interface {}) error {
156
- m , ok := v .(* floodsub. Message )
137
+ m , ok := v .(* pubsubMessage )
157
138
if ! ok {
158
139
return fmt .Errorf ("unexpected type: %T" , v )
159
140
}
@@ -166,31 +147,7 @@ This command outputs data in the following encodings:
166
147
return err
167
148
}),
168
149
},
169
- Type : floodsub.Message {},
170
- }
171
-
172
- func connectToPubSubPeers (ctx context.Context , n * core.IpfsNode , cid cid.Cid ) {
173
- ctx , cancel := context .WithCancel (ctx )
174
- defer cancel ()
175
-
176
- provs := n .Routing .FindProvidersAsync (ctx , cid , 10 )
177
- wg := & sync.WaitGroup {}
178
- for p := range provs {
179
- wg .Add (1 )
180
- go func (pi pstore.PeerInfo ) {
181
- defer wg .Done ()
182
- ctx , cancel := context .WithTimeout (ctx , time .Second * 10 )
183
- defer cancel ()
184
- err := n .PeerHost .Connect (ctx , pi )
185
- if err != nil {
186
- log .Info ("pubsub discover: " , err )
187
- return
188
- }
189
- log .Info ("connected to pubsub peer:" , pi .ID )
190
- }(p )
191
- }
192
-
193
- wg .Wait ()
150
+ Type : pubsubMessage {},
194
151
}
195
152
196
153
var PubsubPubCmd = & cmds.Command {
@@ -210,20 +167,11 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
210
167
cmdkit .StringArg ("data" , true , true , "Payload of message to publish." ).EnableStdin (),
211
168
},
212
169
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
213
- n , err := cmdenv .GetNode (env )
170
+ api , err := cmdenv .GetApi (env )
214
171
if err != nil {
215
172
return err
216
173
}
217
174
218
- // Must be online!
219
- if ! n .OnlineMode () {
220
- return cmdkit .Errorf (cmdkit .ErrClient , ErrNotOnline .Error ())
221
- }
222
-
223
- if n .Floodsub == nil {
224
- return errors .New ("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use." )
225
- }
226
-
227
175
topic := req .Arguments [0 ]
228
176
229
177
err = req .ParseBodyArgs ()
@@ -232,7 +180,7 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
232
180
}
233
181
234
182
for _ , data := range req .Arguments [1 :] {
235
- if err := n . Floodsub .Publish (topic , []byte (data )); err != nil {
183
+ if err := api . PubSub () .Publish (req . Context , topic , []byte (data )); err != nil {
236
184
return err
237
185
}
238
186
}
@@ -254,21 +202,17 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
254
202
` ,
255
203
},
256
204
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
257
- n , err := cmdenv .GetNode (env )
205
+ api , err := cmdenv .GetApi (env )
258
206
if err != nil {
259
207
return err
260
208
}
261
209
262
- // Must be online!
263
- if ! n .OnlineMode () {
264
- return cmdkit .Errorf (cmdkit .ErrClient , ErrNotOnline .Error ())
265
- }
266
-
267
- if n .Floodsub == nil {
268
- return errors .New ("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use." )
210
+ l , err := api .PubSub ().Ls (req .Context )
211
+ if err != nil {
212
+ return err
269
213
}
270
214
271
- return cmds .EmitOnce (res , stringList {n . Floodsub . GetTopics () })
215
+ return cmds .EmitOnce (res , stringList {l })
272
216
},
273
217
Type : stringList {},
274
218
Encoders : cmds.EncoderMap {
@@ -308,26 +252,21 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
308
252
cmdkit .StringArg ("topic" , false , false , "topic to list connected peers of" ),
309
253
},
310
254
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
311
- n , err := cmdenv .GetNode (env )
255
+ api , err := cmdenv .GetApi (env )
312
256
if err != nil {
313
257
return err
314
258
}
315
259
316
- // Must be online!
317
- if ! n .OnlineMode () {
318
- return cmdkit .Errorf (cmdkit .ErrClient , ErrNotOnline .Error ())
319
- }
320
-
321
- if n .Floodsub == nil {
322
- return errors .New ("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use" )
323
- }
324
-
325
260
var topic string
326
261
if len (req .Arguments ) == 1 {
327
262
topic = req .Arguments [0 ]
328
263
}
329
264
330
- peers := n .Floodsub .ListPeers (topic )
265
+ peers , err := api .PubSub ().Peers (req .Context , options .PubSub .Topic (topic ))
266
+ if err != nil {
267
+ return err
268
+ }
269
+
331
270
list := & stringList {make ([]string , 0 , len (peers ))}
332
271
333
272
for _ , peer := range peers {
0 commit comments