probe/service/DiscoveryService.go

180 lines
4.5 KiB
Go

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"
orp "git.loafle.net/overflow/rpc-go/protocol"
"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, datas := msg()
switch messageType {
case types.DiscoveryMessageTypeStart:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveryStart",
datas,
),
"/scanner",
)
// log.Print("Start ", data)
case types.DiscoveryMessageTypeStop:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveryStop",
datas,
),
"/scanner",
)
// log.Print("Stop ", data)
case types.DiscoveryMessageTypeQueueing:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.Queueing",
datas,
),
"/scanner",
)
case types.DiscoveryMessageTypeMode:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveryMode",
datas,
),
"/scanner",
)
// log.Print("Queueing ", data)
case types.DiscoveryMessageTypeQueueingFailed:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.QueueingFailed",
datas,
),
"/scanner",
)
// log.Print("QueueingFailed ", data)
case types.DiscoveryMessageTypeQueueingTimeout:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.QueueingTimeout",
datas,
),
"/scanner",
)
// log.Print("QueueingTimeout ", data)
case types.DiscoveryMessageTypeError:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveryError",
[]interface{}{&orp.Error{Code: orp.E_INTERNAL, Message: datas[0].(error).Error()}},
),
"/scanner",
)
// log.Print("Error ", err)
case types.DiscoveryMessageTypeHost:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveredHost",
datas,
),
"/scanner",
)
// log.Print("Host ", data)
case types.DiscoveryMessageTypePort:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveredPort",
datas,
),
"/scanner",
)
// log.Print("Port ", data)
case types.DiscoveryMessageTypeService:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveredService",
datas,
),
"/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) DiscoverStop(requesterID string, requestID string) error {
log.Print("requestID ", requestID)
s.Discoverer.DiscoverStop(requesterID, requestID)
return nil
}