This repository was archived by the owner on Feb 7, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1212 "name" : " go-ipfs-api" ,
1313 "version" : " 1.0.0"
1414}
15+
Original file line number Diff line number Diff line change 22package shell
33
44import (
5+ "bufio"
56 "bytes"
7+ "context"
68 "encoding/json"
79 "errors"
810 "fmt"
@@ -13,6 +15,8 @@ import (
1315 "strings"
1416 "time"
1517
18+ pstore "github.com/libp2p/go-libp2p-peerstore"
19+ notif "github.com/libp2p/go-libp2p-routing/notifications"
1620 ma "github.com/multiformats/go-multiaddr"
1721 manet "github.com/multiformats/go-multiaddr-net"
1822 files "github.com/whyrusleeping/go-multipart-files"
@@ -336,6 +340,42 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
336340 return & str .Responses [0 ], nil
337341}
338342
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+
339379func (s * Shell ) Refs (hash string , recursive bool ) (<- chan string , error ) {
340380 req := s .newRequest ("refs" , hash )
341381 if recursive {
You can’t perform that action at this time.
0 commit comments