Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 190b506

Browse files
committed
Add experimental findprovs
1 parent 458b0e6 commit 190b506

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

shell.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
package shell
33

44
import (
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"
@@ -371,6 +375,42 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
371375
return &str.Responses[0], nil
372376
}
373377

378+
func (s *Shell) FindProvs(ctx context.Context, cid string) (<-chan pstore.PeerInfo, error) {
379+
ctx, cancel := context.WithCancel(ctx)
380+
381+
resp, err := s.newRequest("dht/findprovs", cid).Send(s.httpcli)
382+
if err != nil {
383+
return nil, err
384+
}
385+
386+
if resp.Error != nil {
387+
return nil, resp.Error
388+
}
389+
390+
outchan := make(chan pstore.PeerInfo, 4)
391+
392+
go func() {
393+
var n notif.QueryEvent
394+
scanner := bufio.NewScanner(resp.Output)
395+
for scanner.Scan() {
396+
json.Unmarshal(scanner.Bytes(), &n)
397+
if n.Type == notif.Provider {
398+
for _, p := range n.Responses {
399+
outchan <- *p
400+
}
401+
}
402+
}
403+
cancel()
404+
}()
405+
406+
go func() {
407+
<-ctx.Done()
408+
resp.Close()
409+
}()
410+
411+
return outchan, nil
412+
}
413+
374414
func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
375415
req := s.newRequest("refs", hash)
376416
if recursive {

0 commit comments

Comments
 (0)