graphvent/lockable.go

324 lines
9.2 KiB
Go

package graphvent
import (
2023-08-11 16:00:36 -06:00
"encoding/binary"
)
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
}
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-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
}
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-07-26 11:56:10 -06:00
return &LockableExt{
Owner: nil,
PendingOwner: nil,
Requirements: reqs,
2023-07-26 11:56:10 -06:00
}
}
// Send the signal to unlock a node from itself
func UnlockLockable(ctx *Context, node *Node) error {
msgs := Messages{}
2023-08-10 23:43:10 -06:00
msgs = msgs.Add(node.ID, node.Key, NewLockSignal("unlock"), node.ID)
return ctx.Send(msgs)
}
// Send the signal to lock a node from itself
func LockLockable(ctx *Context, node *Node) error {
msgs := Messages{}
2023-08-10 23:43:10 -06:00
msgs = msgs.Add(node.ID, node.Key, NewLockSignal("lock"), node.ID)
return ctx.Send(msgs)
}
// Handle a LockSignal and update the extensions owner/requirement states
func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID, signal *StringSignal) Messages {
2023-07-30 23:42:47 -06:00
state := signal.Str
log.Logf("lockable", "LOCK_SIGNAL: %s->%s %+v", source, node.ID, signal)
messages := Messages{}
switch state {
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)
} 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)
} 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)
} 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)
} else {
ext.PendingOwner = nil
for id, state := range(ext.Requirements) {
2023-08-11 16:00:36 -06:00
if state != Locked {
panic("NOT_LOCKED")
}
2023-08-11 16:00:36 -06:00
ext.Requirements[id] = Unlocking
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlock"), id)
}
if source != node.ID {
2023-08-10 23:43:10 -06:00
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlocking"), source)
}
}
}
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)
}
}
case "unlocked":
if source == node.ID {
return nil
}
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-08-11 16:00:36 -06:00
if ext.PendingOwner == nil {
unlocked := 0
for _, s := range(ext.Requirements) {
if s == Unlocked {
unlocked += 1
}
}
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)
}
}
}
}
case "locked":
if source == node.ID {
return nil
}
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-08-11 16:00:36 -06:00
if ext.PendingOwner != nil {
locked := 0
for _, s := range(ext.Requirements) {
if s == Locked {
locked += 1
}
}
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)
}
}
}
}
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)
}
}
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)
} 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)
} else {
owner := source
if len(ext.Requirements) == 0 {
ext.Owner = &owner
ext.PendingOwner = ext.Owner
2023-08-10 23:43:10 -06:00
messages = messages.Add(node.ID, node.Key, NewLockSignal("locked"), source)
} else {
ext.PendingOwner = &owner
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 {
panic("NOT_UNLOCKED")
}
2023-08-11 16:00:36 -06:00
ext.Requirements[id] = Locking
messages = messages.Add(node.ID, node.Key, NewLockSignal("lock"), id)
}
2023-08-11 16:00:36 -06:00
log.Logf("lockable", "LOCK_REQ: %s sending 'lock' to %d requirements", node.ID, len(ext.Requirements))
if source != node.ID {
2023-08-10 23:43:10 -06:00
messages = messages.Add(node.ID, node.Key, NewLockSignal("locking"), source)
}
}
}
default:
log.Logf("lockable", "LOCK_ERR: unkown state %s", state)
}
return messages
}
// 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
func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) Messages {
messages := Messages{}
switch signal.Direction() {
case Up:
if ext.Owner != nil {
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:
for requirement, _ := range(ext.Requirements) {
messages = messages.Add(node.ID, node.Key, signal, requirement)
2023-07-27 12:20:49 -06:00
}
case Direct:
switch signal.Type() {
case LockSignalType:
messages = ext.HandleLockSignal(ctx.Log, node, source, signal.(*StringSignal))
2023-07-27 16:21:27 -06:00
default:
}
default:
2023-07-24 17:07:27 -06:00
}
return messages
2023-07-25 21:43:15 -06:00
}