mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
module exit error
This commit is contained in:
@@ -2,31 +2,33 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/event"
|
||||
"github.com/duanhf2012/origin/log"
|
||||
rpcHandle "github.com/duanhf2012/origin/rpc"
|
||||
"github.com/duanhf2012/origin/util/timer"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const InitModuleId = 1e9
|
||||
|
||||
type IModule interface {
|
||||
SetModuleId(moduleId uint32) bool
|
||||
GetModuleId() uint32
|
||||
AddModule(module IModule) (uint32,error)
|
||||
AddModule(module IModule) (uint32, error)
|
||||
GetModule(moduleId uint32) IModule
|
||||
GetAncestor()IModule
|
||||
GetAncestor() IModule
|
||||
ReleaseModule(moduleId uint32)
|
||||
NewModuleId() uint32
|
||||
GetParent()IModule
|
||||
GetParent() IModule
|
||||
OnInit() error
|
||||
OnRelease()
|
||||
getBaseModule() IModule
|
||||
GetService() IService
|
||||
GetModuleName() string
|
||||
GetEventProcessor()event.IEventProcessor
|
||||
GetEventProcessor() event.IEventProcessor
|
||||
NotifyEvent(ev event.IEvent)
|
||||
}
|
||||
|
||||
@@ -56,7 +58,7 @@ type Module struct {
|
||||
eventHandler event.IEventHandler
|
||||
}
|
||||
|
||||
func (m *Module) SetModuleId(moduleId uint32) bool{
|
||||
func (m *Module) SetModuleId(moduleId uint32) bool {
|
||||
if m.moduleId > 0 {
|
||||
return false
|
||||
}
|
||||
@@ -65,35 +67,35 @@ func (m *Module) SetModuleId(moduleId uint32) bool{
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *Module) GetModuleId() uint32{
|
||||
func (m *Module) GetModuleId() uint32 {
|
||||
return m.moduleId
|
||||
}
|
||||
|
||||
func (m *Module) GetModuleName() string{
|
||||
func (m *Module) GetModuleName() string {
|
||||
return m.moduleName
|
||||
}
|
||||
|
||||
func (m *Module) OnInit() error{
|
||||
func (m *Module) OnInit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Module) AddModule(module IModule) (uint32,error){
|
||||
func (m *Module) AddModule(module IModule) (uint32, error) {
|
||||
//没有事件处理器不允许加入其他模块
|
||||
if m.GetEventProcessor() == nil {
|
||||
return 0,fmt.Errorf("module %+v Event Processor is nil", m.self)
|
||||
return 0, fmt.Errorf("module %+v Event Processor is nil", m.self)
|
||||
}
|
||||
|
||||
pAddModule := module.getBaseModule().(*Module)
|
||||
if pAddModule.GetModuleId()==0 {
|
||||
if pAddModule.GetModuleId() == 0 {
|
||||
pAddModule.moduleId = m.NewModuleId()
|
||||
}
|
||||
|
||||
if m.child == nil {
|
||||
m.child = map[uint32]IModule{}
|
||||
}
|
||||
_,ok := m.child[module.GetModuleId()]
|
||||
_, ok := m.child[module.GetModuleId()]
|
||||
if ok == true {
|
||||
return 0,fmt.Errorf("exists module id %d",module.GetModuleId())
|
||||
return 0, fmt.Errorf("exists module id %d", module.GetModuleId())
|
||||
}
|
||||
pAddModule.IRpcHandler = m.IRpcHandler
|
||||
pAddModule.self = module
|
||||
@@ -105,17 +107,17 @@ func (m *Module) AddModule(module IModule) (uint32,error){
|
||||
pAddModule.eventHandler.Init(m.eventHandler.GetEventProcessor())
|
||||
err := module.OnInit()
|
||||
if err != nil {
|
||||
return 0,err
|
||||
return 0, err
|
||||
}
|
||||
|
||||
m.child[module.GetModuleId()] = module
|
||||
m.ancestor.getBaseModule().(*Module).descendants[module.GetModuleId()] = module
|
||||
|
||||
log.SDebug("Add module ",module.GetModuleName()," completed")
|
||||
return module.GetModuleId(),nil
|
||||
log.SDebug("Add module ", module.GetModuleName(), " completed")
|
||||
return module.GetModuleId(), nil
|
||||
}
|
||||
|
||||
func (m *Module) ReleaseModule(moduleId uint32){
|
||||
func (m *Module) ReleaseModule(moduleId uint32) {
|
||||
pModule := m.GetModule(moduleId).getBaseModule().(*Module)
|
||||
|
||||
//释放子孙
|
||||
@@ -123,19 +125,19 @@ func (m *Module) ReleaseModule(moduleId uint32){
|
||||
m.ReleaseModule(id)
|
||||
}
|
||||
|
||||
pModule.GetEventHandler().Destroy()
|
||||
pModule.self.OnRelease()
|
||||
pModule.GetEventHandler().Destroy()
|
||||
log.SDebug("Release module ", pModule.GetModuleName())
|
||||
for pTimer := range pModule.mapActiveTimer {
|
||||
pTimer.Cancel()
|
||||
}
|
||||
|
||||
for _,t := range pModule.mapActiveIdTimer {
|
||||
for _, t := range pModule.mapActiveIdTimer {
|
||||
t.Cancel()
|
||||
}
|
||||
|
||||
delete(m.child,moduleId)
|
||||
delete (m.ancestor.getBaseModule().(*Module).descendants,moduleId)
|
||||
delete(m.child, moduleId)
|
||||
delete(m.ancestor.getBaseModule().(*Module).descendants, moduleId)
|
||||
|
||||
//清理被删除的Module
|
||||
pModule.self = nil
|
||||
@@ -149,16 +151,17 @@ func (m *Module) ReleaseModule(moduleId uint32){
|
||||
pModule.mapActiveIdTimer = nil
|
||||
}
|
||||
|
||||
func (m *Module) NewModuleId() uint32{
|
||||
m.ancestor.getBaseModule().(*Module).seedModuleId+=1
|
||||
func (m *Module) NewModuleId() uint32 {
|
||||
m.ancestor.getBaseModule().(*Module).seedModuleId += 1
|
||||
return m.ancestor.getBaseModule().(*Module).seedModuleId
|
||||
}
|
||||
|
||||
var timerSeedId uint32
|
||||
func (m *Module) GenTimerId() uint64{
|
||||
for{
|
||||
newTimerId := (uint64(m.GetModuleId())<<32)|uint64(atomic.AddUint32(&timerSeedId,1))
|
||||
if _,ok := m.mapActiveIdTimer[newTimerId];ok == true {
|
||||
|
||||
func (m *Module) GenTimerId() uint64 {
|
||||
for {
|
||||
newTimerId := (uint64(m.GetModuleId()) << 32) | uint64(atomic.AddUint32(&timerSeedId, 1))
|
||||
if _, ok := m.mapActiveIdTimer[newTimerId]; ok == true {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -166,33 +169,32 @@ func (m *Module) GenTimerId() uint64{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (m *Module) GetAncestor()IModule{
|
||||
func (m *Module) GetAncestor() IModule {
|
||||
return m.ancestor
|
||||
}
|
||||
|
||||
func (m *Module) GetModule(moduleId uint32) IModule{
|
||||
iModule,ok := m.GetAncestor().getBaseModule().(*Module).descendants[moduleId]
|
||||
func (m *Module) GetModule(moduleId uint32) IModule {
|
||||
iModule, ok := m.GetAncestor().getBaseModule().(*Module).descendants[moduleId]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
return iModule
|
||||
}
|
||||
|
||||
func (m *Module) getBaseModule() IModule{
|
||||
func (m *Module) getBaseModule() IModule {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *Module) GetParent()IModule{
|
||||
func (m *Module) GetParent() IModule {
|
||||
return m.parent
|
||||
}
|
||||
|
||||
func (m *Module) OnCloseTimer(t timer.ITimer){
|
||||
delete(m.mapActiveIdTimer,t.GetId())
|
||||
delete(m.mapActiveTimer,t)
|
||||
func (m *Module) OnCloseTimer(t timer.ITimer) {
|
||||
delete(m.mapActiveIdTimer, t.GetId())
|
||||
delete(m.mapActiveTimer, t)
|
||||
}
|
||||
|
||||
func (m *Module) OnAddTimer(t timer.ITimer){
|
||||
func (m *Module) OnAddTimer(t timer.ITimer) {
|
||||
if t != nil {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
@@ -204,33 +206,33 @@ func (m *Module) OnAddTimer(t timer.ITimer){
|
||||
|
||||
func (m *Module) AfterFunc(d time.Duration, cb func(*timer.Timer)) *timer.Timer {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer =map[timer.ITimer]struct{}{}
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
}
|
||||
|
||||
return m.dispatcher.AfterFunc(d,nil,cb,m.OnCloseTimer,m.OnAddTimer)
|
||||
return m.dispatcher.AfterFunc(d, nil, cb, m.OnCloseTimer, m.OnAddTimer)
|
||||
}
|
||||
|
||||
func (m *Module) CronFunc(cronExpr *timer.CronExpr, cb func(*timer.Cron)) *timer.Cron {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer =map[timer.ITimer]struct{}{}
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
}
|
||||
|
||||
return m.dispatcher.CronFunc(cronExpr,nil,cb,m.OnCloseTimer,m.OnAddTimer)
|
||||
return m.dispatcher.CronFunc(cronExpr, nil, cb, m.OnCloseTimer, m.OnAddTimer)
|
||||
}
|
||||
|
||||
func (m *Module) NewTicker(d time.Duration, cb func(*timer.Ticker)) *timer.Ticker {
|
||||
if m.mapActiveTimer == nil {
|
||||
m.mapActiveTimer =map[timer.ITimer]struct{}{}
|
||||
m.mapActiveTimer = map[timer.ITimer]struct{}{}
|
||||
}
|
||||
|
||||
return m.dispatcher.TickerFunc(d,nil,cb,m.OnCloseTimer,m.OnAddTimer)
|
||||
return m.dispatcher.TickerFunc(d, nil, cb, m.OnCloseTimer, m.OnAddTimer)
|
||||
}
|
||||
|
||||
func (m *Module) cb(*timer.Timer){
|
||||
func (m *Module) cb(*timer.Timer) {
|
||||
|
||||
}
|
||||
|
||||
func (m *Module) SafeAfterFunc(timerId *uint64,d time.Duration, AdditionData interface{},cb func(uint64,interface{})) {
|
||||
func (m *Module) SafeAfterFunc(timerId *uint64, d time.Duration, AdditionData interface{}, cb func(uint64, interface{})) {
|
||||
if m.mapActiveIdTimer == nil {
|
||||
m.mapActiveIdTimer = map[uint64]timer.ITimer{}
|
||||
}
|
||||
@@ -240,45 +242,45 @@ func (m *Module) SafeAfterFunc(timerId *uint64,d time.Duration, AdditionData int
|
||||
}
|
||||
|
||||
*timerId = m.GenTimerId()
|
||||
t := m.dispatcher.AfterFunc(d,cb,nil,m.OnCloseTimer,m.OnAddTimer)
|
||||
t := m.dispatcher.AfterFunc(d, cb, nil, m.OnCloseTimer, m.OnAddTimer)
|
||||
t.AdditionData = AdditionData
|
||||
t.Id = *timerId
|
||||
m.mapActiveIdTimer[*timerId] = t
|
||||
}
|
||||
|
||||
func (m *Module) SafeCronFunc(cronId *uint64,cronExpr *timer.CronExpr, AdditionData interface{}, cb func(uint64,interface{})) {
|
||||
func (m *Module) SafeCronFunc(cronId *uint64, cronExpr *timer.CronExpr, AdditionData interface{}, cb func(uint64, interface{})) {
|
||||
if m.mapActiveIdTimer == nil {
|
||||
m.mapActiveIdTimer = map[uint64]timer.ITimer{}
|
||||
}
|
||||
|
||||
*cronId = m.GenTimerId()
|
||||
c := m.dispatcher.CronFunc(cronExpr,cb,nil,m.OnCloseTimer,m.OnAddTimer)
|
||||
c := m.dispatcher.CronFunc(cronExpr, cb, nil, m.OnCloseTimer, m.OnAddTimer)
|
||||
c.AdditionData = AdditionData
|
||||
c.Id = *cronId
|
||||
m.mapActiveIdTimer[*cronId] = c
|
||||
}
|
||||
|
||||
func (m *Module) SafeNewTicker(tickerId *uint64,d time.Duration, AdditionData interface{}, cb func(uint64,interface{})) {
|
||||
func (m *Module) SafeNewTicker(tickerId *uint64, d time.Duration, AdditionData interface{}, cb func(uint64, interface{})) {
|
||||
if m.mapActiveIdTimer == nil {
|
||||
m.mapActiveIdTimer = map[uint64]timer.ITimer{}
|
||||
}
|
||||
|
||||
*tickerId = m.GenTimerId()
|
||||
t := m.dispatcher.TickerFunc(d,cb,nil,m.OnCloseTimer,m.OnAddTimer)
|
||||
t := m.dispatcher.TickerFunc(d, cb, nil, m.OnCloseTimer, m.OnAddTimer)
|
||||
t.AdditionData = AdditionData
|
||||
t.Id = *tickerId
|
||||
m.mapActiveIdTimer[*tickerId] = t
|
||||
}
|
||||
|
||||
func (m *Module) CancelTimerId(timerId *uint64) bool{
|
||||
func (m *Module) CancelTimerId(timerId *uint64) bool {
|
||||
if m.mapActiveIdTimer == nil {
|
||||
log.SError("mapActiveIdTimer is nil")
|
||||
return false
|
||||
}
|
||||
|
||||
t,ok := m.mapActiveIdTimer[*timerId]
|
||||
t, ok := m.mapActiveIdTimer[*timerId]
|
||||
if ok == false {
|
||||
log.SError("cannot find timer id ",timerId)
|
||||
log.SError("cannot find timer id ", timerId)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -287,23 +289,21 @@ func (m *Module) CancelTimerId(timerId *uint64) bool{
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (m *Module) OnRelease(){
|
||||
func (m *Module) OnRelease() {
|
||||
}
|
||||
|
||||
func (m *Module) GetService() IService {
|
||||
return m.GetAncestor().(IService)
|
||||
}
|
||||
|
||||
func (m *Module) GetEventProcessor() event.IEventProcessor{
|
||||
func (m *Module) GetEventProcessor() event.IEventProcessor {
|
||||
return m.eventHandler.GetEventProcessor()
|
||||
}
|
||||
|
||||
func (m *Module) NotifyEvent(ev event.IEvent){
|
||||
func (m *Module) NotifyEvent(ev event.IEvent) {
|
||||
m.eventHandler.NotifyEvent(ev)
|
||||
}
|
||||
|
||||
func (m *Module) GetEventHandler() event.IEventHandler{
|
||||
func (m *Module) GetEventHandler() event.IEventHandler {
|
||||
return m.eventHandler
|
||||
}
|
||||
Reference in New Issue
Block a user