Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

add a callback for autonat status changes #24

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type AmbientAutoNAT struct {
// If only a single autoNAT peer is known, then the confidence increases
// for each failure until it reaches 3.
confidence int

callbacks []func(NATStatus, int)
cbLk sync.Mutex
}

// NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host
Expand Down Expand Up @@ -214,9 +217,24 @@ func (as *AmbientAutoNAT) autodetect() {
as.status = NATStatusUnknown
as.addr = nil
}
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()
Expand Down