package service import ( "log" "reflect" "git.loafle.net/overflow_scanner/probe/internal/pubsub" "git.loafle.net/overflow_scanner/probe/internal/rpc" oa "git.loafle.net/overflow/annotation-go" od "git.loafle.net/overflow/di-go" omd "git.loafle.net/overflow/model/discovery" "git.loafle.net/overflow_scanner/probe/discovery" "git.loafle.net/overflow_scanner/probe/discovery/types" ) func init() { od.RegisterType(DiscoveryServiceType) } var DiscoveryServiceType = reflect.TypeOf((*DiscoveryService)(nil)) type DiscoveryService struct { oa.TypeAnnotation `annotation:"@Injectable('name': 'DiscoveryService') @Service()"` Discoverer discovery.Discoverer `annotation:"@Inject('name': 'Discoverer')"` PubSub *pubsub.PubSub `annotation:"@Inject('name': 'PubSub')"` _InitService oa.MethodAnnotation `annotation:"@PostConstruct()"` _DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"` } func (s *DiscoveryService) InitService() { go func() { for { select { case msg, ok := <-s.Discoverer.Message(): if !ok { return } request, messageType, data, err := msg() switch messageType { case types.DiscoveryMessageTypeStart: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.DiscoveryStart", []interface{}{data}, ), "/scanner", ) log.Print("Start ", data) case types.DiscoveryMessageTypeStop: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.DiscoveryStop", []interface{}{data}, ), "/scanner", ) log.Print("Stop ", data) case types.DiscoveryMessageTypeQueueing: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.Queueing", []interface{}{data}, ), "/scanner", ) log.Print("Queueing ", data) case types.DiscoveryMessageTypeFailedQueueing: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.FailedQueueing", []interface{}{data}, ), "/scanner", ) log.Print("FailedQueueing ", data) case types.DiscoveryMessageTypeTimeout: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.Timeout", []interface{}{data}, ), "/scanner", ) log.Print("Timeout ", data) case types.DiscoveryMessageTypeError: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.Error", []interface{}{err}, ), "/scanner", ) log.Print("Error ", err) case types.DiscoveryMessageTypeHost: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.DiscoveredHost", []interface{}{data}, ), "/scanner", ) log.Print("Host ", data) case types.DiscoveryMessageTypePort: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.DiscoveredPort", []interface{}{data}, ), "/scanner", ) log.Print("Port ", data) case types.DiscoveryMessageTypeService: s.PubSub.Pub( rpc.MakeRPCMessage( []string{request.RequesterID()}, "DiscoveryService.DiscoveredService", []interface{}{data}, ), "/scanner", ) log.Print("Service ", data) default: } } } }() } func (s *DiscoveryService) DestroyService() { } func (s *DiscoveryService) DiscoverHost(requesterID string, zone *omd.Zone, dh *omd.DiscoverHost) error { s.Discoverer.DiscoverHost(requesterID, zone, dh) return nil } func (s *DiscoveryService) DiscoverPort(requesterID string, host *omd.Host, dp *omd.DiscoverPort) error { s.Discoverer.DiscoverPort(requesterID, host, dp) return nil } func (s *DiscoveryService) DiscoverService(requesterID string, port *omd.Port, ds *omd.DiscoverService) error { s.Discoverer.DiscoverService(requesterID, port, ds) return nil } func (s *DiscoveryService) StopDiscover() error { return nil }