From 98f05d57f943016d59aba15e0e9cbea678184667 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 15 Aug 2023 19:17:13 -0600 Subject: [PATCH] Added signals to add and remove requirements from a lockable node --- lockable.go | 33 +++++++++++++++++++++++++++++++++ signal.go | 5 +++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lockable.go b/lockable.go index b51f31d..3c6d0b1 100644 --- a/lockable.go +++ b/lockable.go @@ -185,6 +185,37 @@ func (ext *LockableExt) HandleErrorSignal(log Logger, node *Node, source NodeID, return msgs } +func (ext *LockableExt) HandleLinkSignal(log Logger, node *Node, source NodeID, signal *IDStringSignal) Messages { + id := signal.NodeID + action := signal.Str + msgs := Messages {} + if ext.State == Unlocked { + switch action { + case "add": + _, exists := ext.Requirements[id] + if exists == true { + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_requirement"), source) + } else { + ext.Requirements[id] = Unlocked + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "req_added"), source) + } + case "remove": + _, exists := ext.Requirements[id] + if exists == false { + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source) + } else { + delete(ext.Requirements, id) + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "req_removed"), source) + } + default: + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "unknown_action"), source) + } + } else { + msgs = msgs.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocked"), source) + } + return msgs +} + // Handle a LockSignal and update the extensions owner/requirement states func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) Messages { state := signal.Str @@ -321,6 +352,8 @@ func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal } case Direct: switch signal.Type() { + case LinkSignalType: + messages = ext.HandleLinkSignal(ctx.Log, node, source, signal.(*IDStringSignal)) case LockSignalType: messages = ext.HandleLockSignal(ctx.Log, node, source, signal.(*StringSignal)) case ErrorSignalType: diff --git a/signal.go b/signal.go index 022965c..eedae68 100644 --- a/signal.go +++ b/signal.go @@ -232,9 +232,10 @@ func NewStatusSignal(status string, source NodeID) Signal { } } -func NewLinkSignal(state string) Signal { - return &StringSignal{ +func NewLinkSignal(state string, id NodeID) Signal { + return &IDStringSignal{ BaseSignal: NewBaseSignal(LinkSignalType, Direct), + NodeID: id, Str: state, } }