Updated fake client so that it runs the fake event in main.go

graph-rework
noah metz 2023-06-03 21:27:20 -06:00
parent 08eb543af0
commit debeed7091
4 changed files with 46 additions and 15 deletions

@ -13,11 +13,18 @@ import (
func (event * BaseEvent) update(signal GraphSignal) { func (event * BaseEvent) update(signal GraphSignal) {
event.signal <- signal event.signal <- signal
new_signal := signal.Trace(event.ID()) new_signal := signal.Trace(event.ID())
if event.parent != nil && signal.Type() != "abort"{
if event.parent != nil {
SendUpdate(event.parent, new_signal) SendUpdate(event.parent, new_signal)
} else if signal.Type() == "abort" { }
for _, child := range(event.Children()) {
SendUpdate(child, new_signal) for _, resource := range(event.RequiredResources()) {
source_id := ""
if signal.Source() != nil {
source_id = signal.Source().ID()
}
if source_id != resource.ID() {
SendUpdate(resource, new_signal)
} }
} }
} }
@ -159,6 +166,11 @@ func RunEvent(event Event) error {
func AbortEvent(event Event) error { func AbortEvent(event Event) error {
signal := NewSignal(event, "abort") signal := NewSignal(event, "abort")
SendUpdate(event, signal) SendUpdate(event, signal)
event.LockChildren()
for _, child := range(event.Children()) {
AbortEvent(child)
}
event.UnlockChildren()
return nil return nil
} }

@ -81,7 +81,7 @@ func NewBaseNode(name string, description string, id string) BaseNode {
name: name, name: name,
description: description, description: description,
id: id, id: id,
signal: make(chan GraphSignal, 100), signal: make(chan GraphSignal, 1000),
listeners: map[chan GraphSignal]chan GraphSignal{}, listeners: map[chan GraphSignal]chan GraphSignal{},
} }
log.Printf("NEW_NODE: %s - %s", node.ID(), node.Name()) log.Printf("NEW_NODE: %s - %s", node.ID(), node.Name())
@ -112,7 +112,7 @@ func (node * BaseNode) ID() string {
} }
// Create a new listener channel for the node, add it to the nodes listener list, and return the new channel // Create a new listener channel for the node, add it to the nodes listener list, and return the new channel
const listener_buffer = 100 const listener_buffer = 1000
func (node * BaseNode) UpdateChannel() chan GraphSignal { func (node * BaseNode) UpdateChannel() chan GraphSignal {
new_listener := make(chan GraphSignal, listener_buffer) new_listener := make(chan GraphSignal, listener_buffer)
node.RegisterChannel(new_listener) node.RegisterChannel(new_listener)
@ -151,7 +151,10 @@ func (node * BaseNode) UpdateListeners(update GraphSignal) {
case listener <- update: case listener <- update:
default: default:
log.Printf("CLOSED_LISTENER: %s: %p", node.Name(), listener) log.Printf("CLOSED_LISTENER: %s: %p", node.Name(), listener)
go func(node GraphNode, listener chan GraphSignal) {
listener <- NewSignal(node, "listener_closed")
close(listener) close(listener)
}(node, listener)
closed = append(closed, listener) closed = append(closed, listener)
} }
} }

