This commit is contained in:
crusader 2018-04-19 15:24:45 +09:00
parent d4ea8c3daf
commit c6ee110820
8 changed files with 262 additions and 3 deletions

View File

@ -1 +0,0 @@
package container

5
constants.go Normal file
View File

@ -0,0 +1,5 @@
package container
const (
CONTAINER_CRAWLERS = "CONTAINER_CRAWLERS"
)

View File

@ -1,3 +1,5 @@
package: git.loafle.net/overflow/container-go package: git.loafle.net/overflow/container-go
import: import:
- package: git.loafle.net/commons/server-go - package: git.loafle.net/commons/server-go
- package: git.loafle.net/overflow/commons-go
- package: git.loafle.net/overflow/crawler-go

87
service/CrawlerService.go Normal file
View File

@ -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
}

43
service/ProbeService.go Normal file
View File

@ -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
}

View File

@ -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
}

23
service/service.go Normal file
View File

@ -0,0 +1,23 @@
package service
import "reflect"
var (
OrderedServices = []reflect.Type{
ProbeServiceType,
SensorConfigServiceType,
CrawlerServiceType,
}
)
func InitPackage() {
}
func StartPackage() {
}
func StopPackage() {
}
func DestroyPackage() {
}

View File

@ -75,9 +75,7 @@ func (s *RPCServlets) Handle(servletCtx server.ServletCtx,
case <-stopChan: case <-stopChan:
return return
} }
} }
} }
func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code crp.ErrorCode, message string, data interface{}) { func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code crp.ErrorCode, message string, data interface{}) {