This commit is contained in:
crusader 2018-09-04 16:07:45 +09:00
parent 126472bc2d
commit fdb2635edc
6 changed files with 56 additions and 34 deletions

View File

@ -5,11 +5,12 @@ import (
"net"
"reflect"
"go.uber.org/zap"
oa "git.loafle.net/overflow/annotation-go"
oda "git.loafle.net/overflow/di-go/annotation"
olog "git.loafle.net/overflow/log-go"
"git.loafle.net/overflow/server-go"
ossw "git.loafle.net/overflow/server-go/socket/web"
"go.uber.org/zap"
)
type ServerHandler interface {
@ -40,6 +41,15 @@ func (sh *ServerHandlers) OnStart(serverCtx server.ServerCtx) error {
}
func (sh *ServerHandlers) OnStop(serverCtx server.ServerCtx) {
for _, _service := range sh.Services {
as, err := oa.GetMethodAnnotationsByType(reflect.TypeOf(_service), oda.PreDestroyAnnotationType)
if nil == err || nil != as {
for k := range as {
ins := make([]reflect.Value, 0)
reflect.ValueOf(_service).MethodByName(k).Call(ins)
}
}
}
sh.ServerHandlers.OnStop(serverCtx)
}

View File

@ -17,7 +17,7 @@ import (
func New() *ossw.Server {
rpcServerCodec := orpj.NewServerCodec()
od.RegisterResource("Discoverer", discovery.Instance())
od.RegisterSingletonByName("Discoverer", discovery.Instance())
services, err := od.GetInstancesByAnnotationType(annotation.ServiceAnnotationType)
if nil != err {

View File

@ -20,39 +20,13 @@ var DiscoveryServiceType = reflect.TypeOf((*DiscoveryService)(nil))
type DiscoveryService struct {
oa.TypeAnnotation `annotation:"@Injectable('name': 'DiscoveryService') @Service()"`
Discoverer discovery.Discoverer `annotation:"@Resource('name': 'Discoverer')"`
Discoverer discovery.Discoverer `annotation:"@Inject('name': 'Discoverer')"`
_InitService oa.MethodAnnotation `annotation:"@PostConstruct()"`
_DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"`
}
func (s *DiscoveryService) InitService() error {
return nil
}
func (s *DiscoveryService) StartService() error {
go func() {
for {
select {
case msg, ok := <-s.Discoverer.Message():
if !ok {
return
}
log.Print(msg)
}
}
}()
return nil
}
func (s *DiscoveryService) StopService() {
s.Discoverer.Shutdown()
}
func (s *DiscoveryService) DestroyService() {
}
func (s *DiscoveryService) DiscoverHost(requesterID string, zone *omd.Zone, dh *omd.DiscoverHost) error {
func (s *DiscoveryService) InitService() {
go func() {
for {
select {
@ -87,6 +61,13 @@ func (s *DiscoveryService) DiscoverHost(requesterID string, zone *omd.Zone, dh *
}
}
}()
}
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

View File

@ -17,6 +17,17 @@ var MachineServiceType = reflect.TypeOf((*MachineService)(nil))
type MachineService struct {
oa.TypeAnnotation `annotation:"@Injectable('name': 'MachineService') @Service()"`
_InitService oa.MethodAnnotation `annotation:"@PostConstruct()"`
_DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"`
}
func (s *MachineService) InitService() {
}
func (s *MachineService) DestroyService() {
}
func (s *MachineService) Interfaces() ([]*omn.Interface, error) {

View File

@ -15,8 +15,18 @@ var PingServiceType = reflect.TypeOf((*PingService)(nil))
type PingService struct {
oa.TypeAnnotation `annotation:"@Injectable('name': 'PingService') @Service()"`
_InitService oa.MethodAnnotation `annotation:"@PostConstruct()"`
_DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"`
}
func (s *PingService) InitService() {
}
func (s *PingService) DestroyService() {
}
func (s *PingService) PingHost() error {
return nil
}

View File

@ -15,8 +15,18 @@ var SNMPServiceType = reflect.TypeOf((*SNMPService)(nil))
type SNMPService struct {
oa.TypeAnnotation `annotation:"@Injectable('name': 'SNMPService') @Service()"`
_InitService oa.MethodAnnotation `annotation:"@PostConstruct()"`
_DestroyService oa.MethodAnnotation `annotation:"@PreDestroy()"`
}
func (s *SNMPService) InitService() {
}
func (s *SNMPService) DestroyService() {
}
func (s *SNMPService) ScanSNMP() error {
return nil
}