overflow_discovery/discovery/discovery.go

167 lines
3.7 KiB
Go
Raw Normal View History

2017-11-15 12:09:38 +00:00
package discovery
import (
"fmt"
"sync"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
)
var discoverer *discovery
func DiscoveryInit() {
discoverer = &discovery{}
discoverer.start()
}
func DiscoveryDestroy() {
discoverer.stop()
discoverer = nil
}
func DiscoverZone(dz *model.DiscoveryZone) {
discoverer.discoverZone(dz)
}
func DiscoverHost(dh *model.DiscoveryHost) {
2017-11-15 12:33:46 +00:00
discoverer.discoverHost(dh.Zone, dh)
2017-11-15 12:09:38 +00:00
}
func DiscoverPort(dp *model.DiscoveryPort) {
2017-11-15 12:33:46 +00:00
discoverer.discoverPort(dp.Host, dp)
2017-11-15 12:09:38 +00:00
}
func DiscoverService(ds *model.DiscoveryService) {
2017-11-15 12:33:46 +00:00
discoverer.discoverService(ds.Port, ds)
2017-11-15 12:09:38 +00:00
}
func Stop() {
}
type discovery struct {
sendChan chan interface{}
errChan chan error
stopChan chan struct{}
stopWg sync.WaitGroup
}
func (d *discovery) start() {
if d.stopChan != nil {
panic("Discovery: discovery is already running. Stop it before starting it again")
}
d.stopChan = make(chan struct{})
d.sendChan = make(chan interface{})
d.errChan = make(chan error)
logging.Logger().Info(fmt.Sprintf("Discovery: discovery is started"))
}
func (d *discovery) stop() {
if d.stopChan == nil {
panic("Discovery: discovery must be started before stopping it")
}
close(d.stopChan)
d.stopWg.Wait()
d.stopChan = nil
logging.Logger().Info(fmt.Sprintf("Discovery: discovery is stopped"))
}
2017-11-15 12:33:46 +00:00
func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
2017-11-16 11:01:42 +00:00
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) {
scanZone(dz, resultChan, errChan, doneChan)
},
func(result interface{}) {
z := result.(*model.Zone)
2017-11-15 12:33:46 +00:00
logging.Logger().Info(fmt.Sprintf("zone: %v", z))
if nil != dz.DiscoveryHost {
d.discoverHost(z, dz.DiscoveryHost)
}
2017-11-16 11:01:42 +00:00
},
)
2017-11-15 12:09:38 +00:00
}
2017-11-15 12:33:46 +00:00
func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) {
2017-11-16 11:01:42 +00:00
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) {
scanHost(zone, dh, resultChan, errChan, doneChan)
},
func(result interface{}) {
h := result.(*model.Host)
2017-11-15 12:33:46 +00:00
logging.Logger().Info(fmt.Sprintf("host: %v", h))
if nil != dh.DiscoveryPort {
d.discoverPort(h, dh.DiscoveryPort)
}
2017-11-16 11:01:42 +00:00
},
)
2017-11-15 12:09:38 +00:00
}
2017-11-15 12:33:46 +00:00
func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) {
2017-11-16 11:01:42 +00:00
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) {
scanPort(host, dp, resultChan, errChan, doneChan)
},
func(result interface{}) {
p := result.(*model.Port)
2017-11-15 12:33:46 +00:00
logging.Logger().Info(fmt.Sprintf("port: %v", p))
if nil != dp.DiscoveryService {
d.discoverService(p, dp.DiscoveryService)
}
2017-11-16 11:01:42 +00:00
},
)
2017-11-15 12:09:38 +00:00
}
2017-11-15 12:33:46 +00:00
func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService) {
2017-11-16 11:01:42 +00:00
taskScan(d,
func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}) {
scanService(port, ds, resultChan, errChan, doneChan)
},
func(result interface{}) {
s := result.(*model.Service)
logging.Logger().Info(fmt.Sprintf("service: %v", s))
},
)
}
func (d *discovery) sendResult() {
2017-11-15 12:33:46 +00:00
2017-11-16 11:01:42 +00:00
}
func (d *discovery) sendError() {
}
func taskScan(d *discovery, task func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}), onResult func(result interface{})) {
2017-11-15 12:33:46 +00:00
d.stopWg.Add(1)
2017-11-16 11:01:42 +00:00
resultChan := make(chan interface{})
2017-11-15 12:33:46 +00:00
errChan := make(chan error)
doneChan := make(chan struct{})
defer func() {
close(resultChan)
close(errChan)
close(doneChan)
d.stopWg.Done()
}()
2017-11-16 11:01:42 +00:00
go task(resultChan, errChan, doneChan)
2017-11-15 12:33:46 +00:00
for {
select {
2017-11-16 11:01:42 +00:00
case r := <-resultChan:
onResult(r)
2017-11-15 12:33:46 +00:00
case err := <-errChan:
2017-11-16 11:01:42 +00:00
logging.Logger().Info(fmt.Sprintf("task err: %v", err))
2017-11-15 12:33:46 +00:00
case <-doneChan:
2017-11-16 11:01:42 +00:00
logging.Logger().Debug(fmt.Sprintf("Discovery: task is complete"))
return
case <-d.stopChan:
2017-11-15 12:33:46 +00:00
return
}
}
2017-11-15 12:09:38 +00:00
}