|
|
@ -1,13 +1,21 @@
|
|
|
|
package graphvent
|
|
|
|
package graphvent
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ReqState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
Unlocked = ReqState(0)
|
|
|
|
|
|
|
|
Unlocking = ReqState(1)
|
|
|
|
|
|
|
|
Locked = ReqState(2)
|
|
|
|
|
|
|
|
Locking = ReqState(3)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type LockableExt struct{
|
|
|
|
type LockableExt struct{
|
|
|
|
Owner *NodeID `json:"owner"`
|
|
|
|
Owner *NodeID
|
|
|
|
PendingOwner *NodeID `json:"pending_owner"`
|
|
|
|
PendingOwner *NodeID
|
|
|
|
Requirements map[NodeID]string `json:"requirements"`
|
|
|
|
Requirements map[NodeID]ReqState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Field(name string) interface{} {
|
|
|
|
func (ext *LockableExt) Field(name string) interface{} {
|
|
|
@ -29,17 +37,97 @@ func (ext *LockableExt) Type() ExtType {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Serialize() ([]byte, error) {
|
|
|
|
func (ext *LockableExt) Serialize() ([]byte, error) {
|
|
|
|
return json.Marshal(ext)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
func (ext *LockableExt) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
return json.Unmarshal(data, ext)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
|
|
|
func NewLockableExt(requirements []NodeID) *LockableExt {
|
|
|
|
reqs := map[NodeID]string{}
|
|
|
|
var reqs map[NodeID]ReqState = nil
|
|
|
|
|
|
|
|
if requirements != nil {
|
|
|
|
|
|
|
|
reqs = map[NodeID]ReqState{}
|
|
|
|
for _, id := range(requirements) {
|
|
|
|
for _, id := range(requirements) {
|
|
|
|
reqs[id] = "unlocked"
|
|
|
|
reqs[id] = Unlocked
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &LockableExt{
|
|
|
|
return &LockableExt{
|
|
|
|
Owner: nil,
|
|
|
|
Owner: nil,
|
|
|
@ -84,10 +172,10 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
ext.PendingOwner = nil
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
if state != "locked" {
|
|
|
|
if state != Locked {
|
|
|
|
panic("NOT_LOCKED")
|
|
|
|
panic("NOT_LOCKED")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ext.Requirements[id] = "unlocking"
|
|
|
|
ext.Requirements[id] = Unlocking
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlock"), id)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("unlock"), id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if source != node.ID {
|
|
|
|
if source != node.ID {
|
|
|
@ -96,30 +184,33 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "unlocking":
|
|
|
|
case "unlocking":
|
|
|
|
|
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != "unlocking" {
|
|
|
|
} else if state != Unlocking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case "unlocked":
|
|
|
|
case "unlocked":
|
|
|
|
if source == node.ID {
|
|
|
|
if source == node.ID {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != "unlocking" {
|
|
|
|
} else if state != Unlocking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_unlocking"), source)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.Requirements[source] = "unlocked"
|
|
|
|
ext.Requirements[source] = Unlocked
|
|
|
|
|
|
|
|
|
|
|
|
if ext.PendingOwner == nil {
|
|
|
|
if ext.PendingOwner == nil {
|
|
|
|
unlocked := 0
|
|
|
|
unlocked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == "unlocked" {
|
|
|
|
if s == Unlocked {
|
|
|
|
unlocked += 1
|
|
|
|
unlocked += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -131,23 +222,25 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
case "locked":
|
|
|
|
case "locked":
|
|
|
|
if source == node.ID {
|
|
|
|
if source == node.ID {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != "locking" {
|
|
|
|
} else if state != Locking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.Requirements[source] = "locked"
|
|
|
|
ext.Requirements[source] = Locked
|
|
|
|
|
|
|
|
|
|
|
|
if ext.PendingOwner != nil {
|
|
|
|
if ext.PendingOwner != nil {
|
|
|
|
locked := 0
|
|
|
|
locked := 0
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
for _, s := range(ext.Requirements) {
|
|
|
|
if s == "locked" {
|
|
|
|
if s == Locked {
|
|
|
|
locked += 1
|
|
|
|
locked += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -158,13 +251,16 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
case "locking":
|
|
|
|
case "locking":
|
|
|
|
|
|
|
|
if ext.Requirements != nil {
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == false {
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_requirement"), source)
|
|
|
|
} else if state != "locking" {
|
|
|
|
} else if state != Locking {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewErrorSignal(signal.ID(), "not_locking"), source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case "lock":
|
|
|
|
case "lock":
|
|
|
|
if ext.Owner != nil {
|
|
|
|
if ext.Owner != nil {
|
|
|
@ -180,13 +276,14 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.PendingOwner = &owner
|
|
|
|
ext.PendingOwner = &owner
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
for id, state := range(ext.Requirements) {
|
|
|
|
log.Logf("lockable", "LOCK_REQ: %s sending 'lock' to %s", node.ID, id)
|
|
|
|
log.Logf("lockable_detail", "LOCK_REQ: %s sending 'lock' to %s", node.ID, id)
|
|
|
|
if state != "unlocked" {
|
|
|
|
if state != Unlocked {
|
|
|
|
panic("NOT_UNLOCKED")
|
|
|
|
panic("NOT_UNLOCKED")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ext.Requirements[id] = "locking"
|
|
|
|
ext.Requirements[id] = Locking
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("lock"), id)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("lock"), id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Logf("lockable", "LOCK_REQ: %s sending 'lock' to %d requirements", node.ID, len(ext.Requirements))
|
|
|
|
if source != node.ID {
|
|
|
|
if source != node.ID {
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("locking"), source)
|
|
|
|
messages = messages.Add(node.ID, node.Key, NewLockSignal("locking"), source)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -195,7 +292,6 @@ func (ext *LockableExt) HandleLockSignal(log Logger, node *Node, source NodeID,
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
log.Logf("lockable", "LOCK_ERR: unkown state %s", state)
|
|
|
|
log.Logf("lockable", "LOCK_ERR: unkown state %s", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Logf("lockable", "LOCK_MESSAGES: %+v", messages)
|
|
|
|
|
|
|
|
return messages
|
|
|
|
return messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|