Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e539cf6e99 |
@@ -23,9 +23,9 @@ written whole (beside, then renamed over):
|
|||||||
|---|---|
|
|---|---|
|
||||||
| `watch.sock` | the watcher's socket: one JSON line asked, one answered; `state` answers its state, `rating-again` makes it subscribe again and act on the rating read now |
|
| `watch.sock` | the watcher's socket: one JSON line asked, one answered; `state` answers its state, `rating-again` makes it subscribe again and act on the rating read now |
|
||||||
| `check-address` | NetworkManager's check address, written by the watcher at its start and at each change |
|
| `check-address` | NetworkManager's check address, written by the watcher at its start and at each change |
|
||||||
| `pending/` | the kept events, one `<sequence>.json` each, written by the watcher and deleted by the bundle once the bus acknowledged it |
|
| `pending/` | the kept changes, one `<sequence>.json` each holding the event, its sequence and its body with no node, written by the watcher and deleted by the bundle once the bus acknowledged it |
|
||||||
| `names.json` | the bundle's last `uplink-names` call: the names, its result (`ok`, `no holder` or the failure) and when |
|
| `names.json` | the bundle's last `uplink-names` call: the names, its result (`ok`, `no holder` or the failure) and when |
|
||||||
| `sequence` | the next sequence of the watcher's message ids, never reset while the folder lives |
|
| `sequence` | the next sequence of the watcher's kept changes, never reset while the folder lives |
|
||||||
|
|
||||||
The folder's `unknown` is `module`: the node-engine does nothing inside it, and the watcher leaves any other path it
|
The folder's `unknown` is `module`: the node-engine does nothing inside it, and the watcher leaves any other path it
|
||||||
finds there as it is and writes it to its journal once per path. When the module leaves, the node-engine keeps the
|
finds there as it is and writes it to its journal once per path. When the module leaves, the node-engine keeps the
|
||||||
@@ -63,7 +63,8 @@ From NetworkManager's own rating the watcher keeps `node-uplink`'s two events, `
|
|||||||
the address opened, when) and `sign-in-ended` (node, link, network, when, and why: `signed-in`, `left` or
|
the address opened, when) and `sign-in-ended` (node, link, network, when, and why: `signed-in`, `left` or
|
||||||
`no-longer-asked`), at most 256 of them, the oldest dropped past that with a journal line. With the check switched
|
`no-longer-asked`), at most 256 of them, the oldest dropped past that with a journal line. With the check switched
|
||||||
off (not enabled, or not available) it neither opens nor keeps anything. The bundle publishes them in order on the
|
off (not enabled, or not available) it neither opens nor keeps anything. The bundle publishes them in order on the
|
||||||
seat's subject `mesh.seat.node-uplink.event.<event>.<node>`, each under its own message id, awaiting the stream's
|
seat's subject `mesh.seat.node-uplink.event.<event>.<node>`, each under the message id `<node>.<event>.<sequence>`,
|
||||||
|
the bundle's own node filled into the subject, the id and the body (the watcher names no node), awaiting the stream's
|
||||||
acknowledgement, at its start, at each new kept event and when its bus connection comes back; a refused one waits for
|
acknowledgement, at its start, at each new kept event and when its bus connection comes back; a refused one waits for
|
||||||
the next moment. Nothing in the mesh consumes them: they are its record, read with `nats.nats_stream_messages`
|
the next moment. Nothing in the mesh consumes them: they are its record, read with `nats.nats_stream_messages`
|
||||||
filtered to `mesh.seat.node-uplink.event.*.<node>`, their time in `at`.
|
filtered to `mesh.seat.node-uplink.event.*.<node>`, their time in `at`.
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,22 +43,48 @@ func (p *publisher) publishKept(moment string) publishResult {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
for i, k := range kept {
|
for i, k := range kept {
|
||||||
if err := p.publish(k.Subject, k.Body, k.MessageID); err != nil {
|
subject, id, body, err := p.outgoing(k)
|
||||||
|
if err == nil {
|
||||||
|
err = p.publish(subject, body, id)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
p.refused++
|
p.refused++
|
||||||
out.Waiting, out.Refused = len(kept)-i, err.Error()
|
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,
|
journal("publishing %s at %s was refused or not acknowledged: %v; %d left waiting", k.Name(), moment, err,
|
||||||
out.Waiting)
|
out.Waiting)
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
journal("published %s on %s", k.MessageID, k.Subject)
|
journal("published %s on %s", id, subject)
|
||||||
out.Published++
|
out.Published++
|
||||||
if err := p.folder.drop(k); err != nil {
|
if err := p.folder.drop(k); err != nil {
|
||||||
journal("deleting the published %s failed: %v", k.MessageID, err)
|
journal("deleting the published %s failed: %v", id, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// outgoing is a kept change as the bundle publishes it on its own node: the seat's subject, the message id
|
||||||
|
// `<node>.<event>.<sequence>` and the body with its node (§20; lead answer L-3).
|
||||||
|
func (p *publisher) outgoing(k Kept) (string, string, json.RawMessage, error) {
|
||||||
|
node := p.folder.Node
|
||||||
|
if node == "" {
|
||||||
|
return "", "", nil, errors.New("no node named: the tool runner names the bundle's in MESH_NODE")
|
||||||
|
}
|
||||||
|
if k.Event != eventAsked && k.Event != eventEnded {
|
||||||
|
return "", "", nil, fmt.Errorf("the kept change %s is of no event of the seat", k.Name())
|
||||||
|
}
|
||||||
|
var body map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal(k.Body, &body); err != nil {
|
||||||
|
return "", "", nil, fmt.Errorf("the kept change %s holds no body: %w", k.Name(), err)
|
||||||
|
}
|
||||||
|
body["node"], _ = json.Marshal(node)
|
||||||
|
raw, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", nil, err
|
||||||
|
}
|
||||||
|
return eventSubject(k.Event, node), fmt.Sprintf("%s.%s.%d", node, k.Event, k.Sequence), raw, nil
|
||||||
|
}
|
||||||
|
|
||||||
// refusedSince is the publishes refused since the bundle's start.
|
// refusedSince is the publishes refused since the bundle's start.
|
||||||
func (p *publisher) refusedSince() int {
|
func (p *publisher) refusedSince() int {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink"
|
||||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,7 +43,7 @@ func (s *fakeStream) publishes() string {
|
|||||||
func keepThree(t *testing.T, f Folder) {
|
func keepThree(t *testing.T, f Folder) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for _, event := range []string{eventEnded, eventAsked, eventEnded} {
|
for _, event := range []string{eventEnded, eventAsked, eventEnded} {
|
||||||
if _, err := f.keep(event, eventSubject(event, f.Node), map[string]string{"node": f.Node}); err != nil {
|
if _, err := f.keep(event, map[string]string{"link": "wlan0"}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,10 +61,10 @@ func TestTheBundlePublishesTheKeptEventsInOrder(t *testing.T) {
|
|||||||
if r := p.publishKept("the bundle's start"); r.Published != 1 || r.Waiting != 2 || p.refusedSince() != 1 {
|
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())
|
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" {
|
if kept, _ := f.pending(); len(kept) != 2 || kept[0].Name() != "sign-in-asked.2" {
|
||||||
t.Fatalf("waiting %+v", kept)
|
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") {
|
if !strings.Contains(strings.Join(lines(), "\n"), "publishing sign-in-asked.2 at the bundle's start was refused") {
|
||||||
t.Fatalf("the refusal was not written to the journal: %q", lines())
|
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 {
|
if r := p.publishKept("a kept event"); r.Published != 2 || r.Waiting != 0 {
|
||||||
@@ -101,7 +102,7 @@ func TestTheBundlePublishesAtEachMoment(t *testing.T) {
|
|||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
files <- struct{}{} // the start's round ran and was refused; this one publishes what waits
|
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 {
|
if _, err := f.keep(eventAsked, map[string]string{}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
files <- struct{}{}
|
files <- struct{}{}
|
||||||
@@ -129,3 +130,32 @@ func TestTheBundlePublishesAtEachMoment(t *testing.T) {
|
|||||||
t.Fatalf("published %s", got)
|
t.Fatalf("published %s", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The bundle publishes a change the watcher kept with no node under its own: the subject `<event>.<node>`, the message
|
||||||
|
// id `<node>.<event>.<sequence>` and the body's node, the same at every publish of one file; a bundle that names no node
|
||||||
|
// publishes nothing and keeps the change (design 08 sign-in-page §20; lead answer L-3).
|
||||||
|
func TestTheBundleFillsItsNodeIntoAKeptChange(t *testing.T) {
|
||||||
|
journalLines(t)
|
||||||
|
watcher := Folder{Dir: t.TempDir()}
|
||||||
|
if _, err := watcher.keep(eventAsked, nodeuplink.SignInAsked{Link: "wlan0", Network: "guest-example",
|
||||||
|
Address: "http://check.example.net/", At: "2026-11-24T10:00:00Z"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var said []string
|
||||||
|
publish := func(subject string, body json.RawMessage, id string) error {
|
||||||
|
said = append(said, subject+" "+id+" "+string(body))
|
||||||
|
return errors.New("no acknowledgement")
|
||||||
|
}
|
||||||
|
unnamed := &publisher{folder: watcher, publish: publish}
|
||||||
|
if r := unnamed.publishKept("a test"); r.Waiting != 1 || !strings.Contains(r.Refused, "no node named") || len(said) != 0 {
|
||||||
|
t.Fatalf("a bundle with no node: %+v, said %q", r, said)
|
||||||
|
}
|
||||||
|
p := &publisher{folder: Folder{Dir: watcher.Dir, Node: "laptop"}, publish: publish}
|
||||||
|
p.publishKept("a test")
|
||||||
|
p.publishKept("a test")
|
||||||
|
want := `mesh.seat.node-uplink.event.sign-in-asked.laptop laptop.sign-in-asked.1 ` +
|
||||||
|
`{"address":"http://check.example.net/","at":"2026-11-24T10:00:00Z","link":"wlan0","network":"guest-example","node":"laptop"}`
|
||||||
|
if len(said) != 2 || said[0] != want || said[1] != want {
|
||||||
|
t.Fatalf("published %q, want twice %s", said, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,13 +53,20 @@ type Folder struct {
|
|||||||
Node string
|
Node string
|
||||||
}
|
}
|
||||||
|
|
||||||
// theFolder is the account's folder, on the node the node-engine names (MESH_NODE).
|
// theFolder is the account's folder, on the node the tool runner names the bundle (MESH_NODE).
|
||||||
func theFolder() (Folder, error) {
|
func theFolder() (Folder, error) {
|
||||||
|
f, err := accountFolder()
|
||||||
|
f.Node = os.Getenv("MESH_NODE")
|
||||||
|
return f, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// accountFolder is the account's folder with no node: the watcher's, which names none (L-3).
|
||||||
|
func accountFolder() (Folder, error) {
|
||||||
home, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Folder{}, err
|
return Folder{}, err
|
||||||
}
|
}
|
||||||
return Folder{Dir: filepath.Join(home, stateHome), Node: os.Getenv("MESH_NODE")}, nil
|
return Folder{Dir: filepath.Join(home, stateHome)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Folder) path(name string) string { return filepath.Join(f.Dir, name) }
|
func (f Folder) path(name string) string { return filepath.Join(f.Dir, name) }
|
||||||
@@ -108,18 +115,22 @@ func (f Folder) nextSequence() (uint64, error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kept is one event the watcher kept for the bundle to publish: the seat's subject, the bus's message id and the body.
|
// Kept is one change the watcher kept for the bundle to publish: the event's name, its sequence and its body, all
|
||||||
|
// without the node. The watcher names no node; the bundle fills its own into the subject, the message id and the body's
|
||||||
|
// node when it publishes (lead answer L-3).
|
||||||
type Kept struct {
|
type Kept struct {
|
||||||
Sequence uint64 `json:"-"`
|
Sequence uint64 `json:"sequence"`
|
||||||
Subject string `json:"subject"`
|
Event string `json:"event"`
|
||||||
MessageID string `json:"message_id"`
|
|
||||||
Body json.RawMessage `json:"body"`
|
Body json.RawMessage `json:"body"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name is the kept change as the journal names it: `<event>.<sequence>`.
|
||||||
|
func (k Kept) Name() string { return fmt.Sprintf("%s.%d", k.Event, k.Sequence) }
|
||||||
|
|
||||||
func pendingFile(seq uint64) string { return fmt.Sprintf("%010d.json", seq) }
|
func pendingFile(seq uint64) string { return fmt.Sprintf("%010d.json", seq) }
|
||||||
|
|
||||||
// keep writes one event into pending/: its message id `<node>.<event>.<sequence>`, made once (§20).
|
// keep writes one change into pending/ under the next sequence, made once (§20; L-3).
|
||||||
func (f Folder) keep(event, subject string, body any) (Kept, error) {
|
func (f Folder) keep(event string, body any) (Kept, error) {
|
||||||
raw, err := json.Marshal(body)
|
raw, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Kept{}, err
|
return Kept{}, err
|
||||||
@@ -131,7 +142,7 @@ func (f Folder) keep(event, subject string, body any) (Kept, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return Kept{}, err
|
return Kept{}, err
|
||||||
}
|
}
|
||||||
k := Kept{Sequence: seq, Subject: subject, MessageID: fmt.Sprintf("%s.%s.%d", f.Node, event, seq), Body: raw}
|
k := Kept{Sequence: seq, Event: event, Body: raw}
|
||||||
data, err := json.Marshal(k)
|
data, err := json.Marshal(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Kept{}, err
|
return Kept{}, err
|
||||||
|
|||||||
@@ -15,19 +15,20 @@ func aFolder(t *testing.T) Folder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A watcher restarted after its events were published and pending/ emptied makes message ids it never used, so the
|
// A watcher restarted after its events were published and pending/ emptied makes message ids it never used, so the
|
||||||
// bus keeps them rather than dropping them as duplicates (design 08 sign-in-page §12, §25; R3-1).
|
// bus keeps them rather than dropping them as duplicates (design 08 sign-in-page §12, §25; R3-1). The watcher's folder
|
||||||
|
// names no node, and a kept change holds its event, its sequence and its body (L-3).
|
||||||
func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
|
func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
|
||||||
f := aFolder(t)
|
f := Folder{Dir: t.TempDir()}
|
||||||
used := map[string]bool{}
|
used := map[uint64]bool{}
|
||||||
for range 3 {
|
for range 3 {
|
||||||
k, err := f.keep("sign-in-asked", "mesh.seat.node-uplink.event.sign-in-asked.laptop", map[string]string{"node": "laptop"})
|
k, err := f.keep("sign-in-asked", map[string]string{"link": "wlan0"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
used[k.MessageID] = true
|
used[k.Sequence] = true
|
||||||
}
|
}
|
||||||
kept, err := f.pending()
|
kept, err := f.pending()
|
||||||
if err != nil || len(kept) != 3 || kept[0].MessageID != "laptop.sign-in-asked.1" || kept[2].Sequence != 3 {
|
if err != nil || len(kept) != 3 || kept[0].Name() != "sign-in-asked.1" || kept[2].Sequence != 3 {
|
||||||
t.Fatalf("kept %+v %v", kept, err)
|
t.Fatalf("kept %+v %v", kept, err)
|
||||||
}
|
}
|
||||||
for _, k := range kept {
|
for _, k := range kept {
|
||||||
@@ -35,10 +36,14 @@ func TestARestartedWatcherMakesIdsItNeverUsed(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
restarted := Folder{Dir: f.Dir, Node: "laptop"}
|
restarted := Folder{Dir: f.Dir}
|
||||||
k, err := restarted.keep("sign-in-ended", "mesh.seat.node-uplink.event.sign-in-ended.laptop", map[string]string{})
|
k, err := restarted.keep("sign-in-ended", map[string]string{})
|
||||||
if err != nil || used[k.MessageID] || k.MessageID != "laptop.sign-in-ended.4" {
|
if err != nil || used[k.Sequence] || k.Name() != "sign-in-ended.4" {
|
||||||
t.Fatalf("the restarted watcher made %q (%v), having used %v", k.MessageID, err, used)
|
t.Fatalf("the restarted watcher made %q (%v), having used %v", k.Name(), err, used)
|
||||||
|
}
|
||||||
|
raw, _ := os.ReadFile(filepath.Join(f.Dir, pendingName, "0000000004.json"))
|
||||||
|
if string(raw) != `{"sequence":4,"event":"sign-in-ended","body":{}}`+"\n" {
|
||||||
|
t.Fatalf("the kept file holds %s", raw)
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(filepath.Join(f.Dir, pendingName, "0000000004.json")); err != nil {
|
if _, err := os.Stat(filepath.Join(f.Dir, pendingName, "0000000004.json")); err != nil {
|
||||||
t.Fatalf("the kept file is not named by its ten-digit sequence: %v", err)
|
t.Fatalf("the kept file is not named by its ten-digit sequence: %v", err)
|
||||||
@@ -68,7 +73,7 @@ func TestTheFoldersFilesAreWrittenWhole(t *testing.T) {
|
|||||||
if c, err := f.names(); err != nil || c.Result != resultNoHolder || c.Names == nil || c.Failed() {
|
if c, err := f.names(); err != nil || c.Result != resultNoHolder || c.Names == nil || c.Failed() {
|
||||||
t.Fatalf("names read %+v %v", c, err)
|
t.Fatalf("names read %+v %v", c, err)
|
||||||
}
|
}
|
||||||
if _, err := f.keep("sign-in-asked", "s", map[string]string{}); err != nil {
|
if _, err := f.keep("sign-in-asked", map[string]string{}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
os.WriteFile(filepath.Join(f.Dir, pendingName, "0000000009.json.new"), []byte("{"), 0o600)
|
os.WriteFile(filepath.Join(f.Dir, pendingName, "0000000009.json.new"), []byte("{"), 0o600)
|
||||||
@@ -124,7 +129,7 @@ func TestAPathTheWatcherDoesNotKnowIsLeftAndSaidOnce(t *testing.T) {
|
|||||||
sys.set(guest(ratingFull))
|
sys.set(guest(ratingFull))
|
||||||
w := aWatcher(t, sys)
|
w := aWatcher(t, sys)
|
||||||
f := w.Folder
|
f := w.Folder
|
||||||
if _, err := f.keep(eventEnded, "s", map[string]string{}); err != nil {
|
if _, err := f.keep(eventEnded, map[string]string{}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
f.writeCheckAddress("http://check.example.net/")
|
f.writeCheckAddress("http://check.example.net/")
|
||||||
@@ -172,3 +177,15 @@ func TestTheStatedBounds(t *testing.T) {
|
|||||||
t.Fatalf("the bounds are %s, %s, %s and %d", socketBound, openBound, sessionWait, keptBound)
|
t.Fatalf("the bounds are %s, %s, %s and %d", socketBound, openBound, sessionWait, keptBound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The watcher reads no MESH_NODE: its folder names no node, whatever the environment says (L-3).
|
||||||
|
func TestTheWatchersFolderNamesNoNode(t *testing.T) {
|
||||||
|
t.Setenv("MESH_NODE", "laptop")
|
||||||
|
t.Setenv("HOME", t.TempDir())
|
||||||
|
if f, err := accountFolder(); err != nil || f.Node != "" {
|
||||||
|
t.Fatalf("the watcher's folder is %+v (%v)", f, err)
|
||||||
|
}
|
||||||
|
if f, err := theFolder(); err != nil || f.Node != "laptop" {
|
||||||
|
t.Fatalf("the bundle's folder is %+v (%v)", f, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -180,13 +180,10 @@ func newWatcher(f Folder) *Watcher {
|
|||||||
|
|
||||||
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
|
// watch is `uplink-tools watch`: the watcher, until its process is stopped.
|
||||||
func watch([]string) error {
|
func watch([]string) error {
|
||||||
f, err := theFolder()
|
f, err := accountFolder()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if f.Node == "" {
|
|
||||||
return errors.New("no node named: the node-engine names it in MESH_NODE")
|
|
||||||
}
|
|
||||||
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
|
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -410,7 +407,7 @@ func (w *Watcher) asked(r Rating) {
|
|||||||
now := w.Now()
|
now := w.Now()
|
||||||
w.ask = &openAsk{Network: r.Network, Link: r.Link, Since: now, connection: r.Connection, address: r.CheckURI}
|
w.ask = &openAsk{Network: r.Network, Link: r.Link, Since: now, connection: r.Connection, address: r.CheckURI}
|
||||||
w.state.AsksOpened++
|
w.state.AsksOpened++
|
||||||
w.keep(eventAsked, nodeuplink.SignInAsked{Node: w.Folder.Node, Link: r.Link, Network: r.Network, Address: r.CheckURI,
|
w.keep(eventAsked, nodeuplink.SignInAsked{Link: r.Link, Network: r.Network, Address: r.CheckURI,
|
||||||
At: stamp(now)})
|
At: stamp(now)})
|
||||||
w.present(nil, r.Network, r.CheckURI)
|
w.present(nil, r.Network, r.CheckURI)
|
||||||
}
|
}
|
||||||
@@ -421,7 +418,7 @@ func (w *Watcher) ended(link, network, why string) {
|
|||||||
w.state.AsksEnded++
|
w.state.AsksEnded++
|
||||||
}
|
}
|
||||||
w.ask = nil
|
w.ask = nil
|
||||||
w.keep(eventEnded, nodeuplink.SignInEnded{Node: w.Folder.Node, Link: link, Network: network, At: stamp(w.Now()), Why: why})
|
w.keep(eventEnded, nodeuplink.SignInEnded{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.
|
// stamp is a time as the events say it: RFC 3339 in UTC, to the second.
|
||||||
@@ -429,12 +426,12 @@ func stamp(t time.Time) string { return t.UTC().Truncate(time.Second).Format(tim
|
|||||||
|
|
||||||
// keep writes one event into pending/ and holds the folder to its bound, the oldest dropped with a journal line.
|
// keep writes one event into pending/ and holds the folder to its bound, the oldest dropped with a journal line.
|
||||||
func (w *Watcher) keep(event string, body any) {
|
func (w *Watcher) keep(event string, body any) {
|
||||||
k, err := w.Folder.keep(event, eventSubject(event, w.Folder.Node), body)
|
k, err := w.Folder.keep(event, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
journal("keeping %s failed: %v", event, err)
|
journal("keeping %s failed: %v", event, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
journal("kept %s as %s", event, k.MessageID)
|
journal("kept %s", k.Name())
|
||||||
kept, err := w.Folder.pending()
|
kept, err := w.Folder.pending()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
journal("reading the kept events failed: %v", err)
|
journal("reading the kept events failed: %v", err)
|
||||||
@@ -442,11 +439,11 @@ func (w *Watcher) keep(event string, body any) {
|
|||||||
}
|
}
|
||||||
for i := 0; len(kept)-i > keptBound; i++ {
|
for i := 0; len(kept)-i > keptBound; i++ {
|
||||||
if err := w.Folder.drop(kept[i]); err != nil {
|
if err := w.Folder.drop(kept[i]); err != nil {
|
||||||
journal("dropping the kept event %s past the bound of %d failed: %v", kept[i].MessageID, keptBound, err)
|
journal("dropping the kept event %s past the bound of %d failed: %v", kept[i].Name(), keptBound, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
w.state.DroppedPastBound++
|
w.state.DroppedPastBound++
|
||||||
journal("dropped the kept event %s past the bound of %d", kept[i].MessageID, keptBound)
|
journal("dropped the kept event %s past the bound of %d", kept[i].Name(), keptBound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func aWatcher(t *testing.T, sys *fakeSystem) *Watcher {
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
tick := clockStart
|
tick := clockStart
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
return &Watcher{Folder: aFolder(t), Dial: sys.dial, Now: func() time.Time {
|
return &Watcher{Folder: Folder{Dir: t.TempDir()}, Dial: sys.dial, Now: func() time.Time {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
tick = tick.Add(time.Second)
|
tick = tick.Add(time.Second)
|
||||||
@@ -193,8 +193,8 @@ func keptSaid(t *testing.T, f Folder) []string {
|
|||||||
if err := json.Unmarshal(k.Body, &b); err != nil {
|
if err := json.Unmarshal(k.Body, &b); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
event := strings.Split(k.MessageID, ".")[1]
|
event := k.Event
|
||||||
if k.Subject != "mesh.seat.node-uplink.event."+event+".laptop" || b.Node != "laptop" || b.At == "" {
|
if b.Node != "" || b.At == "" {
|
||||||
t.Errorf("kept %+v %+v", k, b)
|
t.Errorf("kept %+v %+v", k, b)
|
||||||
}
|
}
|
||||||
if event == eventAsked && b.Address != "http://check.example.net/" {
|
if event == eventAsked && b.Address != "http://check.example.net/" {
|
||||||
@@ -280,17 +280,17 @@ func TestPastTheBoundTheOldestKeptEventGoes(t *testing.T) {
|
|||||||
lines := journalLines(t)
|
lines := journalLines(t)
|
||||||
w := aWatcher(t, &fakeSystem{})
|
w := aWatcher(t, &fakeSystem{})
|
||||||
for range keptBound {
|
for range keptBound {
|
||||||
if _, err := w.Folder.keep(eventEnded, eventSubject(eventEnded, "laptop"), map[string]string{}); err != nil {
|
if _, err := w.Folder.keep(eventEnded, map[string]string{}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.heard(guest(ratingPortal), true)
|
w.heard(guest(ratingPortal), true)
|
||||||
kept, _ := w.Folder.pending()
|
kept, _ := w.Folder.pending()
|
||||||
if len(kept) != keptBound || kept[0].Sequence != 2 || kept[len(kept)-1].MessageID != "laptop.sign-in-asked.257" {
|
if len(kept) != keptBound || kept[0].Sequence != 2 || kept[len(kept)-1].Name() != "sign-in-asked.257" {
|
||||||
t.Fatalf("kept %d, from %d", len(kept), kept[0].Sequence)
|
t.Fatalf("kept %d, from %d", len(kept), kept[0].Sequence)
|
||||||
}
|
}
|
||||||
said := strings.Join(lines(), "\n")
|
said := strings.Join(lines(), "\n")
|
||||||
if !strings.Contains(said, "dropped the kept event laptop.sign-in-ended.1 past the bound of 256") ||
|
if !strings.Contains(said, "dropped the kept event sign-in-ended.1 past the bound of 256") ||
|
||||||
w.state.DroppedPastBound != 1 {
|
w.state.DroppedPastBound != 1 {
|
||||||
t.Fatalf("the drop was not said: %s", said)
|
t.Fatalf("the drop was not said: %s", said)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,8 @@ type runtime struct {
|
|||||||
in io.Writer
|
in io.Writer
|
||||||
next int
|
next int
|
||||||
pending map[string]chan json.RawMessage
|
pending map[string]chan json.RawMessage
|
||||||
// mayPublish is every subject the module's membership lets it say, as the runtime's mayPublish reads it.
|
// mayPublish is every subject the module's membership lets it say, as the runtime's mayPublish reads it; guarded
|
||||||
|
// by mu.
|
||||||
mayPublish map[string]bool
|
mayPublish map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +147,10 @@ func (r *runtime) handle(id json.RawMessage, method string, params json.RawMessa
|
|||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
}
|
}
|
||||||
json.Unmarshal(params, &p)
|
json.Unmarshal(params, &p)
|
||||||
if !r.mayPublish[p.Subject] {
|
r.mu.Lock()
|
||||||
|
may := r.mayPublish[p.Subject]
|
||||||
|
r.mu.Unlock()
|
||||||
|
if !may {
|
||||||
r.answer(id, nil, fmt.Errorf("%s may not publish %s: its membership does not list it", Module, p.Subject))
|
r.answer(id, nil, fmt.Errorf("%s may not publish %s: its membership does not list it", Module, p.Subject))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -217,7 +221,8 @@ func TestNetworkManagerWhole(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() { os.RemoveAll(home) })
|
t.Cleanup(func() { os.RemoveAll(home) })
|
||||||
f := Folder{Dir: filepath.Join(home, stateHome), Node: "home-server"}
|
// The watcher's folder names no node, as the watcher reads no MESH_NODE (L-3); the bundle has its own.
|
||||||
|
f := Folder{Dir: filepath.Join(home, stateHome)}
|
||||||
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
|
if err := os.MkdirAll(f.Dir, 0o700); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -345,9 +350,9 @@ func TestNetworkManagerWhole(t *testing.T) {
|
|||||||
got = append(got, published{msg.Subject(), msg.Headers().Get(jetstream.MsgIDHeader), msg.Data()})
|
got = append(got, published{msg.Subject(), msg.Headers().Get(jetstream.MsgIDHeader), msg.Data()})
|
||||||
}
|
}
|
||||||
for i, k := range kept {
|
for i, k := range kept {
|
||||||
if got[i].id != Module+"."+k.MessageID || string(got[i].body) != string(k.Body) || got[i].subject != k.Subject {
|
id := fmt.Sprintf("%s.home-server.%s.%d", Module, k.Event, k.Sequence)
|
||||||
t.Errorf("published %d: %s %s %s, kept %s %s %s", i, got[i].subject, got[i].id, got[i].body, k.Subject,
|
if got[i].id != id || got[i].subject != eventSubject(k.Event, "home-server") {
|
||||||
k.MessageID, k.Body)
|
t.Errorf("published %d: %s %s, kept %s", i, got[i].subject, got[i].id, k.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strict := func(raw []byte, into any) {
|
strict := func(raw []byte, into any) {
|
||||||
@@ -379,13 +384,17 @@ func TestNetworkManagerWhole(t *testing.T) {
|
|||||||
t.Fatalf("left in pending/: %+v", left)
|
t.Fatalf("left in pending/: %+v", left)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Another node's subject is not the module's to say: the runtime refuses it and it stays kept.
|
// A subject the membership does not list is refused by the runtime, and the change stays kept.
|
||||||
if _, err := f.keep(eventAsked, nodeuplink.SubjectSignInAsked("other-node"), map[string]string{}); err != nil {
|
r.mu.Lock()
|
||||||
|
delete(r.mayPublish, nodeuplink.SubjectSignInAsked("home-server"))
|
||||||
|
r.mu.Unlock()
|
||||||
|
if _, err := f.keep(eventAsked, nodeuplink.SignInAsked{Link: "wlan0", Network: "guest-example",
|
||||||
|
Address: "http://example.net/portal", At: "2026-11-24T10:10:00Z"}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if text := r.call("uplink_publish_again"); !strings.Contains(text, `"waiting":1`) ||
|
if text := r.call("uplink_publish_again"); !strings.Contains(text, `"waiting":1`) ||
|
||||||
!strings.Contains(text, "its membership does not list it") {
|
!strings.Contains(text, "its membership does not list it") {
|
||||||
t.Fatalf("another node's subject: uplink_publish_again answered %s", text)
|
t.Fatalf("a subject the membership does not list: uplink_publish_again answered %s", text)
|
||||||
}
|
}
|
||||||
if left, _ := f.pending(); len(left) != 1 {
|
if left, _ := f.pending(); len(left) != 1 {
|
||||||
t.Fatalf("the refused event is not kept: %+v", left)
|
t.Fatalf("the refused event is not kept: %+v", left)
|
||||||
|
|||||||
Reference in New Issue
Block a user