dht.go 1.15 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
}

Łukasz Magiera's avatar
Łukasz Magiera committed
42 43 44 45 46 47 48 49
type DhtOptions struct{}

func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
	return func(settings *DhtProvideSettings) error {
		settings.Recursive = recursive
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
50 51 52 53 54 55 56

func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
	return func(settings *DhtFindProvidersSettings) error {
		settings.NumProviders = numProviders
		return nil
	}
}