78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package robot
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"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{
|
|
debug: this.module.options.RobotLog,
|
|
monitor: this.module.statisticalComp,
|
|
index: index,
|
|
account: fmt.Sprintf("%s_%d", this.module.options.RobotName, this.module.options.RobotStart+index),
|
|
serverId: this.module.options.ServerID,
|
|
cycle: true,
|
|
pipeline: this.module.options.Pipeline,
|
|
}
|
|
if err = robot.Init(this.module.options.ServerAddr, robot); err != nil {
|
|
this.module.statisticalComp.AddFailClient(robot, err)
|
|
return
|
|
}
|
|
this.module.statisticalComp.AddSuccClient(robot)
|
|
return
|
|
}
|