dht.go 1.36 KB
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1 2 3 4 5 6
package options

type DhtProvideSettings struct {
	Recursive bool
}

Łukasz Magiera's avatar
Łukasz Magiera committed
7 8 9 10
type DhtFindProvidersSettings struct {
	NumProviders int
}

Łukasz Magiera's avatar
Łukasz Magiera committed
11
type DhtProvideOption func(*DhtProvideSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
12
type DhtFindProvidersOption func(*DhtFindProvidersSettings) error
Łukasz Magiera's avatar
Łukasz Magiera committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

func DhtProvideOptions(opts ...DhtProvideOption) (*DhtProvideSettings, error) {
	options := &DhtProvideSettings{
		Recursive: false,
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}
	return options, nil
}

Łukasz Magiera's avatar
Łukasz Magiera committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
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
}

42
type dhtOpts struct{}
Łukasz Magiera's avatar
Łukasz Magiera committed
43

44 45
var Dht dhtOpts

46
// Recursive is an option for Dht.Provide which specifies whether to provide
47
// the given path recursively
48
func (dhtOpts) Recursive(recursive bool) DhtProvideOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
49 50 51 52 53
	return func(settings *DhtProvideSettings) error {
		settings.Recursive = recursive
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
54

55
// NumProviders is an option for Dht.FindProviders which specifies the
56
// number of peers to look for. Default is 20
57
func (dhtOpts) NumProviders(numProviders int) DhtFindProvidersOption {
Łukasz Magiera's avatar
Łukasz Magiera committed
58 59 60 61 62
	return func(settings *DhtFindProvidersSettings) error {
		settings.NumProviders = numProviders
		return nil
	}
}