Commit db9865b9 authored by Łukasz Magiera's avatar Łukasz Magiera Committed by Steven Allen

coreapi: implement dht api

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent bbd736fd
......@@ -31,6 +31,9 @@ type CoreAPI interface {
// ObjectAPI returns an implementation of Object API
Object() ObjectAPI
// Dht returns an implementation of Dht API
Dht() DhtAPI
// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, Path) (ResolvedPath, error)
......
......@@ -17,7 +17,11 @@ type DhtAPI interface {
// FindProviders finds peers in the DHT who can provide a specific value
// given a key.
FindProviders(context.Context, Path) (<-chan peer.ID, error) //TODO: is path the right choice here?
FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan peer.ID, error) //TODO: is path the right choice here?
// WithNumProviders is an option for FindProviders which specifies the
// number of peers to look for. Default is 20
WithNumProviders(numProviders int) options.DhtFindProvidersOption
// Provide announces to the network that you are providing given values
Provide(context.Context, Path, ...options.DhtProvideOption) error
......
......@@ -4,7 +4,12 @@ type DhtProvideSettings struct {
Recursive bool
}
type DhtFindProvidersSettings struct {
NumProviders int
}
type DhtProvideOption func(*DhtProvideSettings) error
type DhtFindProvidersOption func(*DhtFindProvidersSettings) error
func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
options := &DhtProvideSettings{
......@@ -20,6 +25,20 @@ func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
return options, nil
}
func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersSettings, error) {
options := &DhtFindProvidersSettings{
NumProviders: 20,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type DhtOptions struct{}
func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
......@@ -28,3 +47,10 @@ func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
return nil
}
}
func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
return func(settings *DhtFindProvidersSettings) error {
settings.NumProviders = numProviders
return nil
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment