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)
if is_response == true {
var messages Messages = nil
var changes Changes = nil
var changes = Changes{}
info, waiting := ext.Pending[response.ResponseID()]
if waiting == true {
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 changes Changes = nil
var changes = Changes{}
switch sig := signal.(type) {
case *ACLSignal:

@ -435,6 +435,16 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
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_info, err := GetStructInfo(ctx, wait_info_type)
if err != nil {

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

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