mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化module功能,新增getowner功能
This commit is contained in:
@@ -29,11 +29,7 @@ type COriginNode struct {
|
||||
}
|
||||
|
||||
func (s *COriginNode) Init() {
|
||||
//s.exit = make(chan bool)
|
||||
//s.waitGroup = &sync.WaitGroup{}
|
||||
|
||||
//初始化全局模块
|
||||
|
||||
service.InitLog()
|
||||
imodule := g_module.GetModuleById(sysmodule.SYS_LOG)
|
||||
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].(service.IModule).SetOwnerService(services[i])
|
||||
services[i].(service.IModule).SetOwner(services[i].(service.IModule))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,6 +31,11 @@ type IModule interface {
|
||||
|
||||
GetOwnerService() IService
|
||||
SetOwnerService(iservice IService)
|
||||
|
||||
SetOwner(module IModule)
|
||||
GetOwner() IModule
|
||||
|
||||
getBaseModule() *BaseModule
|
||||
}
|
||||
|
||||
type IService interface {
|
||||
@@ -74,8 +79,7 @@ type BaseService struct {
|
||||
}
|
||||
|
||||
type BaseModule struct {
|
||||
moduleId uint32
|
||||
mapModule map[uint32]IModule
|
||||
moduleId uint32
|
||||
|
||||
ownerService IService
|
||||
tickTime int64
|
||||
@@ -83,7 +87,12 @@ type BaseModule struct {
|
||||
ExitChan chan bool
|
||||
WaitGroup *sync.WaitGroup
|
||||
|
||||
mapModule map[uint32]IModule
|
||||
CurrMaxModuleId uint32
|
||||
rwModuleLocker sync.RWMutex
|
||||
|
||||
selfModule IModule
|
||||
ownerModule IModule
|
||||
}
|
||||
|
||||
func (slf *BaseService) GetServiceId() int {
|
||||
@@ -112,6 +121,7 @@ func (slf *BaseService) OnFetchService(iservice IService) error {
|
||||
}
|
||||
|
||||
func (slf *BaseService) OnSetupService(iservice IService) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -162,17 +172,44 @@ func (slf *BaseModule) GetModuleId() uint32 {
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
||||
slf.rwModuleLocker.RLock()
|
||||
ret, ok := slf.mapModule[moduleId]
|
||||
if ok == false {
|
||||
slf.rwModuleLocker.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
slf.rwModuleLocker.RUnlock()
|
||||
return ret
|
||||
}
|
||||
|
||||
func (slf *BaseModule) genModuleId() uint32 {
|
||||
|
||||
slf.rwModuleLocker.Lock()
|
||||
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 {
|
||||
@@ -188,19 +225,31 @@ func (slf *BaseModule) AddModule(module IModule) uint32 {
|
||||
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)
|
||||
|
||||
slf.rwModuleLocker.Lock()
|
||||
|
||||
if slf.mapModule == nil {
|
||||
slf.mapModule = make(map[uint32]IModule)
|
||||
}
|
||||
|
||||
_, ok := slf.mapModule[module.GetModuleId()]
|
||||
if ok == true {
|
||||
slf.rwModuleLocker.Unlock()
|
||||
return 0
|
||||
}
|
||||
|
||||
slf.mapModule[module.GetModuleId()] = module
|
||||
slf.rwModuleLocker.Unlock()
|
||||
|
||||
go module.RunModule(module)
|
||||
return module.GetModuleId()
|
||||
@@ -214,6 +263,15 @@ func (slf *BaseModule) OnRun() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *BaseModule) SetOwner(ownerModule IModule) {
|
||||
slf.ownerModule = ownerModule
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetOwner() IModule {
|
||||
|
||||
return slf.ownerModule
|
||||
}
|
||||
|
||||
func (slf *BaseModule) GetOwnerService() IService {
|
||||
return slf.ownerService
|
||||
}
|
||||
@@ -229,14 +287,22 @@ func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *BaseModule) getBaseModule() *BaseModule {
|
||||
return slf
|
||||
}
|
||||
|
||||
func (slf *BaseModule) RunModule(module IModule) error {
|
||||
|
||||
module.OnInit()
|
||||
|
||||
//运行所有子模块
|
||||
for _, subModule := range slf.mapModule {
|
||||
go subModule.RunModule(subModule)
|
||||
}
|
||||
slf.rwModuleLocker.RLock()
|
||||
|
||||
/*
|
||||
for _, subModule := range slf.mapModule {
|
||||
go subModule.RunModule(subModule)
|
||||
}*/
|
||||
slf.rwModuleLocker.RUnlock()
|
||||
|
||||
slf.WaitGroup.Add(1)
|
||||
defer slf.WaitGroup.Done()
|
||||
|
||||
Reference in New Issue
Block a user