Skip to content

Commit

Permalink
core: address factory composition for constructPeerHost
Browse files Browse the repository at this point in the history
- Adds AddrsFactory to ConstructPeerHostOpts
- Composes the AddrsFactory option with the relay filter

License: MIT
Signed-off-by: vyzo <[email protected]>
  • Loading branch information
vyzo committed Jul 30, 2017
1 parent a11f632 commit 231f8e4
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ func listenAddresses(cfg *config.Config) ([]ma.Multiaddr, error) {
}

type ConstructPeerHostOpts struct {
AddrsFactory p2pbhost.AddrsFactory
DisableNatPortMap bool
DisableRelay bool
EnableRelayHop bool
Expand Down Expand Up @@ -739,6 +740,7 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
hostOpts = append(hostOpts, p2pbhost.NATPortMap)
}

addrsFactory := opts.AddrsFactory
if !opts.DisableRelay {
filterRelayAddr := func(addrs []ma.Multiaddr) []ma.Multiaddr {
var raddrs []ma.Multiaddr
Expand All @@ -751,7 +753,16 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
}
return raddrs
}
hostOpts = append(hostOpts, p2pbhost.AddrsFactory(filterRelayAddr))

if addrsFactory != nil {
addrsFactory = composeAddrsFactory(addrsFactory, filterRelayAddr)
} else {
addrsFactory = filterRelayAddr
}
}

if addrsFactory != nil {
hostOpts = append(hostOpts, addrsFactory)
}

host := p2pbhost.New(network, hostOpts...)
Expand All @@ -771,6 +782,12 @@ func constructPeerHost(ctx context.Context, id peer.ID, ps pstore.Peerstore, bwr
return host, nil
}

func composeAddrsFactory(f, g p2pbhost.AddrsFactory) p2pbhost.AddrsFactory {
return func(addrs []ma.Multiaddr) []ma.Multiaddr {
return f(g(addrs))
}
}

// startListening on the network addresses
func startListening(ctx context.Context, host p2phost.Host, cfg *config.Config) error {
listenAddrs, err := listenAddresses(cfg)
Expand Down

0 comments on commit 231f8e4

Please sign in to comment.