probe/service/DiscoveryService.go

180 lines
4.5 KiB
Go
Raw Normal View History

2018-08-23 09:21:48 +00:00
package service
import (
2018-09-13 17:14:57 +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-09-10 14:53:55 +00:00
orp "git.loafle.net/overflow/rpc-go/protocol"
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-13 13:05:05 +00:00
request, messageType, datas := msg()
2018-09-04 10:09:15 +00:00
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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-14 07:44:09 +00:00
case types.DiscoveryMessageTypeMode:
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
"DiscoveryService.DiscoveryMode",
datas,
),
"/scanner",
)
// log.Print("Queueing ", data)
2018-09-05 15:12:17 +00:00
case types.DiscoveryMessageTypeQueueingFailed:
2018-09-04 10:09:15 +00:00
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
2018-09-05 15:12:17 +00:00
"DiscoveryService.QueueingFailed",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// log.Print("QueueingFailed ", data)
2018-09-05 15:12:17 +00:00
case types.DiscoveryMessageTypeQueueingTimeout:
2018-09-04 10:09:15 +00:00
s.PubSub.Pub(
rpc.MakeRPCMessage(
[]string{request.RequesterID()},
2018-09-05 15:12:17 +00:00
"DiscoveryService.QueueingTimeout",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// log.Print("QueueingTimeout ", 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()},
2018-09-10 14:53:55 +00:00
"DiscoveryService.DiscoveryError",
2018-09-13 13:05:05 +00:00
[]interface{}{&orp.Error{Code: orp.E_INTERNAL, Message: datas[0].(error).Error()}},
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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",
2018-09-13 13:05:05 +00:00
datas,
2018-09-04 10:09:15 +00:00
),
"/scanner",
)
2018-09-11 12:04:53 +00:00
// 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-09-13 17:14:57 +00:00
func (s *DiscoveryService) DiscoverStop(requesterID string, requestID string) error {
log.Print("requestID ", requestID)
2018-09-14 10:08:14 +00:00
s.Discoverer.DiscoverStop(requesterID, requestID)
2018-08-23 10:00:34 +00:00
return nil
2018-08-23 09:21:48 +00:00
}