From f6646c7254683706952bbda32a62a58954e03533 Mon Sep 17 00:00:00 2001 From: jochen Date: Tue, 24 Nov 2026 21:22:01 +0100 Subject: [PATCH] M5 networkmanager: the bundle publishes the kept events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bundle publishes what the watcher kept, in order and under each event's own message id, deleting each once the stream acknowledged it and keeping a refused one for the next moment: its start, a file event on pending/ and its bus connection's return. The watcher now names the events by the seat's generated contract. Builds: design 08 sign-in-page §1, §20, §22 --- .../networkmanager/cmd/uplink-tools/bundle.go | 87 ++++++++++++ .../cmd/uplink-tools/manager.go | 2 +- .../cmd/uplink-tools/publish.go | 65 +++++++++ .../cmd/uplink-tools/publish_test.go | 128 ++++++++++++++++++ .../networkmanager/cmd/uplink-tools/watch.go | 39 ++---- modules/networkmanager/go.mod | 1 + 6 files changed, 296 insertions(+), 26 deletions(-) create mode 100644 modules/networkmanager/cmd/uplink-tools/bundle.go create mode 100644 modules/networkmanager/cmd/uplink-tools/publish.go create mode 100644 modules/networkmanager/cmd/uplink-tools/publish_test.go diff --git a/modules/networkmanager/cmd/uplink-tools/bundle.go b/modules/networkmanager/cmd/uplink-tools/bundle.go new file mode 100644 index 000000000..e24ff6bb4 --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/bundle.go @@ -0,0 +1,87 @@ +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 + + mu sync.Mutex + missed bool + lastAt time.Time +} + +func newBundle(f Folder) *bundle { + return &bundle{folder: f, pub: &publisher{folder: f, publish: seatPublish}} +} + +// 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 + } + b := newBundle(f) + b.start(fileEvents, stdio.OnHeartbeat) +} + +// start publishes what is kept, then again at each file event on pending/ and at each return of the bus connection. +func (b *bundle) start(files func([]string, <-chan struct{}) <-chan struct{}, onHeartbeat func(func(stdio.Heartbeat)) error) { + pending := filepath.Join(b.folder.Dir, pendingName) + if err := os.MkdirAll(pending, 0o700); err != nil { + journal("making %s failed: %v", pending, err) + } + kept := files([]string{pending}, nil) + b.pub.publishKept("the bundle's start") + if err := onHeartbeat(b.heartbeat); err != nil { + journal("hearing the bus connection's heartbeat failed: %v", err) + } + for range kept { + b.pub.publishKept("a kept event") + } +} + +// 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") +} diff --git a/modules/networkmanager/cmd/uplink-tools/manager.go b/modules/networkmanager/cmd/uplink-tools/manager.go index 6d544cca2..0b294256d 100644 --- a/modules/networkmanager/cmd/uplink-tools/manager.go +++ b/modules/networkmanager/cmd/uplink-tools/manager.go @@ -54,5 +54,5 @@ func parseNmcli(out string) map[string]LinkDNS { // holder is what networkmanager adds to the seat's verbs (design 08 sign-in-page §11): the watcher, `uplink-tools // watch`. func holder(m Machine) holderOwn { - return holderOwn{Commands: map[string]func([]string) error{"watch": watch}} + return holderOwn{Commands: map[string]func([]string) error{"watch": watch}, Start: startBundle} } diff --git a/modules/networkmanager/cmd/uplink-tools/publish.go b/modules/networkmanager/cmd/uplink-tools/publish.go new file mode 100644 index 000000000..b5585259f --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/publish.go @@ -0,0 +1,65 @@ +package main + +// The bundle publishes the kept events (design 08 sign-in-page §20): in sequence order, on the seat's subject, each +// with its message id as the bus's message id (the runtime prefixes it with the module), awaiting the stream's +// acknowledgement, and deletes each file once acknowledged. A publish refused or unacknowledged stays in pending/, is +// written to the journal and counted, and is published again at the next moment: the bundle's start, a file event of +// the kernel on pending/, its bus connection coming back, or uplink_publish_again. + +import ( + "encoding/json" + "sync" +) + +// publisher publishes the kept events, one round at a time. +type publisher struct { + folder Folder + // publish says one event on the seat, de-duplicated by id, and answers once the stream acknowledged it. + publish func(subject string, body json.RawMessage, id string) error + + mu sync.Mutex + refused int +} + +// publishResult is one round: how many were published and how many still wait. +type publishResult struct { + Published int `json:"published"` + Waiting int `json:"waiting"` + Refused string `json:"refused,omitempty"` +} + +// publishKept publishes every kept event in order; the first refused stops the round, so none is published before an +// older one. +func (p *publisher) publishKept(moment string) publishResult { + p.mu.Lock() + defer p.mu.Unlock() + var out publishResult + kept, err := p.folder.pending() + if err != nil { + journal("reading the kept events at %s failed: %v", moment, err) + out.Refused = err.Error() + return out + } + for i, k := range kept { + if err := p.publish(k.Subject, k.Body, k.MessageID); err != nil { + p.refused++ + out.Waiting, out.Refused = len(kept)-i, err.Error() + journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.MessageID, moment, err, + out.Waiting) + return out + } + journal("published %s on %s", k.MessageID, k.Subject) + out.Published++ + if err := p.folder.drop(k); err != nil { + journal("deleting the published %s failed: %v", k.MessageID, err) + } + } + return out +} + +// refusedSince is the publishes refused since the bundle's start. +func (p *publisher) refusedSince() int { + p.mu.Lock() + defer p.mu.Unlock() + return p.refused +} diff --git a/modules/networkmanager/cmd/uplink-tools/publish_test.go b/modules/networkmanager/cmd/uplink-tools/publish_test.go new file mode 100644 index 000000000..d27cf8b28 --- /dev/null +++ b/modules/networkmanager/cmd/uplink-tools/publish_test.go @@ -0,0 +1,128 @@ +package main + +import ( + "encoding/json" + "errors" + "strings" + "sync" + "testing" + "time" + + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// The bus stood in: every publish asked, the ids it refuses once, as the runtime answers a publish the stream did not +// acknowledge. +type fakeStream struct { + mu sync.Mutex + asked []string + refuse map[string]bool +} + +func (s *fakeStream) publish(subject string, body json.RawMessage, id string) error { + s.mu.Lock() + defer s.mu.Unlock() + s.asked = append(s.asked, id) + if s.refuse[id] { + delete(s.refuse, id) + return errors.New("no acknowledgement") + } + if !json.Valid(body) || !strings.HasPrefix(subject, "mesh.seat.node-uplink.event.") { + return errors.New("not an event of the seat") + } + return nil +} + +func (s *fakeStream) publishes() string { + s.mu.Lock() + defer s.mu.Unlock() + return strings.Join(s.asked, " ") +} + +func keepThree(t *testing.T, f Folder) { + t.Helper() + for _, event := range []string{eventEnded, eventAsked, eventEnded} { + if _, err := f.keep(event, eventSubject(event, f.Node), map[string]string{"node": f.Node}); err != nil { + t.Fatal(err) + } + } +} + +// The bundle publishes the kept events in order with their message ids and deletes each once acknowledged; a refused +// one stays, with the ones after it, and is published at the next moment under the same id (design 08 sign-in-page +// §20, §25). +func TestTheBundlePublishesTheKeptEventsInOrder(t *testing.T) { + lines := journalLines(t) + f := aFolder(t) + keepThree(t, f) + stream := &fakeStream{refuse: map[string]bool{"laptop.sign-in-asked.2": true}} + p := &publisher{folder: f, publish: stream.publish} + if r := p.publishKept("the bundle's start"); r.Published != 1 || r.Waiting != 2 || p.refusedSince() != 1 { + t.Fatalf("first round %+v, refused %d", r, p.refusedSince()) + } + if kept, _ := f.pending(); len(kept) != 2 || kept[0].MessageID != "laptop.sign-in-asked.2" { + t.Fatalf("waiting %+v", kept) + } + if !strings.Contains(strings.Join(lines(), "\n"), "publishing laptop.sign-in-asked.2 at the bundle's start was refused") { + t.Fatalf("the refusal was not written to the journal: %q", lines()) + } + if r := p.publishKept("a kept event"); r.Published != 2 || r.Waiting != 0 { + t.Fatalf("second round %+v", r) + } + if got := stream.publishes(); got != "laptop.sign-in-ended.1 laptop.sign-in-asked.2 laptop.sign-in-asked.2 laptop.sign-in-ended.3" { + t.Fatalf("published %s", got) + } + if kept, _ := f.pending(); len(kept) != 0 { + t.Fatalf("left %+v", kept) + } +} + +// The bundle publishes at its start, at each file event on pending/ and when its bus connection comes back: the first +// heartbeat answered after a missed one; an older heartbeat arriving late changes nothing (§20, DMC-4). +func TestTheBundlePublishesAtEachMoment(t *testing.T) { + journalLines(t) + f := aFolder(t) + keepThree(t, f) + stream := &fakeStream{refuse: map[string]bool{"laptop.sign-in-ended.1": true}} + b := &bundle{folder: f, pub: &publisher{folder: f, publish: stream.publish}} + files := make(chan struct{}) + var hook func(stdio.Heartbeat) + done := make(chan struct{}) + go func() { + b.start(func(dirs []string, _ <-chan struct{}) <-chan struct{} { + if len(dirs) != 1 || !strings.HasSuffix(dirs[0], "/pending") { + t.Errorf("watched %v", dirs) + } + return files + }, func(h func(stdio.Heartbeat)) error { hook = h; return nil }) + close(done) + }() + files <- struct{}{} // the start's round ran and was refused; this one publishes what waits + if _, err := f.keep(eventAsked, eventSubject(eventAsked, "laptop"), map[string]string{}); err != nil { + t.Fatal(err) + } + files <- struct{}{} + close(files) + <-done + if kept, _ := f.pending(); len(kept) != 0 { + t.Fatalf("a file event published nothing: %+v", kept) + } + keepThree(t, f) + at := time.Date(2026, 11, 24, 20, 0, 0, 0, time.UTC) + hook(stdio.Heartbeat{At: at}) + if kept, _ := f.pending(); len(kept) != 3 { + t.Fatal("a heartbeat with none missed published") + } + hook(stdio.Heartbeat{At: at.Add(time.Second), Missed: true, Why: "no heartbeat"}) + hook(stdio.Heartbeat{At: at.Add(-time.Second)}) + if kept, _ := f.pending(); len(kept) != 3 { + t.Fatal("an older heartbeat arriving late was taken for the connection's return") + } + hook(stdio.Heartbeat{At: at.Add(2 * time.Second)}) + if kept, _ := f.pending(); len(kept) != 0 { + t.Fatalf("the connection's return published nothing: %+v", kept) + } + if got := stream.publishes(); !strings.HasPrefix(got, "laptop.sign-in-ended.1 laptop.sign-in-ended.1 laptop.sign-in-asked.2") { + t.Fatalf("published %s", got) + } +} diff --git a/modules/networkmanager/cmd/uplink-tools/watch.go b/modules/networkmanager/cmd/uplink-tools/watch.go index 8b9abd6e3..54e89d269 100644 --- a/modules/networkmanager/cmd/uplink-tools/watch.go +++ b/modules/networkmanager/cmd/uplink-tools/watch.go @@ -12,6 +12,8 @@ import ( "net/url" "os" "time" + + "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink" ) // keptBound is the most kept events pending/ holds; past it the oldest goes, with a journal line (§20). @@ -55,35 +57,22 @@ type Rating struct { func (r Rating) checkOn() bool { return r.CheckEnabled && r.CheckAvailable } func (r Rating) active() bool { return r.Connection != "" && r.Connection != "/" } -// The events of node-uplink the watcher keeps, and why an ask ended. +// The events of node-uplink the watcher keeps, and why an ask ended, are the seat's (design 08 sign-in-page §1). const ( - eventAsked = "sign-in-asked" - eventEnded = "sign-in-ended" + eventAsked = nodeuplink.EventSignInAsked + eventEnded = nodeuplink.EventSignInEnded - whySignedIn = "signed-in" - whyLeft = "left" - whyNoLongerAsked = "no-longer-asked" + whySignedIn = nodeuplink.SignInEndedWhySignedIn + whyLeft = nodeuplink.SignInEndedWhyLeft + whyNoLongerAsked = nodeuplink.SignInEndedWhyNoLongerAsked ) // eventSubject is where a node-uplink event of node is published (§1). func eventSubject(event, node string) string { - return "mesh.seat.node-uplink.event." + event + "." + node -} - -type signInAsked struct { - Node string `json:"node"` - Link string `json:"link"` - Network string `json:"network"` - Address string `json:"address"` - At string `json:"at"` -} - -type signInEnded struct { - Node string `json:"node"` - Link string `json:"link"` - Network string `json:"network"` - At string `json:"at"` - Why string `json:"why"` + if event == eventAsked { + return nodeuplink.SubjectSignInAsked(node) + } + return nodeuplink.SubjectSignInEnded(node) } // signal is one thing the system bus said that the watcher hears: the network manager's rating changed, or logind @@ -393,7 +382,7 @@ func (w *Watcher) asked(r Rating) { now := w.Now() w.ask = &openAsk{Network: r.Network, Link: r.Link, Since: now, connection: r.Connection, address: r.CheckURI} w.state.AsksOpened++ - w.keep(eventAsked, signInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI, + w.keep(eventAsked, nodeuplink.SignInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI, At: stamp(now)}) w.present(nil, r.Network, r.CheckURI) } @@ -404,7 +393,7 @@ func (w *Watcher) ended(link, network, why string) { w.state.AsksEnded++ } w.ask = nil - w.keep(eventEnded, signInEnded{Node: w.Folder.Node, Link: link, Network: network, At: stamp(w.Now()), Why: why}) + w.keep(eventEnded, nodeuplink.SignInEnded{Node: w.Folder.Node, Link: link, Network: network, At: stamp(w.Now()), Why: why}) } // stamp is a time as the events say it: RFC 3339 in UTC, to the second. diff --git a/modules/networkmanager/go.mod b/modules/networkmanager/go.mod index 493db66c3..ff59b309b 100644 --- a/modules/networkmanager/go.mod +++ b/modules/networkmanager/go.mod @@ -3,6 +3,7 @@ module networkmanager go 1.26.0 require ( + git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink v0.0.0-00010101000000-000000000000 git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 github.com/godbus/dbus/v5 v5.1.0 golang.org/x/sys v0.48.0