82 lines
1.7 KiB
Go
82 lines
1.7 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.RobotSingleNum {
|
|
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("it_%d", 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()
|
|
}
|