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