From 9384227cf64b2403b56f2d2c664bccc1f39433f4 Mon Sep 17 00:00:00 2001 From: boyce Date: Wed, 20 Feb 2019 15:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9module=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- originnode/global.go | 20 +++--- originnode/node.go | 27 +++++--- service/Service.go | 137 +++++++++++++++++++------------------- service/servicemanager.go | 18 ++--- 4 files changed, 99 insertions(+), 103 deletions(-) diff --git a/originnode/global.go b/originnode/global.go index b181c9e..2fbaf22 100644 --- a/originnode/global.go +++ b/originnode/global.go @@ -15,20 +15,16 @@ type GlobalModule struct { var g_module GlobalModule -func AddModule(module service.IModule) bool { - if module.GetModuleType() == 0 { - return false - } - +func AddModule(module service.IModule) uint32 { return g_module.AddModule(module) } -func GetModuleByType(moduleType uint32) service.IModule { - return g_module.GetModuleByType(moduleType) +func GetModuleById(moduleId uint32) service.IModule { + return g_module.GetModuleById(moduleId) } func GetLog(logmodule uint32) sysmodule.ILogger { - module := g_module.GetModuleByType(logmodule) + module := g_module.GetModuleById(logmodule) if nil == module { return nil } @@ -36,10 +32,10 @@ func GetLog(logmodule uint32) sysmodule.ILogger { return module.(sysmodule.ILogger) } -func InitGlobalModule() { - g_module.InitModule(&g_module) +func InitGlobalModule(exit chan bool, pwaitGroup *sync.WaitGroup) { + g_module.InitModule(exit, pwaitGroup) } -func RunGlobalModule(exit chan bool, pwaitGroup *sync.WaitGroup) { - g_module.RunModule(&g_module, exit, pwaitGroup) +func RunGlobalModule() { + g_module.RunModule(&g_module) } diff --git a/originnode/node.go b/originnode/node.go index a408156..c1bbe5f 100644 --- a/originnode/node.go +++ b/originnode/node.go @@ -29,14 +29,15 @@ type COriginNode struct { } func (s *COriginNode) Init() { - //初始化全局模块 - InitGlobalModule() - service.InitLog() - imodule := g_module.GetModuleByType(sysmodule.SYS_LOG) - service.InstanceServiceMgr().Init(imodule.(service.ILogger)) + //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) - s.exit = make(chan bool) - s.waitGroup = &sync.WaitGroup{} s.sigs = make(chan os.Signal, 1) signal.Notify(s.sigs, syscall.SIGINT, syscall.SIGTERM) } @@ -79,8 +80,8 @@ func (s *COriginNode) Start() { } cluster.InstanceClusterMgr().Start() - RunGlobalModule(s.exit, s.waitGroup) - service.InstanceServiceMgr().Start(s.exit, s.waitGroup) + RunGlobalModule() + service.InstanceServiceMgr().Start() select { case <-s.sigs: @@ -96,9 +97,13 @@ func (s *COriginNode) Stop() { } func NewOrginNode() *COriginNode { + node := new(COriginNode) + node.exit = make(chan bool) + node.waitGroup = &sync.WaitGroup{} + InitGlobalModule(node.exit, node.waitGroup) var syslogmodule sysmodule.LogModule syslogmodule.Init("system", sysmodule.LEVER_INFO) - syslogmodule.SetModuleType(sysmodule.SYS_LOG) + syslogmodule.SetModuleId(sysmodule.SYS_LOG) AddModule(&syslogmodule) err := cluster.InstanceClusterMgr().Init() @@ -107,7 +112,7 @@ func NewOrginNode() *COriginNode { return nil } - return new(COriginNode) + return node } func HasCmdParam(param string) bool { diff --git a/service/Service.go b/service/Service.go index 58326d8..4a6fc7b 100644 --- a/service/Service.go +++ b/service/Service.go @@ -18,16 +18,17 @@ type MethodInfo struct { } type IModule interface { - SetModuleType(moduleType uint32) - GetModuleType() uint32 - DynamicRun(module IModule) - RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error - InitModule(module IModule) error + SetModuleId(moduleId uint32) bool + GetModuleId() uint32 + GetModuleById(moduleId uint32) IModule + AddModule(module IModule) uint32 + //DynamicAddModule(module IModule) uint32 + + RunModule(module IModule) error + InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error OnInit() error OnRun() bool - OnEndRun() - AddModule(module IModule) bool - GetModuleByType(moduleType uint32) IModule + GetOwnerService() IService SetOwnerService(iservice IService) } @@ -35,7 +36,6 @@ type IModule interface { type IService interface { Init(Iservice IService, servicetype int) error OnInit() error - OnEndInit() error OnRun() bool OnFetchService(iservice IService) error OnSetupService(iservice IService) //其他服务被安装 @@ -70,19 +70,20 @@ type BaseService struct { serviceid int servicename string servicetype int - - Status int + Status int } type BaseModule struct { - moduleType uint32 - mapModule map[uint32]IModule + moduleId uint32 + mapModule map[uint32]IModule ownerService IService tickTime int64 ExitChan chan bool WaitGroup *sync.WaitGroup + + CurrMaxModuleId uint32 } func (slf *BaseService) GetServiceId() int { @@ -106,10 +107,6 @@ func (slf *BaseService) GetStatus() int { return slf.Status } -func (slf *BaseService) OnEndInit() error { - return nil -} - func (slf *BaseService) OnFetchService(iservice IService) error { return nil } @@ -152,39 +149,20 @@ func (slf *BaseService) IsTimeOutTick(microSecond int64) bool { nowtm := time.Now().UnixNano() / 1e6 return nowtm-slf.tickTime >= microSecond - } -func (slf *BaseModule) SetModuleType(moduleType uint32) { - slf.moduleType = moduleType -} +func (slf *BaseModule) SetModuleId(moduleId uint32) bool { -func (slf *BaseModule) GetModuleType() uint32 { - return slf.moduleType -} - -func (slf *BaseModule) AddModule(module IModule) bool { - if module.GetModuleType() == 0 { - return false - } - - module.SetOwnerService(slf.ownerService) - - if slf.mapModule == nil { - slf.mapModule = make(map[uint32]IModule) - } - - _, ok := slf.mapModule[module.GetModuleType()] - if ok == true { - return false - } - - slf.mapModule[module.GetModuleType()] = module + slf.moduleId = moduleId return true } -func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule { - ret, ok := slf.mapModule[moduleType] +func (slf *BaseModule) GetModuleId() uint32 { + return slf.moduleId +} + +func (slf *BaseModule) GetModuleById(moduleId uint32) IModule { + ret, ok := slf.mapModule[moduleId] if ok == false { return nil } @@ -192,18 +170,50 @@ func (slf *BaseModule) GetModuleByType(moduleType uint32) IModule { return ret } +func (slf *BaseModule) genModuleId() uint32 { + slf.CurrMaxModuleId++ + return slf.CurrMaxModuleId +} + +func (slf *BaseModule) AddModule(module IModule) uint32 { + if slf.WaitGroup == nil { + GetLogger().Printf(LEVER_FATAL, "AddModule error %s...", fmt.Sprintf("%T", module)) + } + + if module.GetModuleId() > 100000000 { + return 0 + } + + if module.GetModuleId() == 0 { + module.SetModuleId(slf.genModuleId()) + } + + module.SetOwnerService(slf.ownerService) + module.InitModule(slf.ExitChan, slf.WaitGroup) + + if slf.mapModule == nil { + slf.mapModule = make(map[uint32]IModule) + } + + _, ok := slf.mapModule[module.GetModuleId()] + if ok == true { + return 0 + } + + slf.mapModule[module.GetModuleId()] = module + + go module.RunModule(module) + return module.GetModuleId() +} + func (slf *BaseModule) OnInit() error { - return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleType()) + return fmt.Errorf("not implement OnInit moduletype %d ", slf.GetModuleId()) } func (slf *BaseModule) OnRun() bool { return false } -func (slf *BaseModule) OnEndRun() { - -} - func (slf *BaseModule) GetOwnerService() IService { return slf.ownerService } @@ -212,33 +222,27 @@ func (slf *BaseModule) SetOwnerService(iservice IService) { slf.ownerService = iservice } -func (slf *BaseModule) InitModule(module IModule) error { - module.OnInit() - for _, subModule := range slf.mapModule { - go subModule.OnInit() - } - +func (slf *BaseModule) InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error { + slf.CurrMaxModuleId = 100000 + slf.WaitGroup = pwaitGroup + slf.ExitChan = exit return nil } -func (slf *BaseModule) DynamicRun(module IModule) { - module.InitModule(module) - module.RunModule(module, slf.ExitChan, slf.WaitGroup) -} +func (slf *BaseModule) RunModule(module IModule) error { + + module.OnInit() -func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *sync.WaitGroup) error { - slf.ExitChan = exit - slf.WaitGroup = pwaitGroup //运行所有子模块 for _, subModule := range slf.mapModule { - go subModule.RunModule(subModule, exit, pwaitGroup) + go subModule.RunModule(subModule) } - pwaitGroup.Add(1) - defer pwaitGroup.Done() + slf.WaitGroup.Add(1) + defer slf.WaitGroup.Done() for { select { - case <-exit: + case <-slf.ExitChan: GetLogger().Printf(LEVER_WARN, "stopping module %s...", fmt.Sprintf("%T", slf)) fmt.Println("stopping module %s...", fmt.Sprintf("%T", slf)) return nil @@ -250,6 +254,5 @@ func (slf *BaseModule) RunModule(module IModule, exit chan bool, pwaitGroup *syn slf.tickTime = time.Now().UnixNano() / 1e6 } - module.OnEndRun() return nil } diff --git a/service/servicemanager.go b/service/servicemanager.go index 083d333..2aa4db8 100644 --- a/service/servicemanager.go +++ b/service/servicemanager.go @@ -44,30 +44,22 @@ func (slf *CServiceManager) FetchService(s FetchService) IService { return nil } -func (slf *CServiceManager) Init(logger ILogger) bool { +func (slf *CServiceManager) Init(logger ILogger, exit chan bool, pwaitGroup *sync.WaitGroup) bool { slf.logger = logger for _, s := range slf.localserviceMap { - (s.(IModule)).InitModule(s.(IModule)) - } - - // 初始化结束 - for _, s := range slf.localserviceMap { - if s.OnEndInit() != nil { - return false - } + (s.(IModule)).InitModule(exit, pwaitGroup) + //(s.(IModule)).OnInit() } return true } -func (slf *CServiceManager) Start(exit chan bool, pwaitGroup *sync.WaitGroup) bool { +func (slf *CServiceManager) Start() bool { for _, s := range slf.localserviceMap { - go (s.(IModule)).RunModule(s.(IModule), exit, pwaitGroup) + go (s.(IModule)).RunModule(s.(IModule)) } - pwaitGroup.Add(1) - //go slf.CheckServiceTimeTimeout(exit, pwaitGroup) return true }