probe/pcap/pcap.go

61 lines
915 B
Go
Raw Normal View History

2018-08-12 10:24:23 +00:00
package pcap
import (
"sync"
"time"
2018-08-13 07:19:59 +00:00
omd "git.loafle.net/overflow/model/discovery"
2018-08-12 10:24:23 +00:00
)
var mtx sync.Mutex
var instances map[string]PCapScanner
func init() {
instances = make(map[string]PCapScanner, 0)
}
2018-08-13 07:19:59 +00:00
func RetainScanner(zone *omd.Zone) (PCapScanner, error) {
2018-08-12 10:24:23 +00:00
mtx.Lock()
defer mtx.Unlock()
var ps PCapScanner
var ok bool
if ps, ok = instances[zone.Network]; !ok {
ps = newPCapScanner(zone)
if err := ps.start(); nil != err {
return nil, err
}
instances[zone.Network] = ps
}
ps.retain()
return ps, nil
}
2018-08-13 07:19:59 +00:00
func ReleaseScanner(zone *omd.Zone) {
2018-08-12 10:24:23 +00:00
go func() {
time.Sleep(2 * time.Second)
mtx.Lock()
defer mtx.Unlock()
if ps, ok := instances[zone.Network]; ok {
if ps.release() {
ps.stop()
delete(instances, zone.Network)
}
}
}()
}
func ReleaseScannerAll() {
mtx.Lock()
defer mtx.Unlock()
for k, ps := range instances {
ps.stop()
delete(instances, k)
}
}