Simplified TestEventExt with ValidateTransition

gql_cataclysm
noah metz 2023-11-05 23:32:13 -07:00
parent 8d1e273331
commit dee7b917f7
1 changed files with 18 additions and 28 deletions

@ -103,8 +103,9 @@ func (signal EventControlSignal) Permission() Tree {
} }
} }
func (ext *EventExt) UpdateState(node *Node, state string) { func (ext *EventExt) UpdateState(node *Node, changes Changes, state string) {
if ext.State != state { if ext.State != state {
changes.Add(EventExtType, "changes")
ext.State = state ext.State = state
node.QueueSignal(time.Now(), NewEventStateSignal(node.ID, ext.State, time.Now())) node.QueueSignal(time.Now(), NewEventStateSignal(node.ID, ext.State, time.Now()))
} }
@ -125,29 +126,21 @@ type TestEventExt struct {
Length time.Duration Length time.Duration
} }
var test_event_transitions = map[string]struct{ var test_event_commands = map[string]map[string]string{
from_state string
to_state string
}{
"ready?": { "ready?": {
"init", "init": "ready",
"ready",
}, },
"start": { "start": {
"ready", "ready": "running",
"running",
}, },
"abort": { "abort": {
"ready", "ready": "init",
"init",
}, },
"stop": { "stop": {
"running", "running": "stopped",
"stopped",
}, },
"finish": { "finish": {
"running", "running": "done",
"done",
}, },
} }
@ -162,20 +155,17 @@ func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal
if err != nil { if err != nil {
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_event")) messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_event"))
} else { } else {
info, exists := test_event_transitions[sig.Command] ctx.Log.Logf("event", "%s got %s EventControlSignal while in %s", node.ID, sig.Command, event_ext.State)
if exists == true { new_state, error_signal := event_ext.ValidateEventCommand(sig, test_event_commands)
if event_ext.State == info.from_state { if error_signal != nil {
ctx.Log.Logf("event", "%s %s->%s", node.ID, info.from_state, info.to_state) messages = messages.Add(ctx, source, node, nil, error_signal)
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
event_ext.UpdateState(node, info.to_state)
if event_ext.State == "running" {
node.QueueSignal(time.Now().Add(ext.Length), NewEventControlSignal("finish"))
}
} else { } else {
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_state")) switch sig.Command {
case "start":
node.QueueSignal(time.Now().Add(ext.Length), NewEventControlSignal("finish"))
} }
} else { event_ext.UpdateState(node, changes, new_state)
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_command")) messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
} }
} }
} }