change code to interface mode

This commit is contained in:
huangsimin 2018-11-30 18:46:49 +08:00
parent a0c18f4736
commit 2f7b2c151a
2 changed files with 97 additions and 76 deletions

143
task.go
View File

@ -1,115 +1,166 @@
package imitate package imitate
import ( import (
"log" "reflect"
"474420502.top/eson/curl2info" "474420502.top/eson/curl2info"
"474420502.top/eson/requests" "474420502.top/eson/requests"
) )
var register map[string]reflect.Type
// Register 注册 类型 ITask为样本的类型
func Register(name string, itype interface{}) {
register[name] = reflect.TypeOf(itype)
}
func makeRegisterType(name string) reflect.Type {
return reflect.New(register[name]).Type().Elem()
}
// Person 以人为单位 // Person 以人为单位
type Person struct { type Person struct {
Tasks []*Task Tasks []ITask
Conf *Config Conf *Config
} }
// NewPerson 创建一个人实例 // NewPerson 创建一个人实例
func NewPerson(conf string) *Person { func NewPerson() *Person {
person := &Person{} person := &Person{}
person.Conf = NewConfig(conf)
for _, scurl := range person.Conf.Curls { // person.Conf = NewConfig(conf)
// person.Tasks = SplitTasks(person.Conf)
return person
}
// LoadConfig 加载配置
func (person *Person) LoadConfig(conf string) {
person.Conf = NewConfig(conf)
person.Tasks = splitTasks(person.Conf)
}
// NewPersonWithConfig 创建一个person
func NewPersonWithConfig(conf string) *Person {
person := NewPerson()
person.LoadConfig(conf)
return person
}
// SplitTasks 拆开出需求的任务
func splitTasks(conf *Config) []ITask {
var tasks []ITask
for _, scurl := range conf.Curls {
curl, err := curl2info.ParseRawCURL(scurl) curl, err := curl2info.ParseRawCURL(scurl)
if err != nil { if err != nil {
panic(err) panic(err)
} }
at := *&person.Conf.ExecuteAt at := *&conf.ExecuteAt
interval := *&person.Conf.ExecuteInterval interval := *&conf.ExecuteInterval
task := NewTask(curl, &at, &interval) task := makeRegisterType(curl.ITask).Elem().(ITask)
switch person.Conf.Mode { switch conf.Mode {
case 0: case 0:
task.Proxies.Append(person.Conf.Proxies) copy(task.Proxies, conf.Proxies)
person.Tasks = append(person.Tasks, task) tasks = append(tasks, task)
case 1: case 1:
for _, proxy := range person.Conf.Proxies { for _, proxy := range conf.Proxies {
ncurl, err := curl2info.ParseRawCURL(scurl) ncurl, err := curl2info.ParseRawCURL(scurl)
if err != nil { if err != nil {
panic(err) panic(err)
} }
ptask := NewTask(ncurl) ptask := NewTask(ncurl)
for _, exec := range task.Plan.GetLoopValues() { for _, exec := range task.getPlans() {
switch v := exec.GetValue().(type) { switch v := exec.(type) {
case *ExecuteAt: case *ExecuteAt:
clone := &*v clone := &*v
ptask.Plan.Append(clone) ptask.Plan = append(ptask.Plan, clone)
case *ExecuteInterval: case *ExecuteInterval:
clone := &*v clone := &*v
ptask.Plan.Append(clone) ptask.Plan = append(ptask.Plan, clone)
} }
} }
ptask.Proxies.Append(proxy) ptask.Proxies = append(ptask.Proxies, proxy)
person.Tasks = append(person.Tasks, task) tasks = append(tasks, task)
} }
} }
} }
return tasks
return person
} }
// Execute 人的执行所有任务 // Execute 人的执行所有任务
func (person *Person) Execute() { func (person *Person) Execute() {
for _, task := range person.Tasks { for _, task := range person.Tasks {
task.ExecuteOnPlan(task) ExecuteOnPlan(task)
} }
} }
// ITask 继承这个接口的类
type ITask interface { type ITask interface {
Execute() Execute()
SetCurl(Curl *curl2info.CURL)
GetCurl() *curl2info.CURL
AppendPlans(Plans ...IExecute)
GetPlans() []IExecute
GetProxies() []string
SetProxies(proxies string)
} }
// Task 任务 // Task 任务
type Task struct { type Task struct {
Curl *curl2info.CURL ITask
Workflow *requests.Workflow
Plan *CircularLinked curl *curl2info.CURL
Proxies *CircularLinked workflow *requests.Workflow
plans []IExecute
proxies []string
} }
// NewTask 生成一个新任务 //
func NewTask(Curl *curl2info.CURL, Plans ...IExecute) *Task { func (task *Task) SetCurl(curl *curl2info.CURL) {
task.curl = curl
}
task := &Task{} func (task *Task) GetCurl() *curl2info.CURL {
// task.Conf = NewConfig(conf) return task.curl
task.Curl = Curl }
task.Plan = NewCircularLinked()
task.Proxies = NewCircularLinked()
if len(Plans) != 0 { //
for _, plan := range Plans { func (task *Task) AppendPlans(plans ...IExecute) {
task.Plan.Append(plan) if len(plans) != 0 {
for _, plan := range plans {
task.plans = append(task.plans, plan)
} }
} }
}
return task // InitTask 生成一个新任务
func InitTask(task ITask, Curl *curl2info.CURL, Plans ...IExecute) {
// task.Conf = NewConfig(conf)
task.setCurl(Curl)
task.appendPlans(Plans...)
} }
// ExecuteOnPlan 按照计划执行任务并返回结果 // ExecuteOnPlan 按照计划执行任务并返回结果
func (task *Task) ExecuteOnPlan(itask ITask) { func ExecuteOnPlan(task ITask) {
for _, exec := range task.Plan.GetLoopValues() { for _, exec := range task.getPlans() {
iexec := exec.GetValue().(IExecute) if exec.TimeTo() >= 0 {
if iexec.TimeTo() >= 0 { task.Execute() // 事件 在这里变化
itask.Execute() // 事件 在这里变化 exec.CalculateTrigger()
iexec.CalculateTrigger()
} }
} }
} }
// Execute 根据curl信息执行, TODO: 通用方法设置, 多太实现 // Execute 根据curl信息执行, TODO: 通用方法设置, 多太实现
func (task *Task) Execute() { // func (task *Task) Execute() {
resp, err := task.Curl.CreateWorkflow(nil).Execute() // resp, err := task.Curl.CreateWorkflow(nil).Execute()
log.Println(resp, err) // log.Println(resp, err)
} // }

View File

@ -42,33 +42,3 @@ func TestExecutePlan(t *testing.T) {
} }
} }
type C interface {
Execute()
}
type A struct {
C
}
func (a *A) Execute() {
log.Println("A")
}
func (a *A) EE() {
a.Execute()
a.Execute()
}
type B struct {
A
}
func (b *B) Execute() {
log.Println("B")
}
func TestCase1(t *testing.T) {
b := B{}
b.EE()
}