package discovery import ( "sync" "git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/util/net/cidr" discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model" ) var RPCServlet rpc.Servlet var discoverer *discovery func DiscoveryInit() { discoverer = &discovery{} discoverer.start() } func DiscoveryDestroy() { discoverer.stop() discoverer = nil } func DiscoverZone(requesterID string, dz *discoveryM.DiscoveryZone) { discoverer.discoverZone(requesterID, dz) } func DiscoverHost(requesterID string, zone *discoveryM.Zone, dh *discoveryM.DiscoveryHost) { discoverer.discoverHost(requesterID, zone, dh) } func DiscoverPort(requesterID string, host *discoveryM.Host, dp *discoveryM.DiscoveryPort) { discoverer.discoverPort(requesterID, host, dp) } func DiscoverService(requesterID string, port *discoveryM.Port, ds *discoveryM.DiscoveryService) { discoverer.discoverService(requesterID, port, 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 { logging.Logger().Panicf("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().Infof("Discovery: discovery is started") } func (d *discovery) stop() { if d.stopChan == nil { logging.Logger().Panicf("Discovery: discovery must be started before stopping it") } close(d.stopChan) d.stopWg.Wait() d.stopChan = nil logging.Logger().Infof("Discovery: discovery is stopped") } func (d *discovery) discoverZone(requesterID string, dz *discoveryM.DiscoveryZone) { go taskScan(d, func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) { scanZone(dz, resultChan, errChan, doneChan, stopChan) }, func(result interface{}) { z := result.(*discoveryM.Zone) logging.Logger().Debugf("zone: %v", z) d.sendResult("DiscoveryService.DiscoveredZone", requesterID, z) if nil != dz.DiscoveryHost { cr, _ := cidr.NewCIDRRanger(z.Network) dh := &discoveryM.DiscoveryHost{ FirstScanRange: cr.First().String(), LastScanRange: cr.Last().String(), DiscoveryPort: dz.DiscoveryHost.DiscoveryPort, } d.discoverHost(requesterID, z, dh) } }, ) } func (d *discovery) discoverHost(requesterID string, zone *discoveryM.Zone, dh *discoveryM.DiscoveryHost) { go taskScan(d, func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) { scanHost(zone, dh, resultChan, errChan, doneChan, stopChan) }, func(result interface{}) { h := result.(*discoveryM.Host) logging.Logger().Debugf("host: %v", h) d.sendResult("DiscoveryService.DiscoveredHost", requesterID, h) if nil != dh.DiscoveryPort { d.discoverPort(requesterID, h, dh.DiscoveryPort) } }, ) } func (d *discovery) discoverPort(requesterID string, host *discoveryM.Host, dp *discoveryM.DiscoveryPort) { go taskScan(d, func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) { scanPort(host, dp, resultChan, errChan, doneChan, stopChan) }, func(result interface{}) { p := result.(*discoveryM.Port) logging.Logger().Debugf("port: %v", p) d.sendResult("DiscoveryService.DiscoveredPort", requesterID, p) if nil != dp.DiscoveryService { d.discoverService(requesterID, p, dp.DiscoveryService) } }, ) } func (d *discovery) discoverService(requesterID string, port *discoveryM.Port, ds *discoveryM.DiscoveryService) { go taskScan(d, func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}) { scanService(port, ds, resultChan, errChan, doneChan, stopChan) }, func(result interface{}) { s := result.(*discoveryM.Service) d.sendResult("DiscoveryService.DiscoveredService", requesterID, s) logging.Logger().Debugf("service: %s(%s)[%s:%s]", s.ServiceName, s.CryptoType, port.Host.IP, port.PortNumber) }, ) } func (d *discovery) sendResult(method string, args ...interface{}) { go RPCServlet.Send(method, args...) // go notify.Notifier.Notify(method, args...) } func (d *discovery) sendError() { } func taskScan(d *discovery, task func(resultChan chan interface{}, errChan chan error, doneChan chan struct{}, stopChan chan struct{}), onResult func(result interface{})) { d.stopWg.Add(1) resultChan := make(chan interface{}) errChan := make(chan error) stopChan := make(chan struct{}) doneChan := make(chan struct{}) defer func() { close(resultChan) close(errChan) close(stopChan) close(doneChan) d.stopWg.Done() }() go task(resultChan, errChan, doneChan, stopChan) for { select { case r := <-resultChan: onResult(r) case err := <-errChan: logging.Logger().Infof("task err: %v", err) case <-doneChan: logging.Logger().Debugf("Discovery: task is complete") return case <-d.stopChan: close(stopChan) <-doneChan return } } }