go_dreamfactory/modules/robot/robotmgrcomp.go
2023-09-05 18:44:15 +08:00

82 lines
1.8 KiB
Go

package robot
import (
"fmt"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log"
"sync"
"time"
)
/*
客户端管理组件
*/
type robotmgrComp struct {
cbase.ModuleCompBase
service core.IService
module *RobotModule
currRobotNum int32
lock sync.RWMutex
statistical map[string][]*RobotStatistics
}
//组件初始化接口
func (this *robotmgrComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.ModuleCompBase.Init(service, module, comp, options)
this.module = module.(*RobotModule)
this.service = service
this.statistical = make(map[string][]*RobotStatistics)
return
}
func (this *robotmgrComp) Start() (err error) {
err = this.ModuleCompBase.Start()
go this.run()
return
}
func (this *robotmgrComp) run() {
timer := time.NewTicker(time.Second)
defer timer.Stop()
locp:
for {
select {
case <-timer.C:
if this.currRobotNum < this.module.options.RobotTotalNum {
for i := 0; i < int(this.module.options.RobotSingleNum); i++ {
this.createRobot(this.currRobotNum)
this.currRobotNum++
}
} else {
break locp
}
}
}
}
func (this *robotmgrComp) createRobot(index int32) {
var (
robot *Robot
err error
)
robot = &Robot{
robotmgrComp: this,
index: index,
account: fmt.Sprintf("%s_%d", this.module.options.RobotName, index),
serverId: this.module.options.ServerID,
pipeline: this.module.options.Pipeline,
}
if err = robot.Init(this.module.options.ServerAddr, robot); err != nil {
log.Errorln(err)
return
}
return
}
func (this *robotmgrComp) robotEnd(robot *Robot) {
this.lock.Lock()
this.statistical[robot.account] = robot.statistics
this.lock.Unlock()
}