49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package stdio
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// **Who answers a verb, asked by the bus's discovery** (D-root and D3 of the seat mesh-self-check; issue 1955): the
|
|
// runtime asks the services protocol on the account it runs as (ADR 0197) and answers each machine and module that
|
|
// says it serves the verb. The bundle never asks the bus itself.
|
|
|
|
// Answerer is one module on one machine that answers a verb.
|
|
type Answerer struct {
|
|
Machine string `json:"machine"`
|
|
Module string `json:"module"`
|
|
}
|
|
|
|
const discoverTimeout = 10 * time.Second
|
|
|
|
// Discover is every module that answers verb (`<seat>.<verb>` or `<module>.<tool>`), on node when one is named: runtime
|
|
// verb `mesh/discover {verb, node?}`, answered as a list. None answering is an empty list and no error; an error is a
|
|
// question that could not be asked.
|
|
func Discover(verb, node string) ([]Answerer, error) {
|
|
if verb == "" {
|
|
return nil, fmt.Errorf("discover names the verb it asks about")
|
|
}
|
|
c, err := live()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
params := map[string]any{"verb": verb}
|
|
if node != "" {
|
|
params["node"] = node
|
|
}
|
|
raw, err := c.ask("mesh/discover", params, discoverTimeout)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := []Answerer{}
|
|
if len(raw) == 0 || string(raw) == "null" {
|
|
return out, nil
|
|
}
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
return nil, fmt.Errorf("the runtime's answer to discover is not readable: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|