Skip to content

Commit

Permalink
Add command feature
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Nov 1, 2018
1 parent d869e09 commit a8b4979
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
6 changes: 6 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ const (
// RobotStatusError 出错
RobotStatusError
)

// Command 表示一个交互命令结构
type Command struct {
Name string `json:"name"`
Value string `json:"value"`
}
36 changes: 33 additions & 3 deletions basestrategy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goalgo

import (
"encoding/json"
"fmt"
"reflect"
"strings"
Expand All @@ -10,6 +11,7 @@ import (

"runtime/debug"

"github.com/Workiva/go-datastructures/queue"
"github.com/hashicorp/go-plugin"
)

Expand All @@ -29,9 +31,10 @@ type OptionInfo struct {

// BaseStrategy 策略基础类
type BaseStrategy struct {
self interface{}
mutex sync.RWMutex
status RobotStatus
self interface{}
mutex sync.RWMutex
commandQueue queue.Queue
status RobotStatus
}

// SetSelf 设置 self 对象
Expand Down Expand Up @@ -229,6 +232,33 @@ func (s *BaseStrategy) SetOptions(options map[string]interface{}) plugin.BasicEr
return plugin.BasicError{}
}

// QueueCommand 命令入队列
func (s *BaseStrategy) QueueCommand(command string) plugin.BasicError {
cmd := Command{}
err := json.Unmarshal([]byte(command), &cmd)
if err != nil {
return plugin.BasicError{}
}
s.commandQueue.Put(&cmd)
return plugin.BasicError{}
}

// GetCommand 获取一个命令结构
func (s *BaseStrategy) GetCommand() *Command {
_, err := s.commandQueue.Peek()
if err != nil {
return nil
}
result, err := s.commandQueue.Get(1)
if err != nil {
return nil
}
if len(result) != 1 {
return nil
}
return result[0].(*Command)
}

// Start 启动
func (s *BaseStrategy) Start() plugin.BasicError {
go s.run()
Expand Down
19 changes: 19 additions & 0 deletions strategyctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type StrategyCtl interface {
GetState() RobotStatus
GetOptions() (optionMap map[string]*OptionInfo)
SetOptions(options map[string]interface{}) plugin.BasicError
QueueCommand(command string) plugin.BasicError
Start() plugin.BasicError
Stop() plugin.BasicError
Pause() plugin.BasicError
Expand Down Expand Up @@ -73,6 +74,19 @@ func (g *StrategyRPC) SetOptions(options map[string]interface{}) plugin.BasicErr
return resp
}

// QueueCommand ...
func (g *StrategyRPC) QueueCommand(command string) plugin.BasicError {
var resp plugin.BasicError
err := g.client.Call("Plugin.QueueCommand", command, &resp)
if err != nil {
// You usually want your interfaces to return errors. If they don't,
// there isn't much other choice here.
return plugin.BasicError{Message: err.Error()}
}

return resp
}

// Start ...
func (g *StrategyRPC) Start() plugin.BasicError {
var resp plugin.BasicError
Expand Down Expand Up @@ -137,6 +151,11 @@ func (s *StrategyRPCServer) SetOptions(args map[string]interface{}, resp *plugin
return nil
}

func (s *StrategyRPCServer) QueueCommand(args string, resp *plugin.BasicError) error {
*resp = s.Impl.QueueCommand(args)
return nil
}

func (s *StrategyRPCServer) Start(args interface{}, resp *plugin.BasicError) error {
*resp = s.Impl.Start()
return nil
Expand Down

0 comments on commit a8b4979

Please sign in to comment.