aboutsummaryrefslogtreecommitdiff
path: root/dht/options.go
blob: 86d7af768cb665f667709fd433c621e317c74645 (plain)
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
66
67
68
69
70
71
72
73
package dht

import (
	"github.com/felix/dhtsearch/models"
	"github.com/felix/logger"
	"github.com/hashicorp/golang-lru"
)

type Option func(*Node) error

func SetOnAnnouncePeer(f func(models.Peer)) Option {
	return func(n *Node) error {
		n.OnAnnouncePeer = f
		return nil
	}
}

func SetOnBadPeer(f func(models.Peer)) Option {
	return func(n *Node) error {
		n.OnBadPeer = f
		return nil
	}
}

// SetAddress sets the IP address to listen on
func SetAddress(ip string) Option {
	return func(n *Node) error {
		n.address = ip
		return nil
	}
}

// SetPort sets the port to listen on
func SetPort(p int) Option {
	return func(n *Node) error {
		n.port = p
		return nil
	}
}

// SetIPv6 enables IPv6
func SetIPv6(b bool) Option {
	return func(n *Node) error {
		if b {
			n.family = "udp6"
		}
		return nil
	}
}

// SetUDPTimeout sets the number of seconds to wait for UDP connections
func SetUDPTimeout(s int) Option {
	return func(n *Node) error {
		n.udpTimeout = s
		return nil
	}
}

// SetLogger sets the logger
func SetLogger(l logger.Logger) Option {
	return func(n *Node) error {
		n.log = l
		return nil
	}
}

// SetBlacklist sets the size of the node blacklist
func SetBlacklist(bl *lru.ARCCache) Option {
	return func(n *Node) (err error) {
		n.blacklist = bl
		return err
	}
}