71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package robot
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/core/cbase"
|
|
"go_dreamfactory/lego/sys/log"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
客户端管理组件
|
|
*/
|
|
type robotmgrComp struct {
|
|
cbase.ModuleCompBase
|
|
service core.IService
|
|
module *RobotModule
|
|
currRobotNum int32
|
|
}
|
|
|
|
//组件初始化接口
|
|
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
|
|
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{
|
|
index: index,
|
|
account: fmt.Sprintf("test%d", index),
|
|
serverId: "df01",
|
|
pipeline: this.module.options.Pipeline,
|
|
}
|
|
if err = robot.Init(this.module.options.ServerAddr, robot); err != nil {
|
|
log.Errorln(err)
|
|
return
|
|
}
|
|
return
|
|
}
|