package main // The bundle gives the resolver the check's host name (design 08 sign-in-page §13): it calls // `/node-resolver.uplink-names` with the check address's host name, or an empty list when there is no address, // and writes the call to names.json. Which module holds node-resolver on its node it reads from the seat // mesh-module-assigner's assignments; none is `no holder`, which is no failure (DQ-8). A failed call is written to the // journal, stands failed in names.json and is asked again at the next moment, never on a timer. import ( "encoding/json" "fmt" "net/url" "strings" "sync" "time" "git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver" stdio "git.novox.be/novox/mesh-sdk/go" ) const ( // runningPattern is node-engine's running event, as the manifest consumes it: node-engine.running... runningPattern = "node-engine.running.*.*" assignmentsKey = "seat:mesh-module-assigner.assignments" ) // namer calls uplink-names, one call at a time. type namer struct { folder Folder // ask calls a verb on the mesh as the module, through the runtime. ask func(key string, body any) (json.RawMessage, error) now func() time.Time mu sync.Mutex } // hostOf is the check address's host name, lower-cased, as uplink-names takes it: none for an empty address. func hostOf(address string) ([]string, error) { if address == "" { return []string{}, nil } u, err := url.Parse(address) if err != nil || u.Hostname() == "" { return nil, fmt.Errorf("the check address %q names no host", address) } return []string{strings.ToLower(u.Hostname())}, nil } // resolverHolder is the module that holds node-resolver on this node, "" when none does (DQ-8). func (n *namer) resolverHolder() (string, error) { raw, err := n.ask(assignmentsKey, map[string]string{"node": n.folder.Node, "seats": "true"}) if err != nil { return "", fmt.Errorf("reading the assignments: %w", err) } var answer struct { Seats []struct { Seat string `json:"seat"` Holders []struct { Module string `json:"module"` Node string `json:"node"` } `json:"holders"` } `json:"seats"` } if err := json.Unmarshal(raw, &answer); err != nil { return "", fmt.Errorf("the assignments answered in no form this reads: %w", err) } for _, s := range answer.Seats { if s.Seat != noderesolver.Seat { continue } for _, h := range s.Holders { if h.Node == n.folder.Node { return h.Module, nil } } } return "", nil } // give calls uplink-names now with the check address's host name and writes the call to names.json. func (n *namer) give(moment string) NamesCall { n.mu.Lock() defer n.mu.Unlock() call := NamesCall{Names: []string{}, At: n.now()} address, err := n.folder.checkAddress() if err == nil { call.Names, err = hostOf(address) } if err == nil { call.Result, err = n.call(call.Names) } if err != nil { call.Result = err.Error() } if call.Names == nil { call.Names = []string{} } if err := n.folder.writeNames(call); err != nil { journal("writing names.json failed: %v", err) } journal("uplink-names %v at %s: %s", call.Names, moment, call.Result) return call } func (n *namer) call(names []string) (string, error) { holder, err := n.resolverHolder() if err != nil { return "", err } if holder == "" { return resultNoHolder, nil } raw, err := n.ask(uplinkNamesKey(n.folder.Node), noderesolver.UplinkNamesInput{Names: names}) if err != nil { return "", fmt.Errorf("%s/%s.%s: %w", n.folder.Node, noderesolver.Seat, noderesolver.VerbUplinkNames, err) } if _, _, err := noderesolver.DecodeUplinkNamesAnswer(raw); err != nil { return "", fmt.Errorf("uplink-names answered in no form this reads: %w", err) } return resultOK, nil } func uplinkNamesKey(node string) string { return "seat:" + noderesolver.Seat + "." + noderesolver.VerbUplinkNames + "@" + node } // running hears node-engine's running: one for this node's holder of node-resolver, running, is a moment to call // uplink-names; every other is dropped, another node's first (§13). func (n *namer) running(e stdio.Envelope) error { parts := strings.Split(e.Key, ".") if len(parts) != 4 || parts[2] != n.folder.Node { return nil } var body struct { Module string `json:"module"` Stopped bool `json:"stopped"` } if err := json.Unmarshal(e.Body, &body); err != nil || body.Stopped { return nil } holder, err := n.resolverHolder() if err != nil { journal("hearing %s: %v", e.Key, err) return nil } if holder != "" && holder == parts[3] { n.give(holder + " running") } return nil }