|
|
@ -56,24 +56,46 @@ func (ext *ListenerExt) Process(ctx *Context, princ_id NodeID, node *Node, signa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ReqState holds the multiple states of a requirement
|
|
|
|
// ReqState holds the multiple states of a requirement
|
|
|
|
type ReqState struct {
|
|
|
|
type LinkState struct {
|
|
|
|
Link string `json:"link"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Lock string `json:"lock"`
|
|
|
|
Lock string `json:"lock"`
|
|
|
|
|
|
|
|
Initiator NodeID `json:"initiator"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A LockableExt allows a node to be linked to other nodes(via LinkSignal) and locked/unlocked(via LockSignal)
|
|
|
|
// A LockableExt allows a node to be linked to other nodes(via LinkSignal) and locked/unlocked(via LockSignal)
|
|
|
|
type LockableExt struct {
|
|
|
|
type LinkMap map[NodeID]LinkState
|
|
|
|
Owner *NodeID
|
|
|
|
func (m LinkMap) MarshalJSON() ([]byte, error) {
|
|
|
|
PendingOwner *NodeID
|
|
|
|
tmp := map[string]LinkState{}
|
|
|
|
Requirements map[NodeID]ReqState
|
|
|
|
for id, state := range(m) {
|
|
|
|
Dependencies map[NodeID]string
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type LockableExtJSON struct {
|
|
|
|
type LockableExt struct {
|
|
|
|
Owner *NodeID `json:"owner"`
|
|
|
|
Owner *NodeID `json:"owner"`
|
|
|
|
PendingOwner *NodeID `json:"pending_owner"`
|
|
|
|
PendingOwner *NodeID `json:"pending_owner"`
|
|
|
|
Requirements map[string]ReqState `json:"requirements"`
|
|
|
|
Requirements LinkMap `json:"requirements"`
|
|
|
|
Dependencies map[string]string `json:"dependencies"`
|
|
|
|
Dependencies LinkMap `json:"dependencies"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Field(name string) interface{} {
|
|
|
|
func (ext *LockableExt) Field(name string) interface{} {
|
|
|
@ -94,28 +116,13 @@ func (ext *LockableExt) Field(name string) interface{} {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func LoadLockableExt(ctx *Context, data []byte) (Extension, error) {
|
|
|
|
func LoadLockableExt(ctx *Context, data []byte) (Extension, error) {
|
|
|
|
var j LockableExtJSON
|
|
|
|
var ext LockableExt
|
|
|
|
err := json.Unmarshal(data, &j)
|
|
|
|
err := json.Unmarshal(data, &ext)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements, err := LoadIDMap(j.Requirements)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dependencies, err := LoadIDMap(j.Dependencies)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &LockableExt{
|
|
|
|
return &ext, nil
|
|
|
|
Owner: j.Owner,
|
|
|
|
|
|
|
|
PendingOwner: j.PendingOwner,
|
|
|
|
|
|
|
|
Requirements: requirements,
|
|
|
|
|
|
|
|
Dependencies: dependencies,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *ListenerExt) Serialize() ([]byte, error) {
|
|
|
|
func (ext *ListenerExt) Serialize() ([]byte, error) {
|
|
|
@ -127,20 +134,15 @@ func (ext *LockableExt) Type() ExtType {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) Serialize() ([]byte, error) {
|
|
|
|
func (ext *LockableExt) Serialize() ([]byte, error) {
|
|
|
|
return json.MarshalIndent(&LockableExtJSON{
|
|
|
|
return json.MarshalIndent(ext, "", " ")
|
|
|
|
Owner: ext.Owner,
|
|
|
|
|
|
|
|
PendingOwner: ext.PendingOwner,
|
|
|
|
|
|
|
|
Requirements: IDMap(ext.Requirements),
|
|
|
|
|
|
|
|
Dependencies: IDMap(ext.Dependencies),
|
|
|
|
|
|
|
|
}, "", " ")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewLockableExt() *LockableExt {
|
|
|
|
func NewLockableExt() *LockableExt {
|
|
|
|
return &LockableExt{
|
|
|
|
return &LockableExt{
|
|
|
|
Owner: nil,
|
|
|
|
Owner: nil,
|
|
|
|
PendingOwner: nil,
|
|
|
|
PendingOwner: nil,
|
|
|
|
Requirements: map[NodeID]ReqState{},
|
|
|
|
Requirements: map[NodeID]LinkState{},
|
|
|
|
Dependencies: map[NodeID]string{},
|
|
|
|
Dependencies: map[NodeID]LinkState{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -315,9 +317,9 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, source NodeID, node *Node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ext *LockableExt) HandleLinkStartSignal(ctx *Context, source NodeID, node *Node, signal LinkStartSignal) {
|
|
|
|
func (ext *LockableExt) HandleLinkStartSignal(ctx *Context, source NodeID, node *Node, signal IDStateSignal) {
|
|
|
|
ctx.Log.Logf("lockable", "LINK__START_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
|
|
|
ctx.Log.Logf("lockable", "LINK__START_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
|
|
|
link_type := signal.LinkType
|
|
|
|
link_type := signal.State
|
|
|
|
target := signal.ID
|
|
|
|
target := signal.ID
|
|
|
|
switch link_type {
|
|
|
|
switch link_type {
|
|
|
|
case "req":
|
|
|
|
case "req":
|
|
|
@ -340,8 +342,8 @@ func (ext *LockableExt) HandleLinkStartSignal(ctx *Context, source NodeID, node
|
|
|
|
} else if dep_exists == true {
|
|
|
|
} else if dep_exists == true {
|
|
|
|
ctx.Send(node.ID, source, NewLinkStartSignal("already_dep", target))
|
|
|
|
ctx.Send(node.ID, source, NewLinkStartSignal("already_dep", target))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.Requirements[target] = ReqState{"linking", "unlocked"}
|
|
|
|
ext.Requirements[target] = LinkState{"linking", "unlocked", source}
|
|
|
|
ctx.Send(node.ID, target, NewLinkSignal("link_as_req"))
|
|
|
|
ctx.Send(node.ID, target, NewLinkSignal("linked_as_req"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkStartSignal("linking_req", target))
|
|
|
|
ctx.Send(node.ID, source, NewLinkStartSignal("linking_req", target))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -353,14 +355,14 @@ func (ext *LockableExt) HandleLinkSignal(ctx *Context, source NodeID, node *Node
|
|
|
|
ctx.Log.Logf("lockable", "LINK_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
|
|
|
ctx.Log.Logf("lockable", "LINK_SIGNAL: %s->%s %+v", source, node.ID, signal)
|
|
|
|
state := signal.State
|
|
|
|
state := signal.State
|
|
|
|
switch state {
|
|
|
|
switch state {
|
|
|
|
case "link_as_dep":
|
|
|
|
case "linked_as_dep":
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
if exists == true && state.Link == "linked" {
|
|
|
|
if exists == true && state.Link == "linked" {
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("already_req"))
|
|
|
|
ctx.Send(node.ID, state.Initiator, NewLinkStartSignal("linked_as_req", source))
|
|
|
|
} else if state.Link == "linking" {
|
|
|
|
} else if state.Link == "linking" {
|
|
|
|
state.Link = "linked"
|
|
|
|
state.Link = "linked"
|
|
|
|
ext.Requirements[source] = state
|
|
|
|
ext.Requirements[source] = state
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_dep"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_req"))
|
|
|
|
} else if ext.PendingOwner != ext.Owner {
|
|
|
|
} else if ext.PendingOwner != ext.Owner {
|
|
|
|
if ext.Owner == nil {
|
|
|
|
if ext.Owner == nil {
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("locking"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("locking"))
|
|
|
@ -368,17 +370,19 @@ func (ext *LockableExt) HandleLinkSignal(ctx *Context, source NodeID, node *Node
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("unlocking"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("unlocking"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.Requirements[source] = ReqState{"linking", "unlocked"}
|
|
|
|
ext.Requirements[source] = LinkState{"linking", "unlocked", source}
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("link_as_req"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_req"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("lockable", "%s is a dependency of %s", node.ID, source)
|
|
|
|
|
|
|
|
|
|
|
|
case "link_as_req":
|
|
|
|
case "linked_as_req":
|
|
|
|
state, exists := ext.Dependencies[source]
|
|
|
|
state, exists := ext.Dependencies[source]
|
|
|
|
if exists == true && state == "linked" {
|
|
|
|
if exists == true && state.Link == "linked" {
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("already_dep"))
|
|
|
|
ctx.Send(node.ID, state.Initiator, NewLinkStartSignal("linked_as_dep", source))
|
|
|
|
} else if state == "linking" {
|
|
|
|
} else if state.Link == "linking" {
|
|
|
|
ext.Dependencies[source] = "linked"
|
|
|
|
state.Link = "linked"
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_req"))
|
|
|
|
ext.Dependencies[source] = state
|
|
|
|
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_dep"))
|
|
|
|
} else if ext.PendingOwner != ext.Owner {
|
|
|
|
} else if ext.PendingOwner != ext.Owner {
|
|
|
|
if ext.Owner == nil {
|
|
|
|
if ext.Owner == nil {
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("locking"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("locking"))
|
|
|
@ -386,28 +390,7 @@ func (ext *LockableExt) HandleLinkSignal(ctx *Context, source NodeID, node *Node
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("unlocking"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("unlocking"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
ext.Dependencies[source] = "linking"
|
|
|
|
ext.Dependencies[source] = LinkState{"linking", "unlocked", source}
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("link_as_dep"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case "linked_as_dep":
|
|
|
|
|
|
|
|
state, exists := ext.Dependencies[source]
|
|
|
|
|
|
|
|
if exists == false {
|
|
|
|
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("not_linking"))
|
|
|
|
|
|
|
|
} else if state == "linked" {
|
|
|
|
|
|
|
|
} else if state == "linking" {
|
|
|
|
|
|
|
|
ext.Dependencies[source] = "linked"
|
|
|
|
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_req"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Log.Logf("lockable", "%s is a dependency of %s", node.ID, source)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case "linked_as_req":
|
|
|
|
|
|
|
|
state, exists := ext.Requirements[source]
|
|
|
|
|
|
|
|
if exists == false {
|
|
|
|
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("not_linking"))
|
|
|
|
|
|
|
|
} else if state.Link == "linked" {
|
|
|
|
|
|
|
|
} else if state.Link == "linking" {
|
|
|
|
|
|
|
|
state.Link = "linked"
|
|
|
|
|
|
|
|
ext.Requirements[source] = state
|
|
|
|
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_dep"))
|
|
|
|
ctx.Send(node.ID, source, NewLinkSignal("linked_as_dep"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("lockable", "%s is a requirement of %s", node.ID, source)
|
|
|
|
ctx.Log.Logf("lockable", "%s is a requirement of %s", node.ID, source)
|
|
|
@ -424,7 +407,7 @@ func (ext *LockableExt) Process(ctx *Context, source NodeID, node *Node, signal
|
|
|
|
case Up:
|
|
|
|
case Up:
|
|
|
|
owner_sent := false
|
|
|
|
owner_sent := false
|
|
|
|
for dependency, state := range(ext.Dependencies) {
|
|
|
|
for dependency, state := range(ext.Dependencies) {
|
|
|
|
if state == "linked" {
|
|
|
|
if state.Link == "linked" {
|
|
|
|
err := ctx.Send(node.ID, dependency, signal)
|
|
|
|
err := ctx.Send(node.ID, dependency, signal)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("signal", "LOCKABLE_SIGNAL_ERR: %s->%s - %e", node.ID, dependency, err)
|
|
|
|
ctx.Log.Logf("signal", "LOCKABLE_SIGNAL_ERR: %s->%s - %e", node.ID, dependency, err)
|
|
|
@ -462,7 +445,7 @@ func (ext *LockableExt) Process(ctx *Context, source NodeID, node *Node, signal
|
|
|
|
case LockSignalType:
|
|
|
|
case LockSignalType:
|
|
|
|
ext.HandleLockSignal(ctx, source, node, signal.(StateSignal))
|
|
|
|
ext.HandleLockSignal(ctx, source, node, signal.(StateSignal))
|
|
|
|
case LinkStartSignalType:
|
|
|
|
case LinkStartSignalType:
|
|
|
|
ext.HandleLinkStartSignal(ctx, source, node, signal.(LinkStartSignal))
|
|
|
|
ext.HandleLinkStartSignal(ctx, source, node, signal.(IDStateSignal))
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|