2
2
package shell
3
3
4
4
import (
5
+ "bufio"
5
6
"bytes"
7
+ "context"
6
8
"encoding/json"
7
9
"errors"
8
10
"fmt"
@@ -13,6 +15,8 @@ import (
13
15
"strings"
14
16
"time"
15
17
18
+ pstore "github.com/libp2p/go-libp2p-peerstore"
19
+ notif "github.com/libp2p/go-libp2p-routing/notifications"
16
20
ma "github.com/multiformats/go-multiaddr"
17
21
manet "github.com/multiformats/go-multiaddr-net"
18
22
files "github.com/whyrusleeping/go-multipart-files"
@@ -336,6 +340,42 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
336
340
return & str .Responses [0 ], nil
337
341
}
338
342
343
+ func (s * Shell ) FindProvs (ctx context.Context , cid string ) (<- chan pstore.PeerInfo , error ) {
344
+ ctx , cancel := context .WithCancel (ctx )
345
+
346
+ resp , err := s .newRequest ("dht/findprovs" , cid ).Send (s .httpcli )
347
+ if err != nil {
348
+ return nil , err
349
+ }
350
+
351
+ if resp .Error != nil {
352
+ return nil , resp .Error
353
+ }
354
+
355
+ outchan := make (chan pstore.PeerInfo , 4 )
356
+
357
+ go func () {
358
+ var n notif.QueryEvent
359
+ scanner := bufio .NewScanner (resp .Output )
360
+ for scanner .Scan () {
361
+ json .Unmarshal (scanner .Bytes (), & n )
362
+ if n .Type == notif .Provider {
363
+ for _ , p := range n .Responses {
364
+ outchan <- * p
365
+ }
366
+ }
367
+ }
368
+ cancel ()
369
+ }()
370
+
371
+ go func () {
372
+ <- ctx .Done ()
373
+ resp .Close ()
374
+ }()
375
+
376
+ return outchan , nil
377
+ }
378
+
339
379
func (s * Shell ) Refs (hash string , recursive bool ) (<- chan string , error ) {
340
380
req := s .newRequest ("refs" , hash )
341
381
if recursive {
0 commit comments