2018-08-23 09:21:48 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-08-28 15:32:03 +00:00
|
|
|
"log"
|
2018-08-23 09:21:48 +00:00
|
|
|
"reflect"
|
|
|
|
|
2018-09-04 10:09:15 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/internal/pubsub"
|
|
|
|
"git.loafle.net/overflow_scanner/probe/internal/rpc"
|
|
|
|
|
2018-08-23 09:21:48 +00:00
|
|
|
oa "git.loafle.net/overflow/annotation-go"
|
|
|
|
od "git.loafle.net/overflow/di-go"
|
2018-08-28 15:32:03 +00:00
|
|
|
omd "git.loafle.net/overflow/model/discovery"
|
2018-08-29 12:04:23 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery"
|
2018-09-03 16:11:58 +00:00
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/types"
|
2018-08-23 09:21:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
od.RegisterType(DiscoveryServiceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
var DiscoveryServiceType = reflect.TypeOf((*DiscoveryService)(nil))
|
|
|
|
|
|
|
|
type DiscoveryService struct {
|
|
|
|
oa.TypeAnnotation `annotation:"@Injectable('name': 'DiscoveryService') @Service()"`
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-09-04 07:07:45 +00:00
|
|
|
Discoverer discovery.Discoverer `annotation:"@Inject('name': 'Discoverer')"`
|
2018-09-04 10:09:15 +00:00
|
|
|
PubSub *pubsub.PubSub `annotation:"@Inject('name': 'PubSub')"`
|
2018-08-29 12:04:23 +00:00
|
|
|
|
2018-09-04 07:07:45 +00:00
|
|
|
_InitService oa.MethodAnnotation `annotation:"@PostConstruct()"`
|
|
|
|
_DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"`
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 07:07:45 +00:00
|
|
|
func (s *DiscoveryService) InitService() {
|
2018-09-03 16:11:58 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-s.Discoverer.Message():
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:09:15 +00:00
|
|
|
request, messageType, data, err := msg()
|
|
|
|
|
|
|
|
switch messageType {
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeStart:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.DiscoveryStart",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Start ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeStop:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.DiscoveryStop",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Stop ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeQueueing:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.Queueing",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Queueing ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeFailedQueueing:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.FailedQueueing",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("FailedQueueing ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeTimeout:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.Timeout",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Timeout ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeError:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.Error",
|
|
|
|
[]interface{}{err},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Error ", err)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeHost:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.DiscoveredHost",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Host ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypePort:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.DiscoveredPort",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Port ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
case types.DiscoveryMessageTypeService:
|
2018-09-04 10:09:15 +00:00
|
|
|
s.PubSub.Pub(
|
|
|
|
rpc.MakeRPCMessage(
|
|
|
|
[]string{request.RequesterID()},
|
|
|
|
"DiscoveryService.DiscoveredService",
|
|
|
|
[]interface{}{data},
|
|
|
|
),
|
|
|
|
"/scanner",
|
|
|
|
)
|
|
|
|
log.Print("Service ", data)
|
2018-09-03 16:11:58 +00:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-09-04 07:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DiscoveryService) DestroyService() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DiscoveryService) DiscoverHost(requesterID string, zone *omd.Zone, dh *omd.DiscoverHost) error {
|
2018-09-03 16:11:58 +00:00
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
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
|
2018-08-23 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
func (s *DiscoveryService) DiscoverService(requesterID string, port *omd.Port, ds *omd.DiscoverService) error {
|
|
|
|
s.Discoverer.DiscoverService(requesterID, port, ds)
|
2018-08-23 10:00:34 +00:00
|
|
|
return nil
|
2018-08-23 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 10:00:34 +00:00
|
|
|
func (s *DiscoveryService) StopDiscover() error {
|
|
|
|
return nil
|
2018-08-23 09:21:48 +00:00
|
|
|
}
|