diff --git a/auth/authenticator.go b/auth/authenticator.go index 7972366..98b2111 100644 --- a/auth/authenticator.go +++ b/auth/authenticator.go @@ -9,14 +9,12 @@ import ( "git.loafle.net/commons/configuration-go" cdr "git.loafle.net/commons/di-go/registry" logging "git.loafle.net/commons/logging-go" - crc "git.loafle.net/commons/rpc-go/client" occa "git.loafle.net/overflow/commons-go/core/annotation" + occi "git.loafle.net/overflow/commons-go/core/interfaces" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" - "git.loafle.net/overflow/probe/client/central" "git.loafle.net/overflow/probe/config" - // For service - _ "git.loafle.net/overflow/probe/auth/service" + "git.loafle.net/overflow/probe/auth/service" ) type Authenticator struct { @@ -25,6 +23,8 @@ type Authenticator struct { authConfig ocnc.Auth + services []interface{} + stopChan chan struct{} stopWg sync.WaitGroup } @@ -56,9 +56,9 @@ func (a *Authenticator) EndableStart() (<-chan error, error) { if nil != err { return nil, err } + a.services = services - client, err := central.NewAuth(a.HandleTempKey, services) - if nil != err { + if err := occi.ExecServices(a.services, occi.ServiceMethodInit, service.OrderedServices, false); nil != err { return nil, err } @@ -66,7 +66,7 @@ func (a *Authenticator) EndableStart() (<-chan error, error) { a.stopChan = make(chan struct{}) a.stopWg.Add(1) - go a.handleAuthenticator(client, authDoneChan, endChan) + go a.handleAuthenticator(authDoneChan, endChan) return endChan, nil } @@ -78,6 +78,10 @@ func (a *Authenticator) Stop(ctx context.Context) error { close(a.stopChan) a.stopWg.Wait() + if err := occi.ExecServices(a.services, occi.ServiceMethodDestroy, service.OrderedServices, true); nil != err { + return err + } + a.stopChan = nil return nil @@ -87,37 +91,27 @@ func (a *Authenticator) logHeader() string { return "Authenticator:" } -func (a *Authenticator) HandleTempKey(tempKey string) { - logging.Logger().Infof("%s registered by central", a.logHeader()) - a.authConfig.TempKey = &tempKey - if err := configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true); nil != err { - logging.Logger().Errorf("%s %v", a.logHeader(), err) - return - } -} - -func (a *Authenticator) handleAuthenticator(client *crc.Client, authDoneChan chan error, endChan chan<- error) { +func (a *Authenticator) handleAuthenticator(authDoneChan chan error, endChan chan<- error) { var err error defer func() { - if nil != client { - err = client.Stop(context.Background()) - } - a.stopWg.Done() endChan <- err }() - if err = client.Start(); nil != err { - logging.Logger().Error(err) + err = occi.ExecServices(a.services, occi.ServiceMethodStart, service.OrderedServices, false) + if nil != err { return } +LOOP: for { select { case err = <-authDoneChan: - return + break LOOP case <-a.stopChan: - return + break LOOP } } + + err = occi.ExecServices(a.services, occi.ServiceMethodStop, service.OrderedServices, true) } diff --git a/auth/service/NoAuthProbeService.go b/auth/service/NoAuthProbeService.go index cd54934..538ab10 100644 --- a/auth/service/NoAuthProbeService.go +++ b/auth/service/NoAuthProbeService.go @@ -10,8 +10,10 @@ import ( cda "git.loafle.net/commons/di-go/annotation" cdr "git.loafle.net/commons/di-go/registry" logging "git.loafle.net/commons/logging-go" + crc "git.loafle.net/commons/rpc-go/client" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" ocpc "git.loafle.net/overflow/commons-go/probe/config" + "git.loafle.net/overflow/probe/client/central" "git.loafle.net/overflow/probe/config" // For annotation @@ -31,16 +33,36 @@ type NoAuthProbeService struct { Config *config.Config `annotation:"@Resource(name='Config')"` AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"` AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"` + + client *crc.Client +} + +func (s *NoAuthProbeService) Init() error { + client, err := central.NewAuth(s.HandleTempKey, s) + if nil != err { + return err + } + s.client = client + + return nil } func (s *NoAuthProbeService) Start() error { + if err := s.client.Start(); nil != err { + return err + } return nil } -func (s *NoAuthProbeService) Stop(ctx context.Context) error { +func (s *NoAuthProbeService) Stop() { + if err := s.client.Stop(context.Background()); nil != err { + logging.Logger().Error(err) + } +} - return nil +func (s *NoAuthProbeService) Destroy() { + s.client = nil } func (s *NoAuthProbeService) Accept(probeKey string) error { @@ -78,3 +100,13 @@ func (s *NoAuthProbeService) Deny() error { s.AuthDoneChan <- nil return nil } + +func (s *NoAuthProbeService) HandleTempKey(tempKey string) { + logging.Logger().Infof("registered by central") + s.AuthConfig.TempKey = &tempKey + if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err { + logging.Logger().Error(err) + s.AuthDoneChan <- err + return + } +} diff --git a/auth/service/service.go b/auth/service/service.go index 3713b70..1c057f5 100644 --- a/auth/service/service.go +++ b/auth/service/service.go @@ -1,5 +1,13 @@ package service +import "reflect" + +var ( + OrderedServices = []reflect.Type{ + reflect.TypeOf((*NoAuthProbeService)(nil)), + } +) + func InitPackage() { } diff --git a/client/central/auth.go b/client/central/auth.go index 3ab92e7..369d8a2 100644 --- a/client/central/auth.go +++ b/client/central/auth.go @@ -15,7 +15,7 @@ import ( "git.loafle.net/overflow/probe/config" ) -func NewAuth(tempKeyHandler func(tempKey string), services []interface{}) (*crc.Client, error) { +func NewAuth(tempKeyHandler func(tempKey string), services ...interface{}) (*crc.Client, error) { config := config.GetConfig() if nil == config { return nil, fmt.Errorf("Config is not available")