mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-21 17:17:31 +08:00
优化模块与服务间关系
This commit is contained in:
@@ -99,6 +99,8 @@ func (ws *CTest) OnRun() error {
|
|||||||
pTmpModule := ws.GetModuleByType(1)
|
pTmpModule := ws.GetModuleByType(1)
|
||||||
pTmpModuleTest := pTmpModule.(*CTestModule)
|
pTmpModuleTest := pTmpModule.(*CTestModule)
|
||||||
pTmpModuleTest.DoSomething()
|
pTmpModuleTest.DoSomething()
|
||||||
|
pservice := testModule.GetOwnerService()
|
||||||
|
fmt.Printf("%T", pservice)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,10 +24,12 @@ type IBaseModule interface {
|
|||||||
OnRun() error
|
OnRun() error
|
||||||
AddModule(module IBaseModule) bool
|
AddModule(module IBaseModule) bool
|
||||||
GetModuleByType(moduleType uint32) IBaseModule
|
GetModuleByType(moduleType uint32) IBaseModule
|
||||||
|
GetOwnerService() IService
|
||||||
|
SetOwnerService(iservice IService)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IService interface {
|
type IService interface {
|
||||||
Init(Iservice interface{}, servicetype int) error
|
Init(Iservice IService, servicetype int) error
|
||||||
Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error
|
Run(service IService, exit chan bool, pwaitGroup *sync.WaitGroup) error
|
||||||
OnInit() error
|
OnInit() error
|
||||||
OnEndInit() error
|
OnEndInit() error
|
||||||
@@ -76,6 +78,8 @@ type BaseService struct {
|
|||||||
type BaseModule struct {
|
type BaseModule struct {
|
||||||
moduleType uint32
|
moduleType uint32
|
||||||
mapModule map[uint32]IBaseModule
|
mapModule map[uint32]IBaseModule
|
||||||
|
|
||||||
|
ownerService IService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) GetServiceId() int {
|
func (slf *BaseService) GetServiceId() int {
|
||||||
@@ -115,8 +119,9 @@ func (slf *BaseService) OnRemoveService(iservice IService) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) Init(Iservice interface{}, servicetype int) error {
|
func (slf *BaseService) Init(iservice IService, servicetype int) error {
|
||||||
slf.servicename = fmt.Sprintf("%T", Iservice)
|
slf.ownerService = iservice
|
||||||
|
slf.servicename = fmt.Sprintf("%T", iservice)
|
||||||
parts := strings.Split(slf.servicename, ".")
|
parts := strings.Split(slf.servicename, ".")
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
err := fmt.Errorf("BaseService.Init: service name is error: %q", slf.servicename)
|
err := fmt.Errorf("BaseService.Init: service name is error: %q", slf.servicename)
|
||||||
@@ -185,6 +190,8 @@ func (slf *BaseModule) AddModule(module IBaseModule) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.SetOwnerService(slf.ownerService)
|
||||||
|
|
||||||
if slf.mapModule == nil {
|
if slf.mapModule == nil {
|
||||||
slf.mapModule = make(map[uint32]IBaseModule)
|
slf.mapModule = make(map[uint32]IBaseModule)
|
||||||
}
|
}
|
||||||
@@ -213,3 +220,11 @@ func (slf *BaseModule) OnInit() error {
|
|||||||
func (slf *BaseModule) OnRun() error {
|
func (slf *BaseModule) OnRun() error {
|
||||||
return fmt.Errorf("not implement OnRun moduletype %d ", slf.GetModuleType())
|
return fmt.Errorf("not implement OnRun moduletype %d ", slf.GetModuleType())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *BaseModule) GetOwnerService() IService {
|
||||||
|
return slf.ownerService
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *BaseModule) SetOwnerService(iservice IService) {
|
||||||
|
slf.ownerService = iservice
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user