108 lines
1.9 KiB
Go
108 lines
1.9 KiB
Go
package f5
|
|
|
|
import "flag"
|
|
import "sync"
|
|
import "time"
|
|
import "q5"
|
|
|
|
type App_ struct {
|
|
nodeId int
|
|
instanceId int
|
|
terminated bool
|
|
flags map[int32]int32
|
|
servicing bool
|
|
contextHash map[int32]q5.XParams
|
|
loopCond *sync.Cond
|
|
imTopNode *IMMsgNode
|
|
imBotNode *IMMsgNode
|
|
imWorkNode *IMMsgNode
|
|
imMsgMutex sync.RWMutex
|
|
chGoLoopTimerExit chan int
|
|
imMsgHandlers [1024]func(int16,*q5.XParams)
|
|
}
|
|
|
|
func (this *App_) Init() {
|
|
_Timer = &q5.Timer{}
|
|
_Timer.Init(
|
|
q5.GetTickCount,
|
|
nil,
|
|
1000 * 60,
|
|
5000)
|
|
flag.IntVar(&this.nodeId, "n", 0, "node id")
|
|
flag.IntVar(&this.instanceId, "i", 0, "instance id")
|
|
flag.Parse()
|
|
this.loopCond = sync.NewCond(new(sync.Mutex))
|
|
this.chGoLoopTimerExit = make(chan int)
|
|
go this.goLoopTimer()
|
|
}
|
|
|
|
func (this *App_) UnInit() {
|
|
this.chGoLoopTimerExit <- 1
|
|
_Timer.UnInit()
|
|
_Timer = nil
|
|
}
|
|
|
|
func (this *App_) Run() {
|
|
for !this.terminated {
|
|
_Timer.Update()
|
|
this.schedule()
|
|
}
|
|
}
|
|
|
|
func (this *App_) NewUuid() int64 {
|
|
return 0
|
|
}
|
|
|
|
func (this *App_) GetInstanceId() uint32 {
|
|
return uint32(this.instanceId)
|
|
}
|
|
|
|
func (this *App_) GetNodeId() uint32 {
|
|
return uint32(this.nodeId)
|
|
}
|
|
|
|
func (this *App_) HasFlag(flag int32) bool {
|
|
_, ok := this.flags[flag]
|
|
return ok
|
|
}
|
|
|
|
func (this *App_) AddIMMsg(msgId int16, params *q5.XParams) {
|
|
p := new(IMMsgNode)
|
|
p.msgId = msgId
|
|
p.params = params
|
|
|
|
{
|
|
this.imMsgMutex.Lock()
|
|
defer this.imMsgMutex.Unlock()
|
|
if this.imBotNode != nil {
|
|
this.imBotNode.next = p
|
|
this.imBotNode = p
|
|
} else {
|
|
this.imTopNode = p
|
|
this.imBotNode = p
|
|
}
|
|
}
|
|
this.loopCond.Broadcast()
|
|
}
|
|
|
|
func (this *App_) RegisterIMMsgHandle(msgId int16, handle func(int16,*q5.XParams)) {
|
|
this.imMsgHandlers[msgId] = handle
|
|
}
|
|
|
|
func (this *App_) goLoopTimer() {
|
|
for {
|
|
select {
|
|
case <-this.chGoLoopTimerExit:
|
|
return
|
|
case <-time.After(time.Millisecond):
|
|
this.loopCond.Broadcast()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *App_) schedule() {
|
|
this.loopCond.L.Lock()
|
|
this.loopCond.Wait()
|
|
this.loopCond.L.Unlock()
|
|
}
|