graphvent/signal.go

310 lines
6.0 KiB
Go

2023-07-09 14:30:30 -06:00
package graphvent
import (
"fmt"
"time"
2023-09-18 20:11:50 -06:00
"github.com/google/uuid"
)
2023-09-12 19:40:06 -06:00
type SignalDirection uint8
2023-07-09 14:30:30 -06:00
const (
Up SignalDirection = iota
Down
Direct
)
type SignalHeader struct {
2023-09-12 19:40:06 -06:00
Direction SignalDirection `gv:"direction"`
ID uuid.UUID `gv:"id"`
ReqID uuid.UUID `gv:"req_id"`
}
func (header SignalHeader) Header() SignalHeader {
return header
}
func (header SignalHeader) String() string {
return fmt.Sprintf("Signal(%d, %s->%s)", header.Direction, header.ID, header.ReqID)
}
type Signal interface {
fmt.Stringer
Header() SignalHeader
2023-08-10 23:43:10 -06:00
Permission() Tree
2023-07-09 14:30:30 -06:00
}
func WaitForResponse(listener chan Signal, timeout time.Duration, req_id uuid.UUID) (Signal, error) {
var timeout_channel <- chan time.Time
if timeout > 0 {
timeout_channel = time.After(timeout)
}
for true {
select {
case signal := <- listener:
if signal == nil {
return nil, fmt.Errorf("LISTENER_CLOSED")
}
if signal.Header().ReqID == req_id {
return signal, nil
}
case <-timeout_channel:
return nil, fmt.Errorf("LISTENER_TIMEOUT")
}
}
return nil, fmt.Errorf("UNREACHABLE")
}
func WaitForSignal[S Signal](listener chan Signal, timeout time.Duration, check func(S)bool) (S, error) {
var zero S
var timeout_channel <- chan time.Time
if timeout > 0 {
timeout_channel = time.After(timeout)
}
for true {
select {
case signal := <- listener:
if signal == nil {
return zero, fmt.Errorf("LISTENER_CLOSED")
}
sig, ok := signal.(S)
if ok == true {
if check(sig) == true {
return sig, nil
}
}
case <-timeout_channel:
return zero, fmt.Errorf("LISTENER_TIMEOUT")
}
}
return zero, fmt.Errorf("LOOP_ENDED")
}
func NewSignalHeader(direction SignalDirection) SignalHeader {
2023-08-10 23:43:10 -06:00
id := uuid.New()
header := SignalHeader{
ID: id,
ReqID: id,
Direction: direction,
2023-08-10 23:43:10 -06:00
}
return header
2023-08-10 23:43:10 -06:00
}
func NewRespHeader(req_id uuid.UUID, direction SignalDirection) SignalHeader {
header := SignalHeader{
ID: uuid.New(),
ReqID: req_id,
Direction: direction,
2023-07-09 14:30:30 -06:00
}
return header
2023-07-09 14:30:30 -06:00
}
type CreateSignal struct {
SignalHeader
}
func (signal CreateSignal) Permission() Tree {
return Tree{
SerializedType(CreateSignalType): nil,
}
2023-07-09 14:30:30 -06:00
}
2023-07-21 13:59:52 -06:00
func NewCreateSignal() *CreateSignal {
return &CreateSignal{
NewSignalHeader(Direct),
2023-07-27 16:21:27 -06:00
}
}
type StartSignal struct {
SignalHeader
}
func (signal StartSignal) Permission() Tree {
2023-08-10 23:43:10 -06:00
return Tree{
SerializedType(StartSignalType): nil,
2023-08-10 23:43:10 -06:00
}
}
func NewStartSignal() *StartSignal {
return &StartSignal{
NewSignalHeader(Direct),
2023-07-21 13:59:52 -06:00
}
}
type StopSignal struct {
SignalHeader
2023-08-10 23:43:10 -06:00
}
func (signal StopSignal) Permission() Tree {
2023-08-10 23:43:10 -06:00
return Tree{
SerializedType(StopSignalType): nil,
2023-08-10 23:43:10 -06:00
}
}
func NewStopSignal() *StopSignal {
return &StopSignal{
NewSignalHeader(Direct),
}
}
type SuccessSignal struct {
SignalHeader
}
func (signal SuccessSignal) Permission() Tree {
return Tree{
ResultType: {
SerializedType(SuccessSignalType): nil,
},
}
}
func NewSuccessSignal(req_id uuid.UUID) Signal {
return &SuccessSignal{
NewRespHeader(req_id, Direct),
}
}
type ErrorSignal struct {
SignalHeader
Error string
2023-07-29 00:28:44 -06:00
}
func (signal ErrorSignal) Permission() Tree {
return Tree{
ResultType: {
SerializedType(ErrorSignalType): nil,
},
}
2023-07-29 00:28:44 -06:00
}
func NewErrorSignal(req_id uuid.UUID, fmt_string string, args ...interface{}) Signal {
return &ErrorSignal{
NewRespHeader(req_id, Direct),
fmt.Sprintf(fmt_string, args...),
2023-07-29 00:28:44 -06:00
}
}
2023-07-29 00:28:44 -06:00
type ACLTimeoutSignal struct {
SignalHeader
}
func (signal ACLTimeoutSignal) Permission() Tree {
return Tree{
SerializedType(ACLTimeoutSignalType): nil,
2023-07-29 00:28:44 -06:00
}
}
func NewACLTimeoutSignal(req_id uuid.UUID) *ACLTimeoutSignal {
sig := &ACLTimeoutSignal{
NewRespHeader(req_id, Direct),
2023-07-29 00:28:44 -06:00
}
return sig
}
2023-07-29 00:28:44 -06:00
type StatusSignal struct {
SignalHeader
2023-09-12 19:40:06 -06:00
Source NodeID `gv:"source"`
Status string `gv:"status"`
}
func (signal StatusSignal) Permission() Tree {
return Tree{
SerializedType(StatusSignalType): nil,
}
}
func NewStatusSignal(source NodeID, status string) *StatusSignal {
return &StatusSignal{
NewSignalHeader(Up),
source,
status,
2023-07-29 00:28:44 -06:00
}
}
type LinkSignal struct {
SignalHeader
NodeID NodeID
Action string
}
2023-07-29 00:28:44 -06:00
const (
LinkActionBase = "LINK_ACTION"
LinkActionAdd = "ADD"
)
2023-07-29 00:28:44 -06:00
func (signal LinkSignal) Permission() Tree {
return Tree{
SerializedType(LinkSignalType): Tree{
Hash(LinkActionBase, signal.Action): nil,
},
2023-07-29 00:28:44 -06:00
}
}
func NewLinkSignal(action string, id NodeID) Signal {
return &LinkSignal{
NewSignalHeader(Direct),
id,
action,
2023-07-29 00:28:44 -06:00
}
}
2023-07-29 00:28:44 -06:00
type LockSignal struct {
SignalHeader
State string
}
2023-07-29 00:28:44 -06:00
const (
LockStateBase = "LOCK_STATE"
)
2023-07-29 00:28:44 -06:00
func (signal LockSignal) Permission() Tree {
return Tree{
SerializedType(LockSignalType): Tree{
Hash(LockStateBase, signal.State): nil,
},
}
2023-07-29 00:28:44 -06:00
}
func NewLockSignal(state string) *LockSignal {
return &LockSignal{
NewSignalHeader(Direct),
state,
2023-07-29 00:28:44 -06:00
}
}
2023-07-29 00:28:44 -06:00
type ReadSignal struct {
SignalHeader
Extensions map[ExtType][]string `json:"extensions"`
}
2023-07-29 00:28:44 -06:00
func (signal ReadSignal) Permission() Tree {
ret := Tree{}
for ext, fields := range(signal.Extensions) {
field_tree := Tree{}
for _, field := range(fields) {
field_tree[Hash(FieldNameBase, field)] = nil
}
ret[SerializedType(ext)] = field_tree
2023-07-29 00:28:44 -06:00
}
return Tree{SerializedType(ReadSignalType): ret}
}
func NewReadSignal(exts map[ExtType][]string) *ReadSignal {
return &ReadSignal{
NewSignalHeader(Direct),
exts,
}
}
2023-07-29 00:28:44 -06:00
type ReadResultSignal struct {
SignalHeader
NodeID NodeID
NodeType NodeType
Extensions map[ExtType]map[string]SerializedValue
}
func (signal ReadResultSignal) Permission() Tree {
return Tree{
ResultType: {
SerializedType(ReadResultSignalType): nil,
},
}
2023-07-29 00:28:44 -06:00
}
func NewReadResultSignal(req_id uuid.UUID, node_id NodeID, node_type NodeType, exts map[ExtType]map[string]SerializedValue) *ReadResultSignal {
return &ReadResultSignal{
NewRespHeader(req_id, Direct),
node_id,
node_type,
exts,
}
}