优化module功能,新增getowner功能

This commit is contained in:
boyce
2019-02-20 21:02:12 +08:00
parent 9384227cf6
commit 25039a0ef0
2 changed files with 76 additions and 11 deletions

View File

@@ -29,11 +29,7 @@ type COriginNode struct {
} }
func (s *COriginNode) Init() { func (s *COriginNode) Init() {
//s.exit = make(chan bool)
//s.waitGroup = &sync.WaitGroup{}
//初始化全局模块 //初始化全局模块
service.InitLog() service.InitLog()
imodule := g_module.GetModuleById(sysmodule.SYS_LOG) imodule := g_module.GetModuleById(sysmodule.SYS_LOG)
service.InstanceServiceMgr().Init(imodule.(service.ILogger), s.exit, s.waitGroup) service.InstanceServiceMgr().Init(imodule.(service.ILogger), s.exit, s.waitGroup)
@@ -66,7 +62,10 @@ func (s *COriginNode) SetupService(services ...service.IService) {
} }
services[i].OnSetupService(services[j]) services[i].OnSetupService(services[j])
} }
services[i].(service.IModule).SetOwnerService(services[i])
services[i].(service.IModule).SetOwner(services[i].(service.IModule))
} }
} }

View File

@@ -31,6 +31,11 @@ type IModule interface {
GetOwnerService() IService GetOwnerService() IService
SetOwnerService(iservice IService) SetOwnerService(iservice IService)
SetOwner(module IModule)
GetOwner() IModule
getBaseModule() *BaseModule
} }
type IService interface { type IService interface {
@@ -74,8 +79,7 @@ type BaseService struct {
} }
type BaseModule struct { type BaseModule struct {
moduleId uint32 moduleId uint32
mapModule map[uint32]IModule
ownerService IService ownerService IService
tickTime int64 tickTime int64
@@ -83,7 +87,12 @@ type BaseModule struct {
ExitChan chan bool ExitChan chan bool
WaitGroup *sync.WaitGroup WaitGroup *sync.WaitGroup
mapModule map[uint32]IModule
CurrMaxModuleId uint32 CurrMaxModuleId uint32
rwModuleLocker sync.RWMutex
selfModule IModule
ownerModule IModule
} }
func (slf *BaseService) GetServiceId() int { func (slf *BaseService) GetServiceId() int {
@@ -112,6 +121,7 @@ func (slf *BaseService) OnFetchService(iservice IService) error {
} }
func (slf *BaseService) OnSetupService(iservice IService) { func (slf *BaseService) OnSetupService(iservice IService) {
return return
} }
@@ -162,17 +172,44 @@ func (slf *BaseModule) GetModuleId() uint32 {
} }
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule { func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
slf.rwModuleLocker.RLock()
ret, ok := slf.mapModule[moduleId] ret, ok := slf.mapModule[moduleId]
if ok == false { if ok == false {
slf.rwModuleLocker.RUnlock()
return nil return nil
} }
slf.rwModuleLocker.RUnlock()
return ret return ret
} }
func (slf *BaseModule) genModuleId() uint32 { func (slf *BaseModule) genModuleId() uint32 {
slf.rwModuleLocker.Lock()
slf.CurrMaxModuleId++ slf.CurrMaxModuleId++
return slf.CurrMaxModuleId moduleId := slf.CurrMaxModuleId
slf.rwModuleLocker.Unlock()
return moduleId
}
func (slf *BaseModule) RemoveModule(moduleId uint32) bool {
slf.rwModuleLocker.Lock()
_, ok := slf.mapModule[moduleId]
if ok == false {
GetLogger().Printf(LEVER_WARN, "RemoveModule fail %d...", moduleId)
slf.rwModuleLocker.Unlock()
return false
}
delete(slf.mapModule, moduleId)
slf.rwModuleLocker.Unlock()
return true
}
func (slf *BaseModule) IsRoot() bool {
return slf.GetOwner().GetModuleById(slf.GetModuleId()) == nil
} }
func (slf *BaseModule) AddModule(module IModule) uint32 { func (slf *BaseModule) AddModule(module IModule) uint32 {
@@ -188,19 +225,31 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
module.SetModuleId(slf.genModuleId()) module.SetModuleId(slf.genModuleId())
} }
module.SetOwnerService(slf.ownerService) if slf.GetOwner() != nil {
if slf.IsRoot() {
module.SetOwner(slf.GetOwner())
} else {
module.SetOwner(slf.GetOwner().GetModuleById(slf.GetModuleId()))
}
}
module.InitModule(slf.ExitChan, slf.WaitGroup) module.InitModule(slf.ExitChan, slf.WaitGroup)
slf.rwModuleLocker.Lock()
if slf.mapModule == nil { if slf.mapModule == nil {
slf.mapModule = make(map[uint32]IModule) slf.mapModule = make(map[uint32]IModule)
} }
_, ok := slf.mapModule[module.GetModuleId()] _, ok := slf.mapModule[module.GetModuleId()]
if ok == true { if ok == true {
slf.rwModuleLocker.Unlock()
return 0 return 0
} }
slf.mapModule[module.GetModuleId()] = module slf.mapModule[module.GetModuleId()] = module
slf.rwModuleLocker.Unlock()
go module.RunModule(module) go module.RunModule(module)
return module.GetModuleId() return module.GetModuleId()
@@ -214,6 +263,15 @@ func (slf *BaseModule) OnRun() bool {
return false return false
} }
func (slf *BaseModule) SetOwner(ownerModule IModule) {
slf.ownerModule = ownerModule
}
func (slf *BaseModule) GetOwner() IModule {
return slf.ownerModule
}
func (slf *BaseModule) GetOwnerService() IService { func (slf *BaseModule) GetOwnerService() IService {
return slf.ownerService return slf.ownerService
} }
@@ -229,14 +287,22 @@ func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) er
return nil return nil
} }
func (slf *BaseModule) getBaseModule() *BaseModule {
return slf
}
func (slf *BaseModule) RunModule(module IModule) error { func (slf *BaseModule) RunModule(module IModule) error {
module.OnInit() module.OnInit()
//运行所有子模块 //运行所有子模块
for _, subModule := range slf.mapModule { slf.rwModuleLocker.RLock()
go subModule.RunModule(subModule)
} /*
for _, subModule := range slf.mapModule {
go subModule.RunModule(subModule)
}*/
slf.rwModuleLocker.RUnlock()
slf.WaitGroup.Add(1) slf.WaitGroup.Add(1)
defer slf.WaitGroup.Done() defer slf.WaitGroup.Done()