Split to seperate files and removed FSM libary to prepare for embedded callback mechanism
parent
c06ff16fc9
commit
11bf1e5344
@ -0,0 +1,217 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"errors"
|
||||||
|
graphql "github.com/graph-gophers/graphql-go"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update the events listeners, and notify the parent to do the same
|
||||||
|
func (event * BaseEvent) Update() error {
|
||||||
|
err := event.UpdateListeners()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if event.parent != nil{
|
||||||
|
return event.parent.Update()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventInfo interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseEventInfo interface {
|
||||||
|
EventInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventQueueInfo struct {
|
||||||
|
EventInfo
|
||||||
|
priority int
|
||||||
|
state string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEventQueueInfo(priority int) * EventQueueInfo {
|
||||||
|
info := &EventQueueInfo{
|
||||||
|
priority: priority,
|
||||||
|
state: "queued",
|
||||||
|
}
|
||||||
|
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event is the interface that event tree nodes must implement
|
||||||
|
type Event interface {
|
||||||
|
GraphNode
|
||||||
|
Children() []Event
|
||||||
|
ChildInfo(event Event) EventInfo
|
||||||
|
Parent() Event
|
||||||
|
RegisterParent(parent Event) error
|
||||||
|
RequiredResources() []Resource
|
||||||
|
CreatedResources() []Resource
|
||||||
|
AddChild(child Event, info EventInfo) error
|
||||||
|
FindChild(id graphql.ID) Event
|
||||||
|
}
|
||||||
|
|
||||||
|
// BaseEvent is the most basic event that can exist in the event tree.
|
||||||
|
// On start it automatically transitions to completion.
|
||||||
|
// It can optionally require events, which will all need to be locked to start it
|
||||||
|
// It can optionally create resources, which will be locked by default and unlocked on completion
|
||||||
|
// This node by itself doesn't implement any special behaviours for children, so they will be ignored.
|
||||||
|
// When starter, this event automatically transitions to completion and unlocks all it's resources(including created)
|
||||||
|
type BaseEvent struct {
|
||||||
|
BaseNode
|
||||||
|
created_resources []Resource
|
||||||
|
required_resources []Resource
|
||||||
|
children []Event
|
||||||
|
child_info map[Event]EventInfo
|
||||||
|
parent Event
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventQueue is a basic event that can have children.
|
||||||
|
// On start, it attempts to start it's children from the highest 'priority'
|
||||||
|
type EventQueue struct {
|
||||||
|
BaseEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEvent(name string, description string, required_resources []Resource) (* BaseEvent, Resource) {
|
||||||
|
done_resource := NewResource("event_done", "signal that event is done", []Resource{})
|
||||||
|
event := &BaseEvent{
|
||||||
|
BaseNode: BaseNode{
|
||||||
|
name: name,
|
||||||
|
description: description,
|
||||||
|
id: gql_randid(),
|
||||||
|
listeners: []chan error{},
|
||||||
|
},
|
||||||
|
parent: nil,
|
||||||
|
children: []Event{},
|
||||||
|
child_info: map[Event]EventInfo{},
|
||||||
|
created_resources: []Resource{done_resource},
|
||||||
|
required_resources: required_resources,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock the done_resource by default
|
||||||
|
done_resource.Lock(event)
|
||||||
|
|
||||||
|
return event, done_resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEventQueue(name string, description string, required_resources []Resource) (* EventQueue, Resource) {
|
||||||
|
done_resource := NewResource("event_done", "signal that event is done", []Resource{})
|
||||||
|
queue := &EventQueue{
|
||||||
|
BaseEvent: BaseEvent{
|
||||||
|
BaseNode: BaseNode{
|
||||||
|
name: name,
|
||||||
|
description: description,
|
||||||
|
id: gql_randid(),
|
||||||
|
listeners: []chan error{},
|
||||||
|
},
|
||||||
|
parent: nil,
|
||||||
|
children: []Event{},
|
||||||
|
child_info: map[Event]EventInfo{},
|
||||||
|
created_resources: []Resource{done_resource},
|
||||||
|
required_resources: required_resources,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
done_resource.Lock(queue)
|
||||||
|
|
||||||
|
return queue, done_resource
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the nodes parent for upwards propagation of changes
|
||||||
|
func (event * BaseEvent) RegisterParent(parent Event) error{
|
||||||
|
if event.parent != nil {
|
||||||
|
return errors.New("Parent already registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
event.parent = parent
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) Parent() Event {
|
||||||
|
return event.parent
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) RequiredResources() []Resource {
|
||||||
|
return event.required_resources
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) CreatedResources() []Resource {
|
||||||
|
return event.created_resources
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) Children() []Event {
|
||||||
|
return event.children
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) ChildInfo(idx Event) EventInfo {
|
||||||
|
val, ok := event.child_info[idx]
|
||||||
|
if ok == false {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) FindChild(id graphql.ID) Event {
|
||||||
|
if id == event.ID() {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range event.Children() {
|
||||||
|
result := child.FindChild(id)
|
||||||
|
if result != nil {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks that the type of info is equal to EventQueueInfo
|
||||||
|
func (event * EventQueue) AddChild(child Event, info EventInfo) error {
|
||||||
|
if checkType(info, (*EventQueueInfo)(nil)) == false {
|
||||||
|
return errors.New("EventQueue.AddChild passed invalid type for info")
|
||||||
|
}
|
||||||
|
|
||||||
|
return event.addChild(child, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (event * BaseEvent) addChild(child Event, info EventInfo) error {
|
||||||
|
err := child.RegisterParent(event)
|
||||||
|
if err != nil {
|
||||||
|
error_str := fmt.Sprintf("Failed to register %s as a parent of %s, cancelling AddChild", event.ID(), child.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
event.children = append(event.children, child)
|
||||||
|
event.child_info[child] = info
|
||||||
|
event.Update()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overloaded function AddChild checks the info passed and calls the BaseEvent.addChild
|
||||||
|
func (event * BaseEvent) AddChild(child Event, info EventInfo) error {
|
||||||
|
if info != nil {
|
||||||
|
return errors.New("info must be nil for BaseEvent children")
|
||||||
|
}
|
||||||
|
|
||||||
|
return event.addChild(child, info)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkType(first interface{}, second interface{}) bool {
|
||||||
|
if first == nil || second == nil {
|
||||||
|
if first == nil && second == nil {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
first_type := reflect.TypeOf(first)
|
||||||
|
second_type := reflect.TypeOf(second)
|
||||||
|
|
||||||
|
return first_type == second_type
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"errors"
|
||||||
|
graphql "github.com/graph-gophers/graphql-go"
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EventManager struct {
|
||||||
|
dag_nodes map[graphql.ID]Resource
|
||||||
|
root_event Event
|
||||||
|
}
|
||||||
|
|
||||||
|
// root_event's requirements must be in dag_nodes, and dag_nodes must be ordered by dependency(no children first)
|
||||||
|
func NewEventManager(root_event Event, dag_nodes []Resource) * EventManager {
|
||||||
|
|
||||||
|
manager := &EventManager{
|
||||||
|
dag_nodes: map[graphql.ID]Resource{},
|
||||||
|
root_event: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct the DAG
|
||||||
|
for _, resource := range dag_nodes {
|
||||||
|
err := manager.AddResource(resource)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to add %s to EventManager: %s", resource.ID(), err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.AddEvent(nil, root_event, nil)
|
||||||
|
|
||||||
|
return manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
func (manager * EventManager) Run(ctx context.Context) error {
|
||||||
|
//return manager.root_event.Run(ctx)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (manager * EventManager) FindResource(id graphql.ID) Resource {
|
||||||
|
resource, exists := manager.dag_nodes[id]
|
||||||
|
if exists == false {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (manager * EventManager) FindEvent(id graphql.ID) Event {
|
||||||
|
event := manager.root_event.FindChild(id)
|
||||||
|
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
func (manager * EventManager) AddResource(resource Resource) error {
|
||||||
|
_, exists := manager.dag_nodes[resource.ID()]
|
||||||
|
if exists == true {
|
||||||
|
error_str := fmt.Sprintf("%s is already in the resource DAG, cannot add again", resource.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range resource.Children() {
|
||||||
|
_, exists := manager.dag_nodes[child.ID()]
|
||||||
|
if exists == false {
|
||||||
|
error_str := fmt.Sprintf("%s is not in the resource DAG, cannot add %s to DAG", child.ID(), resource.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
manager.dag_nodes[resource.ID()] = resource
|
||||||
|
for _, child := range resource.Children() {
|
||||||
|
child.AddParent(resource)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the node doesn't already exist in the tree
|
||||||
|
// Check the the selected parent exists in the tree
|
||||||
|
// Check that required resources exist in the DAG
|
||||||
|
// Check that created resources don't exist in the DAG
|
||||||
|
// Add resources created by the event to the DAG
|
||||||
|
// Add child to parent
|
||||||
|
func (manager * EventManager) AddEvent(parent Event, child Event, info EventInfo) error {
|
||||||
|
if child == nil {
|
||||||
|
return errors.New("Cannot add nil Event to EventManager")
|
||||||
|
} else if len(child.Children()) != 0 {
|
||||||
|
return errors.New("Adding events recursively not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, resource := range child.RequiredResources() {
|
||||||
|
_, exists := manager.dag_nodes[resource.ID()]
|
||||||
|
if exists == false {
|
||||||
|
error_str := fmt.Sprintf("Required resource %s not in DAG, cannot add event %s", resource.ID(), child.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, resource := range child.CreatedResources() {
|
||||||
|
_, exists := manager.dag_nodes[resource.ID()]
|
||||||
|
if exists == true {
|
||||||
|
error_str := fmt.Sprintf("Created resource %s already exists in DAG, cannot add event %s", resource.ID(), child.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
manager.AddResource(resource)
|
||||||
|
}
|
||||||
|
|
||||||
|
if manager.root_event == nil && parent != nil {
|
||||||
|
error_str := fmt.Sprintf("EventManager has no root, so can't add event to parent")
|
||||||
|
return errors.New(error_str)
|
||||||
|
} else if manager.root_event != nil && parent == nil {
|
||||||
|
// TODO
|
||||||
|
return errors.New("Replacing root event not implemented")
|
||||||
|
} else if manager.root_event == nil && parent == nil {
|
||||||
|
manager.root_event = child
|
||||||
|
return nil;
|
||||||
|
} else {
|
||||||
|
if manager.root_event.FindChild(parent.ID()) == nil {
|
||||||
|
error_str := fmt.Sprintf("Event %s is not present in the event tree, cannot add %s as child", parent.ID(), child.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
if manager.root_event.FindChild(child.ID()) != nil {
|
||||||
|
error_str := fmt.Sprintf("Event %s already exists in the event tree, can not add again", child.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
return parent.AddChild(child, info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,164 @@
|
|||||||
|
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)
|
||||||
|
func (resource * BaseResource) Update() error {
|
||||||
|
err := resource.UpdateListeners()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, parent := range resource.Parents() {
|
||||||
|
err := parent.Update()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resource.lock_holder != nil {
|
||||||
|
resource.lock_holder.Update()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resource is the interface that DAG nodes are made from
|
||||||
|
type Resource interface {
|
||||||
|
GraphNode
|
||||||
|
AddParent(parent Resource) error
|
||||||
|
Children() []Resource
|
||||||
|
Parents() []Resource
|
||||||
|
Lock(event Event) error
|
||||||
|
Unlock(event Event) error
|
||||||
|
Owner() Event
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
children []Resource
|
||||||
|
lock_holder Event
|
||||||
|
state_lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resource * BaseResource) Owner() Event {
|
||||||
|
return resource.lock_holder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grab the state mutex and check the state, if unlocked continue to hold the mutex while doing the same for children
|
||||||
|
// When the bottom of a tree is reached(no more children) go back up and set the lock state
|
||||||
|
func (resource * BaseResource) Lock(event Event) error {
|
||||||
|
var err error = nil
|
||||||
|
locked := false
|
||||||
|
|
||||||
|
resource.state_lock.Lock()
|
||||||
|
if resource.lock_holder != nil {
|
||||||
|
err = errors.New("Resource already locked")
|
||||||
|
} else {
|
||||||
|
all_children_locked := true
|
||||||
|
for _, child := range resource.Children() {
|
||||||
|
err = child.Lock(event)
|
||||||
|
if err != nil {
|
||||||
|
all_children_locked = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if all_children_locked == true {
|
||||||
|
resource.lock_holder = event
|
||||||
|
locked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resource.state_lock.Unlock()
|
||||||
|
|
||||||
|
if locked == true {
|
||||||
|
resource.Update()
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recurse through children, unlocking until no more children
|
||||||
|
// If the child isn't locked by the unlocker
|
||||||
|
func (resource * BaseResource) Unlock(event Event) error {
|
||||||
|
var err error = nil
|
||||||
|
unlocked := false
|
||||||
|
|
||||||
|
resource.state_lock.Lock()
|
||||||
|
if resource.lock_holder == nil {
|
||||||
|
err = errors.New("Resource already unlocked")
|
||||||
|
} else if resource.lock_holder != event {
|
||||||
|
err = errors.New("Resource not locked by parent, can't unlock")
|
||||||
|
} else {
|
||||||
|
all_children_unlocked := true
|
||||||
|
for _, child := range resource.Children() {
|
||||||
|
err = child.Unlock(event)
|
||||||
|
if err != nil {
|
||||||
|
all_children_unlocked = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if all_children_unlocked == true{
|
||||||
|
resource.lock_holder = nil
|
||||||
|
unlocked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resource.state_lock.Unlock()
|
||||||
|
|
||||||
|
if unlocked == true {
|
||||||
|
resource.Update()
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resource * BaseResource) Children() []Resource {
|
||||||
|
return resource.children
|
||||||
|
}
|
||||||
|
|
||||||
|
func (resource * BaseResource) Parents() []Resource {
|
||||||
|
return resource.parents
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a parent to a DAG node
|
||||||
|
func (resource * BaseResource) AddParent(parent Resource) error {
|
||||||
|
// Don't add self as parent
|
||||||
|
if parent.ID() == resource.ID() {
|
||||||
|
error_str := fmt.Sprintf("Will not add %s as parent of itself", parent.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't add parent if it's already a parent
|
||||||
|
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.ID(), resource.ID())
|
||||||
|
return errors.New(error_str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the parent
|
||||||
|
resource.parents = append(resource.parents, parent)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResource(name string, description string, children []Resource) * BaseResource {
|
||||||
|
resource := &BaseResource{
|
||||||
|
BaseNode: BaseNode{
|
||||||
|
name: name,
|
||||||
|
description: description,
|
||||||
|
id: gql_randid(),
|
||||||
|
listeners: []chan error{},
|
||||||
|
},
|
||||||
|
parents: []Resource{},
|
||||||
|
children: children,
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource
|
||||||
|
}
|
Loading…
Reference in New Issue