overflow_discovery/discovery/discovery.go

126 lines
2.3 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) {
discoverer.discoverHost(dh)
}
func DiscoverPort(dp *model.DiscoveryPort) {
discoverer.discoverPort(dp)
}
func DiscoverService(ds *model.DiscoveryService) {
discoverer.discoverService(ds)
}
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"))
d.stopWg.Add(1)
go handle(d)
}
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"))
}
func handle(d *discovery) {
defer d.stopWg.Done()
for {
select {
case <-d.stopChan:
return
case data := <-d.sendChan:
logging.Logger().Info(fmt.Sprintf("chan: %v", data))
}
}
}
func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is start"))
doneChan := make(chan struct{})
d.stopWg.Add(1)
defer d.stopWg.Done()
go scanZone(d, dz, doneChan)
select {
case <-d.stopChan:
<-doneChan
case <-doneChan:
logging.Logger().Debug(fmt.Sprintf("Discovery: DiscoverZone is complete"))
}
}
func (d *discovery) discoverHost(dh *model.DiscoveryHost) {
logging.Logger().Debug(fmt.Sprintf("DiscoverHost"))
}
func (d *discovery) discoverPort(dp *model.DiscoveryPort) {
logging.Logger().Debug(fmt.Sprintf("DiscoverPort"))
}
func (d *discovery) discoverService(ds *model.DiscoveryService) {
logging.Logger().Debug(fmt.Sprintf("DiscoverService"))
}
func (d *discovery) sendResult() {
}
func (d *discovery) sendError() {
}