The bundle calls this node's node-resolver uplink-names with the check address's host at its start, at a changed check-address, at its bus connection's return and when this node's holder of node-resolver runs, reading the holder from the assignments; no holder is recorded and no failure, a failed call stands failed in names.json until the next moment. Builds: design 08 sign-in-page §12, §13, §22
120 lines
3.6 KiB
Go
120 lines
3.6 KiB
Go
package main
|
|
|
|
// The bundle's own work beside the seat's verbs (design 08 sign-in-page §11, §13, §20): started by the tool runner
|
|
// once it reached the bus. It acts at its start, at file events of the kernel in the module's folder, and when its bus
|
|
// connection comes back: the first heartbeat of its consumer answered after a missed one (DMC-4).
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
type bundle struct {
|
|
folder Folder
|
|
pub *publisher
|
|
names *namer
|
|
|
|
mu sync.Mutex
|
|
missed bool
|
|
lastAt time.Time
|
|
}
|
|
|
|
// bundleRuns is what the bundle hears through: file events of the kernel, its consumer's heartbeat and its
|
|
// subscription.
|
|
type bundleRuns struct {
|
|
files func(dirs []string, stop <-chan struct{}) <-chan struct{}
|
|
fileMade func(dir, name string, stop <-chan struct{}) <-chan struct{}
|
|
onHeartbeat func(func(stdio.Heartbeat)) error
|
|
subscribe func(pattern string, handle func(stdio.Envelope) error) error
|
|
}
|
|
|
|
func newBundle(f Folder) *bundle {
|
|
return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish},
|
|
names: &namer{folder: f, ask: stdio.Ask, now: time.Now}}
|
|
}
|
|
|
|
// seatPublish says a kept event through the runtime, which awaits the stream's acknowledgement.
|
|
func seatPublish(subject string, body json.RawMessage, id string) error {
|
|
return stdio.SeatPublish(subject, body, id)
|
|
}
|
|
|
|
// startBundle is networkmanager's bundle's own work, beside serving its tools.
|
|
func startBundle() {
|
|
f, err := theFolder()
|
|
if err != nil {
|
|
journal("the module's folder: %v", err)
|
|
return
|
|
}
|
|
newBundle(f).start(bundleRuns{files: fileEvents, fileMade: fileMade, onHeartbeat: stdio.OnHeartbeat,
|
|
subscribe: stdio.Subscribe})
|
|
}
|
|
|
|
// start publishes what is kept and gives the resolver the check's host name, then does each again at its moments: a
|
|
// file event on pending/ publishes, a changed check-address gives the names, node-resolver's holder running gives the
|
|
// names, and the bus connection's return does both (§13, §20).
|
|
func (b *bundle) start(runs bundleRuns) {
|
|
pending := filepath.Join(b.folder.Dir, pendingName)
|
|
if err := os.MkdirAll(pending, 0o700); err != nil {
|
|
journal("making %s failed: %v", pending, err)
|
|
}
|
|
kept := runs.files([]string{pending}, nil)
|
|
check := runs.fileMade(b.folder.Dir, checkName, nil)
|
|
b.pub.publishKept("the bundle's start")
|
|
b.names.give("the bundle's start")
|
|
if err := runs.subscribe(runningPattern, b.names.running); err != nil {
|
|
journal("subscribing to %s failed: %v", runningPattern, err)
|
|
}
|
|
if err := runs.onHeartbeat(b.heartbeat); err != nil {
|
|
journal("hearing the bus connection's heartbeat failed: %v", err)
|
|
}
|
|
for kept != nil || check != nil {
|
|
select {
|
|
case _, ok := <-kept:
|
|
if !ok {
|
|
kept = nil
|
|
continue
|
|
}
|
|
b.pub.publishKept("a kept event")
|
|
case _, ok := <-check:
|
|
if !ok {
|
|
check = nil
|
|
continue
|
|
}
|
|
b.names.give("a changed check address")
|
|
}
|
|
}
|
|
}
|
|
|
|
// heartbeat hears the consumer's heartbeat: the first one answered after a missed one is the connection come back.
|
|
func (b *bundle) heartbeat(hb stdio.Heartbeat) {
|
|
b.mu.Lock()
|
|
if !hb.At.IsZero() && hb.At.Before(b.lastAt) {
|
|
b.mu.Unlock()
|
|
return
|
|
}
|
|
if !hb.At.IsZero() {
|
|
b.lastAt = hb.At
|
|
}
|
|
back := !hb.Missed && b.missed
|
|
b.missed = hb.Missed
|
|
b.mu.Unlock()
|
|
if hb.Missed {
|
|
journal("the bus connection's heartbeat was missed: %s", hb.Why)
|
|
}
|
|
if back {
|
|
journal("the bus connection came back")
|
|
b.reconnected()
|
|
}
|
|
}
|
|
|
|
// reconnected is the work at the bus connection's return.
|
|
func (b *bundle) reconnected() {
|
|
b.pub.publishKept("the bus connection's return")
|
|
b.names.give("the bus connection's return")
|
|
}
|