@ -126,14 +126,30 @@ func fake_data() (* EventManager, *Arena, *Arena) {
} }
func process_fake_arena(update GraphSignal, arena * Arena) { func process_fake_arena(update GraphSignal, arena * Arena) {
if update.Source() != nil {
log.Printf("FAKE_CLIENT_UPDATE: %s -> %+v", update.Source().ID(), update)
} else {
log.Printf("FAKE_CLIENT_UPDATE: %s -> %+v", update.Source(), update)
}
if update.Type() == "event_start" { if update.Type() == "event_start" {
log.Printf("FAKE_ARENA_ACTION: Match started on %s, queuing autonomous automatically", arena.Name()) log.Printf("FAKE_CLIENT_ACTION: Match started on %s, queuing autonomous automatically", arena.Name())
SendUpdate(arena, NewSignal(nil, "queue_autonomous")) SendUpdate(arena, NewSignal(nil, "queue_autonomous"))
} else if update.Type() == "autonomous_queued" { } else if update.Type() == "autonomous_queued" {
log.Printf("FAKE_ARENA_ACTION: Autonomous queued on %s for %s, starting automatically at requested time", arena.Name(), update.Time()) log.Printf("FAKE_CLIENT_ACTION: Autonomous queued on %s for %s, starting automatically at requested time", arena.Name(), update.Time())
signal := NewSignal(nil, "start_autonomous") signal := NewSignal(nil, "start_autonomous")
signal.time = update.Time() signal.time = update.Time()
SendUpdate(arena, signal) SendUpdate(arena, signal)
} else if update.Type() == "autonomous_done" {
log.Printf("FAKE_CLIENT_ACTION: Autonomous done on %s for %s, queueing driver automatically", arena.Name(), update.Time())
signal := NewSignal(nil, "queue_driver")
SendUpdate(arena, signal)
} else if update.Type() == "driver_queued" {
log.Printf("FAKE_CLIENT_ACTION: Driver queueud on %s for %s, starting driver automatically at requested time", arena.Name(), update.Time())
signal := NewSignal(nil, "start_driver")
signal.time = update.Time()
SendUpdate(arena, signal)
} else if update.Type() == "driver_done" {
log.Printf("FAKE_CLIENT_ACTION: Driver done on %s for %s", arena.Name(), update.Time())
} }
} }

@ -108,7 +108,7 @@ func (arena * Arena) Connect(abort chan error) bool {
log.Printf("Virtual arena %s aborting", arena.Name()) log.Printf("Virtual arena %s aborting", arena.Name())
break break
case update := <- arena.signal: case update := <- arena.signal:
log.Printf("FAKE_ARENA_ACTION: %s : %+v", arena.Name(), update) log.Printf("VIRTUAL_ARENA_ACTION: %s : %+v", arena.Name(), update)
} }
} }
}(arena, abort) }(arena, abort)
@ -130,9 +130,6 @@ type Match struct {
func (match * Match) update(signal GraphSignal) { func (match * Match) update(signal GraphSignal) {
new_signal := signal.Trace(match.ID()) new_signal := signal.Trace(match.ID())
match.BaseEvent.update(new_signal) match.BaseEvent.update(new_signal)
if match.arena.ID() != signal.Last() {
SendUpdate(match.arena, new_signal)
}
} }
func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match { func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match {
@ -156,9 +153,10 @@ func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match
match.handlers["queue_autonomous"] = func(signal GraphSignal) (string, error) { match.handlers["queue_autonomous"] = func(signal GraphSignal) (string, error) {
if match.state != "scheduled" { if match.state != "scheduled" {
log.Printf("BAD_STATE: %s: %s", signal.Type(), match.state) log.Printf("BAD_STATE: %s: %s - %s", signal.Type(), match.state, match.Name())
return "wait", nil return "wait", nil
} }
log.Printf("AUTONOMOUS_QUEUED: %s", match.Name())
match.control = "none" match.control = "none"
match.state = "autonomous_queued" match.state = "autonomous_queued"
match.control_start = time.Now().Add(start_slack) match.control_start = time.Now().Add(start_slack)
@ -170,9 +168,10 @@ func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match
match.handlers["start_autonomous"] = func(signal GraphSignal) (string, error) { match.handlers["start_autonomous"] = func(signal GraphSignal) (string, error) {
if match.state != "autonomous_queued" { if match.state != "autonomous_queued" {
log.Printf("BAD_STATE: %s: %s", signal.Type(), match.state) log.Printf("BAD_STATE: %s: %s - %s", signal.Type(), match.state, match.Name())
return "wait", nil return "wait", nil
} }
log.Printf("AUTONOMOUS_RUNNING: %s", match.Name())
match.control = "program" match.control = "program"
match.state = "autonomous_running" match.state = "autonomous_running"
// TODO replace with typed protobuf // TODO replace with typed protobuf
@ -191,6 +190,7 @@ func NewMatch(alliance0 * Alliance, alliance1 * Alliance, arena * Arena) * Match
log.Printf("BAD_STATE: %s: %s", signal.Type(), match.state) log.Printf("BAD_STATE: %s: %s", signal.Type(), match.state)
return "wait", nil return "wait", nil
} }
log.Printf("AUTONOMOUS_DONE: %s", match.Name())
match.control = "none" match.control = "none"
match.state = "autonomous_done" match.state = "autonomous_done"