Builds: design 47 G §515, §663 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package main
|
|
|
|
// The keeper's sources are the parts whose finding stream the bus module made (design 47 G §515, §663): it takes a source
|
|
// when it hears the seat mesh-broker's `created` of a part's `finding-events` entry, and at its start reads the entries
|
|
// the bus module holds by `list`. No part is named here (§7).
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
broker "git.novox.be/novox/mesh-contracts/go/seats/meshbroker"
|
|
)
|
|
|
|
// FindingEventsTemplate is the template of a part's finding stream in its `requires` (design 47 G §529).
|
|
const FindingEventsTemplate = "finding-events"
|
|
|
|
// BrokerCreated takes one `created` of the bus module: a `finding-events` entry makes its module a source, asked once with
|
|
// `findings-asked` naming it. An entry of another template, or a module already a source, changes nothing.
|
|
func (k *Keeper) BrokerCreated(body []byte) error {
|
|
var said broker.Said
|
|
if err := json.Unmarshal(body, &said); err != nil || said.Module == "" {
|
|
k.Logf("a created of the bus module is not readable: %v", err)
|
|
return nil
|
|
}
|
|
if said.Template != FindingEventsTemplate {
|
|
return nil
|
|
}
|
|
return k.takeSource(said.Module)
|
|
}
|
|
|
|
// takeSource makes the module a source the bus module made a stream for, and asks it once.
|
|
func (k *Keeper) takeSource(module string) error {
|
|
k.mu.Lock()
|
|
k.init()
|
|
if k.created == nil {
|
|
k.created = map[string]bool{}
|
|
}
|
|
if k.created[module] {
|
|
k.mu.Unlock()
|
|
return nil
|
|
}
|
|
k.created[module] = true
|
|
k.mu.Unlock()
|
|
if _, err := k.Ask(Module, module); err != nil {
|
|
return fmt.Errorf("asking %s for its findings: %w", module, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// nudgeBound says, without waiting, that the bus module's entries may be read now (ListBound's retry).
|
|
func (k *Keeper) nudgeBound() {
|
|
k.mu.Lock()
|
|
k.init()
|
|
c := k.boundRetry
|
|
k.mu.Unlock()
|
|
select {
|
|
case c <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// ListBound reads the entries the bus module holds at the keeper's start: every module with a `finding-events` entry is a
|
|
// source. A call that fails is asked again on the next event that says the bus module may answer, its `created` or a
|
|
// node-engine's `running` (the bus module being delivered after the keeper at times), never on a timer.
|
|
func ListBound(k *Keeper, call func(key string, body any) (json.RawMessage, error)) {
|
|
k.mu.Lock()
|
|
k.init()
|
|
retry := k.boundRetry
|
|
k.mu.Unlock()
|
|
for {
|
|
raw, err := call("seat:"+broker.Seat+"."+broker.VerbList, broker.ListInput{})
|
|
var answer broker.ListAnswer
|
|
if err == nil {
|
|
err = json.Unmarshal(raw, &answer)
|
|
}
|
|
if err == nil {
|
|
for _, e := range answer.Entries {
|
|
if e.Template == FindingEventsTemplate {
|
|
if err := k.takeSource(e.Module); err != nil {
|
|
k.Logf("%v", err)
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
k.Logf("the entries of the bus module are not read yet (%v); asked again when it says created or a node-engine says running", err)
|
|
<-retry
|
|
}
|
|
}
|