forked from bluenviron/goroslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleactionserver.go
198 lines (159 loc) · 4.32 KB
/
simpleactionserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package goroslib
import (
"fmt"
"reflect"
"github.com/aler9/goroslib/pkg/actionproc"
)
type goalHandlerPair struct {
gh *ActionServerGoalHandler
goal interface{}
}
// SimpleActionServerConf is the configuration of a SimpleActionServer.
type SimpleActionServerConf struct {
// parent node.
Node *Node
// name of the action.
Name string
// an instance of the action type.
Action interface{}
// (optional) function in the form func(*ActionGoal) that will be
// called when a goal arrives.
OnExecute interface{}
}
// SimpleActionServer is a ROS simple action server, an entity that can provide actions.
type SimpleActionServer struct {
conf SimpleActionServerConf
as *ActionServer
lastGoal *ActionServerGoalHandler
executingGoal *ActionServerGoalHandler
// in
goal chan *goalHandlerPair
terminate chan struct{}
// out
done chan struct{}
}
// NewSimpleActionServer allocates a SimpleActionServer.
func NewSimpleActionServer(conf SimpleActionServerConf) (*SimpleActionServer, error) {
sas := &SimpleActionServer{
conf: conf,
goal: make(chan *goalHandlerPair),
terminate: make(chan struct{}),
done: make(chan struct{}),
}
goal, _, _, err := actionproc.GoalResultFeedback(conf.Action)
if err != nil {
return nil, err
}
if conf.OnExecute != nil {
cbt := reflect.TypeOf(conf.OnExecute)
if cbt.Kind() != reflect.Func {
return nil, fmt.Errorf("OnExecute is not a function")
}
if cbt.NumIn() != 2 {
return nil, fmt.Errorf("OnExecute must accept 2 arguments")
}
if cbt.NumOut() != 0 {
return nil, fmt.Errorf("OnExecute must not return any value")
}
if cbt.In(0) != reflect.TypeOf(&SimpleActionServer{}) {
return nil, fmt.Errorf("OnExecute 1st argument must be %s, while is %v",
reflect.TypeOf(&SimpleActionServer{}), cbt.In(0))
}
if cbt.In(1) != reflect.PtrTo(reflect.TypeOf(goal)) {
return nil, fmt.Errorf("OnExecute 2nd argument must be %s, while is %v",
reflect.PtrTo(reflect.TypeOf(goal)), cbt.In(1))
}
}
sas.as, err = NewActionServer(ActionServerConf{
Node: conf.Node,
Name: conf.Name,
Action: conf.Action,
OnGoal: reflect.MakeFunc(
reflect.FuncOf([]reflect.Type{
reflect.TypeOf(&ActionServerGoalHandler{}),
reflect.PtrTo(reflect.TypeOf(goal)),
}, []reflect.Type{}, false),
sas.onGoal,
).Interface(),
OnCancel: sas.onCancel,
})
if err != nil {
return nil, err
}
go sas.run()
return sas, nil
}
// Close closes a SimpleActionServer.
func (sas *SimpleActionServer) Close() error {
sas.as.Close()
go func() {
for range sas.goal {
}
}()
close(sas.terminate)
<-sas.done
return nil
}
// PublishFeedback publishes a feedback about the current goal.
func (sas *SimpleActionServer) PublishFeedback(fb interface{}) {
sas.executingGoal.PublishFeedback(fb)
}
// SetAborted sets the current goal as aborted.
func (sas *SimpleActionServer) SetAborted(res interface{}) {
sas.executingGoal.SetAborted(res)
}
// SetSucceeded sets the current goal as succeeded.
func (sas *SimpleActionServer) SetSucceeded(res interface{}) {
sas.executingGoal.SetSucceeded(res)
}
func (sas *SimpleActionServer) onGoal(in []reflect.Value) []reflect.Value {
gh := in[0].Interface().(*ActionServerGoalHandler)
goal := in[1].Interface()
if sas.lastGoal != nil {
sas.lastGoal.SetCanceled(reflect.New(sas.as.resType).Interface())
}
sas.lastGoal = gh
gh.SetAccepted()
sas.goal <- &goalHandlerPair{gh, goal}
return []reflect.Value{}
}
func (sas *SimpleActionServer) onCancel(gh *ActionServerGoalHandler) {
gh.SetCanceled(reflect.New(sas.as.resType).Interface())
}
func (sas *SimpleActionServer) run() {
defer close(sas.done)
executeRunning := false
var executeDone chan struct{}
executeLaunch := func(pair *goalHandlerPair) {
sas.executingGoal = pair.gh
executeRunning = true
executeDone = make(chan struct{})
go func() {
defer close(executeDone)
if sas.conf.OnExecute != nil {
reflect.ValueOf(sas.conf.OnExecute).Call([]reflect.Value{
reflect.ValueOf(sas),
reflect.ValueOf(pair.goal),
})
}
}()
}
var nextGoal *goalHandlerPair
for {
select {
case pair := <-sas.goal:
if !executeRunning {
executeLaunch(pair)
} else {
nextGoal = pair
}
case <-executeDone:
if nextGoal != nil {
executeLaunch(nextGoal)
nextGoal = nil
}
case <-sas.terminate:
return
}
}
}