Changed event strings to EventCommand or EventControl

gql_cataclysm
noah metz 2023-11-12 13:36:11 -07:00
parent 59424cecc1
commit c5f95d431d
4 changed files with 29 additions and 17 deletions

@ -48,7 +48,7 @@ func (ext *ACLExt) Process(ctx *Context, node *Node, source NodeID, signal Signa
response, is_response := signal.(ResponseSignal) response, is_response := signal.(ResponseSignal)
if is_response == true { if is_response == true {
var messages Messages = nil var messages Messages = nil
var changes Changes = nil var changes = Changes{}
info, waiting := ext.Pending[response.ResponseID()] info, waiting := ext.Pending[response.ResponseID()]
if waiting == true { if waiting == true {
changes.Add(ACLExtType, "pending") changes.Add(ACLExtType, "pending")
@ -106,7 +106,7 @@ func (ext *ACLExt) Process(ctx *Context, node *Node, source NodeID, signal Signa
} }
var messages Messages = nil var messages Messages = nil
var changes Changes = nil var changes = Changes{}
switch sig := signal.(type) { switch sig := signal.(type) {
case *ACLSignal: case *ACLSignal:

@ -435,6 +435,16 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return nil, err return nil, err
} }
err = ctx.RegisterType(reflect.TypeOf(EventCommand("")), EventCommandType, nil, nil, nil, DeserializeString[EventState])
if err != nil {
return nil, err
}
err = ctx.RegisterType(reflect.TypeOf(EventState("")), EventStateType, nil, nil, nil, DeserializeString[EventState])
if err != nil {
return nil, err
}
wait_info_type := reflect.TypeOf(WaitInfo{}) wait_info_type := reflect.TypeOf(WaitInfo{})
wait_info_info, err := GetStructInfo(ctx, wait_info_type) wait_info_info, err := GetStructInfo(ctx, wait_info_type)
if err != nil { if err != nil {

@ -5,6 +5,9 @@ import (
"fmt" "fmt"
) )
type EventCommand string
type EventState string
type ParentOfPolicy struct { type ParentOfPolicy struct {
PolicyHeader PolicyHeader
Policy Tree Policy Tree
@ -41,7 +44,7 @@ var DefaultEventPolicy = NewParentOfPolicy(Tree{
type EventExt struct { type EventExt struct {
Name string `gv:"name"` Name string `gv:"name"`
State string `gv:"state"` State EventState `gv:"state"`
Parent NodeID `gv:"parent"` Parent NodeID `gv:"parent"`
} }
@ -55,9 +58,9 @@ func NewEventExt(parent NodeID, name string) *EventExt {
type EventStateSignal struct { type EventStateSignal struct {
SignalHeader SignalHeader
Source NodeID Source NodeID `gv:"source"`
State string State EventState `gv:"state"`
Time time.Time Time time.Time `gv:"time"`
} }
func (signal EventStateSignal) Permission() Tree { func (signal EventStateSignal) Permission() Tree {
@ -70,7 +73,7 @@ func (signal EventStateSignal) String() string {
return fmt.Sprintf("EventStateSignal(%s, %s, %s, %+v)", signal.SignalHeader, signal.Source, signal.State, signal.Time) 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 { func NewEventStateSignal(source NodeID, state EventState, t time.Time) *EventStateSignal {
return &EventStateSignal{ return &EventStateSignal{
SignalHeader: NewSignalHeader(Up), SignalHeader: NewSignalHeader(Up),
Source: source, Source: source,
@ -81,14 +84,14 @@ func NewEventStateSignal(source NodeID, state string, t time.Time) *EventStateSi
type EventControlSignal struct { type EventControlSignal struct {
SignalHeader SignalHeader
Command string `gv:"command"` Command EventCommand `gv:"command"`
} }
func (signal EventControlSignal) String() string { func (signal EventControlSignal) String() string {
return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command) return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command)
} }
func NewEventControlSignal(command string) *EventControlSignal { func NewEventControlSignal(command EventCommand) *EventControlSignal {
return &EventControlSignal{ return &EventControlSignal{
NewSignalHeader(Direct), NewSignalHeader(Direct),
command, command,
@ -98,12 +101,12 @@ func NewEventControlSignal(command string) *EventControlSignal {
func (signal EventControlSignal) Permission() Tree { func (signal EventControlSignal) Permission() Tree {
return Tree{ return Tree{
SerializedType(EventControlSignalType): { SerializedType(EventControlSignalType): {
Hash("command", signal.Command): nil, Hash("command", string(signal.Command)): nil,
}, },
} }
} }
func (ext *EventExt) UpdateState(node *Node, changes Changes, state string) { func (ext *EventExt) UpdateState(node *Node, changes Changes, state EventState) {
if ext.State != state { if ext.State != state {
changes.Add(EventExtType, "state") changes.Add(EventExtType, "state")
ext.State = state ext.State = state
@ -126,7 +129,8 @@ type TestEventExt struct {
Length time.Duration Length time.Duration
} }
var test_event_commands = map[string]map[string]string{ type EventCommandMap map[EventCommand]map[EventState]EventState
var test_event_commands = EventCommandMap{
"ready?": { "ready?": {
"init": "ready", "init": "ready",
}, },
@ -173,11 +177,7 @@ func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal
return messages, changes return messages, changes
} }
type TransitionValidation struct { func(ext *EventExt) ValidateEventCommand(signal *EventControlSignal, commands EventCommandMap) (EventState, *ErrorSignal) {
ToState string
}
func(ext *EventExt) ValidateEventCommand(signal *EventControlSignal, commands map[string]map[string]string) (string, *ErrorSignal) {
transitions, command_mapped := commands[signal.Command] transitions, command_mapped := commands[signal.Command]
if command_mapped == false { if command_mapped == false {
return "", NewErrorSignal(signal.Id, "unknown command %s", signal.Command) return "", NewErrorSignal(signal.Id, "unknown command %s", signal.Command)

@ -267,6 +267,8 @@ var (
InterfaceType = NewSerializedType("INTERFACE") InterfaceType = NewSerializedType("INTERFACE")
MapType = NewSerializedType("MAP") MapType = NewSerializedType("MAP")
EventStateType = NewSerializedType("EVENT_STATE")
EventCommandType = NewSerializedType("EVENT_COMMAND")
ReqStateType = NewSerializedType("REQ_STATE") ReqStateType = NewSerializedType("REQ_STATE")
WaitInfoType = NewSerializedType("WAIT_INFO") WaitInfoType = NewSerializedType("WAIT_INFO")
SignalDirectionType = NewSerializedType("SIGNAL_DIRECTION") SignalDirectionType = NewSerializedType("SIGNAL_DIRECTION")