-
Notifications
You must be signed in to change notification settings - Fork 62
/
simple_action_server.go
251 lines (202 loc) · 5.55 KB
/
simple_action_server.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package goroslib
import (
"context"
"fmt"
"reflect"
"sync"
"github.com/bluenviron/goroslib/v2/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
ctx context.Context
ctxCancel func()
as *ActionServer
currentGoal *ActionServerGoalHandler
preemptFlag bool
mutex sync.Mutex
// in
goal chan goalHandlerPair
cancel chan *ActionServerGoalHandler
// out
done chan struct{}
}
// NewSimpleActionServer allocates a SimpleActionServer.
func NewSimpleActionServer(conf SimpleActionServerConf) (*SimpleActionServer, error) {
if reflect.TypeOf(conf.Action).Kind() != reflect.Ptr {
return nil, fmt.Errorf("Action is not a pointer")
}
actionElem := reflect.ValueOf(conf.Action).Elem().Interface()
goal, _, _, err := actionproc.GoalResultFeedback(actionElem)
if err != nil {
return nil, err
}
ctx, ctxCancel := context.WithCancel(context.Background())
sas := &SimpleActionServer{
conf: conf,
ctx: ctx,
ctxCancel: ctxCancel,
goal: make(chan goalHandlerPair),
cancel: make(chan *ActionServerGoalHandler),
done: make(chan struct{}),
}
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.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.PointerTo(reflect.TypeOf(goal)) {
return nil, fmt.Errorf("OnExecute 2nd argument must be %s, while is %v",
reflect.PointerTo(reflect.TypeOf(goal)), cbt.In(1))
}
if cbt.NumOut() != 0 {
return nil, fmt.Errorf("OnExecute must not return any value")
}
}
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.PointerTo(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() {
sas.ctxCancel()
<-sas.done
}
// IsPreemptRequested checks whether the goal has been canceled.
func (sas *SimpleActionServer) IsPreemptRequested() bool {
sas.mutex.Lock()
defer sas.mutex.Unlock()
return sas.preemptFlag
}
// PublishFeedback publishes a feedback about the current goal.
// This can be called only from an OnExecute callback.
func (sas *SimpleActionServer) PublishFeedback(fb interface{}) {
sas.currentGoal.PublishFeedback(fb)
}
// SetAborted sets the current goal as aborted.
// This can be called only from an OnExecute callback.
func (sas *SimpleActionServer) SetAborted(res interface{}) {
sas.currentGoal.SetAborted(res)
}
// SetSucceeded sets the current goal as succeeded.
// This can be called only from an OnExecute callback.
func (sas *SimpleActionServer) SetSucceeded(res interface{}) {
sas.currentGoal.SetSucceeded(res)
}
func (sas *SimpleActionServer) onGoal(in []reflect.Value) []reflect.Value {
gh := in[0].Interface().(*ActionServerGoalHandler)
goal := in[1].Interface()
select {
case sas.goal <- goalHandlerPair{gh, goal}:
case <-sas.ctx.Done():
}
return nil
}
func (sas *SimpleActionServer) onCancel(gh *ActionServerGoalHandler) {
select {
case sas.cancel <- gh:
case <-sas.ctx.Done():
}
}
func (sas *SimpleActionServer) run() {
defer close(sas.done)
executeRunning := false
var executeDone chan struct{}
executeStart := func(goal interface{}, gh *ActionServerGoalHandler, preemptFlag bool) {
sas.currentGoal = gh
sas.preemptFlag = preemptFlag
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(goal),
})
}
}()
}
var lastGoal *ActionServerGoalHandler
var nextGoal *goalHandlerPair
nextGoalPreemptFlag := false
outer:
for {
select {
case pair := <-sas.goal:
if lastGoal != nil {
lastGoal.SetCanceled(reflect.New(sas.as.resType).Interface())
}
lastGoal = pair.gh
pair.gh.SetAccepted()
if !executeRunning {
executeStart(pair.goal, pair.gh, false)
} else {
nextGoal = &pair
nextGoalPreemptFlag = false
}
case gh := <-sas.cancel:
switch gh {
case sas.currentGoal:
sas.mutex.Lock()
sas.preemptFlag = true
sas.mutex.Unlock()
case nextGoal.gh:
nextGoalPreemptFlag = true
}
case <-executeDone:
if nextGoal != nil {
executeStart(nextGoal.goal, nextGoal.gh, nextGoalPreemptFlag)
nextGoal = nil
nextGoalPreemptFlag = false
} else {
lastGoal = nil
}
case <-sas.ctx.Done():
break outer
}
}
sas.ctxCancel()
sas.as.Close()
if executeRunning {
<-executeDone
}
}