probe/service/ProbeClientService.go
crusader 92d7926d3f ing
2018-04-17 23:11:13 +09:00

66 lines
1.4 KiB
Go

package service
import (
"context"
"reflect"
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"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation"
"git.loafle.net/overflow/probe/client/central"
)
var ProbeClientServiceType = reflect.TypeOf((*ProbeClientService)(nil))
func init() {
cdr.RegisterType(ProbeClientServiceType)
}
type ProbeClientService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
DiscoveryService *DiscoveryService `annotation:"@Inject()"`
EncryptionKey string
client *crc.Client
}
func (s *ProbeClientService) InitService() error {
client, err := central.NewProbe(s.HandleEncryptionKey, s.DiscoveryService)
if nil != err {
return err
}
s.client = client
return nil
}
func (s *ProbeClientService) StartService() error {
if err := s.client.Start(); nil != err {
return err
}
return nil
}
func (s *ProbeClientService) StopService() {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
func (s *ProbeClientService) DestroyService() {
s.client = nil
}
func (s *ProbeClientService) HandleEncryptionKey(encryptionKey string) {
logging.Logger().Debugf("encryptionKey arrived %s", encryptionKey)
s.EncryptionKey = encryptionKey
}