2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2024-03-23 02:21:27 -06:00
|
|
|
"github.com/google/uuid"
|
2023-06-23 20:56:09 -06:00
|
|
|
)
|
|
|
|
|
2023-08-15 18:23:06 -06:00
|
|
|
type ReqState byte
|
2023-08-11 16:00:36 -06:00
|
|
|
const (
|
|
|
|
Unlocked = ReqState(0)
|
|
|
|
Unlocking = ReqState(1)
|
|
|
|
Locked = ReqState(2)
|
|
|
|
Locking = ReqState(3)
|
2023-08-15 18:23:06 -06:00
|
|
|
AbortingLock = ReqState(4)
|
2023-08-11 16:00:36 -06:00
|
|
|
)
|
|
|
|
|
2023-11-03 22:51:54 -06:00
|
|
|
var ReqStateStrings = map[ReqState]string {
|
|
|
|
Unlocked: "Unlocked",
|
|
|
|
Unlocking: "Unlocking",
|
|
|
|
Locked: "Locked",
|
|
|
|
Locking: "Locking",
|
|
|
|
AbortingLock: "AbortingLock",
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
func (state ReqState) String() string {
|
|
|
|
str, mapped := ReqStateStrings[state]
|
|
|
|
if mapped == false {
|
|
|
|
return "UNKNOWN_REQSTATE"
|
|
|
|
} else {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
type LockableExt struct{
|
2024-03-28 20:28:07 -06:00
|
|
|
State ReqState `gv:"state"`
|
2023-09-12 20:30:18 -06:00
|
|
|
ReqID *uuid.UUID `gv:"req_id"`
|
2024-03-23 02:21:27 -06:00
|
|
|
Owner *NodeID `gv:"owner"`
|
|
|
|
PendingOwner *NodeID `gv:"pending_owner"`
|
2024-03-08 17:43:30 -07:00
|
|
|
Requirements map[NodeID]ReqState `gv:"requirements" node:"Lockable:"`
|
2024-03-30 23:57:18 -06:00
|
|
|
|
|
|
|
Locked map[NodeID]any
|
|
|
|
Unlocked map[NodeID]any
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
Waiting WaitMap `gv:"waiting_locks" node:":Lockable"`
|
2023-07-28 11:21:18 -06:00
|
|
|
}
|
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
func (ext *LockableExt) PostDeserialize(ctx *Context) error {
|
|
|
|
ext.Locked = map[NodeID]any{}
|
|
|
|
ext.Unlocked = map[NodeID]any{}
|
|
|
|
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state == Unlocked {
|
|
|
|
ext.Unlocked[id] = nil
|
|
|
|
} else if state == Locked {
|
|
|
|
ext.Locked[id] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
2023-11-03 22:51:54 -06:00
|
|
|
var reqs map[NodeID]ReqState = nil
|
2024-03-30 23:57:18 -06:00
|
|
|
var unlocked map[NodeID]any = map[NodeID]any{}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
if len(requirements) != 0 {
|
2023-11-03 22:51:54 -06:00
|
|
|
reqs = map[NodeID]ReqState{}
|
2024-03-23 02:21:27 -06:00
|
|
|
for _, req := range(requirements) {
|
|
|
|
reqs[req] = Unlocked
|
2024-03-30 23:57:18 -06:00
|
|
|
unlocked[req] = nil
|
2023-08-11 16:00:36 -06:00
|
|
|
}
|
2023-08-11 13:01:32 -06:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
return &LockableExt{
|
2023-08-15 18:23:06 -06:00
|
|
|
State: Unlocked,
|
2023-07-27 18:37:06 -06:00
|
|
|
Owner: nil,
|
2023-07-27 19:53:43 -06:00
|
|
|
PendingOwner: nil,
|
2023-08-11 13:01:32 -06:00
|
|
|
Requirements: reqs,
|
2024-03-23 02:21:27 -06:00
|
|
|
Waiting: WaitMap{},
|
2024-03-30 23:57:18 -06:00
|
|
|
|
|
|
|
Locked: map[NodeID]any{},
|
|
|
|
Unlocked: unlocked,
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
func UnlockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
2024-03-23 02:21:27 -06:00
|
|
|
signal := NewUnlockSignal()
|
2024-03-31 00:36:50 -06:00
|
|
|
messages := []Message{{node.ID, signal}}
|
2024-03-04 17:30:42 -07:00
|
|
|
return signal.ID(), ctx.Send(node, messages)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
|
|
|
|
2023-10-01 20:45:44 -06:00
|
|
|
func LockLockable(ctx *Context, node *Node) (uuid.UUID, error) {
|
2024-03-23 02:21:27 -06:00
|
|
|
signal := NewLockSignal()
|
2024-03-31 00:36:50 -06:00
|
|
|
messages := []Message{{node.ID, signal}}
|
2024-03-04 17:30:42 -07:00
|
|
|
return signal.ID(), ctx.Send(node, messages)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2024-03-03 16:37:03 -07:00
|
|
|
func (ext *LockableExt) Load(ctx *Context, node *Node) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *LockableExt) Unload(ctx *Context, node *Node) {
|
2024-03-23 02:21:27 -06:00
|
|
|
return
|
2024-03-03 16:37:03 -07:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
// Handle link signal by adding/removing the requested NodeID
|
|
|
|
// returns an error if the node is not unlocked
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID, signal *LinkSignal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-03 16:37:03 -07:00
|
|
|
var changes Changes = nil
|
2023-10-01 20:45:44 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
switch ext.State {
|
|
|
|
case Unlocked:
|
2023-08-31 19:50:32 -06:00
|
|
|
switch signal.Action {
|
2023-08-15 19:17:13 -06:00
|
|
|
case "add":
|
2023-08-31 19:50:32 -06:00
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
2023-08-15 19:17:13 -06:00
|
|
|
if exists == true {
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_requirement")})
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-15 19:28:15 -06:00
|
|
|
if ext.Requirements == nil {
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements = map[NodeID]ReqState{}
|
2023-08-15 19:28:15 -06:00
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
ext.Requirements[signal.NodeID] = Unlocked
|
2024-03-21 14:13:54 -06:00
|
|
|
changes = append(changes, "requirements")
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
case "remove":
|
2023-08-31 19:50:32 -06:00
|
|
|
_, exists := ext.Requirements[signal.NodeID]
|
2023-08-15 19:17:13 -06:00
|
|
|
if exists == false {
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
2023-08-15 19:17:13 -06:00
|
|
|
} else {
|
2023-08-31 19:50:32 -06:00
|
|
|
delete(ext.Requirements, signal.NodeID)
|
2024-03-21 14:13:54 -06:00
|
|
|
changes = append(changes, "requirements")
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.ID())})
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
default:
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "unknown_action")})
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
default:
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_unlocked: %s", ext.State)})
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-08-15 19:17:13 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
// Handle an UnlockSignal by either transitioning to Unlocked state,
|
|
|
|
// sending unlock signals to requirements, or returning an error signal
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) HandleUnlockSignal(ctx *Context, node *Node, source NodeID, signal *UnlockSignal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-23 02:21:27 -06:00
|
|
|
var changes Changes = nil
|
2023-10-01 20:45:44 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
switch ext.State {
|
|
|
|
case Locked:
|
|
|
|
if source != *ext.Owner {
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_owner")})
|
2023-11-03 22:51:54 -06:00
|
|
|
} else {
|
2024-03-23 02:21:27 -06:00
|
|
|
if len(ext.Requirements) == 0 {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "owner", "pending_owner")
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.Owner = nil
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.PendingOwner = nil
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.State = Unlocked
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
|
2024-03-23 02:21:27 -06:00
|
|
|
} else {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "waiting", "requirements", "pending_owner")
|
2024-03-23 02:21:27 -06:00
|
|
|
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.ReqID = &signal.Id
|
2024-03-23 02:21:27 -06:00
|
|
|
|
|
|
|
ext.State = Unlocking
|
|
|
|
for id := range(ext.Requirements) {
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
2023-08-15 18:23:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
default:
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_locked")})
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-10-01 20:45:44 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
// Handle a LockSignal by either transitioning to a locked state,
|
|
|
|
// sending lock signals to requirements, or returning an error signal
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID, signal *LockSignal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-23 02:21:27 -06:00
|
|
|
var changes Changes = nil
|
2023-10-07 23:00:07 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
switch ext.State {
|
|
|
|
case Unlocked:
|
|
|
|
if len(ext.Requirements) == 0 {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "owner", "pending_owner")
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.Owner = &source
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.PendingOwner = &source
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.State = Locked
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewSuccessSignal(signal.Id)})
|
2024-03-23 02:21:27 -06:00
|
|
|
} else {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "requirements", "waiting", "pending_owner")
|
2023-11-03 22:51:54 -06:00
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.PendingOwner = &source
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.ReqID = &signal.Id
|
2024-03-23 02:21:27 -06:00
|
|
|
|
|
|
|
ext.State = Locking
|
|
|
|
for id := range(ext.Requirements) {
|
|
|
|
lock_signal := NewLockSignal()
|
|
|
|
|
|
|
|
ext.Waiting[lock_signal.Id] = id
|
|
|
|
ext.Requirements[id] = Locking
|
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{id, lock_signal})
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 18:37:06 -06:00
|
|
|
default:
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.Id, "not_unlocked: %s", ext.State)})
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
return messages, changes
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 02:21:27 -06:00
|
|
|
// Handle an error signal by aborting the lock, or retrying the unlock
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeID, signal *ErrorSignal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-23 02:21:27 -06:00
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
id, waiting := ext.Waiting[signal.ReqID]
|
|
|
|
if waiting == true {
|
|
|
|
delete(ext.Waiting, signal.ReqID)
|
|
|
|
changes = append(changes, "waiting")
|
|
|
|
|
|
|
|
switch ext.State {
|
|
|
|
case Locking:
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "requirements")
|
2024-03-23 02:21:27 -06:00
|
|
|
|
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
for req_id, req_state := range(ext.Requirements) {
|
|
|
|
// Unlock locked requirements, and count unlocked requirements
|
|
|
|
switch req_state {
|
|
|
|
case Locked:
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
|
|
|
|
ext.Waiting[unlock_signal.Id] = req_id
|
|
|
|
ext.Requirements[req_id] = Unlocking
|
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{req_id, unlock_signal})
|
2024-03-23 02:21:27 -06:00
|
|
|
case Unlocked:
|
|
|
|
unlocked += 1
|
2023-11-11 14:21:10 -07:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "owner", "state")
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.State = Unlocked
|
|
|
|
ext.Owner = nil
|
|
|
|
} else {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state")
|
2024-03-23 02:51:46 -06:00
|
|
|
ext.State = AbortingLock
|
2024-03-23 02:21:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case Unlocking:
|
2024-03-23 02:51:46 -06:00
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
2024-03-23 02:51:46 -06:00
|
|
|
|
|
|
|
case AbortingLock:
|
2024-03-23 02:21:27 -06:00
|
|
|
req_state := ext.Requirements[id]
|
|
|
|
// Mark failed lock as Unlocked, or retry unlock
|
|
|
|
switch req_state {
|
2023-11-03 22:54:28 -06:00
|
|
|
case Locking:
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.Requirements[id] = Unlocked
|
|
|
|
|
|
|
|
// Check if all requirements unlocked now
|
|
|
|
unlocked := 0
|
|
|
|
for _, req_state := range(ext.Requirements) {
|
|
|
|
if req_state == Unlocked {
|
|
|
|
unlocked += 1
|
2023-11-03 22:54:28 -06:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
|
|
|
|
if unlocked == len(ext.Requirements) {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "owner", "state")
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.State = Unlocked
|
|
|
|
ext.Owner = nil
|
2023-11-13 13:23:58 -07:00
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
case Unlocking:
|
|
|
|
// Handle error for unlocking requirement while unlocking by retrying unlock
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
2024-03-23 02:21:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle a success signal by checking if all requirements have been locked/unlocked
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source NodeID, signal *SuccessSignal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-23 02:21:27 -06:00
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
id, waiting := ext.Waiting[signal.ReqID]
|
|
|
|
if waiting == true {
|
|
|
|
delete(ext.Waiting, signal.ReqID)
|
|
|
|
changes = append(changes, "waiting")
|
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
switch ext.State {
|
2024-03-23 02:21:27 -06:00
|
|
|
case Locking:
|
|
|
|
ext.Requirements[id] = Locked
|
2024-03-30 23:57:18 -06:00
|
|
|
ext.Locked[id] = nil
|
|
|
|
delete(ext.Unlocked, id)
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
if len(ext.Locked) == len(ext.Requirements) {
|
|
|
|
ctx.Log.Logf("lockable", "%s FULL_LOCK: %d", node.ID, len(ext.Locked))
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "owner", "req_id")
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.State = Locked
|
|
|
|
|
2024-03-30 15:42:06 -06:00
|
|
|
ext.Owner = ext.PendingOwner
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.ReqID = nil
|
2024-03-28 20:28:07 -06:00
|
|
|
} else {
|
2024-03-30 23:57:18 -06:00
|
|
|
ctx.Log.Logf("lockable", "%s PARTIAL_LOCK: %d/%d", node.ID, len(ext.Locked), len(ext.Requirements))
|
2024-03-23 02:21:27 -06:00
|
|
|
}
|
2024-03-23 02:51:46 -06:00
|
|
|
case AbortingLock:
|
|
|
|
req_state := ext.Requirements[id]
|
|
|
|
switch req_state {
|
|
|
|
case Locking:
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
unlock_signal := NewUnlockSignal()
|
|
|
|
ext.Waiting[unlock_signal.Id] = id
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{id, unlock_signal})
|
2024-03-23 02:51:46 -06:00
|
|
|
case Unlocking:
|
|
|
|
ext.Requirements[id] = Unlocked
|
2024-03-30 23:57:18 -06:00
|
|
|
ext.Unlocked[id] = nil
|
|
|
|
delete(ext.Locked, id)
|
2024-03-23 02:51:46 -06:00
|
|
|
|
|
|
|
unlocked := 0
|
|
|
|
for _, req_state := range(ext.Requirements) {
|
|
|
|
switch req_state {
|
|
|
|
case Unlocked:
|
|
|
|
unlocked += 1
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2024-03-23 02:51:46 -06:00
|
|
|
if unlocked == len(ext.Requirements) {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "pending_owner", "req_id")
|
2024-03-23 02:21:27 -06:00
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{*ext.PendingOwner, NewErrorSignal(*ext.ReqID, "not_unlocked: %s", ext.State)})
|
2024-03-23 02:51:46 -06:00
|
|
|
ext.State = Unlocked
|
2024-03-23 02:21:27 -06:00
|
|
|
ext.ReqID = nil
|
2024-03-23 02:51:46 -06:00
|
|
|
ext.PendingOwner = nil
|
2023-11-13 13:23:58 -07:00
|
|
|
}
|
2023-11-03 22:54:28 -06:00
|
|
|
}
|
2024-03-23 02:51:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
case Unlocking:
|
|
|
|
ext.Requirements[id] = Unlocked
|
2024-03-30 23:57:18 -06:00
|
|
|
ext.Unlocked[id] = Unlocked
|
|
|
|
delete(ext.Locked, id)
|
2024-03-23 02:51:46 -06:00
|
|
|
|
2024-03-30 23:57:18 -06:00
|
|
|
if len(ext.Unlocked) == len(ext.Requirements) {
|
2024-03-28 20:28:07 -06:00
|
|
|
changes = append(changes, "state", "owner", "req_id")
|
2024-03-23 02:51:46 -06:00
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{*ext.Owner, NewSuccessSignal(*ext.ReqID)})
|
2024-03-23 02:51:46 -06:00
|
|
|
ext.State = Unlocked
|
|
|
|
ext.ReqID = nil
|
|
|
|
ext.Owner = nil
|
|
|
|
}
|
2023-11-03 22:51:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
|
|
|
}
|
|
|
|
|
2024-03-31 00:36:50 -06:00
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) ([]Message, Changes) {
|
|
|
|
var messages []Message = nil
|
2024-03-23 02:21:27 -06:00
|
|
|
var changes Changes = nil
|
2023-10-07 23:00:07 -06:00
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
switch sig := signal.(type) {
|
|
|
|
case *StatusSignal:
|
2024-03-23 02:21:27 -06:00
|
|
|
// Forward StatusSignals up to the owner(unless that would be a cycle)
|
2023-08-11 13:01:32 -06:00
|
|
|
if ext.Owner != nil {
|
2023-07-27 15:27:14 -06:00
|
|
|
if *ext.Owner != node.ID {
|
2024-03-31 00:36:50 -06:00
|
|
|
messages = append(messages, Message{*ext.Owner, signal})
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2024-03-04 17:30:42 -07:00
|
|
|
case *LinkSignal:
|
|
|
|
messages, changes = ext.HandleLinkSignal(ctx, node, source, sig)
|
|
|
|
case *LockSignal:
|
|
|
|
messages, changes = ext.HandleLockSignal(ctx, node, source, sig)
|
2024-03-23 02:21:27 -06:00
|
|
|
case *UnlockSignal:
|
|
|
|
messages, changes = ext.HandleUnlockSignal(ctx, node, source, sig)
|
2024-03-04 17:30:42 -07:00
|
|
|
case *ErrorSignal:
|
|
|
|
messages, changes = ext.HandleErrorSignal(ctx, node, source, sig)
|
|
|
|
case *SuccessSignal:
|
|
|
|
messages, changes = ext.HandleSuccessSignal(ctx, node, source, sig)
|
2023-07-24 17:07:27 -06:00
|
|
|
}
|
2023-11-11 13:53:41 -07:00
|
|
|
|
2024-03-04 17:30:42 -07:00
|
|
|
return messages, changes
|
2023-10-15 15:14:33 -06:00
|
|
|
}
|
|
|
|
|