probe/service/DiscoveryService.go
crusader 502ff3ef89 ing
2018-09-10 23:53:55 +09:00

167 lines
4.2 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, 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.DiscoveryMessageTypeQueueingFailed:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.QueueingFailed",
[]interface{}{data},
),
"/scanner",
)
log.Print("QueueingFailed ", data)
case types.DiscoveryMessageTypeQueueingTimeout:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.QueueingTimeout",
[]interface{}{data},
),
"/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: err.Error()}},
),
"/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
}