diff --git a/probe/probe.go b/probe/probe.go index 105bf85..b950275 100644 --- a/probe/probe.go +++ b/probe/probe.go @@ -4,9 +4,11 @@ import ( "fmt" "sync" + cdr "git.loafle.net/commons_go/di/registry" "git.loafle.net/commons_go/logging" crr "git.loafle.net/commons_go/rpc/registry" oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" + oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" oopccd "git.loafle.net/overflow/overflow_probes/client/central/data" oopccp "git.loafle.net/overflow/overflow_probes/client/central/probe" "git.loafle.net/overflow/overflow_probes/service" @@ -34,28 +36,38 @@ func (pm *probeManagers) Start() error { } probeRPCRegistry := crr.NewRPCRegistry() - probeRPCRegistry.RegisterService(&service.CentralService{}, "") - probeRPCRegistry.RegisterService(&service.ConfigService{}, "") - probeRPCRegistry.RegisterService(&service.CrawlerService{}, "") - probeRPCRegistry.RegisterService(&service.DiscoveryService{}, "") - probeRPCRegistry.RegisterService(&service.LogService{}, "") - probeRPCRegistry.RegisterService(&service.ProbeService{}, "") - probeRPCRegistry.RegisterService(&service.SensorService{}, "") - centralProbeClient := oopccp.New(probeRPCRegistry) + centralDataClient := oopccd.New() + centralClients := map[string]oogwc.Client{ + oocmp.HTTPEntry_Probe: centralProbeClient, + oocmp.HTTPEntry_Data: centralDataClient, + } + cdr.RegisterResource("CentralClients", centralClients) + + centralService := service.GetService("CentralService").(*service.CentralService) + configService := service.GetService("ConfigService").(*service.ConfigService) + crawlerService := service.GetService("CrawlerService").(*service.CrawlerService) + discoveryService := service.GetService("DiscoveryService").(*service.DiscoveryService) + logService := service.GetService("LogService").(*service.LogService) + probeService := service.GetService("ProbeService").(*service.ProbeService) + sensorService := service.GetService("SensorService").(*service.SensorService) + + probeRPCRegistry.RegisterService(centralService, "") + probeRPCRegistry.RegisterService(configService, "") + probeRPCRegistry.RegisterService(crawlerService, "") + probeRPCRegistry.RegisterService(discoveryService, "") + probeRPCRegistry.RegisterService(logService, "") + probeRPCRegistry.RegisterService(probeService, "") + probeRPCRegistry.RegisterService(sensorService, "") + if err := centralProbeClient.Connect(); nil != err { return err } - centralDataClient := oopccd.New() if err := centralDataClient.Connect(); nil != err { return err } - centralS := probeRPCRegistry.GetService("CentralService").(*service.CentralService) - centralS.PutClient(oocmp.HTTPEntry_Probe, centralProbeClient) - centralS.PutClient(oocmp.HTTPEntry_Data, centralDataClient) - pm.stopChan = make(chan struct{}) pm.stopWg.Add(1) diff --git a/service/CentralService.go b/service/CentralService.go index 57a9d71..c80aef5 100644 --- a/service/CentralService.go +++ b/service/CentralService.go @@ -1,15 +1,23 @@ package service import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" ) +func init() { + cdr.RegisterType(reflect.TypeOf((*CentralService)(nil)), &cda.ComponentAnnotation{}) +} + type CentralService struct { - clients map[string]oogwc.Client + CentralClients map[string]oogwc.Client } func (cs *CentralService) PutClient(entryPath string, c oogwc.Client) { - cs.clients[entryPath] = c + cs.CentralClients[entryPath] = c } func (cs *CentralService) Call(entryPath string, result interface{}, method string, params ...interface{}) error { @@ -24,11 +32,11 @@ func (cs *CentralService) Send(entryPath string, method string, params ...interf } func (cs *CentralService) GetClient(entryPath string) oogwc.Client { - return cs.clients[entryPath] + return cs.CentralClients[entryPath] } func (cs *CentralService) CheckClient(entryPath string) bool { - c, ok := cs.clients[entryPath] + c, ok := cs.CentralClients[entryPath] if !ok || nil == c { return false } diff --git a/service/ConfigService.go b/service/ConfigService.go index 89f7f66..0d8f065 100644 --- a/service/ConfigService.go +++ b/service/ConfigService.go @@ -1,4 +1,15 @@ package service +import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" +) + +func init() { + cdr.RegisterType(reflect.TypeOf((*ConfigService)(nil)), &cda.ComponentAnnotation{}) +} + type ConfigService struct { } diff --git a/service/ContainerService.go b/service/ContainerService.go index 56c41f6..f5123c5 100644 --- a/service/ContainerService.go +++ b/service/ContainerService.go @@ -1,9 +1,17 @@ package service import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" crc "git.loafle.net/commons_go/rpc/client" ) +func init() { + cdr.RegisterType(reflect.TypeOf((*ContainerService)(nil)), &cda.ComponentAnnotation{}) +} + type ContainerService struct { clients map[string]*containerState } diff --git a/service/CrawlerService.go b/service/CrawlerService.go index 5d45478..12e0c17 100644 --- a/service/CrawlerService.go +++ b/service/CrawlerService.go @@ -1,9 +1,17 @@ package service import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" ) +func init() { + cdr.RegisterType(reflect.TypeOf((*CrawlerService)(nil)), &cda.ComponentAnnotation{}) +} + type CrawlerService struct { } diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index 4cbd18c..5fedb87 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -1,11 +1,19 @@ package service import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model" oocmp "git.loafle.net/overflow/overflow_commons_go/modules/probe" oocmpsp "git.loafle.net/overflow/overflow_commons_go/modules/probe/service/probe" ) +func init() { + cdr.RegisterType(reflect.TypeOf((*DiscoveryService)(nil)), &cda.ComponentAnnotation{}) +} + type DiscoveryService struct { ContainerService *ContainerService `annotation:"@Inject()"` CentralService *CentralService `annotation:"@Inject()"` diff --git a/service/LogService.go b/service/LogService.go index 6d946b5..be20a38 100644 --- a/service/LogService.go +++ b/service/LogService.go @@ -1,5 +1,16 @@ package service +import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" +) + +func init() { + cdr.RegisterType(reflect.TypeOf((*LogService)(nil)), &cda.ComponentAnnotation{}) +} + type LogService struct { } diff --git a/service/ProbeService.go b/service/ProbeService.go index 46f5f65..29a6777 100644 --- a/service/ProbeService.go +++ b/service/ProbeService.go @@ -1,5 +1,16 @@ package service +import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" +) + +func init() { + cdr.RegisterType(reflect.TypeOf((*ProbeService)(nil)), &cda.ComponentAnnotation{}) +} + type ProbeService struct { } diff --git a/service/SensorService.go b/service/SensorService.go index 22f87c7..23dfceb 100644 --- a/service/SensorService.go +++ b/service/SensorService.go @@ -1,9 +1,17 @@ package service import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" ) +func init() { + cdr.RegisterType(reflect.TypeOf((*SensorService)(nil)), &cda.ComponentAnnotation{}) +} + type SensorService struct { } diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..479178a --- /dev/null +++ b/service/service.go @@ -0,0 +1,48 @@ +package service + +import ( + "fmt" + "reflect" + + cdr "git.loafle.net/commons_go/di/registry" + "git.loafle.net/commons_go/logging" +) + +func InitService() { +} + +func DestroyService() { + +} + +func GetService(name string) interface{} { + var t reflect.Type + switch name { + case "CentralService": + t = reflect.TypeOf((*CentralService)(nil)) + case "ConfigService": + t = reflect.TypeOf((*ConfigService)(nil)) + case "ContainerService": + t = reflect.TypeOf((*ContainerService)(nil)) + case "CrawlerService": + t = reflect.TypeOf((*CrawlerService)(nil)) + case "DiscoveryService": + t = reflect.TypeOf((*DiscoveryService)(nil)) + case "LogService": + t = reflect.TypeOf((*LogService)(nil)) + case "ProbeService": + t = reflect.TypeOf((*ProbeService)(nil)) + case "SensorService": + t = reflect.TypeOf((*SensorService)(nil)) + default: + logging.Logger().Panic(fmt.Sprintf("Probe: Service[%s] is not exist", name)) + return nil + } + + i, err := cdr.GetInstance(t) + if nil != err { + logging.Logger().Panic(fmt.Sprintf("Probe: Getting Service[%s] is failed %v", name, err)) + return nil + } + return i +}