2023-06-23 20:56:09 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-08-11 16:00:36 -06:00
|
|
|
"encoding/binary"
|
2023-06-23 20:56:09 -06:00
|
|
|
)
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
type ReqState int
|
|
|
|
const (
|
|
|
|
Unlocked = ReqState(0)
|
|
|
|
Unlocking = ReqState(1)
|
|
|
|
Locked = ReqState(2)
|
|
|
|
Locking = ReqState(3)
|
|
|
|
)
|
|
|
|
|
|
|
|
type LockableExt struct{
|
|
|
|
Owner *NodeID
|
|
|
|
PendingOwner *NodeID
|
|
|
|
Requirements map[NodeID]ReqState
|
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
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-11 16:00:36 -06:00
|
|
|
ret := make([]byte, 8 + (16 * 2) + (17 * len(ext.Requirements)))
|
|
|
|
if ext.Owner != nil {
|
|
|
|
bytes, err := ext.Owner.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(ret[0:16], bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ext.PendingOwner != nil {
|
|
|
|
bytes, err := ext.PendingOwner.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(ret[16:32], bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(ret[32:40], uint64(len(ext.Requirements)))
|
|
|
|
|
|
|
|
cur := 40
|
|
|
|
for req, state := range(ext.Requirements) {
|
|
|
|
bytes, err := req.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
copy(ret[cur:cur+16], bytes)
|
|
|
|
ret[cur+16] = byte(state)
|
|
|
|
cur += 17
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2023-08-01 20:55:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ext *LockableExt) Deserialize(ctx *Context, data []byte) error {
|
2023-08-11 16:00:36 -06:00
|
|
|
cur := 0
|
|
|
|
all_zero := true
|
|
|
|
for _, b := range(data[cur:cur+16]) {
|
|
|
|
if all_zero == true && b != 0x00 {
|
|
|
|
all_zero = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_zero == false {
|
|
|
|
tmp, err := IDFromBytes(data[cur:cur+16])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ext.Owner = &tmp
|
|
|
|
}
|
|
|
|
cur += 16
|
|
|
|
|
|
|
|
all_zero = true
|
|
|
|
for _, b := range(data[cur:cur+16]) {
|
|
|
|
if all_zero == true && b != 0x00 {
|
|
|
|
all_zero = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_zero == false {
|
|
|
|
tmp, err := IDFromBytes(data[cur:cur+16])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ext.PendingOwner = &tmp
|
|
|
|
}
|
|
|
|
cur += 16
|
|
|
|
|
|
|
|
num_requirements := int(binary.BigEndian.Uint64(data[cur:cur+8]))
|
|
|
|
cur += 8
|
|
|
|
|
|
|
|
if num_requirements != 0 {
|
|
|
|
ext.Requirements = map[NodeID]ReqState{}
|
|
|
|
}
|
|
|
|
for i := 0; i < num_requirements; i++ {
|
|
|
|
id, err := IDFromBytes(data[cur:cur+16])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cur += 16
|
|
|
|
state := ReqState(data[cur])
|
|
|
|
cur += 1
|
|
|
|
ext.Requirements[id] = state
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-26 00:18:11 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 13:01:32 -06:00
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
2023-08-11 16:00:36 -06:00
|
|
|
var reqs map[NodeID]ReqState = nil
|
|
|
|
if requirements != nil {
|
|
|
|
reqs = map[NodeID]ReqState{}
|
|
|
|
for _, id := range(requirements) {
|
|
|
|
reqs[id] = Unlocked
|
|
|
|
}
|
2023-08-11 13:01:32 -06:00
|
|
|
}
|
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-08-11 13:01:32 -06:00
|
|
|
Requirements: reqs,
|
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-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-08-10 23:43:10 -06:00
|
|
|
msgs = msgs.Add(node.ID, node.Key, NewLockSignal("unlock"), node.ID)
|
2023-08-08 14:00:17 -06:00
|
|
|
return ctx.Send(msgs)
|
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-08 14:00:17 -06:00
|
|
|
msgs := Messages{}
|
2023-08-10 23:43:10 -06:00
|
|
|
msgs = msgs.Add(node.ID, node.Key, NewLockSignal("lock"), node.ID)
|
2023-08-08 14:00:17 -06:00
|
|
|
return ctx.Send(msgs)
|
2023-07-27 18:37:06 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:04:18 -06:00
|
|
|
// Handle a LockSignal and update the extensions owner/requirement states
|
2023-08-08 14:00:17 -06:00
|
|
|
func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) Messages {
|
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)
|
|
|
|
|
2023-08-08 14:00:17 -06:00
|
|
|
messages := Messages{}
|
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-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_unlocked"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if source != *ext.Owner {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_owner"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if ext.PendingOwner == nil {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_unlocking"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
if len(ext.Requirements) == 0 {
|
|
|
|
ext.Owner = nil
|
|
|
|
ext.PendingOwner = nil
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlocked"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
} else {
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
for id, state := range(ext.Requirements) {
|
2023-08-11 16:00:36 -06:00
|
|
|
if state != Locked {
|
2023-08-11 13:01:32 -06:00
|
|
|
panic("NOT_LOCKED")
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
ext.Requirements[id] = Unlocking
|
2023-08-11 13:01:32 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlock"), id)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
if source != node.ID {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlocking"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "unlocking":
|
2023-08-11 16:00:36 -06:00
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != Unlocking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
}
|
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-08-11 16:00:36 -06:00
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != Unlocking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
} else {
|
|
|
|
ext.Requirements[source] = Unlocked
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
if ext.PendingOwner == nil {
|
|
|
|
unlocked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == Unlocked {
|
|
|
|
unlocked += 1
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
if len(ext.Requirements) == unlocked {
|
|
|
|
previous_owner := *ext.Owner
|
|
|
|
ext.Owner = nil
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlocked"), previous_owner)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != Locking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
} else {
|
|
|
|
ext.Requirements[source] = Locked
|
2023-07-27 22:25:00 -06:00
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
if ext.PendingOwner != nil {
|
|
|
|
locked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == Locked {
|
|
|
|
locked += 1
|
|
|
|
}
|
2023-07-27 22:25:00 -06:00
|
|
|
}
|
|
|
|
|
2023-08-11 16:00:36 -06:00
|
|
|
if len(ext.Requirements) == locked {
|
|
|
|
ext.Owner = ext.PendingOwner
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("locked"), *ext.Owner)
|
|
|
|
}
|
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":
|
2023-08-11 16:00:36 -06:00
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != Locking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
}
|
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-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_locked"), source)
|
2023-07-27 22:25:00 -06:00
|
|
|
} else if ext.PendingOwner != nil {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "already_locking"), source)
|
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-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("locked"), source)
|
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-08-11 16:00:36 -06:00
|
|
|
log.Logf("lockable_detail", "LOCK_REQ: %s sending 'lock' to %s", node.ID, id)
|
|
|
|
if state != Unlocked {
|
2023-08-11 13:01:32 -06:00
|
|
|
panic("NOT_UNLOCKED")
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
ext.Requirements[id] = Locking
|
2023-08-11 13:01:32 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("lock"), id)
|
2023-07-27 19:53:43 -06:00
|
|
|
}
|
2023-08-11 16:00:36 -06:00
|
|
|
log.Logf("lockable", "LOCK_REQ: %s sending 'lock' to %d requirements", node.ID, len(ext.Requirements))
|
2023-07-27 19:53:43 -06:00
|
|
|
if source != node.ID {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("locking"), source)
|
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
|
|
|
return messages
|
2023-07-27 18:37:06 -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-08 14:00:17 -06:00
|
|
|
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) Messages {
|
|
|
|
messages := Messages{}
|
|
|
|
switch signal.Direction() {
|
2023-07-22 20:21:17 -06:00
|
|
|
case Up:
|
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 {
|
2023-08-10 23:43:10 -06:00
|
|
|
messages = messages.Add(node.ID, node.Key, signal, *ext.Owner)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
|
|
|
case Down:
|
2023-08-11 13:01:32 -06:00
|
|
|
for requirement, _ := range(ext.Requirements) {
|
|
|
|
messages = messages.Add(node.ID, node.Key, signal, requirement)
|
2023-07-27 12:20:49 -06:00
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
case Direct:
|
2023-08-08 14:00:17 -06:00
|
|
|
switch signal.Type() {
|
2023-07-27 18:37:06 -06:00
|
|
|
case LockSignalType:
|
2023-08-08 14:00:17 -06:00
|
|
|
messages = ext.HandleLockSignal(ctx.Log, node, source, signal.(*StringSignal))
|
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
|
|
|
}
|
|
|
|
|