2023-10-29 18:26:14 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EventExt struct {
|
2023-10-30 01:25:18 -06:00
|
|
|
Name string `gv:"name"`
|
|
|
|
State string `gv:"state"`
|
2023-11-04 18:56:35 -06:00
|
|
|
Parent NodeID `gv:"parent"`
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
|
2023-11-04 18:56:35 -06:00
|
|
|
func NewEventExt(parent NodeID, name string) *EventExt {
|
2023-10-29 18:26:14 -06:00
|
|
|
return &EventExt{
|
|
|
|
Name: name,
|
|
|
|
State: "init",
|
|
|
|
Parent: parent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventStateSignal struct {
|
|
|
|
SignalHeader
|
|
|
|
Source NodeID
|
|
|
|
State string
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal EventStateSignal) Permission() Tree {
|
|
|
|
return Tree{
|
|
|
|
SerializedType(StatusType): nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal EventStateSignal) String() string {
|
|
|
|
return fmt.Sprintf("EventStateSignal(%s, %s, %s, %+v)", signal.SignalHeader, signal.Source, signal.State, signal.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventStateSignal(source NodeID, state string, t time.Time) *EventStateSignal {
|
|
|
|
return &EventStateSignal{
|
|
|
|
SignalHeader: NewSignalHeader(Up),
|
|
|
|
Source: source,
|
|
|
|
State: state,
|
|
|
|
Time: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventControlSignal struct {
|
|
|
|
SignalHeader
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:58:38 -06:00
|
|
|
func (signal EventControlSignal) String() string {
|
|
|
|
return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command)
|
|
|
|
}
|
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
func NewEventControlSignal(command string) *EventControlSignal {
|
|
|
|
return &EventControlSignal{
|
|
|
|
NewSignalHeader(Direct),
|
|
|
|
command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal EventControlSignal) Permission() Tree {
|
|
|
|
return Tree{
|
|
|
|
SerializedType(EventControlSignalType): {
|
|
|
|
Hash("command", signal.Command): nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 21:41:06 -06:00
|
|
|
func (ext *EventExt) UpdateState(node *Node, state string) {
|
|
|
|
if ext.State != state {
|
|
|
|
ext.State = state
|
|
|
|
node.QueueSignal(time.Now(), NewEventStateSignal(node.ID, ext.State, time.Now()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
func (ext *EventExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
|
|
|
var changes Changes = nil
|
|
|
|
|
2023-11-04 18:56:35 -06:00
|
|
|
if signal.Direction() == Up && ext.Parent != node.ID {
|
|
|
|
messages = messages.Add(ctx, ext.Parent, node, nil, signal)
|
2023-10-30 19:40:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:58:38 -06:00
|
|
|
type TestEventExt struct {
|
|
|
|
Length time.Duration
|
|
|
|
}
|
2023-10-30 19:40:30 -06:00
|
|
|
|
2023-11-01 19:32:36 -06:00
|
|
|
var test_event_transitions = map[string]struct{
|
2023-10-29 18:26:14 -06:00
|
|
|
from_state string
|
|
|
|
to_state string
|
|
|
|
}{
|
2023-11-02 20:58:38 -06:00
|
|
|
"ready?": {
|
2023-10-29 18:26:14 -06:00
|
|
|
"init",
|
2023-11-02 20:58:38 -06:00
|
|
|
"ready",
|
|
|
|
},
|
|
|
|
"start": {
|
|
|
|
"ready",
|
2023-10-29 18:26:14 -06:00
|
|
|
"running",
|
|
|
|
},
|
2023-11-02 20:58:38 -06:00
|
|
|
"abort": {
|
|
|
|
"ready",
|
|
|
|
"init",
|
|
|
|
},
|
2023-10-29 18:26:14 -06:00
|
|
|
"stop": {
|
|
|
|
"running",
|
2023-11-02 20:58:38 -06:00
|
|
|
"stopped",
|
2023-10-29 18:26:14 -06:00
|
|
|
},
|
|
|
|
"finish": {
|
|
|
|
"running",
|
|
|
|
"done",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-30 01:25:18 -06:00
|
|
|
|
|
|
|
func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) (Messages, Changes) {
|
|
|
|
var messages Messages = nil
|
|
|
|
var changes Changes = nil
|
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
switch sig := signal.(type) {
|
|
|
|
case *EventControlSignal:
|
2023-10-30 01:25:18 -06:00
|
|
|
event_ext, err := GetExt[*EventExt](node, EventExtType)
|
|
|
|
if err != nil {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_event"))
|
|
|
|
} else {
|
2023-11-01 19:32:36 -06:00
|
|
|
info, exists := test_event_transitions[sig.Command]
|
2023-10-30 01:25:18 -06:00
|
|
|
if exists == true {
|
|
|
|
if event_ext.State == info.from_state {
|
2023-11-01 19:32:36 -06:00
|
|
|
ctx.Log.Logf("event", "%s %s->%s", node.ID, info.from_state, info.to_state)
|
2023-10-30 01:25:18 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
|
2023-11-03 21:41:06 -06:00
|
|
|
event_ext.UpdateState(node, info.to_state)
|
2023-11-02 20:58:38 -06:00
|
|
|
if event_ext.State == "running" {
|
|
|
|
node.QueueSignal(time.Now().Add(ext.Length), NewEventControlSignal("finish"))
|
|
|
|
}
|
2023-10-30 01:25:18 -06:00
|
|
|
} else {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_state"))
|
|
|
|
}
|
2023-10-29 18:26:14 -06:00
|
|
|
} else {
|
2023-10-30 01:25:18 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_command"))
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
}
|