2023-05-29 19:17:52 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Resources propagate update up to multiple parents, and not downwards
|
|
|
|
// (subscriber to team won't get update to alliance, but subscriber to alliance will get update to team)
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) update(signal GraphSignal) {
|
2023-06-03 02:45:16 -06:00
|
|
|
new_signal := signal.Trace(resource.ID())
|
2023-06-02 17:31:29 -06:00
|
|
|
if signal.Type() == "lock_changed" {
|
|
|
|
for _, child := range resource.Children() {
|
2023-06-03 02:45:16 -06:00
|
|
|
SendUpdate(child, new_signal)
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, parent := range resource.Parents() {
|
2023-06-03 02:45:16 -06:00
|
|
|
SendUpdate(parent, new_signal)
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
2023-06-03 01:38:35 -06:00
|
|
|
if resource.lock_holder != nil {
|
2023-06-03 02:45:16 -06:00
|
|
|
if resource.lock_holder.ID() != signal.Last() {
|
|
|
|
SendUpdate(resource.lock_holder, new_signal)
|
|
|
|
}
|
2023-06-03 01:38:35 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource is the interface that DAG nodes are made from
|
2023-06-01 13:11:32 -06:00
|
|
|
// A resource needs to be able to represent logical entities and connections to physical entities.
|
|
|
|
// A resource lock could be aborted at any time if this connection is broken, if that happens the event locking it must be aborted
|
|
|
|
// The device connection should be maintained as much as possible(requiring some reconnection behaviour in the background)
|
2023-05-29 19:17:52 -06:00
|
|
|
type Resource interface {
|
|
|
|
GraphNode
|
2023-06-02 17:31:29 -06:00
|
|
|
Owner() Event
|
2023-05-29 19:17:52 -06:00
|
|
|
Children() []Resource
|
|
|
|
Parents() []Resource
|
2023-06-02 17:31:29 -06:00
|
|
|
|
|
|
|
AddParent(parent Resource) error
|
|
|
|
LockParents()
|
|
|
|
UnlockParents()
|
|
|
|
|
|
|
|
SetOwner(owner Event)
|
|
|
|
LockState()
|
|
|
|
UnlockState()
|
|
|
|
|
|
|
|
lock(event Event) error
|
|
|
|
unlock(event Event) error
|
2023-06-01 13:11:32 -06:00
|
|
|
Connect(abort chan error) bool
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func AddParent(resource Resource, parent Resource) error {
|
|
|
|
if parent.ID() == resource.ID() {
|
|
|
|
error_str := fmt.Sprintf("Will not add %s as parent of itself", parent.Name())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
resource.LockParents()
|
|
|
|
for _, p := range resource.Parents() {
|
|
|
|
if p.ID() == parent.ID() {
|
|
|
|
error_str := fmt.Sprintf("%s is already a parent of %s, will not double-bond", p.Name(), resource.Name())
|
|
|
|
return errors.New(error_str)
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 13:11:32 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err := resource.AddParent(parent)
|
|
|
|
resource.UnlockParents()
|
|
|
|
|
|
|
|
return err
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func UnlockResource(resource Resource, event Event) error {
|
|
|
|
var err error = nil
|
|
|
|
resource.LockState()
|
|
|
|
if resource.Owner() == nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
return errors.New("Resource already unlocked")
|
2023-06-01 13:48:38 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if resource.Owner().ID() != event.ID() {
|
|
|
|
resource.UnlockState()
|
|
|
|
return errors.New("Resource not locked by parent, unlock failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var lock_err error = nil
|
|
|
|
for _, child := range resource.Children() {
|
|
|
|
err := UnlockResource(child, event)
|
2023-06-01 13:48:38 -06:00
|
|
|
if err != nil {
|
2023-06-02 17:31:29 -06:00
|
|
|
lock_err = err
|
|
|
|
break
|
2023-06-01 13:48:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if lock_err != nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
err_str := fmt.Sprintf("Resource failed to unlock: %s", lock_err)
|
|
|
|
return errors.New(err_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.SetOwner(nil)
|
|
|
|
|
|
|
|
err = resource.unlock(event)
|
|
|
|
if err != nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
return errors.New("Failed to unlock resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.UnlockState()
|
2023-06-01 13:48:38 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func LockResource(resource Resource, event Event) error {
|
|
|
|
resource.LockState()
|
|
|
|
if resource.Owner() != nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
err_str := fmt.Sprintf("Resource already locked: %s", resource.Name())
|
|
|
|
return errors.New(err_str)
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
|
|
|
|
2023-06-03 01:38:35 -06:00
|
|
|
err := resource.lock(event)
|
|
|
|
if err != nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
err_str := fmt.Sprintf("Failed to lock resource: %s", err)
|
|
|
|
return errors.New(err_str)
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
var lock_err error = nil
|
2023-06-03 01:38:35 -06:00
|
|
|
locked_resources := []Resource{}
|
2023-06-02 17:31:29 -06:00
|
|
|
for _, child := range resource.Children() {
|
|
|
|
err := LockResource(child, event)
|
|
|
|
if err != nil{
|
|
|
|
lock_err = err
|
|
|
|
break
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
2023-06-03 01:38:35 -06:00
|
|
|
locked_resources = append(locked_resources, child)
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if lock_err != nil {
|
|
|
|
resource.UnlockState()
|
|
|
|
err_str := fmt.Sprintf("Resource failed to lock: %s", lock_err)
|
|
|
|
return errors.New(err_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.SetOwner(event)
|
|
|
|
|
2023-06-01 13:48:38 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
resource.UnlockState()
|
2023-06-01 13:11:32 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func NotifyResourceLocked(resource Resource) {
|
|
|
|
signal := NewSignal(resource, "lock_changed")
|
|
|
|
signal.description = "lock"
|
|
|
|
|
|
|
|
go SendUpdate(resource, signal)
|
2023-06-01 13:11:32 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func NotifyResourceUnlocked(resource Resource) {
|
|
|
|
signal := NewSignal(resource, "lock_changed")
|
|
|
|
signal.description = "unlock"
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
go SendUpdate(resource, signal)
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
// BaseResource is the most basic resource that can exist in the DAG
|
|
|
|
// It holds a single state variable, which contains a pointer to the event that is locking it
|
|
|
|
type BaseResource struct {
|
|
|
|
BaseNode
|
|
|
|
parents []Resource
|
|
|
|
parents_lock sync.Mutex
|
|
|
|
children []Resource
|
|
|
|
children_lock sync.Mutex
|
|
|
|
lock_holder Event
|
|
|
|
state_lock sync.Mutex
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) SetOwner(owner Event) {
|
|
|
|
resource.lock_holder = owner
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) LockState() {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.state_lock.Lock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) UnlockState() {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.state_lock.Unlock()
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) Connect(abort chan error) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Owner() Event {
|
|
|
|
return resource.lock_holder
|
|
|
|
}
|
|
|
|
|
|
|
|
//BaseResources don't check anything special when locking/unlocking
|
|
|
|
func (resource * BaseResource) lock(event Event) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) unlock(event Event) error {
|
|
|
|
return nil
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Children() []Resource {
|
|
|
|
return resource.children
|
|
|
|
}
|
|
|
|
|
|
|
|
func (resource * BaseResource) Parents() []Resource {
|
|
|
|
return resource.parents
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) LockParents() {
|
|
|
|
resource.parents_lock.Lock()
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) UnlockParents() {
|
|
|
|
resource.parents_lock.Unlock()
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (resource * BaseResource) AddParent(parent Resource) error {
|
2023-05-29 19:17:52 -06:00
|
|
|
resource.parents = append(resource.parents, parent)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
func NewBaseResource(name string, description string, children []Resource) BaseResource {
|
|
|
|
resource := BaseResource{
|
2023-05-29 19:17:52 -06:00
|
|
|
BaseNode: BaseNode{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
2023-05-31 00:37:51 -06:00
|
|
|
id: randid(),
|
2023-06-01 22:42:47 -06:00
|
|
|
listeners: map[chan GraphSignal]chan GraphSignal{},
|
2023-06-02 17:31:29 -06:00
|
|
|
signal: make(chan GraphSignal, 100),
|
2023-05-29 19:17:52 -06:00
|
|
|
},
|
|
|
|
parents: []Resource{},
|
|
|
|
children: children,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource
|
|
|
|
}
|
2023-06-01 22:42:47 -06:00
|
|
|
|
|
|
|
func NewResource(name string, description string, children []Resource) * BaseResource {
|
|
|
|
resource := NewBaseResource(name, description, children)
|
|
|
|
return &resource
|
|
|
|
}
|