From c6ee1108203f4c3a4d1d043c2d04139d23095bdc Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 19 Apr 2018 15:24:45 +0900 Subject: [PATCH] ing --- const.go | 1 - constants.go | 5 ++ glide.yaml | 2 + service/CrawlerService.go | 87 ++++++++++++++++++++++++++++ service/ProbeService.go | 43 ++++++++++++++ service/SensorConfigService.go | 102 +++++++++++++++++++++++++++++++++ service/service.go | 23 ++++++++ servlet/rpc-servlet.go | 2 - 8 files changed, 262 insertions(+), 3 deletions(-) delete mode 100644 const.go create mode 100644 constants.go create mode 100644 service/CrawlerService.go create mode 100644 service/ProbeService.go create mode 100644 service/SensorConfigService.go create mode 100644 service/service.go diff --git a/const.go b/const.go deleted file mode 100644 index 92b9571..0000000 --- a/const.go +++ /dev/null @@ -1 +0,0 @@ -package container diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..b62b525 --- /dev/null +++ b/constants.go @@ -0,0 +1,5 @@ +package container + +const ( + CONTAINER_CRAWLERS = "CONTAINER_CRAWLERS" +) diff --git a/glide.yaml b/glide.yaml index fabf7eb..1fdada0 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,3 +1,5 @@ package: git.loafle.net/overflow/container-go import: - package: git.loafle.net/commons/server-go +- package: git.loafle.net/overflow/commons-go +- package: git.loafle.net/overflow/crawler-go diff --git a/service/CrawlerService.go b/service/CrawlerService.go new file mode 100644 index 0000000..56bd7b4 --- /dev/null +++ b/service/CrawlerService.go @@ -0,0 +1,87 @@ +package service + +import ( + "fmt" + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + logging "git.loafle.net/commons/logging-go" + "git.loafle.net/overflow/crawler-go" + + // For annotation + _ "git.loafle.net/overflow/commons-go/core/annotation" +) + +var CrawlerServiceType = reflect.TypeOf((*CrawlerService)(nil)) + +func init() { + cdr.RegisterType(CrawlerServiceType) +} + +type CrawlerService struct { + cda.TypeAnnotation `annotation:"@overflow:RPCService()"` + + SensorConfigService *SensorConfigService `annotation:"@Inject()"` + Crawlers map[string]crawler.Crawler `annotation:"@Resource(name=CONTAINER_CRAWLERS)"` +} + +func (s *CrawlerService) InitService() error { + if nil == s.Crawlers { + return fmt.Errorf("Crawlers is not set") + } + + return nil +} + +func (s *CrawlerService) StartService() error { + + return nil +} + +func (s *CrawlerService) StopService() { + +} + +func (s *CrawlerService) DestroyService() { + +} + +func (s *CrawlerService) Auth(crawlerName string, auth map[string]string) error { + _crawler, ok := s.Crawlers[crawlerName] + if !ok { + return fmt.Errorf("There is no crawler[%s]", crawlerName) + } + + logging.Logger().Debugf("Auth invoked with [%v]", auth) + + err := _crawler.Auth(auth) + if nil != err { + return fmt.Errorf("Failed to authentication from crawler[%s] %v", crawlerName, err) + } + logging.Logger().Debugf("Auth success with[%v]", auth) + + return nil +} + +func (s *CrawlerService) Get(sensorConfigID string) (map[string]string, error) { + sensorConfig, ok := s.SensorConfigService.sensorConfigs[sensorConfigID] + if !ok { + return nil, fmt.Errorf("There is no sensor config for id[%s]", sensorConfigID) + } + _crawlerName := sensorConfig.Crawler.Name + _crawler, ok := s.Crawlers[_crawlerName] + if !ok { + return nil, fmt.Errorf("There is no crawler[%s] for id[%s]", _crawlerName, sensorConfigID) + } + + logging.Logger().Debugf("Get invoked with sensor config[%s]", sensorConfigID) + + result, err := _crawler.Get(sensorConfig) + if nil != err { + return nil, fmt.Errorf("Failed to get data from crawler[%s] for id[%s] %v", _crawlerName, sensorConfigID, err) + } + logging.Logger().Debugf("Get success config[%v]", result) + + return result, nil +} diff --git a/service/ProbeService.go b/service/ProbeService.go new file mode 100644 index 0000000..0d353f9 --- /dev/null +++ b/service/ProbeService.go @@ -0,0 +1,43 @@ +package service + +import ( + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + + // For annotation + _ "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 (s *ProbeService) InitService() error { + return nil +} + +func (s *ProbeService) StartService() error { + + return nil +} + +func (s *ProbeService) StopService() { + +} + +func (s *ProbeService) DestroyService() { + +} + +func (s *ProbeService) Send(method string, params ...interface{}) error { + + return nil +} diff --git a/service/SensorConfigService.go b/service/SensorConfigService.go new file mode 100644 index 0000000..b4c02d6 --- /dev/null +++ b/service/SensorConfigService.go @@ -0,0 +1,102 @@ +package service + +import ( + "fmt" + "reflect" + + cda "git.loafle.net/commons/di-go/annotation" + cdr "git.loafle.net/commons/di-go/registry" + "git.loafle.net/commons/logging-go" + ocsm "git.loafle.net/overflow/commons-go/sensorconfig/model" + + // For annotation + _ "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()"` + + sensorConfigs map[string]*ocsm.SensorConfig +} + +func (s *SensorConfigService) InitService() error { + s.sensorConfigs = make(map[string]*ocsm.SensorConfig, 0) + + return nil +} + +func (s *SensorConfigService) StartService() error { + + return nil +} + +func (s *SensorConfigService) StopService() { + +} + +func (s *SensorConfigService) DestroyService() { + +} + +func (s *SensorConfigService) InitConfig(sensorConfigs []*ocsm.SensorConfig) error { + if nil == sensorConfigs || 0 == len(sensorConfigs) { + return nil + } + + for _, sensorConfig := range sensorConfigs { + s.sensorConfigs[sensorConfig.ConfigID] = sensorConfig + } + + logging.Logger().Debugf("Sensor configs[%d] were added", len(sensorConfigs)) + + return nil +} + +func (s *SensorConfigService) AddConfig(sensorConfig *ocsm.SensorConfig) error { + if nil == sensorConfig { + return fmt.Errorf("Sensor config is not valid") + } + + sensorConfigID := sensorConfig.ConfigID + if _, ok := s.sensorConfigs[sensorConfigID]; ok { + return fmt.Errorf("Sensor config[%s] is exist already", sensorConfigID) + } + s.sensorConfigs[sensorConfigID] = sensorConfig + + logging.Logger().Debugf("Sensor config[%d] was added", sensorConfigID) + + return nil +} + +func (s *SensorConfigService) UpdateConfig(sensorConfig *ocsm.SensorConfig) error { + if nil == sensorConfig { + return fmt.Errorf("Sensor config is not valid") + } + + sensorConfigID := sensorConfig.ConfigID + if _, ok := s.sensorConfigs[sensorConfigID]; !ok { + return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID) + } + delete(s.sensorConfigs, sensorConfigID) + s.sensorConfigs[sensorConfigID] = sensorConfig + + logging.Logger().Debugf("Sensor config[%d] was updated", sensorConfigID) + + return nil +} + +func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error { + if _, ok := s.sensorConfigs[sensorConfigID]; !ok { + return fmt.Errorf("Sensor config[%s] is not exist", sensorConfigID) + } + delete(s.sensorConfigs, sensorConfigID) + + logging.Logger().Debugf("Sensor config[%d] was removed", sensorConfigID) + return nil +} diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..bb49d6a --- /dev/null +++ b/service/service.go @@ -0,0 +1,23 @@ +package service + +import "reflect" + +var ( + OrderedServices = []reflect.Type{ + ProbeServiceType, + SensorConfigServiceType, + CrawlerServiceType, + } +) + +func InitPackage() { +} + +func StartPackage() { +} + +func StopPackage() { +} + +func DestroyPackage() { +} diff --git a/servlet/rpc-servlet.go b/servlet/rpc-servlet.go index fb4050a..c216b28 100644 --- a/servlet/rpc-servlet.go +++ b/servlet/rpc-servlet.go @@ -75,9 +75,7 @@ func (s *RPCServlets) Handle(servletCtx server.ServletCtx, case <-stopChan: return } - } - } func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code crp.ErrorCode, message string, data interface{}) {