92 lines
2.1 KiB
Go
92 lines
2.1 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:
|
|
rnum := 0
|
|
for this.currRobotNum < this.module.options.RobotTotalNum {
|
|
this.currRobotNum++
|
|
if err := this.createRobot(this.currRobotNum); err == nil {
|
|
rnum++
|
|
if rnum >= int(this.module.options.RobotSingleNum) {
|
|
break
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
if this.currRobotNum >= this.module.options.RobotTotalNum {
|
|
break locp
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *robotmgrComp) createRobot(index int32) (err error) {
|
|
var (
|
|
robot *Robot
|
|
)
|
|
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: make([]*Pipeline, 0),
|
|
}
|
|
|
|
for _, v := range this.module.options.Pipeline {
|
|
robot.pipeline = append(robot.pipeline, &Pipeline{
|
|
Module: v.Module,
|
|
Exenum: v.Exenum,
|
|
ErrNotStop: v.ErrNotStop,
|
|
})
|
|
}
|
|
|
|
if err = robot.Init(this.module.options.ServerAddr, robot); err != nil {
|
|
this.module.statisticalComp.AddFailClient(robot, err)
|
|
return
|
|
}
|
|
this.module.statisticalComp.AddSuccClient(robot)
|
|
return
|
|
}
|