From 60b9952458f7d1037563ba4430315eb08587c6ed Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Jun 2019 17:12:11 +0200 Subject: [PATCH 1/3] add a callback for autonat status changes --- autonat.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/autonat.go b/autonat.go index 7e839dd..9027200 100644 --- a/autonat.go +++ b/autonat.go @@ -61,11 +61,13 @@ type AmbientAutoNAT struct { // If only a single autoNAT peer is known, then the confidence increases // for each failure until it reaches 3. confidence int + + callback func(NATStatus, int) } // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host // If getAddrs is nil, h.Addrs will be used -func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT { +func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs, cb func(NATStatus, int)) AutoNAT { if getAddrs == nil { getAddrs = h.Addrs } @@ -76,6 +78,7 @@ func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT { getAddrs: getAddrs, peers: make(map[peer.ID][]ma.Multiaddr), status: NATStatusUnknown, + callback: cb, } h.Network().Notify(as) @@ -214,6 +217,9 @@ func (as *AmbientAutoNAT) autodetect() { as.status = NATStatusUnknown as.addr = nil } + if as.callback != nil { + as.callback(as.status, as.confidence) + } as.mx.Unlock() } From e8c27dde89bb76f6b698892981fc40087ff085c9 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Jun 2019 17:30:29 +0200 Subject: [PATCH 2/3] make the callback passable in a separate constructor --- autonat.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/autonat.go b/autonat.go index 9027200..32a63f0 100644 --- a/autonat.go +++ b/autonat.go @@ -67,7 +67,12 @@ type AmbientAutoNAT struct { // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host // If getAddrs is nil, h.Addrs will be used -func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs, cb func(NATStatus, int)) AutoNAT { +func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT { + return NewAutoNATCB(ctx, h, getAddrs, nil) +} + +// NewAutoNATCB creates a new autonat with a callback for state changes +func NewAutoNATCB(ctx context.Context, h host.Host, getAddrs GetAddrs, cb func(NATStatus, int)) AutoNAT { if getAddrs == nil { getAddrs = h.Addrs } From 623b4f328ea493e8f64852aefd1d27d964af3e8d Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 18 Jun 2019 18:08:37 +0200 Subject: [PATCH 3/3] allow for multiple callbacks --- autonat.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/autonat.go b/autonat.go index 32a63f0..96a5781 100644 --- a/autonat.go +++ b/autonat.go @@ -62,17 +62,13 @@ type AmbientAutoNAT struct { // for each failure until it reaches 3. confidence int - callback func(NATStatus, int) + callbacks []func(NATStatus, int) + cbLk sync.Mutex } // NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host // If getAddrs is nil, h.Addrs will be used func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT { - return NewAutoNATCB(ctx, h, getAddrs, nil) -} - -// NewAutoNATCB creates a new autonat with a callback for state changes -func NewAutoNATCB(ctx context.Context, h host.Host, getAddrs GetAddrs, cb func(NATStatus, int)) AutoNAT { if getAddrs == nil { getAddrs = h.Addrs } @@ -83,7 +79,6 @@ func NewAutoNATCB(ctx context.Context, h host.Host, getAddrs GetAddrs, cb func(N getAddrs: getAddrs, peers: make(map[peer.ID][]ma.Multiaddr), status: NATStatusUnknown, - callback: cb, } h.Network().Notify(as) @@ -222,12 +217,24 @@ func (as *AmbientAutoNAT) autodetect() { as.status = NATStatusUnknown as.addr = nil } - if as.callback != nil { - as.callback(as.status, as.confidence) - } + as.runCallbacks() as.mx.Unlock() } +func (as *AmbientAutoNAT) runCallbacks() { + as.cbLk.Lock() + defer as.cbLk.Unlock() + for _, cb := range as.callbacks { + cb(as.status, as.confidence) + } +} + +func (as *AmbientAutoNAT) AddCallback(cb func(NATStatus, int)) { + as.cbLk.Lock() + defer as.cbLk.Unlock() + as.callbacks = append(as.callbacks, cb) +} + func (as *AmbientAutoNAT) getPeers() []peer.AddrInfo { as.mx.Lock() defer as.mx.Unlock()