2023-07-09 14:30:30 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SignalDirection int
|
|
|
|
const (
|
|
|
|
Up SignalDirection = iota
|
|
|
|
Down
|
|
|
|
Direct
|
|
|
|
)
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
type SignalType string
|
|
|
|
|
|
|
|
type Signal interface {
|
|
|
|
Serializable[SignalType]
|
2023-07-09 14:30:30 -06:00
|
|
|
Direction() SignalDirection
|
2023-07-28 00:39:27 -06:00
|
|
|
Permission() Action
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseSignal struct {
|
2023-07-26 15:08:14 -06:00
|
|
|
SignalDirection SignalDirection `json:"direction"`
|
|
|
|
SignalType SignalType `json:"type"`
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func (signal BaseSignal) Type() SignalType {
|
|
|
|
return signal.SignalType
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:39:27 -06:00
|
|
|
func (signal BaseSignal) Permission() Action {
|
|
|
|
return Action(signal.Type())
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
func (signal BaseSignal) Direction() SignalDirection {
|
2023-07-26 15:08:14 -06:00
|
|
|
return signal.SignalDirection
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func (signal BaseSignal) Serialize() ([]byte, error) {
|
|
|
|
return json.MarshalIndent(signal, "", " ")
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewBaseSignal(signal_type SignalType, direction SignalDirection) BaseSignal {
|
2023-07-09 14:30:30 -06:00
|
|
|
signal := BaseSignal{
|
2023-07-26 15:08:14 -06:00
|
|
|
SignalDirection: direction,
|
|
|
|
SignalType: signal_type,
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewDownSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Down)
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewUpSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Up)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewDirectSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Direct)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
const StopSignalType = SignalType("STOP")
|
|
|
|
var StopSignal = NewDownSignal(StopSignalType)
|
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-26 15:08:14 -06:00
|
|
|
func NewIDSignal(signal_type SignalType, direction SignalDirection, id NodeID) IDSignal {
|
2023-07-22 20:21:17 -06:00
|
|
|
return IDSignal{
|
2023-07-26 15:08:14 -06:00
|
|
|
BaseSignal: NewBaseSignal(signal_type, direction),
|
2023-07-22 20:21:17 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-27 18:16:37 -06:00
|
|
|
const StatusSignalType = SignalType("STATUS")
|
2023-07-23 21:14:15 -06:00
|
|
|
func NewStatusSignal(status string, source NodeID) StatusSignal {
|
|
|
|
return StatusSignal{
|
2023-07-27 18:16:37 -06:00
|
|
|
IDSignal: NewIDSignal(StatusSignalType, Up, source),
|
2023-07-23 21:14:15 -06:00
|
|
|
Status: status,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-21 13:59:52 -06:00
|
|
|
|
2023-07-27 18:08:43 -06:00
|
|
|
const LinkSignalType = SignalType("LINK")
|
2023-07-27 18:37:06 -06:00
|
|
|
const LockSignalType = SignalType("LOCK")
|
|
|
|
type StateSignal struct {
|
2023-07-27 18:08:43 -06:00
|
|
|
BaseSignal
|
2023-07-27 16:21:27 -06:00
|
|
|
State string `json:"state"`
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func (signal StateSignal) Serialize() ([]byte, error) {
|
2023-07-27 18:08:43 -06:00
|
|
|
return json.MarshalIndent(signal, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func (signal StateSignal) String() string {
|
2023-07-27 18:08:43 -06:00
|
|
|
ser, _ := signal.Serialize()
|
|
|
|
return string(ser)
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func NewLinkSignal(state string) StateSignal {
|
|
|
|
return StateSignal{
|
2023-07-27 18:08:43 -06:00
|
|
|
BaseSignal: NewDirectSignal(LinkSignalType),
|
2023-07-27 16:21:27 -06:00
|
|
|
State: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func NewLockSignal(state string) StateSignal {
|
|
|
|
return StateSignal{
|
|
|
|
BaseSignal: NewDirectSignal(LockSignalType),
|
|
|
|
State: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|