2023-07-09 14:30:30 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SignalDirection int
|
|
|
|
const (
|
|
|
|
Up SignalDirection = iota
|
|
|
|
Down
|
|
|
|
Direct
|
|
|
|
)
|
|
|
|
|
|
|
|
// GraphSignals are passed around the event tree/resource DAG and cast by Type()
|
|
|
|
type GraphSignal interface {
|
|
|
|
// How to propogate the signal
|
|
|
|
Direction() SignalDirection
|
|
|
|
Type() string
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BaseSignal is the most basic type of signal, it has no additional data
|
|
|
|
type BaseSignal struct {
|
|
|
|
FDirection SignalDirection `json:"direction"`
|
|
|
|
FType string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:07:27 -06:00
|
|
|
func (signal BaseSignal) String() string {
|
|
|
|
ser, err := json.Marshal(signal)
|
2023-07-09 14:30:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return "STATE_SER_ERR"
|
|
|
|
}
|
|
|
|
return string(ser)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Direction() SignalDirection {
|
|
|
|
return signal.FDirection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Type() string {
|
|
|
|
return signal.FType
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func NewBaseSignal(_type string, direction SignalDirection) BaseSignal {
|
2023-07-09 14:30:30 -06:00
|
|
|
signal := BaseSignal{
|
|
|
|
FDirection: direction,
|
|
|
|
FType: _type,
|
|
|
|
}
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func NewDownSignal(_type string) BaseSignal {
|
|
|
|
return NewBaseSignal(_type, Down)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSignal(_type string) BaseSignal {
|
|
|
|
return NewBaseSignal(_type, Up)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func NewDirectSignal(_type string) BaseSignal {
|
|
|
|
return NewBaseSignal(_type, Direct)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
var AbortSignal = NewBaseSignal("abort", Down)
|
2023-07-23 19:04:04 -06:00
|
|
|
var StopSignal = NewBaseSignal("stop", Down)
|
2023-07-22 20:21:17 -06:00
|
|
|
|
|
|
|
type IDSignal struct {
|
|
|
|
BaseSignal
|
|
|
|
ID NodeID `json:"id"`
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-24 17:07:27 -06:00
|
|
|
func (signal IDSignal) String() string {
|
|
|
|
ser, err := json.Marshal(signal)
|
|
|
|
if err != nil {
|
|
|
|
return "STATE_SER_ERR"
|
|
|
|
}
|
|
|
|
return string(ser)
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func NewIDSignal(_type string, direction SignalDirection, id NodeID) IDSignal {
|
|
|
|
return IDSignal{
|
|
|
|
BaseSignal: NewBaseSignal(_type, direction),
|
|
|
|
ID: id,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 21:14:15 -06:00
|
|
|
type StatusSignal struct {
|
|
|
|
IDSignal
|
2023-07-24 17:07:27 -06:00
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal StatusSignal) String() string {
|
|
|
|
ser, err := json.Marshal(signal)
|
|
|
|
if err != nil {
|
|
|
|
return "STATE_SER_ERR"
|
|
|
|
}
|
|
|
|
return string(ser)
|
2023-07-23 21:14:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatusSignal(status string, source NodeID) StatusSignal {
|
|
|
|
return StatusSignal{
|
|
|
|
IDSignal: NewIDSignal("status", Up, source),
|
|
|
|
Status: status,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-21 13:59:52 -06:00
|
|
|
|
|
|
|
type StartChildSignal struct {
|
2023-07-22 20:21:17 -06:00
|
|
|
IDSignal
|
2023-07-21 13:59:52 -06:00
|
|
|
Action string `json:"action"`
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func NewStartChildSignal(child_id NodeID, action string) StartChildSignal {
|
2023-07-21 13:59:52 -06:00
|
|
|
return StartChildSignal{
|
2023-07-22 20:21:17 -06:00
|
|
|
IDSignal: NewIDSignal("start_child", Direct, child_id),
|
2023-07-21 13:59:52 -06:00
|
|
|
Action: action,
|
|
|
|
}
|
|
|
|
}
|