2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// A Listener extension provides a channel that can receive signals on a different thread
|
2023-07-25 21:43:15 -06:00
|
|
|
type ListenerExt struct {
|
2023-07-26 11:56:10 -06:00
|
|
|
Buffer int
|
2023-08-07 20:26:02 -06:00
|
|
|
Chan chan Message
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Create a new listener extension with a given buffer size
|
2023-07-26 11:56:10 -06:00
|
|
|
func NewListenerExt(buffer int) *ListenerExt {
|
|
|
|
return &ListenerExt{
|
|
|
|
Buffer: buffer,
|
2023-08-07 20:26:02 -06:00
|
|
|
Chan: make(chan Message, buffer),
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
2023-06-23 22:19:43 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func (ext *ListenerExt) Field(name string) interface{} {
|
|
|
|
return ResolveFields(ext, name, map[string]func(*ListenerExt)interface{}{
|
|
|
|
"buffer": func(ext *ListenerExt) interface{} {
|
|
|
|
return ext.Buffer
|
|
|
|
},
|
|
|
|
"chan": func(ext *ListenerExt) interface{} {
|
|
|
|
return ext.Chan
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Simple load function, unmarshal the buffer int from json
|
2023-08-01 20:55:15 -06:00
|
|
|
func (ext *ListenerExt) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
err := json.Unmarshal(data, &ext.Buffer)
|
2023-08-07 20:26:02 -06:00
|
|
|
ext.Chan = make(chan Message, ext.Buffer)
|
2023-08-01 20:55:15 -06:00
|
|
|
return err
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:40:33 -06:00
|
|
|
func (listener *ListenerExt) Type() ExtType {
|
2023-07-25 21:43:15 -06:00
|
|
|
return ListenerExtType
|
2023-06-23 22:19:43 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Send the signal to the channel, logging an overflow if it occurs
|
2023-08-07 20:26:02 -06:00
|
|
|
func (ext *ListenerExt) Process(ctx *Context, node *Node, msg Message) []Message {
|
|
|
|
ctx.Log.Logf("listener", "LISTENER_PROCESS: %s - %+v", node.ID, msg.Signal)
|
2023-07-25 21:43:15 -06:00
|
|
|
select {
|
2023-08-07 20:26:02 -06:00
|
|
|
case ext.Chan <- msg:
|
2023-07-25 21:43:15 -06:00
|
|
|
default:
|
2023-07-27 15:27:14 -06:00
|
|
|
ctx.Log.Logf("listener", "LISTENER_OVERFLOW: %s", node.ID)
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return nil
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// ReqState holds the multiple states of a requirement
|
2023-07-30 10:09:04 -06:00
|
|
|
type LinkState struct {
|
2023-07-28 00:04:18 -06:00
|
|
|
Link string `json:"link"`
|
|
|
|
Lock string `json:"lock"`
|
2023-07-30 10:09:04 -06:00
|
|
|
Initiator NodeID `json:"initiator"`
|
2023-07-28 00:04:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A LockableExt allows a node to be linked to other nodes(via LinkSignal) and locked/unlocked(via LockSignal)
|
2023-07-30 10:09:04 -06:00
|
|
|
type LinkMap map[NodeID]LinkState
|
|
|
|
func (m LinkMap) MarshalJSON() ([]byte, error) {
|
|
|
|
tmp := map[string]LinkState{}
|
|
|
|
for id, state := range(m) {
|
|
|
|
tmp[id.String()] = state
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m LinkMap) UnmarshalJSON(data []byte) error {
|
|
|
|
tmp := map[string]LinkState{}
|
|
|
|
err := json.Unmarshal(data, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for id_str, state := range(tmp) {
|
|
|
|
id, err := ParseID(id_str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m[id] = state
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-28 00:04:18 -06:00
|
|
|
}
|
|
|
|
|
2023-07-30 10:09:04 -06:00
|
|
|
type LockableExt struct {
|
2023-07-28 00:04:18 -06:00
|
|
|
Owner *NodeID `json:"owner"`
|
|
|
|
PendingOwner *NodeID `json:"pending_owner"`
|
2023-07-30 10:09:04 -06:00
|
|
|
Requirements LinkMap `json:"requirements"`
|
|
|
|
Dependencies LinkMap `json:"dependencies"`
|
2023-07-28 00:04:18 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func (ext *LockableExt) Field(name string) interface{} {
|
|
|
|
return ResolveFields(ext, name, map[string]func(*LockableExt)interface{}{
|
|
|
|
"owner": func(ext *LockableExt) interface{} {
|
|
|
|
return ext.Owner
|
|
|
|
},
|
|
|
|
"pending_owner": func(ext *LockableExt) interface{} {
|
|
|
|
return ext.PendingOwner
|
|
|
|
},
|
|
|
|
"requirements": func(ext *LockableExt) interface{} {
|
|
|
|
return ext.Requirements
|
|
|
|
},
|
|
|
|
"dependencies": func(ext *LockableExt) interface{} {
|
|
|
|
return ext.Dependencies
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:08:43 -06:00
|
|
|
func (ext *ListenerExt) Serialize() ([]byte, error) {
|
2023-08-01 20:55:15 -06:00
|
|
|
return json.Marshal(ext.Buffer)
|
2023-06-23 20:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *LockableExt) Type() ExtType {
|
|
|
|
return LockableExtType
|
2023-06-28 21:49:23 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (ext *LockableExt) Serialize() ([]byte, error) {
|
2023-08-01 20:55:15 -06:00
|
|
|
return json.Marshal(ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *LockableExt) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
return json.Unmarshal(data, ext)
|
2023-07-26 00:18:11 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func NewLockableExt() *LockableExt {
|
2023-07-26 11:56:10 -06:00
|
|
|
return &LockableExt{
|
2023-07-27 18:37:06 -06:00
|
|
|
Owner: nil,
|
2023-07-27 19:53:43 -06:00
|
|
|
PendingOwner: nil,
|
2023-07-30 10:09:04 -06:00
|
|
|
Requirements: map[NodeID]LinkState{},
|
|
|
|
Dependencies: map[NodeID]LinkState{},
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Send the signal to unlock a node from itself
|
2023-07-27 22:25:00 -06:00
|
|
|
func UnlockLockable(ctx *Context, node *Node) error {
|
2023-08-07 20:26:02 -06:00
|
|
|
return ctx.Send(node.ID, []Message{Message{node.ID, NewLockSignal("unlock")}})
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Send the signal to lock a node from itself
|
2023-07-27 18:37:06 -06:00
|
|
|
func LockLockable(ctx *Context, node *Node) error {
|
2023-08-07 20:26:02 -06:00
|
|
|
return ctx.Send(node.ID, []Message{Message{node.ID, NewLockSignal("lock")}})
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Setup a node to send the initial requirement link signal, then send the signal
|
2023-07-28 12:46:06 -06:00
|
|
|
func LinkRequirement(ctx *Context, dependency NodeID, requirement NodeID) error {
|
2023-08-07 20:26:02 -06:00
|
|
|
return ctx.Send(dependency, []Message{Message{dependency, NewLinkStartSignal("req", requirement)}})
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
2023-07-27 16:21:27 -06:00
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Handle a LockSignal and update the extensions owner/requirement states
|
2023-08-07 20:26:02 -06:00
|
|
|
func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) []Message {
|
2023-07-30 23:42:47 -06:00
|
|
|
state := signal.Str
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LOCK_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
|
|
|
|
|
|
|
messages := []Message{}
|
2023-07-27 18:37:06 -06:00
|
|
|
switch state {
|
2023-07-27 22:25:00 -06:00
|
|
|
case "unlock":
|
|
|
|
if ext.Owner == nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_unlocked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if source != *ext.Owner {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_owner")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if ext.PendingOwner == nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_unlocking")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
ext.Owner = nil
|
|
|
|
ext.PendingOwner = nil
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewLockSignal("unlocked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state.Link == "linked" {
|
|
|
|
if state.Lock != "locked" {
|
|
|
|
panic("NOT_LOCKED")
|
|
|
|
}
|
|
|
|
state.Lock = "unlocking"
|
|
|
|
ext.Requirements[id] = state
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{id, NewLockSignal("unlock")})
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if source != node.ID {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewLockSignal("unlocking")})
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "unlocking":
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Link != "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_linked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Lock != "unlocking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_unlocking")})
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case "unlocked":
|
2023-07-27 19:53:43 -06:00
|
|
|
if source == node.ID {
|
2023-08-07 20:26:02 -06:00
|
|
|
return nil
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 22:25:00 -06:00
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Link != "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_linked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Lock != "unlocking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_unlocking")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
state.Lock = "unlocked"
|
|
|
|
ext.Requirements[source] = state
|
|
|
|
|
|
|
|
if ext.PendingOwner == nil {
|
|
|
|
linked := 0
|
|
|
|
unlocked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s.Link == "linked" {
|
|
|
|
linked += 1
|
|
|
|
}
|
|
|
|
if s.Lock == "unlocked" {
|
|
|
|
unlocked += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if linked == unlocked {
|
|
|
|
previous_owner := *ext.Owner
|
|
|
|
ext.Owner = nil
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{previous_owner, NewLockSignal("unlocked")})
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
case "locked":
|
|
|
|
if source == node.ID {
|
2023-08-07 20:26:02 -06:00
|
|
|
return nil
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Link != "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_linked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Lock != "locking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_locking")})
|
2023-07-27 19:53:43 -06:00
|
|
|
} else {
|
2023-07-27 22:25:00 -06:00
|
|
|
state.Lock = "locked"
|
|
|
|
ext.Requirements[source] = state
|
|
|
|
|
|
|
|
if ext.PendingOwner != nil {
|
|
|
|
linked := 0
|
|
|
|
locked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s.Link == "linked" {
|
|
|
|
linked += 1
|
|
|
|
}
|
|
|
|
if s.Lock == "locked" {
|
|
|
|
locked += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if linked == locked {
|
|
|
|
ext.Owner = ext.PendingOwner
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{*ext.Owner, NewLockSignal("locked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
case "locking":
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_requirement")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Link != "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_linked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if state.Lock != "locking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_locking")})
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-07-27 19:53:43 -06:00
|
|
|
case "lock":
|
|
|
|
if ext.Owner != nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_locked")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if ext.PendingOwner != nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_locking")})
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
owner := source
|
2023-07-27 19:53:43 -06:00
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
ext.Owner = &owner
|
2023-07-27 22:25:00 -06:00
|
|
|
ext.PendingOwner = ext.Owner
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewLockSignal("locked")})
|
2023-07-27 19:53:43 -06:00
|
|
|
} else {
|
2023-07-27 22:25:00 -06:00
|
|
|
ext.PendingOwner = &owner
|
2023-07-27 19:53:43 -06:00
|
|
|
for id, state := range(ext.Requirements) {
|
2023-07-27 22:25:00 -06:00
|
|
|
if state.Link == "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LOCK_REQ: %s sending 'lock' to %s", node.ID, id)
|
2023-07-27 22:25:00 -06:00
|
|
|
if state.Lock != "unlocked" {
|
|
|
|
panic("NOT_UNLOCKED")
|
|
|
|
}
|
|
|
|
state.Lock = "locking"
|
|
|
|
ext.Requirements[id] = state
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{id, NewLockSignal("lock")})
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if source != node.ID {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewLockSignal("locking")})
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 18:37:06 -06:00
|
|
|
default:
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LOCK_ERR: unkown state %s", state)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LOCK_MESSAGES: %+v", messages)
|
|
|
|
return messages
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
func (ext *LockableExt) HandleLinkStartSignal(log Logger, node *Node, source NodeID, signal *IDStringSignal) []Message {
|
2023-07-30 23:42:47 -06:00
|
|
|
link_type := signal.Str
|
2023-07-31 18:29:26 -06:00
|
|
|
target := signal.NodeID
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LINK_START_SIGNAL: %s->%s %s %s", source, node.ID, link_type, target)
|
|
|
|
|
|
|
|
messages := []Message{}
|
2023-07-28 12:46:06 -06:00
|
|
|
switch link_type {
|
|
|
|
case "req":
|
|
|
|
state, exists := ext.Requirements[target]
|
|
|
|
_, dep_exists := ext.Dependencies[target]
|
|
|
|
if ext.Owner != nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already locked")})
|
2023-07-28 12:46:06 -06:00
|
|
|
} else if ext.Owner != ext.PendingOwner {
|
|
|
|
if ext.PendingOwner == nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "unlocking")})
|
2023-07-28 12:46:06 -06:00
|
|
|
} else {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "locking")})
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
} else if exists == true {
|
|
|
|
if state.Link == "linking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_linking_req")})
|
2023-07-28 12:46:06 -06:00
|
|
|
} else if state.Link == "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_req")})
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
} else if dep_exists == true {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_dep")})
|
2023-07-28 12:46:06 -06:00
|
|
|
} else {
|
2023-07-30 10:09:04 -06:00
|
|
|
ext.Requirements[target] = LinkState{"linking", "unlocked", source}
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{target, NewLinkSignal("linked_as_req")})
|
|
|
|
messages = append(messages, Message{source, NewLinkStartSignal("linking_req", target)})
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return messages
|
2023-07-28 12:46:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Handle LinkSignal, updating the extensions requirements and dependencies as necessary
|
|
|
|
// TODO: Add unlink
|
2023-08-07 20:26:02 -06:00
|
|
|
func (ext *LockableExt) HandleLinkSignal(log Logger, node *Node, source NodeID, signal *StringSignal) []Message {
|
|
|
|
log.Logf("lockable", "LINK_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
2023-07-30 23:42:47 -06:00
|
|
|
state := signal.Str
|
2023-08-07 20:26:02 -06:00
|
|
|
|
|
|
|
messages := []Message{}
|
2023-07-27 18:08:43 -06:00
|
|
|
switch state {
|
2023-08-07 20:26:02 -06:00
|
|
|
case "dep_done":
|
2023-07-27 23:05:19 -06:00
|
|
|
state, exists := ext.Requirements[source]
|
2023-08-07 20:26:02 -06:00
|
|
|
if exists == false {
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "not_linking")})
|
2023-07-27 23:05:19 -06:00
|
|
|
} else if state.Link == "linking" {
|
|
|
|
state.Link = "linked"
|
|
|
|
ext.Requirements[source] = state
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "FINISHED_LINKING_REQ: %s->%s", node.ID, source)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
2023-07-30 10:09:04 -06:00
|
|
|
case "linked_as_req":
|
2023-07-27 23:05:19 -06:00
|
|
|
state, exists := ext.Dependencies[source]
|
2023-08-07 20:26:02 -06:00
|
|
|
if exists == false {
|
|
|
|
ext.Dependencies[source] = LinkState{"linked", "unlocked", source}
|
|
|
|
messages = append(messages, Message{source, NewLinkSignal("dep_done")})
|
2023-07-30 10:09:04 -06:00
|
|
|
} else if state.Link == "linking" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_linking")})
|
|
|
|
} else if state.Link == "linked" {
|
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "already_linked")})
|
2023-07-27 23:05:19 -06:00
|
|
|
} else if ext.PendingOwner != ext.Owner {
|
|
|
|
if ext.Owner == nil {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "locking")})
|
2023-07-27 23:05:19 -06:00
|
|
|
} else {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{source, NewErrorSignal(signal.ID(), "unlocking")})
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
2023-07-27 18:16:37 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 18:08:43 -06:00
|
|
|
default:
|
2023-08-07 20:26:02 -06:00
|
|
|
log.Logf("lockable", "LINK_ERROR: unknown state %s", state)
|
2023-07-27 18:08:43 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return messages
|
2023-07-27 16:21:27 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// LockableExts process Up/Down signals by forwarding them to owner, dependency, and requirement nodes
|
|
|
|
// LockSignal and LinkSignal Direct signals are processed to update the requirement/dependency/lock state
|
2023-08-07 20:26:02 -06:00
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, msg Message) []Message {
|
|
|
|
messages := []Message{}
|
|
|
|
switch msg.Signal.Direction() {
|
2023-07-22 20:21:17 -06:00
|
|
|
case Up:
|
2023-08-07 20:26:02 -06:00
|
|
|
ctx.Log.Logf("lockable", "LOCKABLE_DEPENDENCIES: %+v", ext.Dependencies)
|
2023-07-27 12:20:49 -06:00
|
|
|
owner_sent := false
|
2023-07-27 19:53:43 -06:00
|
|
|
for dependency, state := range(ext.Dependencies) {
|
2023-07-30 10:09:04 -06:00
|
|
|
if state.Link == "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{dependency, msg.Signal})
|
2023-07-27 19:53:43 -06:00
|
|
|
if ext.Owner != nil {
|
|
|
|
if dependency == *ext.Owner {
|
|
|
|
owner_sent = true
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
|
2023-07-27 12:20:49 -06:00
|
|
|
if ext.Owner != nil && owner_sent == false {
|
2023-07-27 15:27:14 -06:00
|
|
|
if *ext.Owner != node.ID {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{*ext.Owner, msg.Signal})
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
|
|
|
case Down:
|
2023-07-27 19:53:43 -06:00
|
|
|
for requirement, state := range(ext.Requirements) {
|
2023-07-27 22:25:00 -06:00
|
|
|
if state.Link == "linked" {
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = append(messages, Message{requirement, msg.Signal})
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
case Direct:
|
2023-08-07 20:26:02 -06:00
|
|
|
switch msg.Signal.Type() {
|
2023-07-27 18:08:43 -06:00
|
|
|
case LinkSignalType:
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = ext.HandleLinkSignal(ctx.Log, node, msg.NodeID, msg.Signal.(*StringSignal))
|
2023-07-27 18:37:06 -06:00
|
|
|
case LockSignalType:
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = ext.HandleLockSignal(ctx.Log, node, msg.NodeID, msg.Signal.(*StringSignal))
|
2023-07-28 12:46:06 -06:00
|
|
|
case LinkStartSignalType:
|
2023-08-07 20:26:02 -06:00
|
|
|
messages = ext.HandleLinkStartSignal(ctx.Log, node, msg.NodeID, msg.Signal.(*IDStringSignal))
|
2023-07-27 16:21:27 -06:00
|
|
|
default:
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
default:
|
2023-07-24 17:07:27 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return messages
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
|
|
|
|