From 92d7926d3f5b93532e7b53be37c9ff12fc94747d Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 17 Apr 2018 23:11:13 +0900 Subject: [PATCH] ing --- auth.json | 4 ++ auth/annotation/auth-rpc-service.go | 17 ++++++ auth/authenticator.go | 31 +++++------ auth/service/NoAuthProbeService.go | 12 ++--- auth/service/service.go | 6 ++- client/central/data.go | 8 +-- client/central/probe.go | 12 ++--- client/container/container.go | 45 ++++++++++++++++ client/container/discovery.go | 22 ++++++++ config.json | 1 + main.go | 35 +++++++++--- probe/probe.go | 83 ++++++++++++++--------------- service/CollectorService.go | 38 +++++++++++++ service/ContainerService.go | 19 ++++++- service/CrawlerService.go | 19 ++++++- service/DataClientService.go | 53 ++++++++++++++++++ service/DiscoveryClientService.go | 52 ++++++++++++++++++ service/DiscoveryService.go | 19 ++++++- service/GeneralClientService.go | 48 +++++++++++++++++ service/NetworkClientService.go | 48 +++++++++++++++++ service/ProbeClientService.go | 65 ++++++++++++++++++++++ service/ProbeService.go | 24 --------- service/SensorConfigService.go | 36 +++++++++++++ service/service.go | 17 ++++++ 24 files changed, 602 insertions(+), 112 deletions(-) create mode 100644 auth.json create mode 100644 auth/annotation/auth-rpc-service.go create mode 100644 client/container/container.go create mode 100644 client/container/discovery.go create mode 100644 service/CollectorService.go create mode 100644 service/DataClientService.go create mode 100644 service/DiscoveryClientService.go create mode 100644 service/GeneralClientService.go create mode 100644 service/NetworkClientService.go create mode 100644 service/ProbeClientService.go delete mode 100644 service/ProbeService.go create mode 100644 service/SensorConfigService.go diff --git a/auth.json b/auth.json new file mode 100644 index 0000000..1bdcfbc --- /dev/null +++ b/auth.json @@ -0,0 +1,4 @@ +{ + "tempKey": "cb8ef3c8424511e8b0460242ac120002", + "acceptedDate": "2018-04-17T22:45:11.147531244+09:00" +} \ No newline at end of file diff --git a/auth/annotation/auth-rpc-service.go b/auth/annotation/auth-rpc-service.go new file mode 100644 index 0000000..18d514e --- /dev/null +++ b/auth/annotation/auth-rpc-service.go @@ -0,0 +1,17 @@ +package annotation + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" +) + +var AuthRPCServiceAnnotationType = reflect.TypeOf((*AuthRPCServiceAnnotation)(nil)) + +func init() { + cda.RegisterAnnotation(AuthRPCServiceAnnotationType) +} + +type AuthRPCServiceAnnotation struct { + cda.TypeAnnotation `@annotation:"@overflow:AuthRPCService"` +} diff --git a/auth/authenticator.go b/auth/authenticator.go index e65fe54..832434c 100644 --- a/auth/authenticator.go +++ b/auth/authenticator.go @@ -9,10 +9,10 @@ import ( "git.loafle.net/commons/configuration-go" cdr "git.loafle.net/commons/di-go/registry" logging "git.loafle.net/commons/logging-go" - 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/auth/annotation" "git.loafle.net/overflow/probe/auth/service" ) @@ -23,13 +23,14 @@ type Authenticator struct { services []interface{} + endChan chan error stopChan chan struct{} stopWg sync.WaitGroup } func (a *Authenticator) EndableStart() (<-chan error, error) { if a.stopChan != nil { - return nil, fmt.Errorf("authenticator already running. Stop it before starting it again") + return nil, fmt.Errorf("already running. Stop it before starting it again") } authConfigPath := path.Join(a.ConfigDir, ocnc.ConfigFileName) @@ -50,7 +51,7 @@ func (a *Authenticator) EndableStart() (<-chan error, error) { authDoneChan := make(chan error) cdr.RegisterResource("AuthDoneChan", authDoneChan) - services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType) + services, err := cdr.GetInstancesByAnnotationType(annotation.AuthRPCServiceAnnotationType) if nil != err { return nil, err } @@ -60,40 +61,36 @@ func (a *Authenticator) EndableStart() (<-chan error, error) { return nil, err } - endChan := make(chan error) + a.endChan = make(chan error) a.stopChan = make(chan struct{}) a.stopWg.Add(1) - go a.handleAuthenticator(authDoneChan, endChan) + go a.handleAuthenticator(authDoneChan) - return endChan, nil + return a.endChan, nil } func (a *Authenticator) Stop(ctx context.Context) error { if a.stopChan == nil { - return fmt.Errorf("Authenticator: must be started before stopping it") + return nil } close(a.stopChan) a.stopWg.Wait() - if err := occi.ExecServices(a.services, occi.ServiceMethodDestroy, service.OrderedServices, true); nil != err { - return err - } + occi.ExecServices(a.services, occi.ServiceMethodDestroy, service.OrderedServices, true) a.stopChan = nil + close(a.endChan) + return nil } -func (a *Authenticator) logHeader() string { - return "Authenticator:" -} - -func (a *Authenticator) handleAuthenticator(authDoneChan chan error, endChan chan<- error) { +func (a *Authenticator) handleAuthenticator(authDoneChan chan error) { var err error defer func() { a.stopWg.Done() - endChan <- err + a.endChan <- err }() err = occi.ExecServices(a.services, occi.ServiceMethodStart, service.OrderedServices, false) @@ -111,5 +108,5 @@ LOOP: } } - err = occi.ExecServices(a.services, occi.ServiceMethodStop, service.OrderedServices, true) + occi.ExecServices(a.services, occi.ServiceMethodStop, service.OrderedServices, true) } diff --git a/auth/service/NoAuthProbeService.go b/auth/service/NoAuthProbeService.go index 538ab10..d6b1e84 100644 --- a/auth/service/NoAuthProbeService.go +++ b/auth/service/NoAuthProbeService.go @@ -17,7 +17,7 @@ import ( "git.loafle.net/overflow/probe/config" // For annotation - _ "git.loafle.net/overflow/commons-go/core/annotation" + _ "git.loafle.net/overflow/probe/auth/annotation" ) var NoAuthProbeServiceType = reflect.TypeOf((*NoAuthProbeService)(nil)) @@ -27,7 +27,7 @@ func init() { } type NoAuthProbeService struct { - cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + cda.TypeAnnotation `annotation:"@overflow:AuthRPCService()"` ConfigDir string `annotation:"@Resource(name='ConfigDir')"` Config *config.Config `annotation:"@Resource(name='Config')"` @@ -37,7 +37,7 @@ type NoAuthProbeService struct { client *crc.Client } -func (s *NoAuthProbeService) Init() error { +func (s *NoAuthProbeService) InitService() error { client, err := central.NewAuth(s.HandleTempKey, s) if nil != err { return err @@ -47,7 +47,7 @@ func (s *NoAuthProbeService) Init() error { return nil } -func (s *NoAuthProbeService) Start() error { +func (s *NoAuthProbeService) StartService() error { if err := s.client.Start(); nil != err { return err } @@ -55,13 +55,13 @@ func (s *NoAuthProbeService) Start() error { return nil } -func (s *NoAuthProbeService) Stop() { +func (s *NoAuthProbeService) StopService() { if err := s.client.Stop(context.Background()); nil != err { logging.Logger().Error(err) } } -func (s *NoAuthProbeService) Destroy() { +func (s *NoAuthProbeService) DestroyService() { s.client = nil } diff --git a/auth/service/service.go b/auth/service/service.go index 1c057f5..df79023 100644 --- a/auth/service/service.go +++ b/auth/service/service.go @@ -1,10 +1,12 @@ package service -import "reflect" +import ( + "reflect" +) var ( OrderedServices = []reflect.Type{ - reflect.TypeOf((*NoAuthProbeService)(nil)), + NoAuthProbeServiceType, } ) diff --git a/client/central/data.go b/client/central/data.go index 76e0d52..6679d0e 100644 --- a/client/central/data.go +++ b/client/central/data.go @@ -5,7 +5,7 @@ import ( "net/http" crc "git.loafle.net/commons/rpc-go/client" - ocnpc "git.loafle.net/overflow/commons-go/probe/constants" + ocpc "git.loafle.net/overflow/commons-go/probe/constants" "git.loafle.net/overflow/probe/config" ) @@ -15,15 +15,15 @@ func NewData() (*crc.Client, error) { return nil, fmt.Errorf("Config is not available") } - connector, err := newConnector("Data", ocnpc.HTTPEntry_Data) + connector, err := newConnector("Data", ocpc.HTTPEntry_Data) if nil != err { return nil, err } connector.RequestHeader = func() http.Header { header := make(map[string][]string) - header[ocnpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocnpc.HTTPRequestHeaderValue_Probe_Method_Connect} - header[ocnpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key} + header[ocpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocpc.HTTPRequestHeaderValue_Probe_Method_Connect} + header[ocpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key} return header } connector.ResponseHandler = func(res *http.Response) { diff --git a/client/central/probe.go b/client/central/probe.go index 1567b26..dadfde7 100644 --- a/client/central/probe.go +++ b/client/central/probe.go @@ -5,29 +5,29 @@ import ( "net/http" crc "git.loafle.net/commons/rpc-go/client" - ocnpc "git.loafle.net/overflow/commons-go/probe/constants" + ocpc "git.loafle.net/overflow/commons-go/probe/constants" "git.loafle.net/overflow/probe/config" ) -func NewProbe(encryptionKeyHandler func(encryptionKey string), services []interface{}) (*crc.Client, error) { +func NewProbe(encryptionKeyHandler func(encryptionKey string), services ...interface{}) (*crc.Client, error) { config := config.GetConfig() if nil == config { return nil, fmt.Errorf("Config is not available") } - connector, err := newConnector("Probe", ocnpc.HTTPEntry_Probe) + connector, err := newConnector("Probe", ocpc.HTTPEntry_Probe) if nil != err { return nil, err } connector.RequestHeader = func() http.Header { header := make(map[string][]string) - header[ocnpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocnpc.HTTPRequestHeaderValue_Probe_Method_Connect} - header[ocnpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key} + header[ocpc.HTTPRequestHeaderKey_Probe_Method] = []string{ocpc.HTTPRequestHeaderValue_Probe_Method_Connect} + header[ocpc.HTTPRequestHeaderKey_Probe_ProbeKey] = []string{*config.Probe.Key} return header } connector.ResponseHandler = func(res *http.Response) { - encryptionKey := res.Header.Get(ocnpc.HTTPResponseHeaderKey_Probe_SetEncryptionKey) + encryptionKey := res.Header.Get(ocpc.HTTPResponseHeaderKey_Probe_SetEncryptionKey) if nil != encryptionKeyHandler { encryptionKeyHandler(encryptionKey) } diff --git a/client/container/container.go b/client/container/container.go new file mode 100644 index 0000000..2bb4fd8 --- /dev/null +++ b/client/container/container.go @@ -0,0 +1,45 @@ +package container + +import ( + crc "git.loafle.net/commons/rpc-go/client" + crpj "git.loafle.net/commons/rpc-go/protocol/json" + crr "git.loafle.net/commons/rpc-go/registry" + csc "git.loafle.net/commons/server-go/client" + cssnc "git.loafle.net/commons/server-go/socket/net/client" +) + +func newConnector(name string) (*cssnc.Connectors, error) { + connector := &cssnc.Connectors{ + Network: "tcp4", + Address: "", + } + connector.ReconnectInterval = 5 + connector.ReconnectTryTime = 10 + connector.MaxMessageSize = 4096 + connector.ReadBufferSize = 4096 + connector.WriteBufferSize = 4096 + connector.PongTimeout = 60 + connector.PingTimeout = 10 + connector.PingPeriod = 9 + + connector.Name = name + + return connector, nil +} + +func newClient(name string, connector csc.Connector, services []interface{}) *crc.Client { + codec := crpj.NewClientCodec() + + var rpcRegistry crr.RPCRegistry + if nil != services && 0 < len(services) { + rpcRegistry = crr.NewRPCRegistry() + rpcRegistry.RegisterServices(services...) + } + + return &crc.Client{ + Connector: connector, + Codec: codec, + RPCInvoker: rpcRegistry, + Name: name, + } +} diff --git a/client/container/discovery.go b/client/container/discovery.go new file mode 100644 index 0000000..104b99c --- /dev/null +++ b/client/container/discovery.go @@ -0,0 +1,22 @@ +package container + +import ( + "fmt" + + crc "git.loafle.net/commons/rpc-go/client" + "git.loafle.net/overflow/probe/config" +) + +func NewDiscovery(services ...interface{}) (*crc.Client, error) { + config := config.GetConfig() + if nil == config { + return nil, fmt.Errorf("Config is not available") + } + + connector, err := newConnector("Probe") + if nil != err { + return nil, err + } + + return newClient("Probe", connector, services), nil +} diff --git a/config.json b/config.json index 80cd561..f9423c3 100644 --- a/config.json +++ b/config.json @@ -17,6 +17,7 @@ } }, "probe": { + "key": "d3eb9b99424511e8b0460242ac120002" }, "paths": { "root": "/project/overFlow/probe" diff --git a/main.go b/main.go index f2d72ad..fe2324a 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( ocpc "git.loafle.net/overflow/commons-go/probe/config" "git.loafle.net/overflow/probe/auth" "git.loafle.net/overflow/probe/config" + "git.loafle.net/overflow/probe/probe" ) var ( @@ -48,23 +49,41 @@ func main() { } doneChan, err := instance.(occi.EndableStarter).EndableStart() if nil != err { - logging.Logger().Panic(err) + logging.Logger().Error(err) + os.Exit(1) + } + var ok bool + err, ok = <-doneChan + if !ok { return } - err = <-doneChan if nil != err { - logging.Logger().Panic(err) + logging.Logger().Error(err) + os.Exit(1) } if err := instance.(occi.Stopper).Stop(context.Background()); err != nil { logging.Logger().Errorf("error: %v", err) } - } - // err := s.ListenAndServe() - // if nil != err { - // log.Printf("err: %v", err) - // } + instance = &probe.Probe{} + doneChan, err := instance.(occi.EndableStarter).EndableStart() + if nil != err { + logging.Logger().Error(err) + os.Exit(1) + } + var ok bool + err, ok = <-doneChan + if !ok { + return + } + if nil != err { + logging.Logger().Error(err) + os.Exit(1) + } + if err := instance.(occi.Stopper).Stop(context.Background()); err != nil { + logging.Logger().Errorf("error: %v", err) + } }() interrupt := make(chan os.Signal, 1) diff --git a/probe/probe.go b/probe/probe.go index 6b57044..0c62a64 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -5,80 +5,79 @@ import ( "fmt" "sync" - // For service - _ "git.loafle.net/overflow/probe/service" + cdr "git.loafle.net/commons/di-go/registry" + occa "git.loafle.net/overflow/commons-go/core/annotation" + occi "git.loafle.net/overflow/commons-go/core/interfaces" + + "git.loafle.net/overflow/probe/service" ) type Probe struct { + services []interface{} + + endChan chan error stopChan chan struct{} stopWg sync.WaitGroup } -func (p *Probe) Start() error { +func (p *Probe) EndableStart() (<-chan error, error) { if p.stopChan != nil { - return fmt.Errorf("already running. Stop it before starting it again") + return nil, fmt.Errorf("already running. Stop it before starting it again") } - // services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType) - // if nil != err { - // return err - // } + services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType) + if nil != err { + return nil, err + } + p.services = services - // client, err := central.NewProbe(p.HandleEncryptionKey, services) - // if nil != err { - // return err - // } + if err := occi.ExecServices(p.services, occi.ServiceMethodInit, service.OrderedServices, false); nil != err { + return nil, err + } + p.endChan = make(chan error) p.stopChan = make(chan struct{}) p.stopWg.Add(1) go p.handleProbe() - return nil + return p.endChan, nil } func (p *Probe) Stop(ctx context.Context) error { if p.stopChan == nil { - return fmt.Errorf("must be started before stopping it") + return nil } close(p.stopChan) p.stopWg.Wait() + occi.ExecServices(p.services, occi.ServiceMethodDestroy, service.OrderedServices, true) + p.stopChan = nil + close(p.endChan) return nil } -func (p *Probe) logHeader() string { - return "Probe:" -} - -func (p *Probe) HandleEncryptionKey(encryptionKey string) { - -} - func (p *Probe) handleProbe() { - // var err error - // defer func() { - // if nil != client { - // err = client.Stop(context.Background()) - // } + var err error + defer func() { + p.stopWg.Done() + p.endChan <- err + }() - // a.stopWg.Done() - // endChan <- err - // }() + err = occi.ExecServices(p.services, occi.ServiceMethodStart, service.OrderedServices, false) + if nil != err { + return + } - // if err = client.Start(); nil != err { - // logging.Logger().Error(err) - // return - // } +LOOP: + for { + select { + case <-p.stopChan: + break LOOP + } + } - // for { - // select { - // case err = <-authDoneChan: - // return - // case <-a.stopChan: - // return - // } - // } + occi.ExecServices(p.services, occi.ServiceMethodStop, service.OrderedServices, true) } diff --git a/service/CollectorService.go b/service/CollectorService.go new file mode 100644 index 0000000..3cdcfe8 --- /dev/null +++ b/service/CollectorService.go @@ -0,0 +1,38 @@ +package service + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + _ "git.loafle.net/overflow/commons-go/core/annotation" +) + +var CollectorServiceType = reflect.TypeOf((*CollectorService)(nil)) + +func init() { + cdr.RegisterType(CollectorServiceType) +} + +type CollectorService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + DataClientService *DataClientService `annotation:"@Inject()"` +} + +func (s *CollectorService) InitService() error { + return nil +} + +func (s *CollectorService) StartService() error { + + return nil +} + +func (s *CollectorService) StopService() { + +} + +func (s *CollectorService) DestroyService() { + +} diff --git a/service/ContainerService.go b/service/ContainerService.go index 9ca0f81..d247eb1 100644 --- a/service/ContainerService.go +++ b/service/ContainerService.go @@ -18,7 +18,24 @@ type ContainerService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` } -func (cs *ContainerService) Accept() error { +func (s *ContainerService) InitService() error { + return nil +} + +func (s *ContainerService) StartService() error { + + return nil +} + +func (s *ContainerService) StopService() { + +} + +func (s *ContainerService) DestroyService() { + +} + +func (s *ContainerService) Accept() error { return nil } diff --git a/service/CrawlerService.go b/service/CrawlerService.go index ffd04bd..410b463 100644 --- a/service/CrawlerService.go +++ b/service/CrawlerService.go @@ -18,7 +18,24 @@ type CrawlerService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` } -func (cs *CrawlerService) Accept() error { +func (s *CrawlerService) InitService() error { + return nil +} + +func (s *CrawlerService) StartService() error { + + return nil +} + +func (s *CrawlerService) StopService() { + +} + +func (s *CrawlerService) DestroyService() { + +} + +func (s *CrawlerService) Accept() error { return nil } diff --git a/service/DataClientService.go b/service/DataClientService.go new file mode 100644 index 0000000..4daf9a6 --- /dev/null +++ b/service/DataClientService.go @@ -0,0 +1,53 @@ +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" + _ "git.loafle.net/overflow/commons-go/core/annotation" + "git.loafle.net/overflow/probe/client/central" +) + +var DataClientServiceType = reflect.TypeOf((*DataClientService)(nil)) + +func init() { + cdr.RegisterType(DataClientServiceType) +} + +type DataClientService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + client *crc.Client +} + +func (s *DataClientService) InitService() error { + client, err := central.NewData() + if nil != err { + return err + } + s.client = client + + return nil +} + +func (s *DataClientService) StartService() error { + if err := s.client.Start(); nil != err { + return err + } + + return nil +} + +func (s *DataClientService) StopService() { + if err := s.client.Stop(context.Background()); nil != err { + logging.Logger().Error(err) + } +} + +func (s *DataClientService) DestroyService() { + s.client = nil +} diff --git a/service/DiscoveryClientService.go b/service/DiscoveryClientService.go new file mode 100644 index 0000000..9d96246 --- /dev/null +++ b/service/DiscoveryClientService.go @@ -0,0 +1,52 @@ +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" +) + +var DiscoveryClientServiceType = reflect.TypeOf((*DiscoveryClientService)(nil)) + +func init() { + cdr.RegisterType(DiscoveryClientServiceType) +} + +type DiscoveryClientService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + DiscoveryService *DiscoveryService `annotation:"@Inject()"` + + EncryptionKey string + + client *crc.Client +} + +func (s *DiscoveryClientService) InitService() error { + + return nil +} + +func (s *DiscoveryClientService) StartService() error { + + return nil +} + +func (s *DiscoveryClientService) StopService() { + if nil != s.client { + if err := s.client.Stop(context.Background()); nil != err { + logging.Logger().Error(err) + } + } +} + +func (s *DiscoveryClientService) DestroyService() { + s.client = nil +} diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index 5e6ed2d..f03258e 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -18,7 +18,24 @@ type DiscoveryService struct { cda.TypeAnnotation `annotation:"@overflow:RPCService()"` } -func (cs *DiscoveryService) Accept() error { +func (s *DiscoveryService) InitService() error { + return nil +} + +func (s *DiscoveryService) StartService() error { + + return nil +} + +func (s *DiscoveryService) StopService() { + +} + +func (s *DiscoveryService) DestroyService() { + +} + +func (s *DiscoveryService) Accept() error { return nil } diff --git a/service/GeneralClientService.go b/service/GeneralClientService.go new file mode 100644 index 0000000..a8baef6 --- /dev/null +++ b/service/GeneralClientService.go @@ -0,0 +1,48 @@ +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" +) + +var GeneralClientServiceType = reflect.TypeOf((*GeneralClientService)(nil)) + +func init() { + cdr.RegisterType(GeneralClientServiceType) +} + +type GeneralClientService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + EncryptionKey string + + client *crc.Client +} + +func (s *GeneralClientService) InitService() error { + return nil +} + +func (s *GeneralClientService) StartService() error { + return nil +} + +func (s *GeneralClientService) StopService() { + if nil != s.client { + if err := s.client.Stop(context.Background()); nil != err { + logging.Logger().Error(err) + } + } +} + +func (s *GeneralClientService) DestroyService() { + s.client = nil +} diff --git a/service/NetworkClientService.go b/service/NetworkClientService.go new file mode 100644 index 0000000..edd5f86 --- /dev/null +++ b/service/NetworkClientService.go @@ -0,0 +1,48 @@ +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" +) + +var NetworkClientServiceType = reflect.TypeOf((*NetworkClientService)(nil)) + +func init() { + cdr.RegisterType(NetworkClientServiceType) +} + +type NetworkClientService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + EncryptionKey string + + client *crc.Client +} + +func (s *NetworkClientService) InitService() error { + return nil +} + +func (s *NetworkClientService) StartService() error { + return nil +} + +func (s *NetworkClientService) StopService() { + if nil != s.client { + if err := s.client.Stop(context.Background()); nil != err { + logging.Logger().Error(err) + } + } +} + +func (s *NetworkClientService) DestroyService() { + s.client = nil +} diff --git a/service/ProbeClientService.go b/service/ProbeClientService.go new file mode 100644 index 0000000..88eb85c --- /dev/null +++ b/service/ProbeClientService.go @@ -0,0 +1,65 @@ +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 +} diff --git a/service/ProbeService.go b/service/ProbeService.go deleted file mode 100644 index 883fcdd..0000000 --- a/service/ProbeService.go +++ /dev/null @@ -1,24 +0,0 @@ -package service - -import ( - "reflect" - - cda "git.loafle.net/commons/di-go/annotation" - cdr "git.loafle.net/commons/di-go/registry" - _ "git.loafle.net/overflow/commons-go/core/annotation" -) - -var ProbeServiceType = reflect.TypeOf((*ProbeService)(nil)) - -func init() { - cdr.RegisterType(ProbeServiceType) -} - -type ProbeService struct { - cda.TypeAnnotation `annotation:"@overflow:RPCService()"` -} - -func (cs *ProbeService) Accept() error { - - return nil -} diff --git a/service/SensorConfigService.go b/service/SensorConfigService.go new file mode 100644 index 0000000..54786cd --- /dev/null +++ b/service/SensorConfigService.go @@ -0,0 +1,36 @@ +package service + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + _ "git.loafle.net/overflow/commons-go/core/annotation" +) + +var SensorConfigServiceType = reflect.TypeOf((*SensorConfigService)(nil)) + +func init() { + cdr.RegisterType(SensorConfigServiceType) +} + +type SensorConfigService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` +} + +func (s *SensorConfigService) InitService() error { + return nil +} + +func (s *SensorConfigService) StartService() error { + + return nil +} + +func (s *SensorConfigService) StopService() { + +} + +func (s *SensorConfigService) DestroyService() { + +} diff --git a/service/service.go b/service/service.go index 3713b70..a0b2c3b 100644 --- a/service/service.go +++ b/service/service.go @@ -1,5 +1,22 @@ package service +import "reflect" + +var ( + OrderedServices = []reflect.Type{ + ProbeClientServiceType, + DataClientServiceType, + DiscoveryClientServiceType, + GeneralClientServiceType, + NetworkClientServiceType, + SensorConfigServiceType, + ContainerServiceType, + CrawlerServiceType, + CollectorServiceType, + DiscoveryServiceType, + } +) + func InitPackage() { }