add the ability to provide seed nodes to the network

This commit is contained in:
Asim Aslam 2019-08-29 14:53:30 +01:00
parent 6e3d53e1ee
commit 7479515099
2 changed files with 22 additions and 1 deletions

View File

@ -75,6 +75,7 @@ func newNetwork(opts ...Option) Network {
// init tunnel address to the network bind address // init tunnel address to the network bind address
options.Tunnel.Init( options.Tunnel.Init(
tunnel.Address(options.Address), tunnel.Address(options.Address),
tunnel.Nodes(options.Nodes...),
) )
// init router Id to the network id // init router Id to the network id
@ -135,10 +136,20 @@ func (n *network) resolveNodes() ([]string, error) {
return nil, err return nil, err
} }
nodeMap := make(map[string]bool)
// collect network node addresses // collect network node addresses
nodes := make([]string, len(records)) var nodes []string
for i, record := range records { for i, record := range records {
nodes[i] = record.Address nodes[i] = record.Address
nodeMap[record.Address] = true
}
// append seed nodes if we have them
for _, node := range n.options.Nodes {
if _, ok := nodeMap[node]; !ok {
nodes = append(nodes, node)
}
} }
return nodes, nil return nodes, nil

View File

@ -20,6 +20,8 @@ type Options struct {
Name string Name string
// Address to bind to // Address to bind to
Address string Address string
// Nodes is a list of seed nodes
Nodes []string
// Tunnel is network tunnel // Tunnel is network tunnel
Tunnel tunnel.Tunnel Tunnel tunnel.Tunnel
// Router is network router // Router is network router
@ -51,6 +53,14 @@ func Address(a string) Option {
} }
} }
// Nodes is a list of seed nodes used along
// with resolved node
func Nodes(n ...string) Option {
return func(o *Options) {
o.Nodes = n
}
}
// Tunnel sets the network tunnel // Tunnel sets the network tunnel
func Tunnel(t tunnel.Tunnel) Option { func Tunnel(t tunnel.Tunnel) Option {
return func(o *Options) { return func(o *Options) {