mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 23:24:45 +08:00
优化module功能
This commit is contained in:
@@ -30,7 +30,6 @@ type COriginNode struct {
|
|||||||
|
|
||||||
func (s *COriginNode) Init() {
|
func (s *COriginNode) Init() {
|
||||||
//初始化全局模块
|
//初始化全局模块
|
||||||
service.InitLog()
|
|
||||||
imodule := g_module.GetModuleById(sysmodule.SYS_LOG)
|
imodule := g_module.GetModuleById(sysmodule.SYS_LOG)
|
||||||
service.InstanceServiceMgr().Init(imodule.(service.ILogger), s.exitChan, s.waitGroup)
|
service.InstanceServiceMgr().Init(imodule.(service.ILogger), s.exitChan, s.waitGroup)
|
||||||
|
|
||||||
|
|||||||
@@ -7,47 +7,49 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
//ModuleNone ...
|
||||||
|
MAX_ALLOW_SET_MODULE_ID = iota + 100000000
|
||||||
|
INIT_AUTO_INCREMENT
|
||||||
|
)
|
||||||
|
|
||||||
type IModule interface {
|
type IModule interface {
|
||||||
SetModuleId(moduleId uint32) bool
|
OnInit() error //Module初始化时调用
|
||||||
GetModuleId() uint32
|
OnRun() bool //Module运行时调用
|
||||||
GetModuleById(moduleId uint32) IModule
|
|
||||||
AddModule(module IModule) uint32
|
|
||||||
RunModule(module IModule)
|
|
||||||
InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error
|
|
||||||
|
|
||||||
OnInit() error
|
SetModuleId(moduleId uint32) //手动设置ModuleId
|
||||||
OnRun() bool
|
GetModuleId() uint32 //获取ModuleId
|
||||||
|
GetModuleById(moduleId uint32) IModule //通过ModuleId获取Module
|
||||||
|
AddModule(module IModule) uint32 //添加Module
|
||||||
|
ReleaseModule(moduleId uint32) bool //释放Module
|
||||||
|
|
||||||
GetOwnerService() IService
|
SetSelf(module IModule) //设置保存自己interface
|
||||||
SetOwnerService(iservice IService)
|
GetSelf() IModule //获取自己interface
|
||||||
|
SetOwner(module IModule) //设置父Module
|
||||||
|
GetOwner() IModule //获取父Module
|
||||||
|
SetOwnerService(iservice IService) //设置拥有者服务
|
||||||
|
GetOwnerService() IService //获取拥有者服务
|
||||||
|
GetRoot() IModule //获取Root根Module
|
||||||
|
|
||||||
SetOwner(module IModule)
|
RunModule(module IModule) //手动运行Module
|
||||||
GetOwner() IModule
|
InitModule(exit chan bool, pwaitGroup *sync.WaitGroup) error //手动初始化Module
|
||||||
SetSelf(module IModule)
|
getBaseModule() *BaseModule //获取BaseModule指针
|
||||||
GetSelf() IModule
|
|
||||||
getBaseModule() *BaseModule
|
|
||||||
GetRoot() IModule
|
|
||||||
|
|
||||||
ReleaseModule(moduleId uint32) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseModule struct {
|
type BaseModule struct {
|
||||||
moduleId uint32
|
moduleId uint32
|
||||||
|
|
||||||
tickTime int64
|
|
||||||
|
|
||||||
ExitChan chan bool
|
|
||||||
WaitGroup *sync.WaitGroup
|
|
||||||
|
|
||||||
ownerService IService
|
ownerService IService
|
||||||
mapModule map[uint32]IModule
|
mapModule map[uint32]IModule
|
||||||
ownerModule IModule
|
ownerModule IModule
|
||||||
selfModule IModule
|
selfModule IModule
|
||||||
|
|
||||||
CurrMaxModuleId uint32
|
CurrMaxModuleId uint32
|
||||||
rwModuleLocker *sync.RWMutex
|
corouterstatus int32 //0表示运行状态 //1释放消亡状态
|
||||||
|
|
||||||
corouterstatus int32 //0表示运行状态 //1释放消亡状态
|
rwModuleLocker *sync.RWMutex
|
||||||
|
ExitChan chan bool
|
||||||
|
WaitGroup *sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) GetRoot() IModule {
|
func (slf *BaseModule) GetRoot() IModule {
|
||||||
@@ -66,10 +68,8 @@ func (slf *BaseModule) getLocker() *sync.RWMutex {
|
|||||||
return slf.rwModuleLocker
|
return slf.rwModuleLocker
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) SetModuleId(moduleId uint32) bool {
|
func (slf *BaseModule) SetModuleId(moduleId uint32) {
|
||||||
|
|
||||||
slf.moduleId = moduleId
|
slf.moduleId = moduleId
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) GetModuleId() uint32 {
|
func (slf *BaseModule) GetModuleId() uint32 {
|
||||||
@@ -79,22 +79,21 @@ func (slf *BaseModule) GetModuleId() uint32 {
|
|||||||
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
func (slf *BaseModule) GetModuleById(moduleId uint32) IModule {
|
||||||
locker := slf.GetRoot().getBaseModule().getLocker()
|
locker := slf.GetRoot().getBaseModule().getLocker()
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
defer locker.Unlock()
|
|
||||||
|
|
||||||
ret, ok := slf.mapModule[moduleId]
|
ret, ok := slf.mapModule[moduleId]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
|
|
||||||
|
locker.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
locker.Unlock()
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) genModuleId() uint32 {
|
func (slf *BaseModule) genModuleId() uint32 {
|
||||||
//slf.rwModuleLocker.Lock()
|
|
||||||
slf.CurrMaxModuleId++
|
slf.CurrMaxModuleId++
|
||||||
moduleId := slf.CurrMaxModuleId
|
moduleId := slf.CurrMaxModuleId
|
||||||
//slf.rwModuleLocker.Unlock()
|
|
||||||
|
|
||||||
return moduleId
|
return moduleId
|
||||||
}
|
}
|
||||||
@@ -105,14 +104,13 @@ func (slf *BaseModule) deleteModule(moduleId uint32) bool {
|
|||||||
GetLogger().Printf(LEVER_WARN, "RemoveModule fail %d...", moduleId)
|
GetLogger().Printf(LEVER_WARN, "RemoveModule fail %d...", moduleId)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
//协程退出
|
//协程退出
|
||||||
atomic.AddInt32(&module.getBaseModule().corouterstatus, 1)
|
atomic.AddInt32(&module.getBaseModule().corouterstatus, 1)
|
||||||
module.getBaseModule().ownerService = nil
|
module.getBaseModule().ownerService = nil
|
||||||
module.getBaseModule().mapModule = nil
|
module.getBaseModule().mapModule = nil
|
||||||
module.getBaseModule().ownerModule = nil
|
module.getBaseModule().ownerModule = nil
|
||||||
module.getBaseModule().selfModule = nil
|
module.getBaseModule().selfModule = nil
|
||||||
//module.getBaseModule().ExitChan = nil
|
|
||||||
//module.getBaseModule().WaitGroup = nil
|
|
||||||
|
|
||||||
delete(slf.mapModule, moduleId)
|
delete(slf.mapModule, moduleId)
|
||||||
|
|
||||||
@@ -138,25 +136,15 @@ func (slf *BaseModule) releaseModule(moduleId uint32) bool {
|
|||||||
func (slf *BaseModule) ReleaseModule(moduleId uint32) bool {
|
func (slf *BaseModule) ReleaseModule(moduleId uint32) bool {
|
||||||
locker := slf.GetRoot().getBaseModule().getLocker()
|
locker := slf.GetRoot().getBaseModule().getLocker()
|
||||||
locker.Lock()
|
locker.Lock()
|
||||||
defer locker.Unlock()
|
|
||||||
|
|
||||||
slf.releaseModule(moduleId)
|
slf.releaseModule(moduleId)
|
||||||
|
locker.Unlock()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseModule) IsRoot() bool {
|
func (slf *BaseModule) IsRoot() bool {
|
||||||
return slf.GetOwner() == slf.GetSelf()
|
return slf.GetOwner() == slf.GetSelf()
|
||||||
|
|
||||||
// return slf.GetOwner().GetModuleById(slf.GetModuleId()) == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
//ModuleNone ...
|
|
||||||
MAX_ALLOW_SET_MODULE_ID = iota + 100000000
|
|
||||||
INIT_AUTO_INCREMENT
|
|
||||||
)
|
|
||||||
|
|
||||||
func (slf *BaseModule) GetSelf() IModule {
|
func (slf *BaseModule) GetSelf() IModule {
|
||||||
if slf.selfModule == nil {
|
if slf.selfModule == nil {
|
||||||
return slf
|
return slf
|
||||||
@@ -168,11 +156,13 @@ func (slf *BaseModule) GetSelf() IModule {
|
|||||||
func (slf *BaseModule) AddModule(module IModule) uint32 {
|
func (slf *BaseModule) AddModule(module IModule) uint32 {
|
||||||
//消亡状态不允许加入模块
|
//消亡状态不允许加入模块
|
||||||
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
||||||
|
GetLogger().Printf(LEVER_ERROR, "%T Cannot AddModule %T", slf.GetSelf(), module.GetSelf())
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//用户设置的id不允许大于MAX_ALLOW_SET_MODULE_ID
|
//用户设置的id不允许大于MAX_ALLOW_SET_MODULE_ID
|
||||||
if module.GetModuleId() > MAX_ALLOW_SET_MODULE_ID {
|
if module.GetModuleId() > MAX_ALLOW_SET_MODULE_ID {
|
||||||
|
GetLogger().Printf(LEVER_ERROR, "Module Id %d is error %T", module.GetModuleId(), module.GetSelf())
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,12 +260,13 @@ func (slf *BaseModule) RunModule(module IModule) {
|
|||||||
defer slf.WaitGroup.Done()
|
defer slf.WaitGroup.Done()
|
||||||
for {
|
for {
|
||||||
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
if atomic.LoadInt32(&slf.corouterstatus) != 0 {
|
||||||
|
GetLogger().Printf(LEVER_INFO, "Stopping module %T id is %d...", slf.GetSelf(), module.GetModuleId())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-slf.ExitChan:
|
case <-slf.ExitChan:
|
||||||
GetLogger().Printf(LEVER_WARN, "Stopping module %T...", slf.GetSelf())
|
GetLogger().Printf(LEVER_INFO, "Stopping module %T...", slf.GetSelf())
|
||||||
fmt.Println("Stopping module %T...", slf.GetSelf())
|
fmt.Println("Stopping module %T...", slf.GetSelf())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
@@ -284,6 +275,5 @@ func (slf *BaseModule) RunModule(module IModule) {
|
|||||||
if module.OnRun() == false {
|
if module.OnRun() == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//slf.tickTime = time.Now().UnixNano() / 1e6
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MethodInfo struct {
|
type MethodInfo struct {
|
||||||
@@ -27,25 +24,10 @@ type IService interface {
|
|||||||
GetServiceName() string
|
GetServiceName() string
|
||||||
SetServiceName(serviceName string) bool
|
SetServiceName(serviceName string) bool
|
||||||
GetServiceId() int
|
GetServiceId() int
|
||||||
IsTimeOutTick(microSecond int64) bool
|
|
||||||
|
|
||||||
GetStatus() int
|
GetStatus() int
|
||||||
}
|
}
|
||||||
|
|
||||||
var Log *log.Logger
|
|
||||||
var logFile *os.File
|
|
||||||
|
|
||||||
func InitLog() {
|
|
||||||
fileName := "system-" + time.Now().Format("2006-01-02") + ".log"
|
|
||||||
var err error
|
|
||||||
logFile, err = os.Create(fileName)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("open file error")
|
|
||||||
}
|
|
||||||
Log = log.New(logFile, "", log.Lshortfile|log.LstdFlags)
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseService struct {
|
type BaseService struct {
|
||||||
BaseModule
|
BaseModule
|
||||||
|
|
||||||
@@ -76,12 +58,9 @@ func (slf *BaseService) OnFetchService(iservice IService) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) OnSetupService(iservice IService) {
|
func (slf *BaseService) OnSetupService(iservice IService) {
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) OnRemoveService(iservice IService) {
|
func (slf *BaseService) OnRemoveService(iservice IService) {
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) Init(iservice IService) error {
|
func (slf *BaseService) Init(iservice IService) error {
|
||||||
@@ -99,9 +78,3 @@ func (slf *BaseService) Init(iservice IService) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *BaseService) IsTimeOutTick(microSecond int64) bool {
|
|
||||||
|
|
||||||
nowtm := time.Now().UnixNano() / 1e6
|
|
||||||
return nowtm-slf.tickTime >= microSecond
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user