1
This commit is contained in:
parent
81321f5d0c
commit
0917867fac
65
app.go
65
app.go
@ -9,7 +9,14 @@ import (
|
|||||||
"q5"
|
"q5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App_ struct {
|
type UserApp interface {
|
||||||
|
GetPkgName() string
|
||||||
|
Init()
|
||||||
|
Update()
|
||||||
|
UnInit()
|
||||||
|
}
|
||||||
|
|
||||||
|
type F5App struct {
|
||||||
nodeId int
|
nodeId int
|
||||||
instanceId int
|
instanceId int
|
||||||
terminated bool
|
terminated bool
|
||||||
@ -29,11 +36,11 @@ type App_ struct {
|
|||||||
imMsgHandlers [1024]func(int16, q5.Args)
|
imMsgHandlers [1024]func(int16, q5.Args)
|
||||||
maxRunDelay int64
|
maxRunDelay int64
|
||||||
maxScheduleTime int64
|
maxScheduleTime int64
|
||||||
updateFunc func ()
|
userApp UserApp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) Init(updateFunc func ()) {
|
func (this *F5App) init(userApp UserApp) {
|
||||||
this.updateFunc = updateFunc
|
this.userApp = userApp
|
||||||
this.nowTime = time.Now()
|
this.nowTime = time.Now()
|
||||||
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
|
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
|
||||||
_Timer = &q5.XTimer{}
|
_Timer = &q5.XTimer{}
|
||||||
@ -61,15 +68,17 @@ func (this *App_) Init(updateFunc func ()) {
|
|||||||
os.Getpid())
|
os.Getpid())
|
||||||
this.installTimer()
|
this.installTimer()
|
||||||
go this.goLoopTimer()
|
go this.goLoopTimer()
|
||||||
|
this.userApp.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) UnInit() {
|
func (this *F5App) unInit() {
|
||||||
this.chGoLoopTimerExit <- 1
|
this.chGoLoopTimerExit <- 1
|
||||||
_Timer.UnInit()
|
_Timer.UnInit()
|
||||||
_Timer = nil
|
_Timer = nil
|
||||||
|
this.userApp.UnInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) Run() {
|
func (this *F5App) run() {
|
||||||
for !this.terminated {
|
for !this.terminated {
|
||||||
this.nowTime = time.Now()
|
this.nowTime = time.Now()
|
||||||
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
|
atomic.StoreInt64(&this.nowUnixNano, this.nowTime.UnixNano())
|
||||||
@ -77,7 +86,7 @@ func (this *App_) Run() {
|
|||||||
beginTick := q5.GetTickCount()
|
beginTick := q5.GetTickCount()
|
||||||
_Timer.Update()
|
_Timer.Update()
|
||||||
this.dispatchIMMsg()
|
this.dispatchIMMsg()
|
||||||
this.updateFunc()
|
this.userApp.Update()
|
||||||
this.schedule()
|
this.schedule()
|
||||||
endTick := q5.GetTickCount()
|
endTick := q5.GetTickCount()
|
||||||
|
|
||||||
@ -87,32 +96,32 @@ func (this *App_) Run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) NewUuid() int64 {
|
func (this *F5App) NewUuid() int64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) GetInstanceId() uint32 {
|
func (this *F5App) GetInstanceId() uint32 {
|
||||||
return uint32(this.instanceId)
|
return uint32(this.instanceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) GetNodeId() uint32 {
|
func (this *F5App) GetNodeId() uint32 {
|
||||||
return uint32(this.nodeId)
|
return uint32(this.nodeId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) GetPkgName() string {
|
func (this *F5App) GetPkgName() string {
|
||||||
return this.pkgName
|
return this.pkgName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) SetPkgName(pkgName string) {
|
func (this *F5App) SetPkgName(pkgName string) {
|
||||||
this.pkgName = pkgName
|
this.pkgName = pkgName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) HasFlag(flag int32) bool {
|
func (this *F5App) HasFlag(flag int32) bool {
|
||||||
_, ok := this.flags[flag]
|
_, ok := this.flags[flag]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) AddIMMsg(msgId int16, params q5.Args) {
|
func (this *F5App) AddIMMsg(msgId int16, params q5.Args) {
|
||||||
p := new(IMMsgNode)
|
p := new(IMMsgNode)
|
||||||
p.msgId = msgId
|
p.msgId = msgId
|
||||||
p.params = params
|
p.params = params
|
||||||
@ -130,11 +139,11 @@ func (this *App_) AddIMMsg(msgId int16, params q5.Args) {
|
|||||||
this.loopCond.Broadcast()
|
this.loopCond.Broadcast()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) {
|
func (this *F5App) RegisterIMMsgHandle(msgId int16, handle func(int16,q5.Args)) {
|
||||||
this.imMsgHandlers[msgId] = handle
|
this.imMsgHandlers[msgId] = handle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) goLoopTimer() {
|
func (this *F5App) goLoopTimer() {
|
||||||
var waitMs int64 = 1000 * 10
|
var waitMs int64 = 1000 * 10
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -151,33 +160,33 @@ func (this *App_) goLoopTimer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) schedule() {
|
func (this *F5App) schedule() {
|
||||||
this.chGoLoopWait <- Timer().GetIdleTime()
|
this.chGoLoopWait <- Timer().GetIdleTime()
|
||||||
this.loopCond.L.Lock()
|
this.loopCond.L.Lock()
|
||||||
this.loopCond.Wait()
|
this.loopCond.Wait()
|
||||||
this.loopCond.L.Unlock()
|
this.loopCond.L.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) NotifyLoopCond() {
|
func (this *F5App) NotifyLoopCond() {
|
||||||
this.loopCond.Broadcast()
|
this.loopCond.Broadcast()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) NowUnix() int64 {
|
func (this *F5App) NowUnix() int64 {
|
||||||
return this.nowUnixNano / int64(time.Second)
|
return this.nowUnixNano / int64(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) NowUnixMilli() int64 {
|
func (this *F5App) NowUnixMilli() int64 {
|
||||||
return this.nowUnixNano / int64(time.Millisecond)
|
return this.nowUnixNano / int64(time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) NowUnixNano() int64 {
|
func (this *F5App) NowUnixNano() int64 {
|
||||||
return this.nowUnixNano
|
return this.nowUnixNano
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) outputRuningLog() {
|
func (this *F5App) outputRuningLog() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) dispatchIMMsg() {
|
func (this *F5App) dispatchIMMsg() {
|
||||||
this.imMsgMutex.Lock()
|
this.imMsgMutex.Lock()
|
||||||
this.imWorkNode = this.imTopNode
|
this.imWorkNode = this.imTopNode
|
||||||
this.imTopNode = nil
|
this.imTopNode = nil
|
||||||
@ -193,15 +202,15 @@ func (this *App_) dispatchIMMsg() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *App_) installTimer() {
|
func (this *F5App) installTimer() {
|
||||||
Timer().SetInterval(1000 * 60,
|
Timer().SetInterval(1000 * 60,
|
||||||
func (ev int32, params *q5.Args) {
|
func (ev int32, params *q5.Args) {
|
||||||
if ev == q5.TIMER_EXEC_EVENT {
|
if ev == q5.TIMER_EXEC_EVENT {
|
||||||
SysLog().Info("max_run_delay:%d max_schedule_time:%d",
|
SysLog().Info("max_run_delay:%d max_schedule_time:%d",
|
||||||
App.maxRunDelay,
|
App().maxRunDelay,
|
||||||
App.maxScheduleTime)
|
App().maxScheduleTime)
|
||||||
App.maxRunDelay = 0
|
App().maxRunDelay = 0
|
||||||
App.maxScheduleTime = 0
|
App().maxScheduleTime = 0
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
13
global.go
13
global.go
@ -2,7 +2,7 @@ package f5
|
|||||||
|
|
||||||
import "q5"
|
import "q5"
|
||||||
|
|
||||||
var App *App_
|
var app *F5App
|
||||||
var _Timer *q5.XTimer
|
var _Timer *q5.XTimer
|
||||||
var _SysLog *SysLog_
|
var _SysLog *SysLog_
|
||||||
var _TgLog *TGLog_
|
var _TgLog *TGLog_
|
||||||
@ -18,3 +18,14 @@ func SysLog() *SysLog_ {
|
|||||||
func TgLog() *TGLog_ {
|
func TgLog() *TGLog_ {
|
||||||
return _TgLog
|
return _TgLog
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func App() *F5App {
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
func Run(userApp UserApp) {
|
||||||
|
app = new(F5App)
|
||||||
|
app.init(userApp)
|
||||||
|
app.run()
|
||||||
|
app.unInit()
|
||||||
|
}
|
||||||
|
@ -125,7 +125,7 @@ func (this *SysLog_) goSaveToFile() {
|
|||||||
this.botNode = nil
|
this.botNode = nil
|
||||||
this.msgMutex.Unlock()
|
this.msgMutex.Unlock()
|
||||||
if workNode != nil {
|
if workNode != nil {
|
||||||
logDir := fmt.Sprintf(SYS_LOG_ROOT, App.GetPkgName())
|
logDir := fmt.Sprintf(SYS_LOG_ROOT, App().GetPkgName())
|
||||||
fileName := fmt.Sprintf(TGLOG_FILENAME, os.Getpid(), time.Now().Format("20060102"))
|
fileName := fmt.Sprintf(TGLOG_FILENAME, os.Getpid(), time.Now().Format("20060102"))
|
||||||
q5.ForceCreateDir(logDir)
|
q5.ForceCreateDir(logDir)
|
||||||
if f, err := os.OpenFile(logDir + fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666); err == nil {
|
if f, err := os.OpenFile(logDir + fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666); err == nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user