-
Notifications
You must be signed in to change notification settings - Fork 0
/
consume_queue.go
67 lines (58 loc) · 1.64 KB
/
consume_queue.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
package decorators
import "github.com/gorustyt/go-behavior/core"
func init() {
core.SetPorts(&ConsumeQueue{}, core.InputPortWithDefaultValue("queue", &core.ProtectedQueue{}))
core.SetPorts(ConsumeQueue{}, core.OutputPortWithDefaultValue("popped_item", &core.ProtectedQueue{}))
}
type ConsumeQueue struct {
*core.DecoratorNode
runningChild bool
}
func NewConsumeQueue(name string, cfg *core.NodeConfig, args ...interface{}) core.ITreeNode {
return &ConsumeQueue{DecoratorNode: core.NewDecoratorNode(name, cfg)}
}
func (n *ConsumeQueue) Tick() core.NodeStatus {
// by default, return SUCCESS, even if queue is empty
statusToBeReturned := core.NodeStatus_SUCCESS
if n.runningChild {
childState := n.Child().ExecuteTick()
n.runningChild = childState == core.NodeStatus_RUNNING
if n.runningChild {
return core.NodeStatus_RUNNING
} else {
n.HaltChild()
statusToBeReturned = childState
}
}
var queue *core.ProtectedQueue
v, err := n.GetInput("queue", queue)
if err != nil {
panic(err)
}
queue = v.(*core.ProtectedQueue)
if queue != nil {
queue.Mtx.Lock()
items := queue.Items
for items.Len() != 0 {
n.SetStatus(core.NodeStatus_RUNNING)
val := items.Front()
items.Remove(val)
n.SetOutput("popped_item", val)
queue.Mtx.Unlock()
childState := n.Child().ExecuteTick()
queue.Mtx.Lock()
n.runningChild = childState == core.NodeStatus_RUNNING
if n.runningChild {
return core.NodeStatus_RUNNING
} else {
n.HaltChild()
if childState == core.NodeStatus_FAILURE {
return core.NodeStatus_FAILURE
}
statusToBeReturned = childState
}
}
queue.Mtx.Unlock()
}
return statusToBeReturned
}