graphsync.go 2.1 KB
Newer Older
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
package graphsync

import (
	"context"

	"github.com/ipfs/go-graphsync/messagequeue"
	"github.com/ipfs/go-graphsync/peermanager"

	"github.com/ipfs/go-graphsync/ipldbridge"
	gsnet "github.com/ipfs/go-graphsync/network"
	"github.com/ipfs/go-graphsync/requestmanager"
	ipld "github.com/ipld/go-ipld-prime"
	"github.com/libp2p/go-libp2p-peer"
)

// ResponseProgress is the fundamental unit of responses making progress in
// Graphsync.
type ResponseProgress = requestmanager.ResponseProgress

// ResponseError is an error that occurred during a traversal.
type ResponseError = requestmanager.ResponseError

// GraphSync is an instance of a GraphSync exchange that implements
// the graphsync protocol.
type GraphSync struct {
	ipldBridge     ipldbridge.IPLDBridge
	network        gsnet.GraphSyncNetwork
	loader         ipldbridge.Loader
	requestManager *requestmanager.RequestManager
	peerManager    *peermanager.PeerManager
	ctx            context.Context
	cancel         context.CancelFunc
}

// New creates a new GraphSync Exchange on the given network,
// using the given bridge to IPLD and the given link loader.
func New(parent context.Context, network gsnet.GraphSyncNetwork,
	ipldBridge ipldbridge.IPLDBridge, loader ipldbridge.Loader) *GraphSync {
	ctx, cancel := context.WithCancel(parent)

	createMessageQueue := func(ctx context.Context, p peer.ID) peermanager.PeerQueue {
		return messagequeue.New(ctx, p, network)
	}
	peerManager := peermanager.New(ctx, createMessageQueue)
	requestManager := requestmanager.New(ctx, ipldBridge)
	graphSync := &GraphSync{
		ipldBridge:     ipldBridge,
		network:        network,
		loader:         loader,
		requestManager: requestManager,
		peerManager:    peerManager,
		ctx:            ctx,
		cancel:         cancel,
	}

	requestManager.SetDelegate(peerManager)
	requestManager.Startup()

	return graphSync
}

// Request initiates a new GraphSync request to the given peer using the given selector spec.
func (gs *GraphSync) Request(ctx context.Context, p peer.ID, rootedSelector ipld.Node) (<-chan ResponseProgress, <-chan ResponseError) {
	return gs.requestManager.SendRequest(ctx, p, rootedSelector)
}